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

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

@@ -1,7 +1,6 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4
-import android.view.View;
5 4
 import android.view.ViewGroup;
6 5
 import android.widget.FrameLayout;
7 6
 
@@ -35,8 +34,14 @@ public class StackController extends ViewController {
35 34
 		getView().addView(child.getView());
36 35
 	}
37 36
 
37
+	public boolean canPop() {
38
+		return childControllers.size() > 1;
39
+	}
40
+
38 41
 	public void pop() {
39
-		childControllers.pop();
42
+		if (canPop()) {
43
+			childControllers.pop();
44
+		}
40 45
 	}
41 46
 
42 47
 	public ViewController peek() {
@@ -53,7 +58,7 @@ public class StackController extends ViewController {
53 58
 
54 59
 	@Override
55 60
 	public boolean handleBack() {
56
-		if (size() > 1) {
61
+		if (canPop()) {
57 62
 			pop();
58 63
 			return true;
59 64
 		} else {
@@ -62,7 +67,7 @@ public class StackController extends ViewController {
62 67
 	}
63 68
 
64 69
 	@Override
65
-	protected View onCreateView() {
70
+	protected ViewGroup onCreateView() {
66 71
 		return new FrameLayout(getActivity());
67 72
 	}
68 73
 }

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

@@ -6,12 +6,11 @@ import android.view.View;
6 6
 
7 7
 public abstract class ViewController {
8 8
 	private final Activity activity;
9
-	private final View view;
9
+	private View view;
10 10
 	private StackController stackController;
11 11
 
12 12
 	public ViewController(Activity activity) {
13 13
 		this.activity = activity;
14
-		this.view = onCreateView();
15 14
 	}
16 15
 
17 16
 	protected abstract View onCreateView();
@@ -34,6 +33,9 @@ public abstract class ViewController {
34 33
 	}
35 34
 
36 35
 	public View getView() {
36
+		if (view == null) {
37
+			view = onCreateView();
38
+		}
37 39
 		return view;
38 40
 	}
39 41
 }

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

@@ -7,12 +7,10 @@ import android.widget.FrameLayout;
7 7
 import com.reactnativenavigation.BaseTest;
8 8
 import com.reactnativenavigation.mocks.SimpleViewController;
9 9
 
10
-import org.junit.Ignore;
11 10
 import org.junit.Test;
12 11
 
13 12
 import static org.assertj.core.api.Java6Assertions.assertThat;
14 13
 
15
-@Ignore
16 14
 public class StackControllerTest extends BaseTest {
17 15
 
18 16
 	private Activity activity;
@@ -58,8 +56,6 @@ public class StackControllerTest extends BaseTest {
58 56
 		assertThat(uut.getChildControllers()).containsExactly(child2, child1);
59 57
 		uut.pop();
60 58
 		assertThat(uut.getChildControllers()).containsExactly(child1);
61
-		uut.pop();
62
-		assertThat(uut.getChildControllers()).isEmpty();
63 59
 	}
64 60
 
65 61
 	@Test
@@ -100,6 +96,29 @@ public class StackControllerTest extends BaseTest {
100 96
 		assertThat(uut.handleBack()).isFalse();
101 97
 	}
102 98
 
99
+	@Test
100
+	public void popDoesNothingWhenZeroOrOneChild() throws Exception {
101
+		assertThat(uut.getChildControllers().size()).isZero();
102
+		uut.pop();
103
+		assertThat(uut.getChildControllers().size()).isZero();
104
+
105
+		uut.push(child1);
106
+		uut.pop();
107
+		assertThat(uut.getChildControllers().size()).isEqualTo(1);
108
+	}
109
+
110
+	@Test
111
+	public void canPopWhenSizeIsMoreThanOne() throws Exception {
112
+		assertThat(uut.getChildControllers().size()).isZero();
113
+		assertThat(uut.canPop()).isFalse();
114
+		uut.push(child1);
115
+		assertThat(uut.getChildControllers().size()).isEqualTo(1);
116
+		assertThat(uut.canPop()).isFalse();
117
+		uut.push(child2);
118
+		assertThat(uut.getChildControllers().size()).isEqualTo(2);
119
+		assertThat(uut.canPop()).isTrue();
120
+	}
121
+
103 122
 	@Test
104 123
 	public void constructsSelfWithFrameLayout() throws Exception {
105 124
 		assertThat(uut.getView())