Skip to content

Instantly share code, notes, and snippets.

@ilyanengen
ilyanengen / Debouncer.swift
Last active February 2, 2025 12:04
Debouncer. Work logic: 1.Takes a duration and an async block to execute 2.Cancels previous tasks when a new event comes in 3.Waits for the specified duration before executing the block . Use case: 1.Perfect for search functionality 2.Prevents excessive API calls 3.Waits 0.5 seconds after the user stops typing before searching
import Foundation
final class Debouncer {
private let block: @Sendable () async -> Void
private let duration: ContinuousClock.Duration
private var task: Task<Void, Never>?
init(duration: ContinuousClock.Duration, block: @Sendable @escaping () async -> Void) {
self.duration = duration
self.block = block
@ilyanengen
ilyanengen / CircleLoadingView.swift
Last active September 19, 2024 15:12
CircleLoadingView - loading indicator with loop animation
struct CircleLoadingView: View {
@State private var rotationDegree: Double = 0
var body: some View {
ZStack {
Circle()
.stroke(lineWidth: 4)
.frame(width: 40, height: 40)
.foregroundStyle(.gray.opacity(0.3))
@ilyanengen
ilyanengen / AppleColors.swift
Last active July 15, 2024 15:07
Simple Color picker with standard 120 apple colors
//
// AppleColors.swift
// ToDoStack
//
// Created by Ilya Biltuev on 23.04.2024.
//
import UIKit
extension UIColor {
@ilyanengen
ilyanengen / KeyboardAppearListener
Last active June 25, 2023 16:36
A class to handle keyboard appear on view controller (move safeArea up and down)
import Foundation
class KeyboardAppearListener {
private weak var viewController: UIViewController?
private var isKeyboardShown: Bool = false
init(viewController: UIViewController) {
self.viewController = viewController
@ilyanengen
ilyanengen / gist:693a546eeaf01c64af08c9d50e87d198
Created September 15, 2021 19:26
Custom class of UIButton with loading indicator
import UIKit
class LoadingButton: UIButton {
var originalButtonText: String?
var activityIndicator: UIActivityIndicatorView!
@IBInspectable
let activityIndicatorColor: UIColor = .lightGray
@ilyanengen
ilyanengen / gist:82be888bb3e64ae97613044b022ebbb1
Created September 29, 2020 15:06 — forked from bwhiteley/gist:049e4bede49e71a6d2e2
Initialize Swift subclass of UIView, designed in .xib
// Create CustomView.xib, set File's Owner to CustomView.
// Link the top level view in the XIB to the contentView outlet.
class CustomView : UIView {
@IBOutlet private var contentView:UIView?
// other outlets
override init(frame: CGRect) { // for using CustomView in code
super.init(frame: frame)
self.commonInit()
@ilyanengen
ilyanengen / gist:ffb5e23e87ad181f44f0cdd009553b15
Created October 5, 2018 14:57
Http client with automatic mapping into model from completion handler
import Foundation
import Alamofire
typealias JSON = [String: Any]
typealias StringClosure = (String?, Error?) -> ()
typealias JsonClosure = (JSON?, Error?) -> ()
typealias ResponseClosure<T: Decodable> = (T?, Error?) -> ()
private final class HttpClient {
@ilyanengen
ilyanengen / gist:b627f2cda5afee6843938a785c9f086e
Created August 24, 2018 14:15
loading indicator on ViewController
import UIKit
private var loadingViewKey: UInt8 = 0
extension UIViewController {
// MARK: - Loading Indicator
var loadingIndicator: UIActivityIndicatorView {
get {
@ilyanengen
ilyanengen / UIAlertController+TextField.swift
Created August 21, 2018 08:22 — forked from ole/UIAlertController+TextField.swift
A UIAlertController with a text field and the ability to perform validation on the text the user has entered while the alert is on screen. The OK button is only enabled when the entered text passes validation. More info: https://oleb.net/2018/uialertcontroller-textfield/
import UIKit
/// A validation rule for text input.
public enum TextValidationRule {
/// Any input is valid, including an empty string.
case noRestriction
/// The input must not be empty.
case nonEmpty
/// The enitre input must match a regular expression. A matching substring is not enough.
case regularExpression(NSRegularExpression)