Skip to content

Instantly share code, notes, and snippets.

View V8tr's full-sized avatar

Vadim Bulavin V8tr

View GitHub Profile
@V8tr
V8tr / RefactoringAppDelegate-Composite.swift
Last active August 3, 2018 08:04
Refactoring Massive App Delegate using Composite pattern. See blog post for more details: https://www.vadimbulavin.com/refactoring-massive-app-delegate
enum AppDelegateFactory {
static func makeDefault() -> AppDelegateType {
return CompositeAppDelegate(appDelegates: [PushNotificationsAppDelegate(), StartupConfiguratorAppDelegate(), ThirdPartiesConfiguratorAppDelegate()])
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
@V8tr
V8tr / RefactoringAppDelegate-Composite.swift
Last active June 13, 2021 18:26
Refactoring Massive App Delegate using Composite pattern. See blog post for more details: https://www.vadimbulavin.com/refactoring-massive-app-delegate
class PushNotificationsAppDelegate: AppDelegateType {
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Registered successfully
}
}
class StartupConfiguratorAppDelegate: AppDelegateType {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Perform startup configurations, e.g. build UI stack, setup UIApperance
return true
@V8tr
V8tr / RefactoringAppDelegate-Composite.swift
Last active August 3, 2018 08:04
Refactoring Massive App Delegate using Mediator pattern. See blog post for more details: https://www.vadimbulavin.com/refactoring-massive-app-delegate
typealias AppDelegateType = UIResponder & UIApplicationDelegate
class CompositeAppDelegate: AppDelegateType {
private let appDelegates: [AppDelegateType]
init(appDelegates: [AppDelegateType]) {
self.appDelegates = appDelegates
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
@V8tr
V8tr / RefactoringAppDelegate-Mediator.swift
Created August 2, 2018 14:48
Refactoring Massive App Delegate using Mediator pattern. See blog post for more details: https://www.vadimbulavin.com/refactoring-massive-app-delegate
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let mediator = AppLifecycleMediator.makeDefaultMediator()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return true
}
}
@V8tr
V8tr / RefactoringAppDelegate-Mediator.swift
Last active August 2, 2018 14:47
Refactoring Massive App Delegate using Mediator pattern. See blog post for more details: https://www.vadimbulavin.com/refactoring-massive-app-delegate
// MARK: - AppLifecycleListener
protocol AppLifecycleListener {
func onAppWillEnterForeground()
func onAppDidEnterBackground()
func onAppDidFinishLaunching()
}
// MARK: - Mediator
@V8tr
V8tr / RefactoringAppDelegate-Command.swift
Last active August 3, 2018 08:58
Refactoring Massive App Delegate using Command pattern. See blog post for more details: https://www.vadimbulavin.com/refactoring-massive-app-delegate
// MARK: - Builder
final class StartupCommandsBuilder {
private var window: UIWindow!
func setKeyWindow(_ window: UIWindow) -> StartupCommandsBuilder {
self.window = window
return self
}
@V8tr
V8tr / MassiveAppDelegate-Commands.swift
Last active August 6, 2018 08:38
Refactoring Massive App Delegate using Command pattern. See blog post for more details: https://www.vadimbulavin.com/refactoring-massive-app-delegate
protocol Command {
func execute()
}
struct InitializeThirdPartiesCommand: Command {
func execute() {
// Third parties are initialized here
}
}
@V8tr
V8tr / AverageBenchmark.swift
Last active July 23, 2018 08:11
Average benchmark for arbitrary block of code in Swift. Used to compensate deviations of individual benchmark iterations. See blog post for more details: http://www.vadimbulavin.com/benchmarking-locking-apis/
func averageBenchmark(iterations: Int, numberOfAttempts: Int, block: (Int) -> Void) -> TimeInterval {
var accumulatedResult: TimeInterval = 0
for _ in 0..<numberOfAttempts {
let result = benchmark {
for i in 0..<iterations {
block(i)
}
}
accumulatedResult += result
@V8tr
V8tr / Bechmark.swift
Last active January 20, 2021 12:07
Benchmark arbitrary block of code in Swift. See blog post for more details: http://www.vadimbulavin.com/benchmarking-locking-apis/
func benchmark(block: () -> Void) -> TimeInterval {
let startTime = CFAbsoluteTimeGetCurrent()
block()
let endTime = CFAbsoluteTimeGetCurrent()
let totalTime = endTime - startTime
return totalTime
}
@V8tr
V8tr / ReadWriteLockAtomicProperty.swift
Created July 14, 2018 07:15
Atomic property in Swift using ReadWriteLock. See blog post for more details: http://www.vadimbulavin.com/atomic-properties/
final class ReadWriteLock {
private var rwlock: pthread_rwlock_t = {
var rwlock = pthread_rwlock_t()
pthread_rwlock_init(&rwlock, nil)
return rwlock
}()
func writeLock() {
pthread_rwlock_wrlock(&rwlock)
}