-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
RNNControllerFactory.m
304 lines (249 loc) · 12.8 KB
/
RNNControllerFactory.m
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#import "RNNControllerFactory.h"
#import "BottomTabPresenterCreator.h"
#import "BottomTabsPresenterCreator.h"
#import "RNNBottomTabsController.h"
#import "RNNComponentViewController.h"
#import "RNNExternalViewController.h"
#import "RNNSideMenuController.h"
#import "RNNSplitViewController.h"
#import "RNNStackController.h"
#import "RNNTopTabsViewController.h"
@implementation RNNControllerFactory {
id<RNNComponentViewCreator> _creator;
RNNExternalComponentStore *_store;
RCTBridge *_bridge;
RNNReactComponentRegistry *_componentRegistry;
BottomTabsAttachModeFactory *_bottomTabsAttachModeFactory;
}
#pragma mark public
- (instancetype)initWithRootViewCreator:(id<RNNComponentViewCreator>)creator
eventEmitter:(RNNEventEmitter *)eventEmitter
store:(RNNExternalComponentStore *)store
componentRegistry:(RNNReactComponentRegistry *)componentRegistry
andBridge:(RCTBridge *)bridge
bottomTabsAttachModeFactory:(BottomTabsAttachModeFactory *)bottomTabsAttachModeFactory {
self = [super init];
_creator = creator;
_eventEmitter = eventEmitter;
_bridge = bridge;
_store = store;
_componentRegistry = componentRegistry;
_bottomTabsAttachModeFactory = bottomTabsAttachModeFactory;
return self;
}
- (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
_defaultOptions = defaultOptions;
_bottomTabsAttachModeFactory.defaultOptions = defaultOptions;
}
- (UIViewController *)createLayout:(NSDictionary *)layout {
UIViewController *layoutViewController = [self fromTree:layout];
return layoutViewController;
}
- (NSArray<RNNLayoutProtocol> *)createChildrenLayout:(NSArray *)children {
NSMutableArray<RNNLayoutProtocol> *childViewControllers =
[NSMutableArray<RNNLayoutProtocol> new];
for (NSDictionary *layout in children) {
[childViewControllers addObject:[self fromTree:layout]];
}
return childViewControllers;
}
#pragma mark private
- (UIViewController *)fromTree:(NSDictionary *)json {
RNNLayoutNode *node = [RNNLayoutNode create:json];
UIViewController *result;
if (node.isComponent) {
result = [self createComponent:node];
}
else if (node.isStack) {
result = [self createStack:node];
}
else if (node.isTabs) {
result = [self createBottomTabs:node];
}
else if (node.isTopTabs) {
result = [self createTopTabs:node];
}
else if (node.isSideMenuRoot) {
result = [self createSideMenu:node];
}
else if (node.isSideMenuCenter) {
result = [self createSideMenuChild:node type:RNNSideMenuChildTypeCenter];
}
else if (node.isSideMenuLeft) {
result = [self createSideMenuChild:node type:RNNSideMenuChildTypeLeft];
}
else if (node.isSideMenuRight) {
result = [self createSideMenuChild:node type:RNNSideMenuChildTypeRight];
}
else if (node.isExternalComponent) {
result = [self createExternalComponent:node];
}
else if (node.isSplitView) {
result = [self createSplitView:node];
}
if (!result) {
@throw [NSException
exceptionWithName:@"UnknownControllerType"
reason:[@"Unknown controller type " stringByAppendingString:node.type]
userInfo:nil];
}
return result;
}
- (UIViewController *)createComponent:(RNNLayoutNode *)node {
RNNLayoutInfo *layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
RNNNavigationOptions *options =
[[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
RNNButtonsPresenter *buttonsPresenter =
[[RNNButtonsPresenter alloc] initWithComponentRegistry:_componentRegistry
eventEmitter:_eventEmitter];
RNNComponentPresenter *presenter =
[[RNNComponentPresenter alloc] initWithComponentRegistry:_componentRegistry
defaultOptions:_defaultOptions
buttonsPresenter:buttonsPresenter];
RNNComponentViewController *component =
[[RNNComponentViewController alloc] initWithLayoutInfo:layoutInfo
rootViewCreator:_creator
eventEmitter:_eventEmitter
presenter:presenter
options:options
defaultOptions:_defaultOptions];
return component;
}
- (UIViewController *)createExternalComponent:(RNNLayoutNode *)node {
RNNLayoutInfo *layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
RNNNavigationOptions *options =
[[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
RNNButtonsPresenter *buttonsPresenter =
[[RNNButtonsPresenter alloc] initWithComponentRegistry:_componentRegistry
eventEmitter:_eventEmitter];
RNNComponentPresenter *presenter =
[[RNNComponentPresenter alloc] initWithComponentRegistry:_componentRegistry
defaultOptions:_defaultOptions
buttonsPresenter:buttonsPresenter];
UIViewController *externalVC = [_store getExternalComponent:layoutInfo bridge:_bridge];
RNNExternalViewController *component =
[[RNNExternalViewController alloc] initWithLayoutInfo:layoutInfo
eventEmitter:_eventEmitter
presenter:presenter
options:options
defaultOptions:_defaultOptions
viewController:externalVC];
return component;
}
- (UIViewController *)createStack:(RNNLayoutNode *)node {
RNNStackPresenter *presenter =
[[RNNStackPresenter alloc] initWithComponentRegistry:_componentRegistry
defaultOptions:_defaultOptions];
RNNLayoutInfo *layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
RNNNavigationOptions *options =
[[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
RNNStackController *stack =
[[RNNStackController alloc] initWithLayoutInfo:layoutInfo
creator:_creator
options:options
defaultOptions:_defaultOptions
presenter:presenter
eventEmitter:_eventEmitter
childViewControllers:childViewControllers];
return stack;
}
- (UIViewController *)createBottomTabs:(RNNLayoutNode *)node {
RNNLayoutInfo *layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
RNNNavigationOptions *options =
[[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
BottomTabsBasePresenter *presenter =
[BottomTabsPresenterCreator createWithDefaultOptions:_defaultOptions];
NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
BottomTabPresenter *bottomTabPresenter =
[BottomTabPresenterCreator createWithDefaultOptions:_defaultOptions];
RNNDotIndicatorPresenter *dotIndicatorPresenter =
[[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:_defaultOptions];
BottomTabsBaseAttacher *bottomTabsAttacher = [_bottomTabsAttachModeFactory fromOptions:options];
return [[RNNBottomTabsController alloc] initWithLayoutInfo:layoutInfo
creator:_creator
options:options
defaultOptions:_defaultOptions
presenter:presenter
bottomTabPresenter:bottomTabPresenter
dotIndicatorPresenter:dotIndicatorPresenter
eventEmitter:_eventEmitter
childViewControllers:childViewControllers
bottomTabsAttacher:bottomTabsAttacher];
}
- (UIViewController *)createTopTabs:(RNNLayoutNode *)node {
RNNLayoutInfo *layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
RNNNavigationOptions *options =
[[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
RNNBasePresenter *presenter = [[RNNBasePresenter alloc] initWithDefaultOptions:_defaultOptions];
NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
RNNTopTabsViewController *topTabsController =
[[RNNTopTabsViewController alloc] initWithLayoutInfo:layoutInfo
creator:_creator
options:options
defaultOptions:_defaultOptions
presenter:presenter
eventEmitter:_eventEmitter
childViewControllers:childViewControllers];
return topTabsController;
}
- (UIViewController *)createSideMenu:(RNNLayoutNode *)node {
RNNLayoutInfo *layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
RNNNavigationOptions *options =
[[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
RNNSideMenuPresenter *presenter =
[[RNNSideMenuPresenter alloc] initWithDefaultOptions:_defaultOptions];
NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
RNNSideMenuController *sideMenu =
[[RNNSideMenuController alloc] initWithLayoutInfo:layoutInfo
creator:_creator
childViewControllers:childViewControllers
options:options
defaultOptions:_defaultOptions
presenter:presenter
eventEmitter:_eventEmitter];
return sideMenu;
}
- (UIViewController *)createSideMenuChild:(RNNLayoutNode *)node type:(RNNSideMenuChildType)type {
UIViewController *childVc = [self fromTree:node.children[0]];
RNNLayoutInfo *layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
RNNNavigationOptions *options =
[[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc]
initWithLayoutInfo:layoutInfo
creator:_creator
options:options
defaultOptions:_defaultOptions
presenter:[[RNNBasePresenter alloc] initWithComponentRegistry:_componentRegistry
defaultOptions:_defaultOptions]
eventEmitter:_eventEmitter
childViewController:childVc
type:type];
return sideMenuChild;
}
- (UIViewController *)createSplitView:(RNNLayoutNode *)node {
RNNLayoutInfo *layoutInfo = [[RNNLayoutInfo alloc] initWithNode:node];
RNNNavigationOptions *options =
[[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
RNNSplitViewControllerPresenter *presenter =
[[RNNSplitViewControllerPresenter alloc] initWithDefaultOptions:_defaultOptions];
NSArray *childViewControllers = [self extractChildrenViewControllersFromNode:node];
RNNSplitViewController *splitViewController =
[[RNNSplitViewController alloc] initWithLayoutInfo:layoutInfo
creator:_creator
options:options
defaultOptions:_defaultOptions
presenter:presenter
eventEmitter:_eventEmitter
childViewControllers:childViewControllers];
return splitViewController;
}
- (NSArray<UIViewController *> *)extractChildrenViewControllersFromNode:(RNNLayoutNode *)node {
NSMutableArray *childrenArray = [NSMutableArray new];
for (NSDictionary *child in node.children) {
UIViewController *childVc = [self fromTree:child];
[childrenArray addObject:childVc];
}
return childrenArray;
}
@end