Browse Source

WOW! no recursion on app layout creation - iterative baby!

Ran Greenberg 7 years ago
parent
commit
64bae885b1
2 changed files with 91 additions and 102 deletions
  1. 90
    101
      ios/RNNViewController.m
  2. 1
    1
      src2/commands/valid-commands.js

+ 90
- 101
ios/RNNViewController.m View File

@@ -1,15 +1,8 @@
1
-//
2
-//  RNNViewController.m
3
-//  ReactNativeNavigation
4
-//
5
-//  Created by Ran Greenberg on 21/12/2016.
6
-//  Copyright © 2016 artal. All rights reserved.
7
-//
8
-
9 1
 #import "RNNViewController.h"
10 2
 #import "RCTRootView.h"
11 3
 #import "MMDrawerController.h"
12 4
 
5
+
13 6
 #define SCREEN                  @"screen"
14 7
 #define SIDE_MENU               @"sideMenu"
15 8
 #define TABS                    @"tabs"
@@ -18,141 +11,137 @@
18 11
 #define SIDE_MENU_RIGHT         @"right"
19 12
 
20 13
 
21
-@interface RNNViewController ()
22
-@end
14
+typedef enum
15
+{
16
+    SideMenuModeNone        = 0,
17
+    SideMenuModeLeft        = 1 << 0,
18
+    SideMenuModeRight       = 1 << 1,
19
+} SideMenuMode;
23 20
 
24 21
 
25 22
 @implementation RNNViewController
26 23
 
27 24
 
