Browse Source

findParentStackControllerForChildId

Daniel Zlotin 7 years ago
parent
commit
9c4babbb73

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

66
 	}
66
 	}
67
 
67
 
68
 	public void push(final String fromId, final ViewController viewController) {
68
 	public void push(final String fromId, final ViewController viewController) {
69
-		ViewController found = findControllerById(fromId);
70
-		if (found == null) return;
71
-
72
-		StackController parentStackController = found.getParentStackController();
73
-		if (parentStackController == null) return;
74
-
75
-		parentStackController.push(viewController);
69
+		StackController parentStackController = findParentStackControllerForChildId(fromId);
70
+		if (parentStackController != null) {
71
+			parentStackController.push(viewController);
72
+		}
76
 	}
73
 	}
77
 
74
 
78
 	public void pop(final String fromId) {
75
 	public void pop(final String fromId) {
79
-		ViewController found = findControllerById(fromId);
80
-		if (found == null) return;
81
-
82
-		StackController parentStackController = found.getParentStackController();
83
-		if (parentStackController == null) return;
84
-
85
-		parentStackController.pop();
76
+		StackController parentStackController = findParentStackControllerForChildId(fromId);
77
+		if (parentStackController != null) {
78
+			parentStackController.pop();
79
+		}
86
 	}
80
 	}
87
 }
81
 }

+ 8
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ParentController.java View File

2
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
 import android.support.annotation.NonNull;
4
 import android.support.annotation.NonNull;
5
+import android.support.annotation.Nullable;
5
 import android.view.View;
6
 import android.view.View;
6
 import android.view.ViewGroup;
7
 import android.view.ViewGroup;
7
 
8
 
24
 
25
 
25
 	public abstract Collection<ViewController> getChildControllers();
26
 	public abstract Collection<ViewController> getChildControllers();
26
 
27
 
28
+	@Nullable
27
 	public ViewController findControllerById(final String id) {
29
 	public ViewController findControllerById(final String id) {
28
 		ViewController fromSuper = super.findControllerById(id);
30
 		ViewController fromSuper = super.findControllerById(id);
29
 		if (fromSuper != null) {
31
 		if (fromSuper != null) {
37
 
39
 
38
 		return null;
40
 		return null;
39
 	}
41
 	}
42
+
43
+	@Nullable
44
+	public StackController findParentStackControllerForChildId(final String childId) {
45
+		ViewController found = findControllerById(childId);
46
+		return found != null ? found.getParentStackController() : null;
47
+	}
40
 }
48
 }

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

90
 		assertThat(uut.findControllerById("child1")).isEqualTo(child1);
90
 		assertThat(uut.findControllerById("child1")).isEqualTo(child1);
91
 		assertThat(uut.findControllerById("child2")).isEqualTo(child2);
91
 		assertThat(uut.findControllerById("child2")).isEqualTo(child2);
92
 	}
92
 	}
93
+
94
+	@Test
95
+	public void findParentStackControllerForChildId() throws Exception {
96
+		ViewController child1 = new SimpleViewController(activity, "child1");
97
+		ViewController child2 = new SimpleViewController(activity, "child2");
98
+
99
+		final StackController someInnerStack = new StackController(activity, "stack1");
100
+		someInnerStack.push(child1);
101
+		someInnerStack.push(child2);
102
+
103
+		ParentController uut = new ParentController(activity, "uut") {
104
+			@Override
105
+			public Collection<ViewController> getChildControllers() {
106
+				return Arrays.<ViewController>asList(someInnerStack);
107
+			}
108
+
109
+			@NonNull
110
+			@Override
111
+			protected View createView() {
112
+				return new FrameLayout(activity);
113
+			}
114
+		};
115
+
116
+		assertThat(uut.findParentStackControllerForChildId("not existing child")).isNull();
117
+		assertThat(uut.findParentStackControllerForChildId("child2")).isEqualTo(someInnerStack);
118
+	}
93
 }
119
 }