Skip to content

Instantly share code, notes, and snippets.

View Sorix's full-sized avatar

Vasily Ulianov Sorix

View GitHub Profile
@Sorix
Sorix / UIColor+hex.swift
Created January 27, 2017 13:24
UIColor with hex methods (init from hex, return hex value)
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)
@Sorix
Sorix / KeyboardRelatedConstraintAnimator.swift
Last active January 11, 2018 22:55
Move view when keyboard appears, changes frame, disappears
import UIKit
/// Animate and modify constraint's constant when keyboard appears, changes frames or disappears.
/// Use-case: move content above keyboard when it appears.
class KeyboardRelatedConstraintAnimator {
unowned let constraint: NSLayoutConstraint
unowned let view: UIView
private var defaultValue: CGFloat
@Sorix
Sorix / SingleFetchedResultController.swift
Last active January 13, 2018 12:46
NSFetchedResultsController for single object
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 / safeTest.swift
Last active July 5, 2019 21:31
Array performance test for safe and normal get
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 / CurrentValue.swift
Created October 27, 2019 12:02
Basic publisher implementation to non-combine code
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)
}
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 / FRCCollectionViewDataSource.swift
Created October 10, 2017 23:15
NSFetchedResultsControllerDelegate for UICollectionView
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 April 12, 2017 14:13
Colourable UINavigationController that supports different colors for navigation bars among different view controllers
//
// 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 / KeyboardChangeFrameObserver.swift
Last active May 21, 2022 16:03
Report keyboard height changes
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 / RoundedLabel.swift
Last active July 1, 2022 01:33
IBDesignable UILabel with rounded corners and paddings
import UIKit
@IBDesignable
class RoundedLabel: UILabel {
var edgeInsets: UIEdgeInsets {
if autoPadding {
if cornerRadius == 0 {
return UIEdgeInsets.zero
} else {