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 / CVPixelBufferPixelFormatNames.swift
Created October 21, 2019 17:09 — forked from skyebook/CVPixelBufferPixelFormatNames.swift
Easily get the pixel format name of a CVPixelBuffer
public func CVPixelBufferGetPixelFormatName(pixelBuffer: CVPixelBuffer) -> String {
let p = CVPixelBufferGetPixelFormatType(pixelBuffer)
switch p {
case kCVPixelFormatType_1Monochrome: return "kCVPixelFormatType_1Monochrome"
case kCVPixelFormatType_2Indexed: return "kCVPixelFormatType_2Indexed"
case kCVPixelFormatType_4Indexed: return "kCVPixelFormatType_4Indexed"
case kCVPixelFormatType_8Indexed: return "kCVPixelFormatType_8Indexed"
case kCVPixelFormatType_1IndexedGray_WhiteIsZero: return "kCVPixelFormatType_1IndexedGray_WhiteIsZero"
case kCVPixelFormatType_2IndexedGray_WhiteIsZero: return "kCVPixelFormatType_2IndexedGray_WhiteIsZero"
case kCVPixelFormatType_4IndexedGray_WhiteIsZero: return "kCVPixelFormatType_4IndexedGray_WhiteIsZero"
@astrokin
astrokin / UIAlertController+TextField.swift
Created August 20, 2018 20:23 — 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)
@astrokin
astrokin / Activity.swift
Created June 8, 2018 13:26 — forked from zwaldowski/Activity.swift
os_activity_t for Swift 3
//
// Activity.swift
//
// Created by Zachary Waldowski on 8/21/16.
// Copyright © 2016 Zachary Waldowski. Licensed under MIT.
//
import os.activity
private final class LegacyActivityContext {
@astrokin
astrokin / AZTextFrame.swift
Created April 14, 2018 14:25 — forked from azimin/AZTextFrame.swift
Helper for calculating String and NSAttributedString frame
import UIKit
class AZTextFrameAttributes: NSObject {
// MARK: - Properties
fileprivate(set) var width: CGFloat = 0
fileprivate(set) var string: String?
fileprivate(set) var attributedString: NSAttributedString?
@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 / 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:
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)
// 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
// 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