浏览代码

lifecycle methods inconsistant

Daniel Zlotin 8 年前
父节点
当前提交
2d95d1b19a

+ 6
- 6
lib/android/app/src/main/java/com/reactnativenavigation/layout/ReactRootViewController.java 查看文件

24
 	}
24
 	}
25
 
25
 
26
 	@Override
26
 	@Override
27
-	public void onDestroy() {
28
-		super.onDestroy();
27
+	public void destroy() {
28
+		super.destroy();
29
 		if (reactRootView != null) reactRootView.unmountReactApplication();
29
 		if (reactRootView != null) reactRootView.unmountReactApplication();
30
 		reactRootView = null;
30
 		reactRootView = null;
31
 	}
31
 	}
32
 
32
 
33
 	@Override
33
 	@Override
34
-	public void onAppear() {
35
-		super.onAppear();
34
+	public void onViewAppeared() {
35
+		super.onViewAppeared();
36
 		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).containerStart(getId());
36
 		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).containerStart(getId());
37
 	}
37
 	}
38
 
38
 
39
 	@Override
39
 	@Override
40
-	public void onDisappear() {
41
-		super.onDisappear();
40
+	public void onViewDisappear() {
41
+		super.onViewDisappear();
42
 		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).containerStop(getId());
42
 		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).containerStop(getId());
43
 	}
43
 	}
44
 
44
 

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java 查看文件

32
 
32
 
33
 	@ReactMethod
33
 	@ReactMethod
34
 	public void setRoot(final ReadableMap rawLayoutTree) {
34
 	public void setRoot(final ReadableMap rawLayoutTree) {
35
+		final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
35
 		handle(new Runnable() {
36
 		handle(new Runnable() {
36
 			@Override
37
 			@Override
37
 			public void run() {
38
 			public void run() {
38
-				final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
39
 				final ViewController viewController = newLayoutFactory().create(layoutTree);
39
 				final ViewController viewController = newLayoutFactory().create(layoutTree);
40
 				navigator().setRoot(viewController);
40
 				navigator().setRoot(viewController);
41
 			}
41
 			}
44
 
44
 
45
 	@ReactMethod
45
 	@ReactMethod
46
 	public void push(final String onContainerId, final ReadableMap rawLayoutTree) {
46
 	public void push(final String onContainerId, final ReadableMap rawLayoutTree) {
47
+		final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
47
 		handle(new Runnable() {
48
 		handle(new Runnable() {
48
 			@Override
49
 			@Override
49
 			public void run() {
50
 			public void run() {
50
-				final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
51
 				final ViewController viewController = newLayoutFactory().create(layoutTree);
51
 				final ViewController viewController = newLayoutFactory().create(layoutTree);
52
 				navigator().push(onContainerId, viewController);
52
 				navigator().push(onContainerId, viewController);
53
 			}
53
 			}

+ 3
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java 查看文件

39
 	 */
39
 	 */
40
 
40
 
41
 	public void setRoot(final ViewController viewController) {
41
 	public void setRoot(final ViewController viewController) {
42
-		getView().removeAllViews();
42
+		if (root != null) {
43
+			root.destroy();
44
+		}
43
 
45
 
44
 		root = viewController;
46
 		root = viewController;
45
 		getView().addView(viewController.getView());
47
 		getView().addView(viewController.getView());

+ 3
- 23
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ParentController.java 查看文件

40
 		return null;
40
 		return null;
41
 	}
41
 	}
42
 
42
 
43
-	/*
44
-	 * lifecycle
45
-	 */
46
-
47
-	@Override
48
-	public void onAppear() {
49
-		super.onAppear();
50
-		for (ViewController child : getChildControllers()) {
51
-			child.onAppear();
52
-		}
53
-	}
54
-
55
-	@Override
56
-	public void onDisappear() {
57
-		super.onDisappear();
58
-		for (ViewController child : getChildControllers()) {
59
-			child.onDisappear();
60
-		}
61
-	}
62
-
63
 	@Override
43
 	@Override
64
-	public void onDestroy() {
65
-		super.onDestroy();
44
+	public void destroy() {
45
+		super.destroy();
66
 		for (ViewController child : getChildControllers()) {
46
 		for (ViewController child : getChildControllers()) {
67
-			child.onDestroy();
47
+			child.destroy();
68
 		}
48
 		}
69
 	}
49
 	}
70
 }
50
 }

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java 查看文件

56
 			@Override
56
 			@Override
57
 			public void run() {
57
 			public void run() {
58
 				getView().removeView(poppedTop.getView());
58
 				getView().removeView(poppedTop.getView());
59
-				poppedTop.onDestroy();
59
+				poppedTop.destroy();
60
 			}
60
 			}
61
 		});
61
 		});
62
 	}
62
 	}
66
 			pop();
66
 			pop();
67
 		} else {
67
 		} else {
68
 			stack.remove(childController.getId());
68
 			stack.remove(childController.getId());
69
-			childController.onDestroy();
69
+			childController.destroy();
70
 		}
70
 		}
71
 	}
71
 	}
