Bladeren bron

android onAppear

Daniel Zlotin 7 jaren geleden
bovenliggende
commit
30c2211cff

+ 1
- 0
lib/android/app/build.gradle Bestand weergeven

@@ -57,5 +57,6 @@ dependencies {
57 57
     testCompile 'junit:junit:4.12'
58 58
     testCompile 'org.robolectric:robolectric:3.3'
59 59
     testCompile 'org.assertj:assertj-core:2.5.0'
60
+    testCompile 'com.squareup.assertj:assertj-android:1.1.1'
60 61
     testCompile 'org.mockito:mockito-core:2.7.5'
61 62
 }

+ 12
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java Bestand weergeven

@@ -4,10 +4,11 @@ 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.ViewTreeObserver;
7 8
 
8 9
 import com.reactnativenavigation.utils.StringUtils;
9 10
 
10
-public abstract class ViewController {
11
+public abstract class ViewController implements ViewTreeObserver.OnGlobalLayoutListener {
11 12
 
12 13
 	private final Activity activity;
13 14
 	private final String id;
@@ -43,6 +44,7 @@ public abstract class ViewController {
43 44
 	public View getView() {
44 45
 		if (view == null) {
45 46
 			view = createView();
47
+			view.getViewTreeObserver().addOnGlobalLayoutListener(this);
46 48
 		}
47 49
 		return view;
48 50
 	}
@@ -59,4 +61,13 @@ public abstract class ViewController {
59 61
 	public ViewController findControllerById(String id) {
60 62
 		return isSameId(id) ? this : null;
61 63
 	}
64
+
65
+	public void onAppear() {
66
+	}
67
+
68
+	@Override
69
+	public void onGlobalLayout() {
70
+		if (getView().isShown())
71
+			onAppear();
72
+	}
62 73
 }

+ 24
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ViewControllerTest.java Bestand weergeven

@@ -2,13 +2,20 @@ package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4 4
 import android.view.View;
5
+import android.view.ViewParent;
5 6
 
6 7
 import com.reactnativenavigation.BaseTest;
7 8
 import com.reactnativenavigation.mocks.SimpleViewController;
8 9
 
10
+import org.assertj.android.api.Assertions;
9 11
 import org.junit.Test;
12
+import org.robolectric.Shadows;
10 13
 
11 14
 import static org.assertj.core.api.Java6Assertions.assertThat;
15
+import static org.mockito.Mockito.mock;
16
+import static org.mockito.Mockito.spy;
17
+import static org.mockito.Mockito.times;
18
+import static org.mockito.Mockito.verify;
12 19
 
13 20
 public class ViewControllerTest extends BaseTest {
14 21
 
@@ -74,4 +81,21 @@ public class ViewControllerTest extends BaseTest {
74 81
 		assertThat(uut.findControllerById("456")).isNull();
75 82
 		assertThat(uut.findControllerById("uut")).isEqualTo(uut);
76 83
 	}
84
+
85
+	@Test
86
+	public void onAppear_WhenVisibleOnScreen() throws Exception {
87
+		ViewController spy = spy(uut);
88
+		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
89
+		Assertions.assertThat(spy.getView()).isNotShown();
90
+		verify(spy, times(0)).onAppear();
91
+
92
+		Shadows.shadowOf(spy.getView()).setMyParent(mock(ViewParent.class));
93
+		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
94
+		Assertions.assertThat(spy.getView()).isShown();
95
+
96
+		verify(spy, times(1)).onAppear();
97
+	}
98
+
99
+
77 100
 }
101
+