Skip to content

Instantly share code, notes, and snippets.

Petro Korienev soxjke

Block or report user

Report or block soxjke

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Debouncer.swift
public class Debouncer<T: Equatable> {
private(set) var value: T?
private var valueTimestamp: Date = Date()
private var interval: TimeInterval
private var queue: DispatchQueue
private var callbacks: [(T) -> ()] = []
private var debounceWorkItem: DispatchWorkItem = DispatchWorkItem {}
public init(_ interval: TimeInterval,
on queue: DispatchQueue = .main) {
View ViewController+Throttler.swift
private(set) var searchTermThrottle = Throttler<String>(1.5)
override func viewDidLoad() {
super.viewDidLoad()
searchField.addTarget(self, action: #selector(textDidChange(_:)), for: .editingChanged)
searchTermThrottle.on { (searchTerm) in
GithubAPI.search(term: searchTerm) { [weak self] result in
switch (result) {
case .success(let models): self?.onSuccess(models)
case .failure(let error): self?.onError(error)
View Throttler.swift
import Foundation
public class Throttler<T> {
private(set) var value: T? = nil
private var valueTimestamp: Date? = nil
private var interval: TimeInterval
private var queue: DispatchQueue
private var callbacks: [(T) -> ()] = []
public init(_ interval: TimeInterval, on queue: DispatchQueue = .main) {
self.interval = interval
View Throttled.swift
private var valueTimestamp: Date? = nil
private let interval: TimeInterval = 1.5
...
@objc func textDidChange(_ sender: UITextField) {
searchTerm = sender.text ?? ""
if let valueTimestamp = valueTimestamp {
if Date().timeIntervalSince(valueTimestamp) > interval {
self.valueTimestamp = nil
View ViewController.swift
import UIKit
class ViewController: UIViewController {
private(set) var data: [GithubAPI.Response.Model] = []
private(set) var searchTerm: String = ""
@IBOutlet private var tableView: UITableView!
@IBOutlet private var searchField: UITextField!
@IBOutlet private var errorLabel: UILabel!
override func viewDidLoad() {
@soxjke
soxjke / main.swift
Created Nov 28, 2019
Arabic-2-Roman
View main.swift
extension Int {
enum RomanDigits: Int, CaseIterable {
case I = 1
case IV = 4
case V = 5
case IX = 9
case X = 10
case XL = 40
case L = 50
case XC = 90
@soxjke
soxjke / AWSIdentityManager+Fixtures.h
Last active Apr 8, 2019
AWSAuthCore crash workaround
View AWSIdentityManager+Fixtures.h
#import <AWSAuthCore/AWSAuthCore.h>
@interface AWSIdentityManager (Fixtures)
@end
@soxjke
soxjke / PINCodeView.h
Last active Feb 24, 2019
PINCodeField
View PINCodeView.h
@interface PINCodeView : UIView
- (instancetype)initWithNumberOfDigits:(NSUInteger)digits
keyboardType:(UIKeyboardType)keyboardType
autocapitalizationType:(UITextAutocapitalizationType)autocapitalizationType;
@property (nonatomic, copy) NSString *code;
- (void)becameFirstResponder;
@end
View Message forwarding
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
NSMethodSignature * __block signature = nil;
[self.implementations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
signature = [obj methodSignatureForSelector:sel];
*stop = (nil != signature);
}];
return signature;
}
- (BOOL)respondsToSelector:(SEL)aSelector {
View ViewModel.swift
enum UIState {
case current
case forecast(page: Int)
}
enum UIEvent {
case turnCurrent
case turnForecast
case turnLeft
case turnRight
You can’t perform that action at this time.