28
-#pragma mark - Static function
29
-
30
-
31
-+ (UIViewController*)controllerWithLayout:(NSDictionary *)layout bridge:(RCTBridge *)bridge {
32
-    
33
-    UIViewController *controller = nil;
34
-    NSDictionary *screen = layout[SCREEN];
35
-    NSArray *tabs = layout[TABS];
25
++(SideMenuMode)sideMenuModeForLayout:(NSDictionary*)layout {
36 26
     NSDictionary *sideMenu = layout[SIDE_MENU];
37
-    
27
+    SideMenuMode mode = SideMenuModeNone;
38 28
     if (sideMenu) {
39
-        NSDictionary *centerScreenDictionary;
40
-        if (screen) {
41
-            centerScreenDictionary = @{SCREEN: screen};
29
+        if (sideMenu[SIDE_MENU_LEFT]) {
30
+            mode |= SideMenuModeLeft;
42 31
         }
43
-        else if (tabs){
44
-            centerScreenDictionary = @{TABS: layout[TABS]};
32
+        if (sideMenu[SIDE_MENU_RIGHT]) {
33
+            mode |= SideMenuModeRight;
45 34
         }
46
-        else {
47
-            return nil;
48
-        }
49
-        
50
-        UIViewController *centerViewController = [RNNViewController controllerWithLayout:centerScreenDictionary bridge:bridge];
51
-        controller = [RNNViewController sideMenuWithLayout:sideMenu centerViewController:centerViewController bridge:bridge];
52
-        
53
-        return controller;
54 35
     }
36
+    return mode;
37
+}
38
+
39
+
40
++ (UIViewController*)controllerWithLayout:(NSDictionary *)layout bridge:(RCTBridge *)bridge {
41
+    UIViewController *controller = nil;
42
+    SideMenuMode sideMenuMode = [RNNViewController sideMenuModeForLayout:layout];
43
+    UIViewController *centerViewController = [RNNViewController centerControllerWithLayout:layout bridge:bridge];
55 44
     
56
-    if (tabs) {
57
-        controller = [RNNViewController tabBarWithTabsArray:tabs bridge:bridge];
58
-        return controller;
45
+    if (sideMenuMode == SideMenuModeNone) {
46
+        controller = centerViewController;
59 47
     }
60
-    
61
-    if (screen) {
62
-        NSString *screenKey = screen[SCREEN_KEY];
63
-        if (screenKey) {
64
-            controller = [RNNViewController navigationControllerWithScreenKey:screenKey bridge:bridge];
65
-        }
66
-        
67
-        return controller;
48
+    else {
49
+        NSDictionary *sideMenuLayout = layout[SIDE_MENU];
50
+        controller = [RNNViewController sideMenuControllerWithSideMenuLayout:sideMenuLayout
51
+                                                                sideMenuMode:sideMenuMode
52
+                                                        centerViewController:centerViewController
53
+                                                                      bridge:bridge];
68 54
     }
69
-    
70 55
     return controller;
71 56
 }
72 57
 
73 58
 
74
-+(UIViewController*)controllerWithScreenKey:(NSString*)screenKey bridge:(RCTBridge *)bridge {
75
-    
59
++(UIViewController*)centerControllerWithLayout:(NSDictionary*)layout bridge:(RCTBridge*)bridge {
76 60
     UIViewController *controller = nil;
77 61
     
78
-    RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:screenKey initialProperties:nil];
79
-    if (!reactView) return nil;
80
-    
81
-    controller = [UIViewController new];
82
-    controller.view = reactView;
62
+    NSDictionary *tabs = layout[TABS];
63
+    NSDictionary *screen = layout[SCREEN];
64
+    if (tabs) {
65
+        controller = [RNNViewController tabBarControllerWithArray:tabs bridge:bridge];
66
+    }
67
+    else if (screen) {
68
+        NSDictionary *screenLayout = @{SCREEN: screen};
69
+        controller = [RNNViewController controllerWithScreenLayout:screenLayout bridge:bridge];
70
+    }
83 71
     
84 72
     return controller;
85 73
 }
86 74
 
87 75
 
88
-+(UINavigationController*)navigationControllerWithScreenKey:(NSString*)screenKey bridge:(RCTBridge*)bridge {
76
++(MMDrawerController*)sideMenuControllerWithSideMenuLayout:(NSDictionary*)sideMenuLayout sideMenuMode:(SideMenuMode)sideMenuMode centerViewController:(UIViewController*)centerVC bridge:(RCTBridge*)bridge {
77
+    UIViewController *leftVC, *rightVC = nil;
89 78
     
90
-    UINavigationController *controller = nil;
79
+    if (sideMenuMode & SideMenuModeLeft) {
80
+        NSDictionary *leftLayout = sideMenuLayout[SIDE_MENU_LEFT];
81
+        NSString *leftScreenKey = leftLayout[SCREEN_KEY];
82
+        leftVC = [RNNViewController controllerWithScreenKey:leftScreenKey bridge:bridge];
83
+    }
84
+    if (sideMenuMode & SideMenuModeRight) {
85
+        NSDictionary *rightLayout = sideMenuLayout[SIDE_MENU_RIGHT];
86
+        NSString *rightScreenKey = rightLayout[SCREEN_KEY];
87
+        rightVC = [RNNViewController controllerWithScreenKey:rightScreenKey bridge:bridge];
88
+    }
91 89
     
92
-    UIViewController *viewController = [RNNViewController controllerWithScreenKey:screenKey bridge:bridge];
93
-    if (!viewController) return nil;
90
+    MMDrawerController *controller = [[MMDrawerController alloc] initWithCenterViewController:centerVC
91
+                                                                     leftDrawerViewController:leftVC
92
+                                                                    rightDrawerViewController:rightVC];
94 93
     
95
-    controller = [[UINavigationController alloc] initWithRootViewController:viewController];
96
-    [controller.tabBarItem setTitle:@"tab"];
94
+    controller.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
95
+    controller.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
97 96
     
98 97
     return controller;
99 98
 }
100 99
 
101 100
 
102
-+(UIViewController*)sideMenuWithLayout:(NSDictionary*)layout centerViewController:(UIViewController*)centerViewController bridge:(RCTBridge*)bridge {
103
-    
104
-    UIViewController *leftViewController, *rightViewController, *sideMenuViewController = nil;
101
++(UITabBarController*)tabBarControllerWithArray:(NSArray*)tabs bridge:(RCTBridge*)bridge {
102
+    UIViewController *controller = nil;
105 103
     
106
-    NSDictionary *left = layout[SIDE_MENU_LEFT];
107
-    if (left) {
108
-        NSString *leftScreenKey = left[SCREEN_KEY];
109
-        leftViewController = [RNNViewController controllerWithScreenKey:leftScreenKey bridge:bridge];
104
+    NSMutableArray *tabsVC = [NSMutableArray new];
105
+    for (NSDictionary* tab in tabs) {
106
+        UIViewController *tabVC = [RNNViewController controllerWithScreenLayout:tab bridge:bridge];
107
+        if (tabVC) {
108
+            [tabsVC addObject:tabVC];
109
+        }
110 110
     }
111
-    
112
-    NSDictionary *right = layout[SIDE_MENU_RIGHT];
113
-    if (right) {
114
-        NSString *rightScreenKey = right[SCREEN_KEY];
115
-        rightViewController = [RNNViewController controllerWithScreenKey:rightScreenKey bridge:bridge];
111
+    if ([tabsVC count] > 0) {
112
+        UITabBarController *tabController = [[UITabBarController alloc] init];
113
+        tabController.viewControllers = tabsVC;
114
+        controller = tabController;
116 115
     }
117
-    
118
-    if (rightViewController || leftViewController) {
119
-        sideMenuViewController = [[MMDrawerController alloc] initWithCenterViewController:centerViewController leftDrawerViewController:leftViewController rightDrawerViewController:rightViewController];
120
-        
121
-        // TODO: CHANGE THIS
122
-        ((MMDrawerController*)sideMenuViewController).openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
123
-        ((MMDrawerController*)sideMenuViewController).closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
124
-    }
125
-    
126
-    return sideMenuViewController;
116
+    return controller;
127 117
 }
128 118
 
129
-+(UITabBarController*)tabBarWithTabsArray:(NSArray*)tabsArray bridge:(RCTBridge*)bridge {
130
-    
131
-    UITabBarController *tabBarController = nil;
132
-    NSMutableArray *tabsViewControllersArray = [[NSMutableArray alloc] init];
133
-    
134
-    for (NSDictionary *tab in tabsArray) {
135
-        UIViewController *tabViewController = [RNNViewController controllerWithLayout:tab bridge:bridge];
136
-        if (tabViewController) {
137
-            [tabsViewControllersArray addObject:tabViewController];
119
+
120
++(UIViewController*)controllerWithScreenLayout:(NSDictionary*)screenLayout bridge:(RCTBridge*)bridge {
121
+    UIViewController *controller = nil;
122
+    NSDictionary *screen = screenLayout[SCREEN];
123
+    NSDictionary *screenKey = screen[SCREEN_KEY];
124
+    if (screen && screenKey) {
125
+        UIViewController *rootVC = [RNNViewController controllerWithScreenKey:screenKey bridge:bridge];
126
+        if (rootVC) {
127
+            controller = [[UINavigationController alloc] initWithRootViewController:rootVC];
128
+            [controller.tabBarItem setTitle:screenKey];
138 129
         }
139 130
     }
140
-    if ([tabsViewControllersArray count] > 0) {
141
-        tabBarController = [UITabBarController new];
142
-        tabBarController.viewControllers = tabsViewControllersArray;
143
-    }
144
-    
145
-    return tabBarController;
146
-}
147
-
148
--(void)viewWillAppear:(BOOL)animated {
149
-    [super viewWillAppear:animated];
150
-    
131
+    return controller;
151 132
 }
152 133
 
153 134
 
154
-
135
++(UIViewController*)controllerWithScreenKey:(NSString*)screenKey bridge:(RCTBridge*)bridge {
136
+    UIViewController *controller = nil;
137
+    if (screenKey) {
138
+        RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:screenKey initialProperties:nil];
139
+        if (!reactView) return nil;
140
+        controller = [UIViewController new];
141
+        controller.view = reactView;
142
+    }
143
+    return controller;
144
+}
155 145
 
156 146
 
157 147
 @end
158
-

+ 1
- 1
src2/commands/valid-commands.js View File

@@ -1,6 +1,6 @@
1 1
 export const singleScreenApp = {
2 2
   screen: {
3
-    key: 'example.FirstTabScreen'
3
+    key: 'com.example.FirstTabScreen'
4 4
   }
5 5
 };
6 6