Преглед изворни кода

lifecycle methods inconsistant

Daniel Zlotin пре 8 година
родитељ
комит
2d95d1b19a

+ 6
- 6
lib/android/app/src/main/java/com/reactnativenavigation/layout/ReactRootViewController.java Прегледај датотеку

@@ -24,21 +24,21 @@ public class ReactRootViewController extends ViewController {
24 24
 	}
25 25
 
26 26
 	@Override
27
-	public void onDestroy() {
28
-		super.onDestroy();
27
+	public void destroy() {
28
+		super.destroy();
29 29
 		if (reactRootView != null) reactRootView.unmountReactApplication();
30 30
 		reactRootView = null;
31 31
 	}
32 32
 
33 33
 	@Override
34
-	public void onAppear() {
35
-		super.onAppear();
34
+	public void onViewAppeared() {
35
+		super.onViewAppeared();
36 36
 		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).containerStart(getId());
37 37
 	}
38 38
 
39 39
 	@Override
40
-	public void onDisappear() {
41
-		super.onDisappear();
40
+	public void onViewDisappear() {
41
+		super.onViewDisappear();
42 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,10 +32,10 @@ public class NavigationModule extends ReactContextBaseJavaModule {
32 32
 
33 33
 	@ReactMethod
34 34
 	public void setRoot(final ReadableMap rawLayoutTree) {
35
+		final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
35 36
 		handle(new Runnable() {
36 37
 			@Override
37 38
 			public void run() {
38
-				final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
39 39
 				final ViewController viewController = newLayoutFactory().create(layoutTree);
40 40
 				navigator().setRoot(viewController);
41 41
 			}
@@ -44,10 +44,10 @@ public class NavigationModule extends ReactContextBaseJavaModule {
44 44
 
45 45
 	@ReactMethod
46 46
 	public void push(final String onContainerId, final ReadableMap rawLayoutTree) {
47
+		final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
47 48
 		handle(new Runnable() {
48 49
 			@Override
49 50
 			public void run() {
50
-				final LayoutNode layoutTree = LayoutNodeParser.parse(JSONParser.parse(rawLayoutTree));
51 51
 				final ViewController viewController = newLayoutFactory().create(layoutTree);
52 52
 				navigator().push(onContainerId, viewController);
53 53
 			}

+ 3
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java Прегледај датотеку

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

+ 3
- 23
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ParentController.java Прегледај датотеку

@@ -40,31 +40,11 @@ public abstract class ParentController extends ViewController {
40 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 43
 	@Override
64
-	public void onDestroy() {
65
-		super.onDestroy();
44
+	public void destroy() {
45
+		super.destroy();
66 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,7 +56,7 @@ public class StackController extends ParentController {
56 56
 			@Override
57 57
 			public void run() {
58 58
 				getView().removeView(poppedTop.getView());
59
-				poppedTop.onDestroy();
59
+				poppedTop.destroy();
60 60
 			}
61 61
 		});
62 62
 	}
@@ -66,7 +66,7 @@ public class StackController extends ParentController {
66 66
 			pop();
67 67
 		} else {
68 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,6 +4,7 @@ import android.app.Activity;
4 4
 import android.support.annotation.NonNull;
5 5
 import android.support.annotation.Nullable;
6 6
 import android.view.View;
7
+import android.view.ViewGroup;
7 8
 import android.view.ViewTreeObserver;
8 9
 
9 10
 import com.reactnativenavigation.utils.StringUtils;
@@ -63,21 +64,24 @@ public abstract class ViewController implements ViewTreeObserver.OnGlobalLayoutL
63 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 76
 		if (isShown) {
76 77
 			isShown = false;
77
-			onDisappear();
78
+			onViewDisappear();
78 79
 		}
79 80
 		if (view != null) {
80 81
 			view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
82
+			if (view.getParent() instanceof ViewGroup) {
83
+				((ViewGroup) view.getParent()).removeView(view);
84
+			}
81 85
 			view = null;
82 86
 		}
83 87
 	}
@@ -86,10 +90,10 @@ public abstract class ViewController implements ViewTreeObserver.OnGlobalLayoutL
86 90
 	public void onGlobalLayout() {
87 91
 		if (!isShown && isViewShown()) {
88 92
 			isShown = true;
89
-			onAppear();
93
+			onViewAppeared();
90 94
 		} else if (isShown && !isViewShown()) {
91 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,9 +85,9 @@ public class ParentControllerTest extends BaseTest {
85 85
 		ViewController child1 = spy(new SimpleViewController(activity, "child1"));
86 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 93
 	@Test
@@ -95,9 +95,9 @@ public class ParentControllerTest extends BaseTest {
95 95
 		ViewController child1 = spy(new SimpleViewController(activity, "child1"));
96 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 103
 	@Test
@@ -105,8 +105,8 @@ public class ParentControllerTest extends BaseTest {
105 105
 		ViewController child1 = spy(new SimpleViewController(activity, "child1"));
106 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,9 +250,9 @@ public class StackControllerTest extends BaseTest {
250 250
 		uut.push(child2);
251 251
 		uut.push(child3);
252 252
 
253
-		verify(child3, times(0)).onDestroy();
253
+		verify(child3, times(0)).destroy();
254 254
 		uut.pop();
255
-		verify(child3, times(1)).onDestroy();
255
+		verify(child3, times(1)).destroy();
256 256
 	}
257 257
 
258 258
 	@Test
@@ -264,9 +264,9 @@ public class StackControllerTest extends BaseTest {
264 264
 		uut.push(child2);
265 265
 		uut.push(child3);
266 266
 
267
-		verify(child2, times(0)).onDestroy();
267
+		verify(child2, times(0)).destroy();
268 268
 		uut.popSpecific(child2);
269
-		verify(child2, times(1)).onDestroy();
269
+		verify(child2, times(1)).destroy();
270 270
 	}
271 271
 
272 272
 	@Test
@@ -278,11 +278,11 @@ public class StackControllerTest extends BaseTest {
278 278
 		uut.push(child2);
279 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 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 288
 	private void assertHasSingleChildViewOfController(ViewController childController) {

+ 20
- 14
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ViewControllerTest.java Прегледај датотеку

@@ -13,6 +13,7 @@ import org.robolectric.Shadows;
13 13
 
14 14
 import java.lang.reflect.Field;
15 15
 
16
+import static org.assertj.core.api.Assertions.fail;
16 17
 import static org.assertj.core.api.Java6Assertions.assertThat;
17 18
 import static org.mockito.Mockito.mock;
18 19
 import static org.mockito.Mockito.spy;
@@ -89,13 +90,13 @@ public class ViewControllerTest extends BaseTest {
89 90
 		ViewController spy = spy(uut);
90 91
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
91 92
 		Assertions.assertThat(spy.getView()).isNotShown();
92
-		verify(spy, times(0)).onAppear();
93
+		verify(spy, times(0)).onViewAppeared();
93 94
 
94 95
 		Shadows.shadowOf(spy.getView()).setMyParent(mock(ViewParent.class));
95 96
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
96 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 102
 	@Test
@@ -107,7 +108,7 @@ public class ViewControllerTest extends BaseTest {
107 108
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
108 109
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
109 110
 
110
-		verify(spy, times(1)).onAppear();
111
+		verify(spy, times(1)).onViewAppeared();
111 112
 	}
112 113
 
113 114
 	@Test
@@ -116,13 +117,13 @@ public class ViewControllerTest extends BaseTest {
116 117
 		Shadows.shadowOf(spy.getView()).setMyParent(mock(ViewParent.class));
117 118
 		Assertions.assertThat(spy.getView()).isShown();
118 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 123
 		spy.getView().setVisibility(View.GONE);
123 124
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
124 125
 		Assertions.assertThat(spy.getView()).isNotShown();
125
-		verify(spy, times(1)).onDisappear();
126
+		verify(spy, times(1)).onViewDisappear();
126 127
 	}
127 128
 
128 129
 	@Test
@@ -135,23 +136,23 @@ public class ViewControllerTest extends BaseTest {
135 136
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
136 137
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
137 138
 		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
138
-		verify(spy, times(1)).onDisappear();
139
+		verify(spy, times(1)).onViewDisappear();
139 140
 	}
140 141
 
141 142
 	@Test
142 143
 	public void onDestroy_RemovesGlobalLayoutListener() throws Exception {
143
-		new SimpleViewController(activity, "ensureNotNull").onDestroy();
144
+		new SimpleViewController(activity, "ensureNotNull").destroy();
144 145
 
145 146
 		ViewController spy = spy(uut);
146 147
 		View view = spy.getView();
147 148
 		Shadows.shadowOf(view).setMyParent(mock(ViewParent.class));
148 149
 
149
-		spy.onDestroy();
150
+		spy.destroy();
150 151
 
151 152
 		Assertions.assertThat(view).isShown();
152 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 157
 		Field field = ViewController.class.getDeclaredField("view");
157 158
 		field.setAccessible(true);
@@ -164,11 +165,16 @@ public class ViewControllerTest extends BaseTest {
164 165
 		Shadows.shadowOf(spy.getView()).setMyParent(mock(ViewParent.class));
165 166
 		Assertions.assertThat(spy.getView()).isShown();
166 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