Browse Source

removed store, viewControllers now have id

Daniel Zlotin 7 years ago
parent
commit
f6924be92e

+ 1
- 3
lib/android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java View File

@@ -11,15 +11,13 @@ import com.reactnativenavigation.react.NavigationReactNativeHost;
11 11
 
12 12
 public abstract class NavigationApplication extends Application implements ReactApplication, Application.ActivityLifecycleCallbacks {
13 13
 
14
-	private Store store;
15 14
 	private NavigationReactNativeHost reactNativeHost;
16 15
 	private NavigationReactInitializer initializer;
17 16
 
18 17
 	@Override
19 18
 	public void onCreate() {
20 19
 		super.onCreate();
21
-		store = new Store();
22
-		reactNativeHost = new NavigationReactNativeHost(this, isDebug(), store);
20
+		reactNativeHost = new NavigationReactNativeHost(this, isDebug());
23 21
 		initializer = new NavigationReactInitializer(reactNativeHost.getReactInstanceManager(), isDebug());
24 22
 		registerActivityLifecycleCallbacks(this);
25 23
 	}

+ 4
- 13
lib/android/app/src/main/java/com/reactnativenavigation/layout/LayoutFactory.java View File

@@ -3,7 +3,6 @@ package com.reactnativenavigation.layout;
3 3
 import android.app.Activity;
4 4
 
5 5
 import com.facebook.react.ReactInstanceManager;
6
-import com.reactnativenavigation.Store;
7 6
 import com.reactnativenavigation.layout.impl.ReactRootViewController;
8 7
 import com.reactnativenavigation.viewcontrollers.StackController;
9 8
 import com.reactnativenavigation.viewcontrollers.ViewController;
@@ -12,21 +11,13 @@ public class LayoutFactory {
12 11
 
13 12
 	private final Activity activity;
14 13
 	private final ReactInstanceManager reactInstanceManager;
15
-	private final Store store;
16 14
 
17
-	public LayoutFactory(Activity activity, final ReactInstanceManager reactInstanceManager, final Store store) {
15
+	public LayoutFactory(Activity activity, final ReactInstanceManager reactInstanceManager) {
18 16
 		this.activity = activity;
19 17
 		this.reactInstanceManager = reactInstanceManager;
20
-		this.store = store;
21 18
 	}
22 19
 
23
-	public ViewController createAndSaveToStore(LayoutNode node) {
24
-		ViewController viewController = create(node);
25
-		store.setViewController(node.id, viewController);
26
-		return viewController;
27
-	}
28
-
29
-	private ViewController create(final LayoutNode node) {
20
+	public ViewController create(final LayoutNode node) {
30 21
 		switch (node.type) {
31 22
 			case Container:
32 23
 				return createContainer(node);
@@ -86,9 +77,9 @@ public class LayoutFactory {
86 77
 	}
87 78
 
88 79
 	private ViewController createContainerStack(LayoutNode node) {
89
-		StackController stackController = new StackController(activity);
80
+		StackController stackController = new StackController(activity, node.id);
90 81
 		for (LayoutNode child : node.children) {
91
-			stackController.push(createAndSaveToStore(child));
82
+			stackController.push(create(child));
92 83
 		}
93 84
 		return stackController;
94 85
 	}

+ 2
- 4
lib/android/app/src/main/java/com/reactnativenavigation/layout/impl/ReactRootViewController.java View File

@@ -11,14 +11,12 @@ import com.reactnativenavigation.viewcontrollers.ViewController;
11 11
 
12 12
 public class ReactRootViewController extends ViewController {
13 13
 
14
-	private final String id;
15 14
 	private final String name;
16 15
 	private final ReactInstanceManager reactInstanceManager;
17 16
 	private boolean attachedToReactInstance = false;
18 17
 
19 18
 	public ReactRootViewController(final Activity activity, final String id, final String name, final ReactInstanceManager reactInstanceManager) {
20
-		super(activity);
21
-		this.id = id;
19
+		super(activity, id);
22 20
 		this.name = name;
23 21
 		this.reactInstanceManager = reactInstanceManager;
24 22
 	}
@@ -61,7 +59,7 @@ public class ReactRootViewController extends ViewController {
61 59
 	protected View createView() {
62 60
 		ReactRootView reactRootView = new ReactRootView(getActivity());
63 61
 		Bundle opts = new Bundle();
64
-		opts.putString("id", this.id);
62
+		opts.putString("id", getId());
65 63
 		reactRootView.startReactApplication(this.reactInstanceManager, this.name, opts);
66 64
 		reactRootView.setEventListener(new ReactRootView.ReactRootViewEventListener() {
67 65
 			@Override

+ 8
- 11
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java View File

@@ -8,7 +8,6 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule;
8 8
 import com.facebook.react.bridge.ReactMethod;
9 9
 import com.facebook.react.bridge.ReadableMap;
10 10
 import com.reactnativenavigation.NavigationActivity;
11
-import com.reactnativenavigation.Store;
12 11
 import com.reactnativenavigation.layout.LayoutFactory;
13 12
 import com.reactnativenavigation.layout.LayoutNode;
14 13
 import com.reactnativenavigation.parse.JSONParser;
@@ -20,12 +19,10 @@ import com.reactnativenavigation.viewcontrollers.ViewController;
20 19
 public class NavigationModule extends ReactContextBaseJavaModule {
21 20
 	private static final String NAME = "RNNBridgeModule";
22 21
 	private final ReactInstanceManager reactInstanceManager;
23
-	private final Store store;
24 22
 
25
-	public NavigationModule(final ReactApplicationContext reactContext, final ReactInstanceManager reactInstanceManager, final Store store) {
23
+	public NavigationModule(final ReactApplicationContext reactContext, final ReactInstanceManager reactInstanceManager) {
26 24
 		super(reactContext);
27 25
 		this.reactInstanceManager = reactInstanceManager;
28
-		this.store = store;
29 26
 	}
30 27
 
31 28
 	@Override
@@ -39,7 +36,7 @@ public class NavigationModule extends ReactContextBaseJavaModule {
39 36
 			@Override
40 37
 			public void run() {
41 38
 				final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
42
-				final ViewController viewController = newLayoutFactory().createAndSaveToStore(layoutTree);
39
+				final ViewController viewController = newLayoutFactory().create(layoutTree);
43 40
 				navigator().setRoot(viewController);
44 41
 			}
45 42
 		});
@@ -51,8 +48,8 @@ public class NavigationModule extends ReactContextBaseJavaModule {
51 48
 			@Override
52 49
 			public void run() {
53 50
 				final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
54
-				final ViewController viewController = newLayoutFactory().createAndSaveToStore(layoutTree);
55
-				store.getViewController(onContainerId).getStackController().push(viewController);
51
+				final ViewController viewController = newLayoutFactory().create(layoutTree);
52
+//				store.getViewController(onContainerId).getStackController().push(viewController);
56 53
 			}
57 54
 		});
58 55
 	}
@@ -62,7 +59,7 @@ public class NavigationModule extends ReactContextBaseJavaModule {
62 59
 		handle(new Runnable() {
63 60
 			@Override
64 61
 			public void run() {
65
-				store.getViewController(onContainerId).getStackController().pop(store.getViewController(onContainerId));
62
+//				store.getViewController(onContainerId).getStackController().pop(store.getViewController(onContainerId));
66 63
 			}
67 64
 		});
68 65
 	}
@@ -72,7 +69,7 @@ public class NavigationModule extends ReactContextBaseJavaModule {
72 69
 		handle(new Runnable() {
73 70
 			@Override
74 71
 			public void run() {
75
-				store.getViewController(onContainerId).getStackController().popTo(store.getViewController(toContainerId));
72
+//				store.getViewController(onContainerId).getStackController().popTo(store.getViewController(toContainerId));
76 73
 			}
77 74
 		});
78 75
 	}
@@ -82,7 +79,7 @@ public class NavigationModule extends ReactContextBaseJavaModule {
82 79
 		handle(new Runnable() {
83 80
 			@Override
84 81
 			public void run() {
85
-				store.getViewController(onContainerId).getStackController().popToRoot();
82
+//				store.getViewController(onContainerId).getStackController().popToRoot();
86 83
 			}
87 84
 		});
88 85
 	}
@@ -97,7 +94,7 @@ public class NavigationModule extends ReactContextBaseJavaModule {
97 94
 
98 95
 	@NonNull
99 96
 	private LayoutFactory newLayoutFactory() {
100
-		return new LayoutFactory(activity(), reactInstanceManager, store);
97
+		return new LayoutFactory(activity(), reactInstanceManager);
101 98
 	}
102 99
 
103 100
 	private void handle(Runnable task) {

+ 2
- 5
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationPackage.java View File

@@ -6,7 +6,6 @@ import com.facebook.react.bridge.JavaScriptModule;
6 6
 import com.facebook.react.bridge.NativeModule;
7 7
 import com.facebook.react.bridge.ReactApplicationContext;
8 8
 import com.facebook.react.uimanager.ViewManager;
9
-import com.reactnativenavigation.Store;
10 9
 
11 10
 import java.util.Arrays;
12 11
 import java.util.Collections;
@@ -15,17 +14,15 @@ import java.util.List;
15 14
 public class NavigationPackage implements ReactPackage {
16 15
 
17 16
 	private ReactNativeHost reactNativeHost;
18
-	private Store store;
19 17
 
20
-	public NavigationPackage(final ReactNativeHost reactNativeHost, final Store store) {
18
+	public NavigationPackage(final ReactNativeHost reactNativeHost) {
21 19
 		this.reactNativeHost = reactNativeHost;
22
-		this.store = store;
23 20
 	}
24 21
 
25 22
 	@Override
26 23
 	public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
27 24
 		return Arrays.<NativeModule>asList(
28
-				new NavigationModule(reactContext, reactNativeHost.getReactInstanceManager(), store)
25
+				new NavigationModule(reactContext, reactNativeHost.getReactInstanceManager())
29 26
 		);
30 27
 	}
31 28
 

+ 2
- 5
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationReactNativeHost.java View File

@@ -5,7 +5,6 @@ import android.app.Application;
5 5
 import com.facebook.react.ReactNativeHost;
6 6
 import com.facebook.react.ReactPackage;
7 7
 import com.facebook.react.shell.MainReactPackage;
8
-import com.reactnativenavigation.Store;
9 8
 
10 9
 import java.util.Arrays;
11 10
 import java.util.List;
@@ -13,12 +12,10 @@ import java.util.List;
13 12
 public class NavigationReactNativeHost extends ReactNativeHost {
14 13
 
15 14
 	private final boolean isDebug;
16
-	private Store store;
17 15
 
18
-	public NavigationReactNativeHost(Application application, boolean isDebug, final Store store) {
16
+	public NavigationReactNativeHost(Application application, boolean isDebug) {
19 17
 		super(application);
20 18
 		this.isDebug = isDebug;
21
-		this.store = store;
22 19
 	}
23 20
 
24 21
 	@Override
@@ -30,7 +27,7 @@ public class NavigationReactNativeHost extends ReactNativeHost {
30 27
 	protected List<ReactPackage> getPackages() {
31 28
 		return Arrays.<ReactPackage>asList(
32 29
 				new MainReactPackage(),
33
-				new NavigationPackage(this, store)
30
+				new NavigationPackage(this)
34 31
 		);
35 32
 	}
36 33
 }

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java View File

@@ -9,7 +9,7 @@ public class Navigator extends ViewController {
9 9
 	private boolean activityResumed = false;
10 10
 
11 11
 	public Navigator(final Activity activity) {
12
-		super(activity);
12
+		super(activity, "navigator");
13 13
 	}
14 14
 
15 15
 	@NonNull

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java View File

@@ -11,8 +11,8 @@ import java.util.ArrayDeque;
11 11
 public class StackController extends ViewController {
12 12
 	private final ArrayDeque<ViewController> stack = new ArrayDeque<>();
13 13
 
14
-	public StackController(final Activity activity) {
15
-		super(activity);
14
+	public StackController(final Activity activity, String id) {
15
+		super(activity, id);
16 16
 	}
17 17
 
18 18
 	public void push(final ViewController child) {

+ 7
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java View File

@@ -9,9 +9,11 @@ public abstract class ViewController {
9 9
 	private View view;
10 10
 	private final Activity activity;
11 11
 	private StackController stackController;
12
+	private String id;
12 13
 
13
-	public ViewController(Activity activity) {
14
+	public ViewController(Activity activity, String id) {
14 15
 		this.activity = activity;
16
+		this.id = id;
15 17
 	}
16 18
 
17 19
 	@NonNull
@@ -41,4 +43,8 @@ public abstract class ViewController {
41 43
 		}
42 44
 		return view;
43 45
 	}
46
+
47
+	public String getId() {
48
+		return id;
49
+	}
44 50
 }

+ 0
- 31
lib/android/app/src/test/java/com/reactnativenavigation/StoreTest.java View File

@@ -1,31 +0,0 @@
1
-package com.reactnativenavigation;
2
-
3
-import android.app.Activity;
4
-
5
-import com.reactnativenavigation.mocks.SimpleViewController;
6
-
7
-import org.junit.Test;
8
-
9
-import static org.assertj.core.api.Java6Assertions.assertThat;
10
-import static org.mockito.Mockito.mock;
11
-
12
-public class StoreTest extends BaseTest {
13
-
14
-	private Store uut;
15
-
16
-	@Override
17
-	public void beforeEach() {
18
-		super.beforeEach();
19
-		uut = new Store();
20
-	}
21
-
22
-	@Test
23
-	public void holdsViewControllersById() throws Exception {
24
-		SimpleViewController viewController = new SimpleViewController(mock(Activity.class), "my controller");
25
-
26
-		assertThat(uut.getViewController("the id")).isNull();
27
-
28
-		uut.setViewController("the id", viewController);
29
-		assertThat(uut.getViewController("the id")).isEqualTo(viewController);
30
-	}
31
-}

+ 3
- 6
lib/android/app/src/test/java/com/reactnativenavigation/mocks/SimpleViewController.java View File

@@ -6,11 +6,8 @@ import android.view.View;
6 6
 import com.reactnativenavigation.viewcontrollers.ViewController;
7 7
 
8 8
 public class SimpleViewController extends ViewController {
9
-	private final String name;
10
-
11
-	public SimpleViewController(final Activity activity, String name) {
12
-		super(activity);
13
-		this.name = name;
9
+	public SimpleViewController(final Activity activity, String id) {
10
+		super(activity, id);
14 11
 	}
15 12
 
16 13
 	@Override
@@ -20,6 +17,6 @@ public class SimpleViewController extends ViewController {
20 17
 
21 18
 	@Override
22 19
 	public String toString() {
23
-		return "SimpleViewController " + name;
20
+		return "SimpleViewController " + getId();
24 21
 	}
25 22
 }

+ 0
- 2
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java View File

@@ -58,8 +58,6 @@ public class NavigatorTest extends BaseTest {
58 58
 		assertHasSingleChildViewOf(child2);
59 59
 	}
60 60
 
61
-
62
-
63 61
 	private void assertHasSingleChildViewOf(ViewController vc) {
64 62
 		assertThat(uut.getView().getChildCount()).isEqualTo(1);
65 63
 		assertThat(uut.getView().getChildAt(0)).isEqualTo(vc.getView()).isNotNull();

+ 2
- 2
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java View File

@@ -23,7 +23,7 @@ public class StackControllerTest extends BaseTest {
23 23
 	public void beforeEach() {
24 24
 		super.beforeEach();
25 25
 		activity = newActivity();
26
-		uut = new StackController(activity);
26
+		uut = new StackController(activity, "uut");
27 27
 		child1 = new SimpleViewController(activity, "child1");
28 28
 		child2 = new SimpleViewController(activity, "child2");
29 29
 		child3 = new SimpleViewController(activity, "child3");
@@ -77,7 +77,7 @@ public class StackControllerTest extends BaseTest {
77 77
 		uut.push(child1);
78 78
 		assertThat(child1.getStackController()).isEqualTo(uut);
79 79
 
80
-		StackController anotherNavController = new StackController(activity);
80
+		StackController anotherNavController = new StackController(activity, "another");
81 81
 		anotherNavController.push(child2);
82 82
 		assertThat(child2.getStackController()).isEqualTo(anotherNavController);
83 83
 	}

+ 7
- 2
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ViewControllerTest.java View File

@@ -35,7 +35,7 @@ public class ViewControllerTest extends BaseTest {
35 35
 	@Test
36 36
 	public void canOverrideViewCreation() throws Exception {
37 37
 		final View otherView = new View(activity);
38
-		ViewController myController = new ViewController(activity) {
38
+		ViewController myController = new ViewController(activity, "vc") {
39 39
 			@Override
40 40
 			protected View createView() {
41 41
 				return otherView;
@@ -47,7 +47,7 @@ public class ViewControllerTest extends BaseTest {
47 47
 	@Test
48 48
 	public void holdsAReferenceToStackControllerOrNull() throws Exception {
49 49
 		assertThat(uut.getStackController()).isNull();
50
-		StackController nav = new StackController(activity);
50
+		StackController nav = new StackController(activity, "stack");
51 51
 		nav.push(uut);
52 52
 		assertThat(uut.getStackController()).isEqualTo(nav);
53 53
 	}
@@ -56,4 +56,9 @@ public class ViewControllerTest extends BaseTest {
56 56
 	public void handleBackDefaultFalse() throws Exception {
57 57
 		assertThat(uut.handleBack()).isFalse();
58 58
 	}
59
+
60
+	@Test
61
+	public void holdsId() throws Exception {
62
+		assertThat(uut.getId()).isEqualTo("uut");
63
+	}
59 64
 }