72
 
72
 

+ 10
- 6
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java 查看文件

4
 import android.support.annotation.NonNull;
4
 import android.support.annotation.NonNull;
5
 import android.support.annotation.Nullable;
5
 import android.support.annotation.Nullable;
6
 import android.view.View;
6
 import android.view.View;
7
+import android.view.ViewGroup;
7
 import android.view.ViewTreeObserver;
8
 import android.view.ViewTreeObserver;
8
 
9
 
9
 import com.reactnativenavigation.utils.StringUtils;
10
 import com.reactnativenavigation.utils.StringUtils;
63
 		return isSameId(id) ? this : null;
64
 		return isSameId(id) ? this : null;
64
 	}
65
 	}
65
 
66
 
66
-	public void onAppear() {
67
+	public void onViewAppeared() {
67
 		//
68
 		//
68
 	}
69
 	}
69
 
70
 
70
-	public void onDisappear() {
71
+	public void onViewDisappear() {
71
 		//
72
 		//
72
 	}
73
 	}
73
 
74
 
74
-	public void onDestroy() {
75
+	public void destroy() {
75
 		if (isShown) {
76
 		if (isShown) {
76
 			isShown = false;
77
 			isShown = false;
77
-			onDisappear();
78
+			onViewDisappear();
78
 		}
79
 		}
79
 		if (view != null) {
80
 		if (view != null) {
80
 			view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
81
 			view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
82
+			if (view.getParent() instanceof ViewGroup) {
83
+				((ViewGroup) view.getParent()).removeView(view);
84
+			}
81
 			view = null;
85
 			view = null;
82
 		}
86
 		}
83
 	}
87
 	}
86
 	public void onGlobalLayout() {
90
 	public void onGlobalLayout() {
87
 		if (!isShown && isViewShown()) {
91
 		if (!isShown && isViewShown()) {
88
 			isShown = true;
92
 			isShown = true;
89
-			onAppear();
93
+			onViewAppeared();
90
 		} else if (isShown && !isViewShown()) {
94
 		} else if (isShown && !isViewShown()) {
91
 			isShown = false;
95
 			isShown = false;
92
-			onDisappear();
96
+			onViewDisappear();
93
 		}
97
 		}
94
 	}
98
 	}
95
 
99
 

+ 9
- 9
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ParentControllerTest.java 查看文件

85
 		ViewController child1 = spy(new SimpleViewController(activity, "child1"));
85
 		ViewController child1 = spy(new SimpleViewController(activity, "child1"));
86
 		children.add(child1);
86
 		children.add(child1);
87
 
87
 
88
-		verify(child1, times(0)).onAppear();
89
-		uut.onAppear();
90
-		verify(child1, times(1)).onAppear();
88
+		verify(child1, times(0)).onViewAppeared();
89
+		uut.onViewAppeared();
90
+		verify(child1, times(1)).onViewAppeared();
91
 	}
91
 	}
92
 
92
 
93
 	@Test
93
 	@Test
95
 		ViewController child1 = spy(new SimpleViewController(activity, "child1"));
95
 		ViewController child1 = spy(new SimpleViewController(activity, "child1"));
96
 		children.add(child1);
96
 		children.add(child1);
97
 
97
 
