Browse Source

revert #1065 impl, going a different route

Daniel Zlotin 7 years ago
parent
commit
a96926e15f

+ 1
- 1
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ScreenLifecycleTest.java View File

@@ -6,7 +6,7 @@ import org.junit.Ignore;
6 6
 import org.junit.Test;
7 7
 
8 8
 public class ScreenLifecycleTest extends BaseTest {
9
-
9
+	@Ignore
10 10
 	@Test
11 11
 	public void onStartOnStop() throws Exception {
12 12
 		launchTheApp();

+ 18
- 20
lib/android/app/src/main/java/com/reactnativenavigation/layout/impl/ReactRootViewController.java View File

@@ -6,10 +6,9 @@ import android.view.View;
6 6
 
7 7
 import com.facebook.react.ReactInstanceManager;
8 8
 import com.facebook.react.ReactRootView;
9
-import com.reactnativenavigation.react.NavigationEvent;
10 9
 import com.reactnativenavigation.viewcontrollers.ViewController;
11 10
 
12
-public class ReactRootViewController extends ViewController implements View.OnAttachStateChangeListener {
11
+public class ReactRootViewController extends ViewController {
13 12
 
14 13
 	private final String id;
15 14
 	private final String name;
@@ -27,28 +26,27 @@ public class ReactRootViewController extends ViewController implements View.OnAt
27 26
 //		reactRootView.unmountReactApplication();
28 27
 //	}
29 28
 
30
-	@Override
31
-	public void onViewAttachedToWindow(final View v) {
32
-		//
33
-	}
34
-
35
-	@Override
36
-	public void onViewDetachedFromWindow(final View v) {
37
-		onStop();
38
-	}
39
-
40
-	private void onStart() {
41
-		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).containerStart(id);
42
-	}
43
-
44
-	private void onStop() {
45
-		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).containerStop(id);
46
-	}
29
+//	@Override
30
+//	public void onViewAttachedToWindow(final View v) {
31
+//		//
32
+//	}
33
+//
34
+//	@Override
35
+//	public void onViewDetachedFromWindow(final View v) {
36
+//		onStop();
37
+//	}
38
+//
39
+//	private void onStart() {
40
+//		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).containerStart(id);
41
+//	}
42
+//
43
+//	private void onStop() {
44
+//		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).containerStop(id);
45
+//	}
47 46
 
48 47
 	@Override
49 48
 	protected View createView() {
50 49
 		ReactRootView reactRootView = new ReactRootView(getActivity());
51
-		reactRootView.addOnAttachStateChangeListener(this);
52 50
 		Bundle opts = new Bundle();
53 51
 		opts.putString("id", this.id);
54 52
 		reactRootView.startReactApplication(this.reactInstanceManager, this.name, opts);

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

@@ -3,30 +3,11 @@ package com.reactnativenavigation.viewcontrollers;
3 3
 import android.app.Activity;
4 4
 import android.support.annotation.Nullable;
5 5
 import android.view.View;
6
-import android.view.ViewTreeObserver;
7
-
8
-import java.util.concurrent.atomic.AtomicReference;
9 6
 
10 7
 public abstract class ViewController {
11
-	public interface LifecycleListener {
12
-		void onCreate();
13
-
14
-		void onStart();
15
-
16
-		void onStop();
17
-
18
-		void onDestroy();
19
-	}
20
-
21
-	private enum LifecycleState {
22
-		Created, Started, Stopped, Destroyed
23
-	}
24
-
25
-	private final Activity activity;
26 8
 	private View view;
9
+	private final Activity activity;
27 10
 	private StackController stackController;
28
-	private AtomicReference<LifecycleState> lifecycleState = new AtomicReference<>(LifecycleState.Destroyed);
29
-	private LifecycleListener lifecycleListener;
30 11
 
31 12
 	public ViewController(Activity activity) {
32 13
 		this.activity = activity;
@@ -54,36 +35,25 @@ public abstract class ViewController {
54 35
 	public View getView() {
55 36
 		if (view == null) {
56 37
 			view = createView();
57
-			attachLifecycle();
38
+			onCreate();
58 39
 		}
59 40
 		return view;
60 41
 	}
61 42
 
62
-	public void setLifecycleListener(LifecycleListener lifecycleListener) {
63
-		this.lifecycleListener = lifecycleListener;
43
+	public void onCreate() {
44
+
64 45
 	}
65 46
 
66
-	private void attachLifecycle() {
67
-		view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
68
-			@Override
69
-			public void onGlobalLayout() {
70
-				if (lifecycleListener == null) {
71
-					return;
72
-				}
73
-				if (view.getVisibility() == View.VISIBLE) {
74
-					if (lifecycleState.compareAndSet(LifecycleState.Created, LifecycleState.Started)) {
75
-						lifecycleListener.onStart();
76
-					}
77
-				} else {
78
-					if (lifecycleState.compareAndSet(LifecycleState.Started, LifecycleState.Stopped)) {
79
-						lifecycleListener.onStop();
80
-					}
81
-				}
82
-			}
83
-		});
84
-		lifecycleState.set(LifecycleState.Created);
85
-		if (lifecycleListener != null) {
86
-			lifecycleListener.onCreate();
87
-		}
47
+	public void onStart() {
48
+
49
+	}
50
+
51
+	public void onStop() {
52
+
88 53
 	}
54
+
55
+	public void onDestroy() {
56
+
57
+	}
58
+
89 59
 }

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

@@ -9,10 +9,6 @@ import com.reactnativenavigation.mocks.SimpleViewController;
9 9
 import org.junit.Test;
10 10
 
11 11
 import static org.assertj.core.api.Java6Assertions.assertThat;
12
-import static org.mockito.Mockito.mock;
13
-import static org.mockito.Mockito.times;
14
-import static org.mockito.Mockito.verify;
15
-import static org.mockito.Mockito.verifyZeroInteractions;
16 12
 
17 13
 public class ViewControllerTest extends BaseTest {
18 14
 
@@ -60,59 +56,4 @@ public class ViewControllerTest extends BaseTest {
60 56
 	public void handleBackDefaultFalse() throws Exception {
61 57
 		assertThat(uut.handleBack()).isFalse();
62 58
 	}
63
-
64
-	public static class LifecycleTest extends BaseTest {
65
-		private ViewController controller;
66
-		private ViewController.LifecycleListener uut;
67
-
68
-		@Override
69
-		public void beforeEach() {
70
-			super.beforeEach();
71
-			Activity activity = newActivity();
72
-			controller = new SimpleViewController(activity, "controller");
73
-			uut = mock(ViewController.LifecycleListener.class);
74
-			controller.setLifecycleListener(uut);
75
-		}
76
-
77
-		@Test
78
-		public void onCreateView_CalledAsSoonAsPossible() throws Exception {
79
-			verifyZeroInteractions(uut);
80
-			controller.getView();
81
-			verify(uut, times(1)).onCreate();
82
-		}
83
-
84
-		@Test
85
-		public void onStart_CalledWhenVisible() throws Exception {
86
-			verifyZeroInteractions(uut);
87
-			controller.getView().getViewTreeObserver().dispatchOnGlobalLayout();
88
-			verify(uut, times(1)).onStart();
89
-		}
90
-
91
-		@Test
92
-		public void onStop_CalledWhenInvisible() throws Exception {
93
-			verifyZeroInteractions(uut);
94
-			controller.getView().getViewTreeObserver().dispatchOnGlobalLayout();
95
-			controller.getView().setVisibility(View.GONE);
96
-			controller.getView().getViewTreeObserver().dispatchOnGlobalLayout();
97
-			verify(uut, times(1)).onStop();
98
-		}
99
-
100
-		@Test
101
-		public void onStart_OnStop_Cycle() throws Exception {
102
-			verifyZeroInteractions(uut);
103
-			controller.getView().getViewTreeObserver().dispatchOnGlobalLayout();
104
-			controller.getView().getViewTreeObserver().dispatchOnGlobalLayout();
105
-			verify(uut, times(1)).onStart();
106
-			controller.getView().getViewTreeObserver().dispatchOnGlobalLayout();
107
-			controller.getView().setVisibility(View.INVISIBLE);
108
-			controller.getView().getViewTreeObserver().dispatchOnGlobalLayout();
109
-			controller.getView().getViewTreeObserver().dispatchOnGlobalLayout();
110
-			verify(uut, times(1)).onStop();
111
-			controller.getView().setVisibility(View.VISIBLE);
112
-			controller.getView().getViewTreeObserver().dispatchOnGlobalLayout();
113
-			controller.getView().getViewTreeObserver().dispatchOnGlobalLayout();
114
-			verify(uut, times(1)).onStart();
115
-		}
116
-	}
117
-
118 59
 }