Browse Source

simple native recursive parsing

Daniel Zlotin 7 years ago
parent
commit
db0c822df6

+ 1
- 1
ios/RNNBridgeModule.m View File

14
 
14
 
15
 RCT_EXPORT_METHOD(startApp:(NSDictionary*)layout)
15
 RCT_EXPORT_METHOD(startApp:(NSDictionary*)layout)
16
 {
16
 {
17
-    UIApplication.sharedApplication.delegate.window.rootViewController = [RNNControllerFactory createRootViewController:layout];
17
+    UIApplication.sharedApplication.delegate.window.rootViewController = [[RNNControllerFactory new] createRootViewController:layout];
18
     [UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
18
     [UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
19
 }
19
 }
20
 
20
 

+ 1
- 1
ios/RNNControllerFactory.h View File

4
 
4
 
5
 @interface RNNControllerFactory : NSObject
5
 @interface RNNControllerFactory : NSObject
6
 
6
 
7
-+(UIViewController*)createRootViewController:(NSDictionary*)layout;
7
+-(UIViewController*)createRootViewController:(NSDictionary*)layout;
8
 
8
 
9
 @end
9
 @end

+ 35
- 16
ios/RNNControllerFactory.m View File

6
 
6
 
7
 @implementation RNNControllerFactory
7
 @implementation RNNControllerFactory
8
 
8
 
9
-+(UIViewController *)createRootViewController:(NSDictionary *)layout
9
+-(UIViewController *)createRootViewController:(NSDictionary *)layout
10
 {
10
 {
11
-    if ([layout[@"type"] isEqualToString:@"ContainerStack"]) {
12
-        UINavigationController* stack = [[UINavigationController alloc] init];
13
-        
14
-        NSString* containerName = [layout[@"children"] objectAtIndex:0][@"data"][@"name"];
15
-        NSString* containerId = [layout[@"children"] objectAtIndex:0][@"id"];
16
-        
17
-        [stack setViewControllers:@[[RNNControllerFactory create:containerName containerId:containerId]] animated:false];
18
-        
19
-        return stack;
20
-    }
11
+    return [self fromTree:layout];
12
+}
13
+
14
+-(UIViewController*)fromTree:(NSDictionary*)node
15
+{
16
+    NSString* nodeType = node[@"type"];
17
+    
18
+    NSString* nodeId = node[@"id"];
19
+    NSArray* children = node[@"children"];
20
+    NSDictionary* data = node[@"data"];
21
     
21
     
22
-//    NSString* containerName = layout[@"container"][@"name"];
23
-//    NSString* containerId = layout[@"container"][@"id"];
22
+    if ([nodeType isEqualToString:@"Container"])
23
+    {
24
+        return [self createContainer:nodeId data:data];
25
+    } else if([nodeType isEqualToString:@"ContainerStack"])
26
+    {
27
+        return [self createContainerStack:nodeId data:data children:children];
28
+    }
24
     
29
     
25
-//    return [RNNControllerFactory create:containerName containerId:containerId];
26
-    return nil;
30
+    @throw @"unknown container type";
27
 }
31
 }
28
 
32
 
29
-+(UIViewController*)create:(NSString*)containerName containerId:(NSString*)containerId
33
+-(UIViewController*)createContainer:(NSString*)containerId data:(NSDictionary*)data
30
 {
34
 {
35
+    NSString* containerName = data[@"name"];
36
+                                   
31
     RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:RNN.instance.bridge
37
     RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:RNN.instance.bridge
32
                                                       moduleName:containerName
38
                                                       moduleName:containerName
33
                                                initialProperties:@{@"containerId": containerId}];
39
                                                initialProperties:@{@"containerId": containerId}];
37
     return controller;
43
     return controller;
38
 }
44
 }
39
 
45
 
