Browse Source

stack refactor

Daniel Zlotin 8 years ago
parent
commit
8d0e813997

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

@@ -51,7 +51,7 @@ public class NavigationModule extends ReactContextBaseJavaModule {
51 51
 				final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
52 52
 				LayoutFactory factory = new LayoutFactory(activity(), reactInstanceManager, store);
53 53
 				final ViewController rootView = factory.createAndSaveToStore(layoutTree);
54
-				store.getViewController(onContainerId).getParentStackController().push(rootView);
54
+				store.getViewController(onContainerId).getStackController().push(rootView);
55 55
 			}
56 56
 		});
57 57
 	}
@@ -62,7 +62,7 @@ public class NavigationModule extends ReactContextBaseJavaModule {
62 62
 			@Override
63 63
 			public void run() {
64 64
 				ViewController layout = store.getViewController(onContainerId);
65
-				layout.getParentStackController().pop(layout);
65
+				layout.getStackController().pop(layout);
66 66
 			}
67 67
 		});
68 68
 	}

+ 0
- 17
lib/android/app/src/main/java/com/reactnativenavigation/utils/Stack.java View File

@@ -1,17 +0,0 @@
1
-package com.reactnativenavigation.utils;
2
-
3
-public interface Stack<E> extends Iterable<E> {
4
-
5
-	void push(E element);
6
-
7
-	E pop();
8
-
9
-	E peek();
10
-
11
-	boolean remove(E element);
12
-
13
-	int size();
14
-
15
-	boolean isEmpty();
16
-
17
-}

+ 0
- 6
lib/android/app/src/main/java/com/reactnativenavigation/utils/StackImpl.java View File

@@ -1,6 +0,0 @@
1
-package com.reactnativenavigation.utils;
2
-
3
-import java.util.ArrayDeque;
4
-
5
-public class StackImpl<E> extends ArrayDeque<E> implements Stack<E> {
6
-}

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

@@ -1,38 +1,23 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4
+import android.support.annotation.Nullable;
4 5
 import android.view.ViewGroup;
5 6
 import android.widget.FrameLayout;
6 7
 
7
-import com.reactnativenavigation.utils.Stack;
8
-import com.reactnativenavigation.utils.StackImpl;
8
+import java.util.ArrayDeque;
9 9
 
10 10
 public class StackController extends ViewController {
11
-	private Stack<ViewController> childControllers = new StackImpl<>();
11
+	private final ArrayDeque<ViewController> childControllers = new ArrayDeque<>();
12 12
 
13 13
 	public StackController(final Activity activity) {
14 14
 		super(activity);
15 15
 	}
16 16
 
17
-	@Override
18
-	public ViewGroup getView() {
19
-		return (ViewGroup) super.getView();
20
-	}
21
-
22
-	public Stack<ViewController> getChildControllers() {
23
-		return childControllers;
24
-	}
25
-
26
-	public void setChildControllers(ViewController... childControllers) {
27
-		for (ViewController childController : childControllers) {
28
-			push(childController);
29
-		}
30
-	}
31
-
32 17
 	public void push(final ViewController child) {
33 18
 		ViewController previousTop = peek();
34 19
 
35
-		child.setParentStackController(this);
20
+		child.setStackController(this);
36 21
 		childControllers.push(child);
37 22
 		getView().addView(child.getView());
38 23
 
@@ -86,8 +71,23 @@ public class StackController extends ViewController {
86 71
 		}
87 72
 	}
88 73
 
74
+	@Override
75
+	public ViewGroup getView() {
76
+		return (ViewGroup) super.getView();
77
+	}
78
+
89 79
 	@Override
90 80
 	protected ViewGroup onCreateView() {
91 81
 		return new FrameLayout(getActivity());
92 82
 	}
83
+
84
+	@Nullable
85
+	@Override
86
+	public StackController getStackController() {
87
+		return this;
88
+	}
89
+
90
+	ArrayDeque<ViewController> getStack() {
91
+		return childControllers;
92
+	}
93 93
 }

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

@@ -24,11 +24,11 @@ public abstract class ViewController {
24 24
 	}
25 25
 
26 26
 	@Nullable
