-
Notifications
You must be signed in to change notification settings - Fork 3
/
RoutingProtocol.swift
175 lines (135 loc) · 4.98 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// RoutingProtocol.swift
// VIPER Generics
//
// Created by David Alejandro (davidlondono) on 1/7/16.
// Copyright © 2016 David Alejandro (davidlondono). All rights reserved.
//
import UIKit
protocol RoutingProtocolBase: class {
typealias InteractorType: InteractorProtocolBase
typealias PresenterType: PresenterProtocolBase
typealias 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()
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.presentViewController(viewController, animated: animated, completion: completition)
}
}
func extraConfigViewController() {
//optional function
}
}
enum RoutingProtocolError: ErrorType {
case wrongInteractorProtocolPresenter
case wrongViewProtocolPresenter
case wrongPresenterProtocolInteractor
case wrongPresenterProtocolView
case wrongRoutingProtocolPresenter
}
protocol ViewProtocolPresenterBase: class {
}
protocol ViewControllerProtocolBase: ViewProtocolPresenterBase {
typealias PresenterType = protocol<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 {
typealias InteractorType = protocol<InteractorProtocolPresenterBase>
typealias ViewControllerType = protocol<ViewProtocolPresenterBase>
typealias RoutingType
var viewController: ViewControllerType? {get set}
var interactor: InteractorType! {get set}
var routing: RoutingType! {get set}
init()
}
protocol InteractorProtocolPresenterBase: class {
}
protocol InteractorProtocolBase:InteractorProtocolPresenterBase {
typealias PresenterType = protocol<PresenterProtocolInteractorBase>
var presenter:PresenterType? {get set}
init()
}
protocol myPresenterView: PresenterProtocolViewBase{
}
protocol myViewPresener: ViewProtocolPresenterBase{
}
protocol myInteractorPresener: InteractorProtocolPresenterBase{
}
class myPresenter: PresenterProtocolBase {
typealias ViewControllerType = myViewPresener
weak var viewController: myViewPresener?
var interactor: myInteractorPresener!
var routing: AnyObject!
required init(){
}
}
class myView: ViewControllerProtocolBase {
var presenter:myPresenterView!
}
class BaseViewController<P:PresenterProtocolViewBase>: ViewControllerProtocolBase{
//typealias PresenterType = P
var presenter:P!
}