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 / Optional.swift
Created February 12, 2019 15:38
Optional+Logging
public extension Optional {
func expectedToBe(_ logMessage: String? = nil, line: Int = #line, file: StaticString = #file) -> Wrapped? {
switch self {
case let .some(some): return some
case .none:
print("[OPTIONAL] \(#function) \(file):\(line) message \(String(describing: logMessage))")
return .none
}
}
}
@astrokin
astrokin / undoer.swift
Created February 4, 2019 06:59
undo / redo
import UIKit
class ViewController: UIViewController {
var state: State {
didSet {
label.text = state.debugDescription
// While the code is running on the main thread, the undo manager doesn’t update its state until after `registerUndo` returns. Using the DispatchQueue block allows the UI update to wait until this undo/redo operation completes.
DispatchQueue.main.async {
@astrokin
astrokin / UIDevice+Type
Created November 9, 2018 13:19
UIDevice type detection
public extension UIDevice {
public enum `Type` {
case iPad
case iPhone_unknown
case iPhone_5_5S_5C
case iPhone_6_6S_7_8
case iPhone_6_6S_7_8_PLUS
case iPhone_X_Xs
case iPhone_Xs_Max
@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)
### Keybase proof
I hereby claim:
* I am astrokin on github.
* I am manyak (https://keybase.io/manyak) on keybase.
* I have a public key ASAlGYdaDIVXcae81zp0WPM4c2bb5Ky18U_7EHu0dVlgowo
To claim this, I am signing this object:
@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 / FontFitUtil.swift
Last active April 14, 2018 20:16
FontFitUtil
import Foundation
class FontFitUtil {
class func bestFontForText(_ text: String, font: UIFont, container: UIView) -> UIFont {
return bestFontForText(text, font: font, container: container.bounds.size)
}
class func bestFontForText(_ text: String, font: UIFont, container: CGSize) -> UIFont {
//make rect to calculate text a little bit smaller
@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?
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) -> ()) {
@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)
}