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

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

6
 
6
 
7
 import com.facebook.react.ReactInstanceManager;
7
 import com.facebook.react.ReactInstanceManager;
8
 import com.facebook.react.ReactRootView;
8
 import com.facebook.react.ReactRootView;
9
-import com.reactnativenavigation.react.NavigationEvent;
10
 import com.reactnativenavigation.viewcontrollers.ViewController;
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
 	private final String id;
13
 	private final String id;
15
 	private final String name;
14
 	private final String name;
27
 //		reactRootView.unmountReactApplication();
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
 	@Override
47
 	@Override
49
 	protected View createView() {
48
 	protected View createView() {
50
 		ReactRootView reactRootView = new ReactRootView(getActivity());
49
 		ReactRootView reactRootView = new ReactRootView(getActivity());
51
-		reactRootView.addOnAttachStateChangeListener(this);
52
 		Bundle opts = new Bundle();
50
 		Bundle opts = new Bundle();
53
 		opts.putString("id", this.id);
51
 		opts.putString("id", this.id);
54
 		reactRootView.startReactApplication(this.reactInstanceManager, this.name, opts);
52
 		reactRootView.startReactApplication(this.reactInstanceManager, this.name, opts);

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

3
 import android.app.Activity;
3
 import android.app.Activity;
4
 import android.support.annotation.Nullable;
4
 import android.support.annotation.Nullable;
5
 import android.view.View;
5
 import android.view.View;
6
-import android.view.ViewTreeObserver;
7
-
8
-import java.util.concurrent.atomic.AtomicReference;
9
 
6
 
10
 public abstract class ViewController {
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
 	private View view;
8
 	private View view;
9
+	private final Activity activity;
27
 	private StackController stackController;
10
 	private StackController stackController;
28
-	private AtomicReference<LifecycleState> lifecycleState = new AtomicReference<>(LifecycleState.Destroyed);
29
-	private LifecycleListener lifecycleListener;
30
 
11
 
31
 	public ViewController(Activity activity) {
12
 	public ViewController(Activity activity) {
32
 		this.activity = activity;
13
 		this.activity = activity;
54
 	public View getView() {
35
 	public View getView() {
55
 		if (view == null) {
36
 		if (view == null) {
56
 			view = createView();
37
 			view = createView();
57
-			attachLifecycle();
38
+			onCreate();
58
 		}
39
 		}
59
 		return view;
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
 import org.junit.Test;
9
 import org.junit.Test;
10
 
10
 
11
 import static org.assertj.core.api.Java6Assertions.assertThat;
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
 public class ViewControllerTest extends BaseTest {
13
 public class ViewControllerTest extends BaseTest {
18
 
14
 
60
 	public void handleBackDefaultFalse() throws Exception {
56
 	public void handleBackDefaultFalse() throws Exception {
61
 		assertThat(uut.handleBack()).isFalse();
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
 }