Ver código fonte

viewController holds ref to navigationController

Daniel Zlotin 8 anos atrás
pai
commit
aa191947bc

+ 0
- 12
lib/android/app/src/main/java/com/reactnativenavigation/NavigationActivity.java Ver arquivo

@@ -3,7 +3,6 @@ package com.reactnativenavigation;
3 3
 import android.os.Bundle;
4 4
 import android.support.annotation.Nullable;
5 5
 import android.support.v7.app.AppCompatActivity;
6
-import android.view.View;
7 6
 import android.widget.FrameLayout;
8 7
 
9 8
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
@@ -11,7 +10,6 @@ import com.reactnativenavigation.layout.Layout;
11 10
 import com.reactnativenavigation.layout.impl.StackLayout;
12 11
 
13 12
 public class NavigationActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler {
14
-	private View contentView;
15 13
 	private Layout layout;
16 14
 
17 15
 	@Override
@@ -39,11 +37,6 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
39 37
 		app().getInitializer().onActivityDestroyed(this);
40 38
 	}
41 39
 
42
-	@Override
43
-	public void setContentView(View contentView) {
44
-		super.setContentView(contentView);
45
-		this.contentView = contentView;
46
-	}
47 40
 
48 41
 	public void setLayout(Layout layout) {
49 42
 		this.layout = layout;
@@ -53,11 +46,6 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
53 46
 		return layout;
54 47
 	}
55 48
 
56
-	@Nullable
57
-	public View getContentView() {
58
-		return contentView;
59
-	}
60
-
61 49
 	@Override
62 50
 	public void invokeDefaultOnBackPressed() {
63 51
 		onBackPressed();

+ 2
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/NavigationController.java Ver arquivo

@@ -8,7 +8,7 @@ public class NavigationController extends ViewController {
8 8
 
9 9
 	public NavigationController(ViewController... childControllers) {
10 10
 		for (ViewController childController : childControllers) {
11
-			this.childControllers.push(childController);
11
+			push(childController);
12 12
 		}
13 13
 	}
14 14
 
@@ -18,6 +18,7 @@ public class NavigationController extends ViewController {
18 18
 
19 19
 	public void push(final ViewController child) {
20 20
 		childControllers.push(child);
21
+		child.setNavigationController(this);
21 22
 	}
22 23
 
23 24
 	public void pop() {

+ 11
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java Ver arquivo

@@ -1,9 +1,11 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3
+import android.support.annotation.Nullable;
3 4
 import android.view.View;
4 5
 
5 6
 public class ViewController {
6 7
 	private View view;
8
+	private NavigationController navigationController;
7 9
 
8 10
 	public View getView() {
9 11
 		return view;
@@ -12,4 +14,13 @@ public class ViewController {
12 14
 	public void setView(final View view) {
13 15
 		this.view = view;
14 16
 	}
17
+
18
+	@Nullable
19
+	public NavigationController getNavigationController() {
20
+		return navigationController;
21
+	}
22
+
23
+	void setNavigationController(final NavigationController navigationController) {
24
+		this.navigationController = navigationController;
25
+	}
15 26
 }

+ 9
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigationControllerTest.java Ver arquivo

@@ -63,4 +63,13 @@ public class NavigationControllerTest extends BaseTest {
63 63
 		assertThat(uut.isEmpty()).isFalse();
64 64
 	}
65 65
 
66
+	@Test
67
+	public void pushAssignsRefToSelfOnPushedController() throws Exception {
68
+		assertThat(child1.getNavigationController()).isNull();
69
+		uut.push(child1);
70
+		assertThat(child1.getNavigationController()).isEqualTo(uut);
71
+
72
+		NavigationController anotherNavController = new NavigationController(child2);
73
+		assertThat(child2.getNavigationController()).isEqualTo(anotherNavController);
74
+	}
66 75
 }

+ 15
- 2
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ViewControllerTest.java Ver arquivo

@@ -10,14 +10,27 @@ import org.robolectric.shadow.api.Shadow;
10 10
 import static org.assertj.core.api.Java6Assertions.assertThat;
11 11
 
12 12
 public class ViewControllerTest extends BaseTest {
13
+
14
+	private ViewController uut;
15
+
16
+	@Override
17
+	public void beforeEach() {
18
+		super.beforeEach();
19
+		uut = new ViewController();
20
+	}
21
+
13 22
 	@Test
14 23
 	public void holdsAView() throws Exception {
15
-		ViewController uut = new ViewController();
16 24
 		assertThat(uut.getView()).isNull();
17 25
 		View view = Shadow.newInstanceOf(View.class);
18 26
 		uut.setView(view);
19 27
 		assertThat(uut.getView()).isEqualTo(view);
20 28
 	}
21 29
 
22
-
30
+	@Test
31
+	public void holdsAReferenceToNavigationControllerOrNull() throws Exception {
32
+		assertThat(uut.getNavigationController()).isNull();
33
+		NavigationController nav = new NavigationController(uut);
34
+		assertThat(uut.getNavigationController()).isEqualTo(nav);
35
+	}
23 36
 }