Skip to content

Instantly share code, notes, and snippets.

View descorp's full-sized avatar

Vladimir Abramichev descorp

  • Adyen
  • Amsterdam
View GitHub Profile
@descorp
descorp / Event.swift
Created November 22, 2019 13:44
Swift's C#-like events
import Foundation
public protocol Invocable: class {
func invoke(_ notification: String, _ data: Any)
}
public protocol Observer: Invocable {
associatedtype Value
typealias Callback = (_ notification: String, _ data: Value) -> Void
var callback: Callback { get }
@descorp
descorp / BaseTableHandler.swift
Created November 22, 2019 13:39
Swift Universal Table handlers: allows to load and manage table collections for single type and multiple types of cells
import UIKit
class BaseTableHandler<T> : NSObject, UITableViewDelegate, UITableViewDataSource {
typealias TableSection = (name: String?, items: [T])
typealias Action = (T)->Void
var collection: [TableSection] = []
weak var table: UITableView?
private let selectAction: Action?
@descorp
descorp / TagCloudCollectionView.swift
Created November 22, 2019 13:33
Swift TagCloudCollectionView
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class EdgeInsetLabel: UILabel {
private let textInsets: UIEdgeInsets
init(inset: UIEdgeInsets) {
textInsets = inset
public protocol TableCellGenerator: class {
var identifier: UITableViewCell.Type { get }
var cellHeight: CGFloat { get }
var estimatedCellHeight: CGFloat? { get }
func generate(tableView: UITableView, for indexPath: IndexPath) -> UITableViewCell
func registerCell(in tableView: UITableView)
}
extension XCTest {
func expectToEventually(_ test: @autoclosure () -> Bool, timeout: TimeInterval = 1.0, message: String = "") {
let runLoop = RunLoop.current
let timeoutDate = Date(timeIntervalSinceNow: timeout)
repeat {
// 1
if test() {
return
}
// 2
@descorp
descorp / DecimalExtension.swift
Created October 24, 2018 13:10
Float Precision
extension Decimal {
init(value: NSNumber, precision: Int) {
let exponent = pow(10.0, Double(precision))
let mantisa = Decimal((value.doubleValue * exponent).rounded())
let sign = value.floatValue > 0 ? FloatingPointSign.plus : FloatingPointSign.minus
self = Decimal(sign: sign, exponent: -precision, significand: mantisa)
}
}
@descorp
descorp / Compare.m
Last active September 26, 2018 15:14
Date Extension
- (NSComparisonResult)compareDatesWith:(NSDate*)otherDate {
NSCalendar* calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:self];
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:otherDate];
NSInteger comparison = [comp1 year] * 10000 + [comp1 month] * 100 + [comp1 day] -
([comp2 year] * 10000 + [comp2 month] * 100 + [comp2 day]);
return comparison > 0 ? NSOrderedDescending : comparison < 0 ? NSOrderedAscending : NSOrderedSame;
@implementation JsonCleaner
- (NSDictionary*)clearJsonDictionaryFromPrivateProperties:(NSDictionary*)jsonDictionary {
NSMutableDictionary *tempDict = [NSMutableDictionary dictionaryWithDictionary:jsonDictionary];
for (NSString* key in tempDict.allKeys) {
if ([tempDict[key] isKindOfClass:NSDictionary.class]) {
tempDict[key] = [self clearJsonDictionaryFromPrivateProperties:tempDict[key]];
} else if ([tempDict[key] isKindOfClass:NSArray.class] &&
[tempDict[key][0] isKindOfClass:NSDictionary.class]) {
NSMutableArray *array = [NSMutableArray array];
@descorp
descorp / DtoPrinter.swift
Created July 10, 2018 10:00
DTO printer
import Foundation
import PortfolioSummaryClient
protocol ClassInfo : CustomDebugStringConvertible {
var className: String { get }
var properties: String { get }
}
extension NSObject : ClassInfo {
@descorp
descorp / Array_profiler.cs
Last active October 20, 2017 09:16
Xamarin iOS Array profiler
public interface IProfilable {
TimeSpan MeasureWrite();
TimeSpan MeasureRead();
TimeSpan MeasureFind();
}
public class Result {
public Measurement[] measurements;
public Result(params Measurement[] array) {