Skip to content

Instantly share code, notes, and snippets.

@cipolleschi
cipolleschi / LoadContentVC3.swift
Last active April 18, 2020 19:29
Using protocols for dependency injection.
/// Protocol definition for the FileManager
protocol AppFileManager {
func contents(atPath: String)
}
/// Protocol conformance of the system FileManager
extension FileManager: AppFileManager {}
/// ViewController
class MyViewController: UIViewController {
@cipolleschi
cipolleschi / LoadContentVC3Test.swift
Last active April 18, 2020 19:31
Test by using protocols
import XCTest
import Foundation
/// File manager stub to override the methods we need
class FileManagerStub: AppFileManager {
func getContent(atPath: Stirng) -> Data? {
return Data()
}
}
@cipolleschi
cipolleschi / LoadContentVCTest.swift
Last active April 18, 2020 19:31
Load content test
import XCTest
import Foundation
/// File manager stub to override the methods we need
class FileManagerStub: FileManager {
override func getContent(atPath: Stirng) -> Data? {
return Data()
}
}
@cipolleschi
cipolleschi / Notification.swift
Last active April 18, 2020 11:49
Schedule notifications at given times
import UIKit
protocol NotificationScheduler {
func scheduleNotification(with title: String)
}
class NotificationManager {
let notificationScheduler: NotificationScheduler
init(notificationScheduler: NotificationScheduler) {
@cipolleschi
cipolleschi / Notification2.swift
Last active April 18, 2020 12:00
Improved notification
import UIKit
protocol NotificationScheduler {
func scheduleNotification(with title: String)
}
protocol TimeProvider {
var now: Date { get }
func calendar(with identifier: Calendar.Identifier) -> Calendar
}
import XCTest
class MockedNotificationScheduler: NotificationScheduler {
struct Notification: Equatable {
let title: String
}
var scheduledNotifications: [Notification] = []
func scheduleNotification(with title: String) {
@cipolleschi
cipolleschi / AView.swift
Created April 26, 2020 12:57
Basic structure of viewmodel and view
import Tempura
import BonMot
import PinLayout
struct AVM: ViewModelWithState {
init(state: AppState) {
}
}
class AView: UIView, ViewControllerModellableView {
var observers: [String: <#ObserverType#>] = [:]
func add(observer: <#ObserverType#>) -> String {
let uuid = UUID().uuidString
self.observers[uuid] = observer
return uuid
}
func remove(observer uuid: String) {
self.observers.remove(at: uuid)
struct <#ASideEffect#>: AppSideEffect {
func sideEffect(_ context: SideEffectContext<AppState, DependenciesContainer>) throws {
}
}
struct <#AStateUpdater#>: AppStateUpdater {
func updateState(_ state: inout AppState) {
static func waitForStateUpdate(context: SideEffectContext<AppState, DependenciesContainer>,
attempt: Int = 50,
shouldPass: @escaping (_ context: SideEffectContext<AppState, DependenciesContainer>) -> Bool) -> Promise<Void> {
func recursiveWait(context: SideEffectContext<AppState, DependenciesContainer>,
attempt: Int,
shouldPass: @escaping (_ context: SideEffectContext<AppState, DependenciesContainer>) -> Bool,
resolve: @escaping (())->(),
reject: @escaping (Error)->()) {