98
-		verify(child1, times(0)).onDisappear();
99
-		uut.onDisappear();
100
-		verify(child1, times(1)).onDisappear();
98
+		verify(child1, times(0)).onViewDisappear();
99
+		uut.onViewDisappear();
100
+		verify(child1, times(1)).onViewDisappear();
101
 	}
101
 	}
102
 
102
 
103
 	@Test
103
 	@Test
105
 		ViewController child1 = spy(new SimpleViewController(activity, "child1"));
105
 		ViewController child1 = spy(new SimpleViewController(activity, "child1"));
106
 		children.add(child1);
106
 		children.add(child1);
107
 
107
 
108
-		verify(child1, times(0)).onDestroy();
109
-		uut.onDestroy();
110
-		verify(child1, times(1)).onDestroy();
108
+		verify(child1, times(0)).destroy();
109
+		uut.destroy();
110
+		verify(child1, times(1)).destroy();
111
 	}
111
 	}
112
 }
112
 }

+ 8
- 8
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java 查看文件

250
 		uut.push(child2);
250
 		uut.push(child2);
251
 		uut.push(child3);
251
 		uut.push(child3);
252
 
252
 
253
-		verify(child3, times(0)).onDestroy();
253
+		verify(child3, times(0)).destroy();
254
 		uut.pop();
254
 		uut.pop();
255
-		verify(child3, times(1)).onDestroy();
255
+		verify(child3, times(1)).destroy();
256
 	}
256
 	}
257
 
257
 
258
 	@Test
258
 	@Test
264
 		uut.push(child2);
264
 		uut.push(child2);
265
 		uut.push(child3);
265
 		uut.push(child3);
266
 
266
 
267
-		verify(child2, times(0)).onDestroy();
267
+		verify(child2, times(0)).destroy();
268
 		uut.popSpecific(child2);
268
 		uut.popSpecific(child2);
269
-		verify(child2, times(1)).onDestroy();
269
+		verify(child2, times(1)).destroy();
270
 	}
270
 	}
271
 
271
 
272
 	@Test
272
 	@Test
278
 		uut.push(child2);
278
 		uut.push(child2);
279
 		uut.push(child3);
279
 		uut.push(child3);
280
 
280
 
281
-		verify(child2, times(0)).onDestroy();
282
-		verify(child3, times(0)).onDestroy();
281
+		verify(child2, times(0)).destroy();
282
+		verify(child3, times(0)).destroy();
283
 		uut.popTo(child1);
283
 		uut.popTo(child1);
284
-		verify(child2, times(1)).onDestroy();
285
-		verify(child3, times(1)).onDestroy();
284
+		verify(child2, times(1)).destroy();
285
+		verify(child3, times(1)).destroy();
286
 	}
286
 	}
287
 
287
 
288
 	private void assertHasSingleChildViewOfController(ViewController childController) {
288
 	private void assertHasSingleChildViewOfController(ViewController childController) {

+ 20
- 14
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ViewControllerTest.java 查看文件

13
 
13
 
14
 import java.lang.reflect.Field;
14
 import java.lang.reflect.Field;
15
 
15
 
16
+import static org.assertj.core.api.Assertions.fail;
16
 import static org.assertj.core.api.Java6Assertions.assertThat;
17
 import static org.assertj.core.api.Java6Assertions.assertThat;
17
 import static org.mockito.Mockito.mock;
18
 import static org.mockito.Mockito.mock;
18
 import static org.mockito.Mockito.spy;
19
 import static org.mockito.Mockito.spy;
89
 		ViewController spy = spy(uut);
90
 		ViewController spy = spy(uut);
90
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
91
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
91
 		Assertions.assertThat(spy.getView()).isNotShown();
92
 		Assertions.assertThat(spy.getView()).isNotShown();
92
-		verify(spy, times(0)).onAppear();
93
+		verify(spy, times(0)).onViewAppeared();
93
 
94
 
94
 		Shadows.shadowOf(spy.getView()).setMyParent(mock(ViewParent.class));
95
 		Shadows.shadowOf(spy.getView()).setMyParent(mock(ViewParent.class));
95
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
96
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
96
 		Assertions.assertThat(spy.getView()).isShown();
97
 		Assertions.assertThat(spy.getView()).isShown();
97
 
98
 
98
-		verify(spy, times(1)).onAppear();
99
+		verify(spy, times(1)).onViewAppeared();
99
 	}
100
 	}
