From 22e9b949c668716035102027cecf6b209aec029e Mon Sep 17 00:00:00 2001 From: Thomas Verbeek Date: Tue, 15 Sep 2020 01:03:56 +1200 Subject: [PATCH] Simplified subscription code. Added documentation. --- Sources/VIPER/VIPER.swift | 67 +++++++++++-------------------- Tests/VIPERTests/VIPERTests.swift | 3 -- 2 files changed, 23 insertions(+), 47 deletions(-) diff --git a/Sources/VIPER/VIPER.swift b/Sources/VIPER/VIPER.swift index f9f601d..dfa7d8b 100644 --- a/Sources/VIPER/VIPER.swift +++ b/Sources/VIPER/VIPER.swift @@ -2,13 +2,6 @@ import Combine import Foundation import ObjectiveC -private struct Key { - static var interaction = "VIPER.interaction" - static var presentation = "VIPER.presentation" - static var navigation = "VIPER.navigation" - static var view = "VIPER.view" -} - /** A VIPER View represents the UI logic of your screen. @@ -106,7 +99,15 @@ public protocol VIPERRouter { container. */ associatedtype Builder + + /** + A navigation message that the router receives when instructed to navigate. + */ associatedtype Navigation + + /** + The view associated with this Router. + */ associatedtype View var builder: Builder { get } @@ -123,11 +124,16 @@ public protocol VIPERRouter { /** Invoked when the view is assembled. Override this to configure the view. + - Parameters: + - view: The view to configure. */ func viewDidLoad(view: View) /** Invoked when the router receives a navigation instruction. + - Parameters: + - navigation: A navigation instruction for the Router to interpret and process. + - view: The view provided as a presentation context. */ func receive(navigation: Navigation, for view: View) } @@ -183,19 +189,23 @@ public final class VIPERModule() + + view.interactor.sink { [interactor] userInteraction in interactor.receive(userInteraction: userInteraction) - } + }.store(in: &subscriptions) - view.presentationSubscription = interactor.presenter.sink { [weak view] presentation in + interactor.presenter.sink { [weak view] presentation in view?.receive(input: Presenter.map(input: presentation)) - } + }.store(in: &subscriptions) - view.navigationSubscription = interactor.router.sink { [router, weak view] navigation in + interactor.router.sink { [router, weak view] navigation in guard let view = view else { return } router.receive(navigation: navigation, for: view) - } + }.store(in: &subscriptions) + objc_setAssociatedObject(view, "VIPER.subscriptions", subscriptions, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) + router.viewDidLoad(view: view) return (view: view, interactor: interactor, router: router) @@ -220,34 +230,3 @@ public final class VIPERModule