27
-	public StackController getParentStackController() {
27
+	public StackController getStackController() {
28 28
 		return stackController;
29 29
 	}
30 30
 
31
-	void setParentStackController(final StackController stackController) {
31
+	void setStackController(final StackController stackController) {
32 32
 		this.stackController = stackController;
33 33
 	}
34 34
 

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

@@ -36,26 +36,28 @@ public class StackControllerTest extends BaseTest {
36 36
 
37 37
 	@Test
38 38
 	public void holdsAStackOfViewControllers() throws Exception {
39
-		assertThat(uut.getChildControllers()).isEmpty();
40
-		uut.setChildControllers(child1, child2, child3);
41
-		assertThat(uut.getChildControllers()).containsOnly(child3, child2, child1);
39
+		assertThat(uut.getStack()).isEmpty();
40
+		uut.push(child1);
41
+		uut.push(child2);
42
+		uut.push(child3);
43
+		assertThat(uut.getStack()).containsOnly(child3, child2, child1);
42 44
 		assertThat(uut.peek()).isEqualTo(child3);
43 45
 	}
44 46
 
45 47
 	@Test
46 48
 	public void push() throws Exception {
47
-		assertThat(uut.getChildControllers()).isEmpty();
49
+		assertThat(uut.getStack()).isEmpty();
48 50
 		uut.push(child1);
49
-		assertThat(uut.getChildControllers()).containsOnly(child1);
51
+		assertThat(uut.getStack()).containsOnly(child1);
50 52
 	}
51 53
 
52 54
 	@Test
53 55
 	public void pop() throws Exception {
54 56
 		uut.push(child1);
55 57
 		uut.push(child2);
56
-		assertThat(uut.getChildControllers()).containsOnly(child2, child1);
58
+		assertThat(uut.getStack()).containsOnly(child2, child1);
57 59
 		uut.pop();
58
-		assertThat(uut.getChildControllers()).containsOnly(child1);
60
+		assertThat(uut.getStack()).containsOnly(child1);
59 61
 	}
60 62
 
61 63
 	@Test
@@ -71,13 +73,13 @@ public class StackControllerTest extends BaseTest {
71 73
 
72 74
 	@Test
73 75
 	public void pushAssignsRefToSelfOnPushedController() throws Exception {
74
-		assertThat(child1.getParentStackController()).isNull();
76
+		assertThat(child1.getStackController()).isNull();
75 77
 		uut.push(child1);
76
-		assertThat(child1.getParentStackController()).isEqualTo(uut);
78
+		assertThat(child1.getStackController()).isEqualTo(uut);
77 79
 
78 80
 		StackController anotherNavController = new StackController(activity);
79
-		anotherNavController.setChildControllers(child2);
80
-		assertThat(child2.getParentStackController()).isEqualTo(anotherNavController);
81
+		anotherNavController.push(child2);
82
+		assertThat(child2.getStackController()).isEqualTo(anotherNavController);
81 83
 	}
82 84
 
83 85
 	@Test
@@ -98,24 +100,24 @@ public class StackControllerTest extends BaseTest {
98 100
 
99 101
 	@Test
100 102
 	public void popDoesNothingWhenZeroOrOneChild() throws Exception {
101
-		assertThat(uut.getChildControllers()).isEmpty();
103
+		assertThat(uut.getStack()).isEmpty();
102 104
 		uut.pop();
103
-		assertThat(uut.getChildControllers()).isEmpty();
105
+		assertThat(uut.getStack()).isEmpty();
104 106
 
105 107
 		uut.push(child1);
106 108
 		uut.pop();
107
-		assertThat(uut.getChildControllers()).containsOnly(child1);
109
+		assertThat(uut.getStack()).containsOnly(child1);
108 110
 	}
109 111
 
110 112
 	@Test
111 113
 	public void canPopWhenSizeIsMoreThanOne() throws Exception {
112
-		assertThat(uut.getChildControllers()).isEmpty();
114
+		assertThat(uut.getStack()).isEmpty();
113 115
 		assertThat(uut.canPop()).isFalse();
114 116
 		uut.push(child1);
115
-		assertThat(uut.getChildControllers()).containsOnly(child1);
117
+		assertThat(uut.getStack()).containsOnly(child1);
116 118
 		assertThat(uut.canPop()).isFalse();
117 119
 		uut.push(child2);
118
-		assertThat(uut.getChildControllers()).containsOnly(child1, child2);
120
+		assertThat(uut.getStack()).containsOnly(child1, child2);
119 121
 		assertThat(uut.canPop()).isTrue();
120 122
 	}
121 123
 
@@ -157,7 +159,7 @@ public class StackControllerTest extends BaseTest {
157 159
 		uut.push(child1);
158 160
 		uut.push(child2);
159 161
 		uut.pop(child2);
160
-		assertThat(uut.getChildControllers()).containsOnly(child1);
162
+		assertThat(uut.getStack()).containsOnly(child1);
161 163
 		assertHasSingleChildViewOfController(child1);
162 164
 	}
163 165
 
@@ -168,10 +170,15 @@ public class StackControllerTest extends BaseTest {
168 170
 		assertHasSingleChildViewOfController(child2);
169 171
 
170 172
 		uut.pop(child1);
171
-		assertThat(uut.getChildControllers()).containsOnly(child2);
173
+		assertThat(uut.getStack()).containsOnly(child2);
172 174
 		assertHasSingleChildViewOfController(child2);
173 175
 	}
174 176
 
177
+	@Test
178
+	public void getStackControllerReturnsSelf() throws Exception {
179
+		assertThat(uut.getStackController()).isEqualTo(uut);
180
+	}
181
+
175 182
 	private void assertHasSingleChildViewOfController(ViewController childController) {
176 183
 		assertThat(uut.getView().getChildCount()).isEqualTo(1);
177 184
 		assertThat(uut.getView().getChildAt(0)).isEqualTo(childController.getView());

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

@@ -46,10 +46,10 @@ public class ViewControllerTest extends BaseTest {
46 46
 
47 47
 	@Test
48 48
 	public void holdsAReferenceToStackControllerOrNull() throws Exception {
49
-		assertThat(uut.getParentStackController()).isNull();
49
+		assertThat(uut.getStackController()).isNull();
50 50
 		StackController nav = new StackController(activity);
51 51
 		nav.push(uut);
52
-		assertThat(uut.getParentStackController()).isEqualTo(nav);
52
+		assertThat(uut.getStackController()).isEqualTo(nav);
53 53
 	}
54 54
 
55 55
 	@Test