123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
-
- #import "RNNControllerFactory.h"
- #import "RNNLayoutNode.h"
- #import "RNNRootViewController.h"
- #import "RNNSideMenuController.h"
- #import "RNNSideMenuChildVC.h"
-
-
- @implementation RNNControllerFactory {
- id<RNNRootViewCreator> _creator;
- RNNStore *_store;
- RNNEventEmitter *_eventEmitter;
- }
-
- # pragma mark public
-
-
- - (instancetype)initWithRootViewCreator:(id <RNNRootViewCreator>)creator
- store:(RNNStore *)store
- eventEmitter:(RNNEventEmitter*)eventEmitter {
-
- self = [super init];
- _creator = creator;
- _store = store;
- _eventEmitter = eventEmitter;
-
- return self;
- }
-
- - (UIViewController*)createLayoutAndSaveToStore:(NSDictionary*)layout {
- return [self fromTree:layout];
- }
-
- # pragma mark private
-
- - (UIViewController*)fromTree:(NSDictionary*)json {
- RNNLayoutNode* node = [RNNLayoutNode create:json];
-
- UIViewController* result;
-
- if ( node.isContainer) {
- result = [self createContainer:node];
- }
-
- else if (node.isContainerStack) {
- result = [self createContainerStack:node];
- }
-
- else if (node.isTabs) {
- result = [self createTabs: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];
- }
-
- if (!result) {
- @throw [NSException exceptionWithName:@"UnknownControllerType" reason:[@"Unknown controller type " stringByAppendingString:node.type] userInfo:nil];
- }
-
- [_store setContainer:result containerId:node.nodeId];
-
- return result;
- }
-
- - (RNNRootViewController*)createContainer:(RNNLayoutNode*)node {
- return [[RNNRootViewController alloc] initWithNode:node rootViewCreator:_creator eventEmitter:_eventEmitter];
- }
-
- - (UINavigationController*)createContainerStack:(RNNLayoutNode*)node {
- UINavigationController* vc = [[UINavigationController alloc] init];
-
- NSMutableArray* controllers = [NSMutableArray new];
- for (NSDictionary* child in node.children) {
- [controllers addObject:[self fromTree:child]];
- }
- [vc setViewControllers:controllers];
-
- return vc;
- }
-
- -(UITabBarController*)createTabs:(RNNLayoutNode*)node {
- UITabBarController* vc = [[UITabBarController alloc] init];
-
- NSMutableArray* controllers = [NSMutableArray new];
- for (NSDictionary *child in node.children) {
- UIViewController* childVc = [self fromTree:child];
-
- UITabBarItem* item = [[UITabBarItem alloc] initWithTitle:@"A Tab" image:nil tag:1];
- [childVc setTabBarItem:item];
- [controllers addObject:childVc];
- }
- [vc setViewControllers:controllers];
-
- return vc;
- }
-
- - (UIViewController*)createSideMenu:(RNNLayoutNode*)node {
- NSMutableArray* childrenVCs = [NSMutableArray new];
-
-
- for (NSDictionary *child in node.children) {
- UIViewController *vc = [self fromTree:child];
- [childrenVCs addObject:vc];
- }
-
- RNNSideMenuController *sideMenu = [[RNNSideMenuController alloc] initWithControllers:childrenVCs];
- return sideMenu;
- }
-
-
- - (UIViewController*)createSideMenuChild:(RNNLayoutNode*)node type:(RNNSideMenuChildType)type {
- UIViewController* child = [self fromTree:node.children[0]];
- RNNSideMenuChildVC *sideMenuChild = [[RNNSideMenuChildVC alloc] initWithChild: child type:type];
-
- return sideMenuChild;
- }
-
-
-
- @end
|