Parcourir la source

simple native recursive parsing

Daniel Zlotin il y a 8 ans
Parent
révision
db0c822df6

+ 1
- 1
ios/RNNBridgeModule.m Voir le fichier

@@ -14,7 +14,7 @@ RCT_EXPORT_MODULE();
14 14
 
15 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 18
     [UIApplication.sharedApplication.delegate.window makeKeyAndVisible];
19 19
 }
20 20
 

+ 1
- 1
ios/RNNControllerFactory.h Voir le fichier

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

+ 35
- 16
ios/RNNControllerFactory.m Voir le fichier

@@ -6,28 +6,34 @@
6 6
 
7 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 37
     RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:RNN.instance.bridge
32 38
                                                       moduleName:containerName
33 39
                                                initialProperties:@{@"containerId": containerId}];
@@ -37,4 +43,17 @@
37 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 59
 @end

+ 1
- 1
src/commands/Commands.js Voir le fichier

@@ -7,6 +7,6 @@ export default class Commands {
7 7
   }
8 8
 
9 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 Voir le fichier

@@ -5,24 +5,8 @@ export default class LayoutTreeParser {
5 5
     this.uniqueIdProvider = uniqueIdProvider;
6 6
   }
7 7
 
8
-  parseSimpleApi(params) {
8
+  parseSimpleJSON(params) {
9 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 10
     return layout;
27 11
   }
28 12
 

+ 3
- 3
src/commands/LayoutTreeParser.test.js Voir le fichier

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