Skip to content

Instantly share code, notes, and snippets.

Avatar

Vasily Ulianov Sorix

View GitHub Profile
View DeselectRowViewWillAppear.swift
if let selectedIndexPath = methodsTableView?.indexPathForSelectedRow {
if let coordinator = transitionCoordinator {
coordinator.animate(alongsideTransition: { context in
self.methodsTableView?.deselectRow(at: selectedIndexPath, animated: true)
}) { context in
if context.isCancelled {
self.methodsTableView?.selectRow(at: selectedIndexPath, animated: false, scrollPosition: .none)
}
}
} else {
@Sorix
Sorix / CurrentValue.swift
Created Oct 27, 2019
Basic publisher implementation to non-combine code
View CurrentValue.swift
import Foundation
/// An object that wraps a single value and publishes a new element whenever the value changes.
@propertyWrapper struct CurrentValue<Output> {
private let publisher = BasicPublisher()
var value: Output {
didSet {
publisher.handler?(oldValue, wrappedValue)
}
@Sorix
Sorix / safeTest.swift
Last active Jul 5, 2019
Array performance test for safe and normal get
View safeTest.swift
import Foundation
import XCTest
extension Collection where Indices.Iterator.Element == Index {
subscript(safe index: Index) -> Iterator.Element? {
return indices.contains(index) ? self[index] : nil
}
}
class PerfomanceTester: XCTestCase {
@Sorix
Sorix / Package.swift
Last active Nov 30, 2021
Example of Package.swift with environment variables support
View Package.swift
// swift-tools-version:4.0
import PackageDescription
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
enum Enviroment: String {
@Sorix
Sorix / SingleFetchedResultController.swift
Last active Jan 13, 2018
NSFetchedResultsController for single object
View SingleFetchedResultController.swift
import CoreData
protocol SingleFetchedResultsControllerDelegate: class {
func controller(didChange anObject: NSFetchRequestResult, for type: SingleFetchedResultsChangeType)
func controller(error: SingleFetchedResultsControllerError)
}
extension SingleFetchedResultsControllerDelegate {
func controller(_ controller: SingleFetchedResultsController<NSFetchRequestResult>, error: SingleFetchedResultsControllerError) { }
}
@Sorix
Sorix / KeyboardChangeFrameObserver.swift
Last active Nov 13, 2020
Report keyboard height changes
View KeyboardChangeFrameObserver.swift
import UIKit
/// Observer that will fire events when keyboard frame will be changed (shown, hidden or resized)
/// - Note: Call `addKeyboardFrameChangesObserver()` on init, e.g. on `viewWillAppear`
/// and `removeKeyboardFrameChangesObserver()` on deinit, e.g. on `viewDidDisappear`
public protocol KeyboardChangeFrameObserver: class {
func willChangeKeyboardFrame(height: CGFloat, animationDuration: TimeInterval, animationOptions: UIView.AnimationOptions)
}
public extension KeyboardChangeFrameObserver {
@Sorix
Sorix / FRCCollectionViewDataSource.swift
Created Oct 10, 2017
NSFetchedResultsControllerDelegate for UICollectionView
View FRCCollectionViewDataSource.swift
protocol FRCCollectionViewDelegate: class {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
}
class FRCCollectionViewDataSource<FetchRequestResult: NSFetchRequestResult>: NSObject, UICollectionViewDataSource, NSFetchedResultsControllerDelegate {
let frc: NSFetchedResultsController<FetchRequestResult>
weak var collectionView: UICollectionView?
weak var delegate: FRCCollectionViewDelegate?
@Sorix
Sorix / ColorableNavigationController.swift
Created Apr 12, 2017
Colourable UINavigationController that supports different colors for navigation bars among different view controllers
View ColorableNavigationController.swift
//
// ColorableNavigationController.swift
//
// Created by Vasily Ulianov on 26.10.16.
//
import UIKit
/// Navigation bar colors for `ColorableNavigationController`, called on `push` & `pop` actions
public protocol NavigationBarColorable: class {
var navigationTintColor: UIColor? { get }
@Sorix
Sorix / AsynchronousOperation.swift
Last active Oct 9, 2021
Subclass of NSOperation (Operation) to make it asynchronous in Swift 3, 4, 5
View AsynchronousOperation.swift
// Created by Vasily Ulianov on 09.02.17, updated in 2019.
// License: MIT
import Foundation
/// Subclass of `Operation` that adds support of asynchronous operations.
/// 1. Call `super.main()` when override `main` method.
/// 2. When operation is finished or cancelled set `state = .finished` or `finish()`
open class AsynchronousOperation: Operation {
public override var isAsynchronous: Bool {
@Sorix
Sorix / UIColor+hex.swift
Created Jan 27, 2017
UIColor with hex methods (init from hex, return hex value)
View UIColor+hex.swift
import UIKit
public extension UIColor {
/// Initializes and returns a color object using the specified hex value
///
/// - Parameter hex: hex value, example: `#ffffff` or `ffffff`
public convenience init(hex: String) {
let hexString = hex.trimmingCharacters(in: .whitespacesAndNewlines)
let scanner = Scanner(string: hexString)