Skip to content

Instantly share code, notes, and snippets.

View SergLam's full-sized avatar
😊
Try to make your code better!

Serhii Liamtsev SergLam

😊
Try to make your code better!
View GitHub Profile
@SergLam
SergLam / AsyncSequence+Ext.swift
Created December 25, 2021 12:25
AsyncSequence extension to iterate elements in a synchronous-like way
import Foundation
extension AsyncSequence {
func forEach(_ body: (Element) async throws -> Void) async throws {
for try await element in self {
try await body(element)
}
}
}
@SergLam
SergLam / MulticastDelegate.swift
Created November 4, 2021 22:16
Multicast Delegate - wrapper class to implement one to many delegate with Array of optionals wrapper
import Foundation
// https://betterprogramming.pub/implement-a-multicast-delegate-design-pattern-in-swift-5-72079d695cfe
public class MulticastDelegate<T> {
// 1
private class Wrapper {
weak var delegate: AnyObject?
init(_ delegate: AnyObject) {
@SergLam
SergLam / ApplicationCoordinator.swift
Created July 22, 2021 20:41
CloseAppElegantly.swift
func quit() {
// home button pressed programmatically - to thorw app to background
DispatchQueue.main.async {
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
// terminaing app in background
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
exit(EXIT_SUCCESS)
})
}
@SergLam
SergLam / updating_ruby_with_rvm_on_a_mac.md
Created June 22, 2021 08:44 — forked from wrburgess/updating_ruby_with_rvm_on_a_mac.md
Updating Ruby with rvm or rbenv on a Mac

RBENV

  • rbenv install -l
  • rbenv install 2.6.5
  • rbenv local 2.6.5
  • gem install bundler
  • bundle install

RVM

@SergLam
SergLam / DebouncingVC.swift
Created March 9, 2021 09:44
Debounce user input to text / search field - native Swift implementation
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var mySrchBar: UISearchBar!
private var lastSearchTxt = ""
override func viewDidLoad() {
super.viewDidLoad()
}
@SergLam
SergLam / pre-commit.sh
Created March 4, 2021 21:46
Git hooks - pre-commit check example
#!/usr/bin/env bash
set -eu
failed=0
test_pattern='\b(fdescribe|fit|fcontext|xdescribe|xit|xcontext)\b'
if git diff-index -p -M --cached HEAD -- '*Tests.swift' '*Specs.swift' | grep '^+' | egrep "$test_pattern" >/dev/null 2>&1
then
echo "COMMIT REJECTED for fdescribe/fit/fcontext/xdescribe/xit/xcontext." >&2
echo "Remove focused and disabled tests before committing." >&2
@SergLam
SergLam / GradientLabel.swift
Created January 11, 2021 22:06
Gradient Label for iOS in Swift
import UIKit
final class GradientLabel: UILabel {
private var colors: [UIColor] = [.supAzure, .supAppleFive]
private var startPoint: CGPoint = CGPoint(x: 0.0, y: 0.5)
private var endPoint: CGPoint = CGPoint(x: 1.0, y: 0.5)
@SergLam
SergLam / BubbleTriangleView.swift
Created January 11, 2021 22:04
Bubble Triangle View in Swift for iOS
import UIKit
final class BubbleTriangleView: UIView {
private var viewBorderWidth: CGFloat = 3
private var radius: CGFloat = 15
private var viewFillColor: UIColor = .supAzure
private var viewBorderColor: UIColor = .white
@SergLam
SergLam / CountingLabel.swift
Created January 11, 2021 22:01
Animated counting UILabel for iOS in Swift
// Source - https://medium.com/@topLayoutGuide/swift-3-so-i-wanted-to-animate-a-label-14dd2b332ef9
//
// https://github.com/dataxpress/UICountingLabel/
//
import UIKit
enum CountingMethod {
case easeInOut
case easeIn
import UIKit
/// Represents a single `NSLayoutConstraint`
enum LayoutAnchor {
case constant(attribute: NSLayoutConstraint.Attribute,
relation: NSLayoutConstraint.Relation,
constant: CGFloat)
case relative(attribute: NSLayoutConstraint.Attribute,
relation: NSLayoutConstraint.Relation,