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 / UIImageView+ImageLoading.swift
Created April 21, 2022 19:03
Swift Extension for iOS: Load image from URL + set to an UIImageView
import UIKit
extension UIImageView {
func downloadImage(from url: URL) {
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
@SergLam
SergLam / CertificatePinningURLSessionDelegate.swift
Created February 19, 2022 13:14
Certificate and Public Key Pinning for URLSession using Swift
// Based on https://code.tutsplus.com/articles/securing-communications-on-ios--cms-28529
import Foundation
import Security
struct Certificate {
let certificate: SecCertificate
let data: Data
}
extension Certificate {
@SergLam
SergLam / MockDataConstants.swift
Last active March 31, 2025 10:21
Mock Data URLs - links to audio, video and image files
import Foundation
struct MockDataConstants {
// MARK: - Image files
static let imageUrls: [String] = [
"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg",
"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ElephantsDream.jpg",
"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerBlazes.jpg",
"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/ForBiggerEscapes.jpg",
@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)