46
+-(UINavigationController*)createContainerStack:(NSString*)containerId data:(NSDictionary*)data children:(NSArray*)children
47
+{
48
+    UINavigationController* vc = [[UINavigationController alloc] init];
49
+    
50
+    NSMutableArray* controllers = [NSMutableArray new];
51
+    for (NSDictionary* node in children) {
52
+        [controllers addObject:[self fromTree:node]];
53
+    }
54
+    [vc setViewControllers:controllers];
55
+    
56
+    return vc;
57
+}
58
+
40
 @end
59
 @end

+ 1
- 1
src/commands/Commands.js View File

7
   }
7
   }
8
 
8
 
9
   startApp(simpleApi) {
9
   startApp(simpleApi) {
10
-    this.nativeCommandsSender.startApp(this.layoutTreeParser.parseSimpleApi(simpleApi));
10
+    this.nativeCommandsSender.startApp(this.layoutTreeParser.parseSimpleJSON(simpleApi));
11
   }
11
   }
12
 }
12
 }

+ 1
- 17
src/commands/LayoutTreeParser.js View File

5
     this.uniqueIdProvider = uniqueIdProvider;
5
     this.uniqueIdProvider = uniqueIdProvider;
6
   }
6
   }
7
 
7
 
8
-  parseSimpleApi(params) {
8
+  parseSimpleJSON(params) {
9
     const layout = this.createContainerStackWithContainer(params.container);
9
     const layout = this.createContainerStackWithContainer(params.container);
10
-
11
-    //const layout = _.cloneDeep(params);
12
-    //if (layout.container) {
13
-    //  this.generateIdFor(layout.container);
14
-    //}
15
-    //if (layout.sideMenu) {
16
-    //  if (layout.sideMenu.left) {
17
-    //    this.generateIdFor(layout.sideMenu.left);
18
-    //  }
19
-    //  if (layout.sideMenu.right) {
20
-    //    this.generateIdFor(layout.sideMenu.right);
21
-    //  }
22
-    //}
23
-    //if (layout.tabs) {
24
-    //  _.forEach(layout.tabs, this.generateIdFor);
25
-    //}
26
     return layout;
10
     return layout;
27
   }
11
   }
28
 
12
 

+ 3
- 3
src/commands/LayoutTreeParser.test.js View File

10
   });
10
   });
11
 
11
 
12
   it('parses single screen', () => {
12
   it('parses single screen', () => {
13
-    expect(uut.parseSimpleApi(SimpleLayouts.singleScreenApp))
13
+    expect(uut.parseSimpleJSON(SimpleLayouts.singleScreenApp))
14
       .toEqual({
14
       .toEqual({
15
         type: 'ContainerStack',
15
         type: 'ContainerStack',
16
         id: 'ContainerStack+UNIQUE_ID',
16
         id: 'ContainerStack+UNIQUE_ID',
28
   });
28
   });
29
 
29
 
30
   it('parses single screen with props', () => {
30
   it('parses single screen with props', () => {
31
-    expect(uut.parseSimpleApi(SimpleLayouts.singleScreenWithAditionalParams))
31
+    expect(uut.parseSimpleJSON(SimpleLayouts.singleScreenWithAditionalParams))
32
       .toEqual({
32
       .toEqual({
33
         type: 'ContainerStack',
33
         type: 'ContainerStack',
34
         id: 'ContainerStack+UNIQUE_ID',
34
         id: 'ContainerStack+UNIQUE_ID',
55
   });
55
   });
56
 
56
 
57
   xit('parses tab based', () => {
57
   xit('parses tab based', () => {
58
-    expect(uut.parseSimpleApi(SimpleLayouts.tabBasedApp))
58
+    expect(uut.parseSimpleJSON(SimpleLayouts.tabBasedApp))
59
       .toEqual({
59
       .toEqual({
60
         type: 'Tabs',
60
         type: 'Tabs',
61
         id: 'Tabs+UNIQUE_ID',
61
         id: 'Tabs+UNIQUE_ID',