Skip to content

Instantly share code, notes, and snippets.

View astrokin's full-sized avatar
💭
always say yes

Alexey Strokin astrokin

💭
always say yes
View GitHub Profile
@astrokin
astrokin / DOAlertService.swift
Last active July 6, 2017 13:41
DO Alert Service
enum AlertActionStyle: Int {
case `default`
case cancel
case destructive
}
enum AlertControllerStyle : Int {
case actionSheet
case alert
}
import UIKit
typealias VoidClosure = () -> Void
typealias AlertAction = (title: String, action: VoidClosure?, style: UIAlertActionStyle)
final class AlertService {
static let shared = AlertService()
@astrokin
astrokin / PageViewController.swift
Created November 15, 2017 22:12
PageViewController
protocol PageControllerIndexable: class {
var pageIndex: Int { get set }
}
protocol PageViewControllerDatasource: class {
var startingViewController: PageControllerIndexable? { get }
var nextViewController: PageControllerIndexable? { get }
var previousViewController: PageControllerIndexable? { get }
var currentIndex: Int { get }
}
// https://github.com/avito-tech
private class ObserverBox<T>: Hashable {
typealias Observer = T
private(set) weak var disposable: AnyObject?
private let objectIdentifier: ObjectIdentifier
var observers = [Observer]()
let hashValue: Int
// https://github.com/avito-tech
//
// Sampler is like a baby of Debouncer and Throttler.
//
// Unlike Debouncer, sampler executes action immediately (if there are no actions before, for time > delay)
// Unlike Throttler it guarantees that last action in sequence will be executed (see last error in example)
/// CAUTION: This class isn't thread-safe
public final class Sampler {
// MARK: - Public properite
public func with<T>(_ item: inout T, action: (inout T) -> Void) {
action(&item)
}
public func with<T>(_ item: T, action: (T) -> Void) {
action(item)
}
public func with<T: AnyObject>(_ item: T, action: (T) -> Void) {
action(item)
@astrokin
astrokin / Result.swift
Created February 10, 2018 08:53 — forked from maxsokolov/Result.swift
A nice one
enum Result<T, E> {
case value(T)
case error(E)
var value: T? {
switch self {
case .value(let value):
return value
case .error:
@astrokin
astrokin / Timer.swift
Created February 10, 2018 08:53 — forked from maxsokolov/Timer.swift
gcd based timer using swift
private let timerQueue = DispatchQueue(label: "com.timer.queue", attributes: [])
final class Timer : NSObject {
private var timer: DispatchSourceTimer?
var active: Bool {
return timer != nil
}
func start(_ interval: Int, repeats: Bool = false, handler: @escaping () -> Void) {
@astrokin
astrokin / .swift
Created February 12, 2018 11:31
Keyboard handling
//MARK: - Observers
extension UIViewController {
func addObserverForNotification(_ notificationName: Notification.Name, actionBlock: @escaping (Notification) -> Void) {
NotificationCenter.default.addObserver(forName: notificationName, object: nil, queue: OperationQueue.main, using: actionBlock)
}
func removeObserver(_ observer: AnyObject, notificationName: Notification.Name) {
NotificationCenter.default.removeObserver(observer, name: notificationName, object: nil)
}
final class ThreadSafe<A> {
private var _value: A
private let queue = DispatchQueue(label: "ThreadSafe")
init(_ value: A) {
self._value = value
}
var value: A {
return queue.sync { _value }
}
func atomically(_ transform: (inout A) -> ()) {