Skip to content

Commit

Permalink
Simplified subscription code. Added documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomverbeek committed Sep 14, 2020
1 parent 473c377 commit 22e9b94
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 47 deletions.
67 changes: 23 additions & 44 deletions Sources/VIPER/VIPER.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 }
Expand All @@ -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)
}
Expand Down Expand Up @@ -183,19 +189,23 @@ public final class VIPERModule<View: VIPERView & NSObject, Interactor: VIPERInte
let interactor = Interactor(entities: entities)
let view = View(input: Presenter.map(input: interactor.presenter.value))

view.interactionSubscription = view.interactor.sink { [interactor] userInteraction in
var subscriptions = Set<AnyCancellable>()

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)
Expand All @@ -220,34 +230,3 @@ public final class VIPERModule<View: VIPERView & NSObject, Interactor: VIPERInte
}

}

internal extension NSObject {

var interactionSubscription: AnyCancellable? {
get {
objc_getAssociatedObject(self, &Key.interaction) as? AnyCancellable
}
set {
objc_setAssociatedObject(self, &Key.interaction, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}

var presentationSubscription: AnyCancellable? {
get {
objc_getAssociatedObject(self, &Key.presentation) as? AnyCancellable
}
set {
objc_setAssociatedObject(self, &Key.presentation, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}

var navigationSubscription: AnyCancellable? {
get {
objc_getAssociatedObject(self, &Key.navigation) as? AnyCancellable
}
set {
objc_setAssociatedObject(self, &Key.navigation, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}

}
3 changes: 0 additions & 3 deletions Tests/VIPERTests/VIPERTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ extension VIPERTests {
XCTAssertNotNil(view)
XCTAssertNotNil(interactor)
XCTAssertNotNil(router)
XCTAssertNotNil(view?.interactionSubscription)
XCTAssertNotNil(view?.presentationSubscription)
XCTAssertNotNil(view?.navigationSubscription)

// act
view = nil
Expand Down

0 comments on commit 22e9b94

Please sign in to comment.