Skip to content

Instantly share code, notes, and snippets.

import UIKit
/* KeyPath extension for listable paths */
protocol KeyPathListable { }
extension KeyPathListable {
var allChildrenCount: Int {
let mirror = Mirror(reflecting: self)
return mirror.children.count
}
var item: DispatchWorkItem!
item = DispatchWorkItem {
sleep(2)
guard !item.isCancelled else {
print("Work has been cancelled")
return
}
print("Work has been done")
}
// MARK: UIView
public extension UIView {
private static var someTagKey: UInt8 = 0
var someTag: String? {
get { return objc_getAssociatedObject(self, &Self.someTagKey) as? String }
set { objc_setAssociatedObject(self, &Self.someTagKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
}
}
let updates = Updates(
bundle: Bundle.main.bundleId,
version: Bundle.main.appVersion)
updates.check { (status: UpdatesStatus) in
print("App update status: \(status)")
}
// MARK: Bundle
public extension Bundle {
var appVersion: String {
return self.info(for: "CFBundleShortVersionString", or: "0.0.0")
}
var bundleId: String {
return self.info(for: "CFBundleIdentifier")
}
// MARK: Updates
public class Updates: UpdatesProtocol {
enum StorageKeys: String {
case postponedVersion = "UpdatesPostponedVersion"
}
private let userDefaults = UserDefaults.standard
private let queue = DispatchQueue(label: "com.limbo.FormsUpdates", qos: .default)
private let bundleId: String
private let currentVersion: Version?
// MARK: UpdatesStatus
public enum UpdatesStatus {
case newVersion(_ version: Version)
case noChanges
case postponed
case undefined
public var isAvailable: Bool {
switch self {
case .newVersion: return true
// MARK: UpdatesProtocol
public protocol UpdatesProtocol {
func check(completion: @escaping ((UpdatesStatus) -> Void))
func check(cancelToken: CancelToken?, completion: @escaping ((UpdatesStatus) -> Void))
func reset()
func postpone()
}
// MARK: String
public extension String {
var asVersion: Version? {
return Version(self)
}
}
// MARK: Version
extension Version: Equatable, Comparable {
public func isEqual(_ version: Version?) -> Bool {
guard let version: String = version?.version else { return false }
return version.compare(self.version, options: String.CompareOptions.numeric) == .orderedSame
}
public func isGreater(_ version: Version?) -> Bool {
guard let version: String = version?.version else { return false }
return version.compare(self.version, options: String.CompareOptions.numeric) == .orderedAscending