Daniel Zlotin преди 8 години
родител
ревизия
8d0e813997

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java Целия файл

51
 				final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
51
 				final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
52
 				LayoutFactory factory = new LayoutFactory(activity(), reactInstanceManager, store);
52
 				LayoutFactory factory = new LayoutFactory(activity(), reactInstanceManager, store);
53
 				final ViewController rootView = factory.createAndSaveToStore(layoutTree);
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
 			@Override
62
 			@Override
63
 			public void run() {
63
 			public void run() {
64
 				ViewController layout = store.getViewController(onContainerId);
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 Целия файл

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 Целия файл

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 Целия файл

1
 package com.reactnativenavigation.viewcontrollers;
1
 package com.reactnativenavigation.viewcontrollers;
2
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
+import android.support.annotation.Nullable;
4
 import android.view.ViewGroup;
5
 import android.view.ViewGroup;
5
 import android.widget.FrameLayout;
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
 public class StackController extends ViewController {
10
 public class StackController extends ViewController {
11
-	private Stack<ViewController> childControllers = new StackImpl<>();
11
+	private final ArrayDeque<ViewController> childControllers = new ArrayDeque<>();
12
 
12
 
13
 	public StackController(final Activity activity) {
13
 	public StackController(final Activity activity) {
14
 		super(activity);
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
 	public void push(final ViewController child) {
17
 	public void push(final ViewController child) {
33
 		ViewController previousTop = peek();
18
 		ViewController previousTop = peek();
34
 
19
 
35
-		child.setParentStackController(this);
20
+		child.setStackController(this);
36
 		childControllers.push(child);
21
 		childControllers.push(child);
37
 		getView().addView(child.getView());
22
 		getView().addView(child.getView());
38
 
23
 
86
 		}
71
 		}
87
 	}
72
 	}
88
 
73
 
74
+	@Override
75
+	public ViewGroup getView() {
76
+		return (ViewGroup) super.getView();
77
+	}
78
+
89
 	@Override
79
 	@Override
90
 	protected ViewGroup onCreateView() {
80
 	protected ViewGroup onCreateView() {
91
 		return new FrameLayout(getActivity());
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 Целия файл

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

+ 26
- 19
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java Целия файл

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

+ 2
- 2
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ViewControllerTest.java Целия файл

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