Skip to content

Instantly share code, notes, and snippets.

View gringoireDM's full-sized avatar

Giuseppe Lanza gringoireDM

View GitHub Profile

Allow @escaping for optional closures as functions parameters

Introduction

A closure is said to escape a function when the closure is passed as an argument

@gringoireDM
gringoireDM / eventsCaptureMERLin.swift
Created May 22, 2019 18:46
Ways to capture events in MERLin
class MyModule: NSObject, EventsProducer {
...
public var events: Observable<ProductDetailPageEvent>
...
}
let producer = MyModule(usingContext: ...)
//you can use EventsProucer subscript
producer[event: ProductDetailPageEvent.didAddToCart] //didAddToCart has a BagProductProtocol in the payload
@gringoireDM
gringoireDM / AppDelegateExample.swift
Last active May 22, 2019 18:54
An example of AppDelegate using MERLin
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var moduleManager: BaseModuleManager = BaseModuleManager()
var router: SimpleRouter!
lazy var eventsListeners: [AnyEventsListening] = {
[
ConsoleLogEventsListener(),
@gringoireDM
gringoireDM / RestaurantsListModule.swift
Last active May 22, 2019 15:56
An example of Module in MERLin
extension UIStoryboard {
static var restaurantsList: UIStoryboard {
return UIStoryboard(name: "RestaurantsList", bundle: Bundle(for: RestaurantsListModule.self))
}
}
public class RestaurantsListModule: NSObject, ModuleProtocol, EventsProducer, PageRepresenting {
public var context: ModuleContext
public var pageName: String = "Restaurants List"
@gringoireDM
gringoireDM / EventsListenersExamples.swift
Created May 22, 2019 15:48
Examples of Events Listeners
//This events listener is interested in any event from any producer
//The purpose of this listener is to log the event with a print
class EventsLogger: AnyEventsListening {
@discardableResult func registerToEvents(for producer: AnyEventsProducer) -> Bool {
producer.anyEvent
.map { String(describing: $0) }
.subscribe(onNext: { print($0) })
.disposed(by: producer.disposeBag)
return true
}
@gringoireDM
gringoireDM / SimpleRouter.swift
Last active May 22, 2019 19:58
MERLin minimum Router implementation
class SimpleRouter: Router {
var viewControllersFactory: ViewControllersFactory?
required init(withFactory factory: ViewControllersFactory) {
viewControllersFactory = factory
}
var topViewController: UIViewController { return rootNavigationController }
private lazy var rootNavigationController: UINavigationController = {
let presentableStep = PresentableRoutingStep(withStep: .restaurantsList(), presentationMode: .none)
public enum ProductDetailPageEvent: EventProtocol {
case productLoaded(FullProductProtocol)
case didAddToCart(BagProductProtocol)
case didAddProductToWaitlist(FullProductProtocol)
case didShareProduct(MinimumProductProtocol)
case didChangeSize(newSize: ProductSizeProtocol)
case didChangeColor(newColor: ProductColorProtocol)
}