forked from davidlondono/VIPER-Generics-Generator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoutingProtocol.swift
154 lines (123 loc) · 4.75 KB
/
RoutingProtocol.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// RoutingProtocol.swift
// VIPER Generics
//
// Created by Alejo (alejouribesanchez) on 12/7/16.
// Copyright © 2016 gapps. All rights reserved.
import UIKit
protocol RoutingProtocolBase: class {
associatedtype InteractorType: InteractorProtocolBase
associatedtype PresenterType: PresenterProtocolBase
associatedtype ViewControllerType: ViewControllerProtocolBase
weak var viewController: ViewControllerType! {get set}
weak var interactor: InteractorType! {get set}
weak var presenter: PresenterType! {get set}
//function for loading the view
func loadView() -> ViewControllerType
//extra config executed after the viewControllerConfigured()
func extraConfigViewController()
static func buildViewController() throws -> ViewControllerType
init()
}
extension RoutingProtocolBase{
func viewControllerConfigured() throws -> ViewControllerType{
let newViewController = loadView()
let newPresenter = PresenterType()
let newInteractor = InteractorType()
presenter = newPresenter
interactor = newInteractor
viewController = newViewController
if let newInteractor = newInteractor as? PresenterType.InteractorType{
presenter.interactor = newInteractor
} else {
throw RoutingProtocolError.wrongInteractorProtocolPresenter
}
if let viewController = viewController as? PresenterType.ViewControllerType{
presenter.viewController = viewController
} else {
throw RoutingProtocolError.wrongViewProtocolPresenter
}
if let selfRouting = self as? PresenterType.RoutingType {
presenter.routing = selfRouting
} else {
throw RoutingProtocolError.wrongRoutingProtocolPresenter
}
if let presenter = presenter as? InteractorType.PresenterType{
interactor.presenter = presenter
} else {
throw RoutingProtocolError.wrongPresenterProtocolInteractor
}
if let presenter = presenter as? ViewControllerType.PresenterType {
viewController.presenter = presenter
} else {
throw RoutingProtocolError.wrongPresenterProtocolView
}
self.extraConfigViewController()
return viewController
}
func presentScene(router: Self, animated: Bool = true, completition: (() -> Void)?) throws {
guard let viewController = try viewControllerConfigured() as? UIViewController else {
return
}
if let routerController = router.viewController as? UIViewController{
routerController.present(viewController, animated: animated, completion: completition)
}
}
func loadFromWindow(window: UIWindow) throws{
if let controller = try self.viewControllerConfigured() as? UIViewController{
window.rootViewController = controller
}
}
func extraConfigViewController() {
//optional function
}
static func buildViewController() throws -> ViewControllerType{
let routing = Self.init()
return try routing.viewControllerConfigured()
}
}
enum RoutingProtocolError: Error {
case wrongInteractorProtocolPresenter
case wrongViewProtocolPresenter
case wrongPresenterProtocolInteractor
case wrongPresenterProtocolView
case wrongRoutingProtocolPresenter
}
protocol ViewProtocolPresenterBase: class {
}
protocol ViewControllerProtocolBase: ViewProtocolPresenterBase {
associatedtype PresenterType = PresenterProtocolViewBase
var presenter:PresenterType! {get set}
}
protocol PresenterProtocolViewBase: class {
func viewWillAppear()
func viewDidAppear()
func viewWillDisappear()
func viewDidDisappear()
func viewDidLoad()
}
extension PresenterProtocolViewBase {
func viewWillAppear(){}
func viewDidAppear(){}
func viewWillDisappear(){}
func viewDidDisappear(){}
func viewDidLoad(){}
}
protocol PresenterProtocolInteractorBase: class {
}
protocol PresenterProtocolBase: PresenterProtocolViewBase,PresenterProtocolInteractorBase {
associatedtype InteractorType //= protocol<InteractorProtocolPresenterBase>
associatedtype ViewControllerType //= protocol<ViewProtocolPresenterBase>
associatedtype RoutingType
var viewController: ViewControllerType? {get set}
var interactor: InteractorType! {get set}
var routing: RoutingType! {get set}
init()
}
protocol InteractorProtocolPresenterBase: class {
}
protocol InteractorProtocolBase:InteractorProtocolPresenterBase {
associatedtype PresenterType = PresenterProtocolInteractorBase
var presenter:PresenterType? {get set}
init()
}