100
 
101
 
101
 	@Test
102
 	@Test
107
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
108
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
108
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
109
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
109
 
110
 
110
-		verify(spy, times(1)).onAppear();
111
+		verify(spy, times(1)).onViewAppeared();
111
 	}
112
 	}
112
 
113
 
113
 	@Test
114
 	@Test
116
 		Shadows.shadowOf(spy.getView()).setMyParent(mock(ViewParent.class));
117
 		Shadows.shadowOf(spy.getView()).setMyParent(mock(ViewParent.class));
117
 		Assertions.assertThat(spy.getView()).isShown();
118
 		Assertions.assertThat(spy.getView()).isShown();
118
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
119
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
119
-		verify(spy, times(1)).onAppear();
120
-		verify(spy, times(0)).onDisappear();
120
+		verify(spy, times(1)).onViewAppeared();
121
+		verify(spy, times(0)).onViewDisappear();
121
 
122
 
122
 		spy.getView().setVisibility(View.GONE);
123
 		spy.getView().setVisibility(View.GONE);
123
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
124
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
124
 		Assertions.assertThat(spy.getView()).isNotShown();
125
 		Assertions.assertThat(spy.getView()).isNotShown();
125
-		verify(spy, times(1)).onDisappear();
126
+		verify(spy, times(1)).onViewDisappear();
126
 	}
127
 	}
127
 
128
 
128
 	@Test
129
 	@Test
135
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
136
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
136
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
137
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
137
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
138
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
138
-		verify(spy, times(1)).onDisappear();
139
+		verify(spy, times(1)).onViewDisappear();
139
 	}
140
 	}
140
 
141
 
141
 	@Test
142
 	@Test
142
 	public void onDestroy_RemovesGlobalLayoutListener() throws Exception {
143
 	public void onDestroy_RemovesGlobalLayoutListener() throws Exception {
143
-		new SimpleViewController(activity, "ensureNotNull").onDestroy();
144
+		new SimpleViewController(activity, "ensureNotNull").destroy();
144
 
145
 
145
 		ViewController spy = spy(uut);
146
 		ViewController spy = spy(uut);
146
 		View view = spy.getView();
147
 		View view = spy.getView();
147
 		Shadows.shadowOf(view).setMyParent(mock(ViewParent.class));
148
 		Shadows.shadowOf(view).setMyParent(mock(ViewParent.class));
148
 
149
 
149
-		spy.onDestroy();
150
+		spy.destroy();
150
 
151
 
151
 		Assertions.assertThat(view).isShown();
152
 		Assertions.assertThat(view).isShown();
152
 		view.getViewTreeObserver().dispatchOnGlobalLayout();
153
 		view.getViewTreeObserver().dispatchOnGlobalLayout();
153
-		verify(spy, times(0)).onAppear();
154
-		verify(spy, times(0)).onDisappear();
154
+		verify(spy, times(0)).onViewAppeared();
155
+		verify(spy, times(0)).onViewDisappear();
155
 
156
 
156
 		Field field = ViewController.class.getDeclaredField("view");
157
 		Field field = ViewController.class.getDeclaredField("view");
157
 		field.setAccessible(true);
158
 		field.setAccessible(true);
164
 		Shadows.shadowOf(spy.getView()).setMyParent(mock(ViewParent.class));
165
 		Shadows.shadowOf(spy.getView()).setMyParent(mock(ViewParent.class));
165
 		Assertions.assertThat(spy.getView()).isShown();
166
 		Assertions.assertThat(spy.getView()).isShown();
166
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
167
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
167
-		verify(spy, times(1)).onAppear();
168
+		verify(spy, times(1)).onViewAppeared();
168
 
169
 
169
-		spy.onDestroy();
170
+		spy.destroy();
170
 
171
 
171
-		verify(spy, times(1)).onDisappear();
172
+		verify(spy, times(1)).onViewDisappear();
173
+	}
174
+
175
+	@Test
176
+	public void onDestroy_RemovesSelfFromParentIfExists() throws Exception {
177
+		fail("implement");
172
 	}
178
 	}
173
 }
179
 }
174
 
180