소스 검색

pop and popTo

Daniel Zlotin 8 년 전
부모
커밋
5cc5067c74

+ 19
- 9
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java 파일 보기

66
 	}
66
 	}
67
 
67
 
68
 	public void push(final String fromId, final ViewController viewController) {
68
 	public void push(final String fromId, final ViewController viewController) {
69
-		StackController parentStackController = findParentStackControllerForChildId(fromId);
70
-		if (parentStackController != null) {
71
-			parentStackController.push(viewController);
69
+		ViewController from = findControllerById(fromId);
70
+		if (from != null) {
71
+			StackController parentStackController = from.getParentStackController();
72
+			if (parentStackController != null) {
73
+				parentStackController.push(viewController);
74
+			}
72
 		}
75
 		}
73
 	}
76
 	}
74
 
77
 
75
 	public void pop(final String fromId) {
78
 	public void pop(final String fromId) {
76
-		StackController parentStackController = findParentStackControllerForChildId(fromId);
77
-		if (parentStackController != null) {
78
-			parentStackController.pop();
79
+		ViewController from = findControllerById(fromId);
80
+		if (from != null) {
81
+			StackController parentStackController = from.getParentStackController();
82
+			if (parentStackController != null) {
83
+				parentStackController.pop(from);
84
+			}
79
 		}
85
 		}
80
 	}
86
 	}
81
 
87
 
82
 	public void popTo(final String fromId, final String toId) {
88
 	public void popTo(final String fromId, final String toId) {
83
-		StackController parentStackController = findParentStackControllerForChildId(fromId);
84
-		if (parentStackController != null) {
85
-			parentStackController.popTo(findControllerById(toId));
89
+		ViewController from = findControllerById(fromId);
90
+		ViewController to = findControllerById(toId);
91
+		if (from != null && to != null) {
92
+			StackController parentStackController = from.getParentStackController();
93
+			if (parentStackController != null) {
94
+				parentStackController.popTo(to);
95
+			}
86
 		}
96
 		}
87
 	}
97
 	}
88
 }
98
 }

+ 6
- 11
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ParentController.java 파일 보기

8
 import java.util.Collection;
8
 import java.util.Collection;
9
 
9
 
10
 public abstract class ParentController extends ViewController {
10
 public abstract class ParentController extends ViewController {
11
+
11
 	public ParentController(final Activity activity, final String id) {
12
 	public ParentController(final Activity activity, final String id) {
12
 		super(activity, id);
13
 		super(activity, id);
13
 	}
14
 	}
22
 	@Override
23
 	@Override
23
 	protected abstract ViewGroup createView();
24
 	protected abstract ViewGroup createView();
24
 
25
 
26
+	@NonNull
25
 	public abstract Collection<ViewController> getChildControllers();
27
 	public abstract Collection<ViewController> getChildControllers();
26
 
28
 
27
 	@Nullable
29
 	@Nullable
30
+	@Override
28
 	public ViewController findControllerById(final String id) {
31
 	public ViewController findControllerById(final String id) {
29
 		ViewController fromSuper = super.findControllerById(id);
32
 		ViewController fromSuper = super.findControllerById(id);
30
-		if (fromSuper != null) {
31
-			return fromSuper;
32
-		}
33
+		if (fromSuper != null) return fromSuper;
33
 
34
 
34
 		for (ViewController child : getChildControllers()) {
35
 		for (ViewController child : getChildControllers()) {
35
-			ViewController found = child.findControllerById(id);
36
-			if (found != null) return found;
36
+			ViewController fromChild = child.findControllerById(id);
37
+			if (fromChild != null) return fromChild;
37
 		}
38
 		}
38
 
39
 
39
 		return null;
40
 		return null;
40
 	}
41
 	}
41
-
42
-	@Nullable
43
-	public StackController findParentStackControllerForChildId(final String childId) {
44
-		ViewController found = findControllerById(childId);
45
-		return found != null ? found.getParentStackController() : null;
46
-	}
47
 }
42
 }

+ 1
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java 파일 보기

103
 		return stack.containsId(id);
103
 		return stack.containsId(id);
104
 	}
104
 	}
105
 
105
 
106
+	@NonNull
106
 	@Override
107
 	@Override
107
 	public Collection<ViewController> getChildControllers() {
108
 	public Collection<ViewController> getChildControllers() {
108
 		return stack.values();
109
 		return stack.values();

+ 8
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java 파일 보기

8
 import com.reactnativenavigation.utils.StringUtils;
8
 import com.reactnativenavigation.utils.StringUtils;
9
 
9
 
10
 public abstract class ViewController {
10
 public abstract class ViewController {
11
+
11
 	private final Activity activity;
12
 	private final Activity activity;
12
 	private final String id;
13
 	private final String id;
13
 	private View view;
14
 	private View view;
50
 		return id;
51
 		return id;
51
 	}
52
 	}
52
 
53
 
53
-	public ViewController findControllerById(final String id) {
54
-		return StringUtils.isEqual(this.id, id) ? this : null;
54
+	public boolean isSameId(final String id) {
55
+		return StringUtils.isEqual(this.id, id);
56
+	}
57
+
58
+	@Nullable
59
+	public ViewController findControllerById(String id) {
60
+		return isSameId(id) ? this : null;
55
 	}
61
 	}
56
 }
62
 }

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java 파일 보기

127
 		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
127
 		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
128
 		uut.setRoot(bottomTabsController);
128
 		uut.setRoot(bottomTabsController);
129
 
129
 
130
-		uut.pop(child2.getId());
130
+		uut.pop("child4");
131
 
131
 
132
 		assertThat(stack2.getChildControllers()).containsOnly(child2, child3);
132
 		assertThat(stack2.getChildControllers()).containsOnly(child2, child3);
133
 	}
133
 	}

+ 30
- 70
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ParentControllerTest.java 파일 보기

10
 
10
 
11
 import org.junit.Test;
11
 import org.junit.Test;
12
 
12
 
13
-import java.util.Arrays;
13
+import java.util.ArrayList;
14
 import java.util.Collection;
14
 import java.util.Collection;
15
-import java.util.Collections;
15
+import java.util.List;
16
 
16
 
17
 import static org.assertj.core.api.Java6Assertions.assertThat;
17
 import static org.assertj.core.api.Java6Assertions.assertThat;
18
 
18
 
19
 public class ParentControllerTest extends BaseTest {
19
 public class ParentControllerTest extends BaseTest {
20
 
20
 
21
 	private Activity activity;
21
 	private Activity activity;
22
+	private List<ViewController> children;
23
+	private ParentController uut;
22
 
24
 
23
 	@Override
25
 	@Override
24
 	public void beforeEach() {
26
 	public void beforeEach() {
25
 		super.beforeEach();
27
 		super.beforeEach();
26
 		activity = newActivity();
28
 		activity = newActivity();
27
-	}
28
-
29
-	@Test
30
-	public void holdsViewGroup() throws Exception {
31
-		ParentController uut = new ParentController(activity, "uut") {
32
-			@Override
33
-			public Collection<ViewController> getChildControllers() {
34
-				return Collections.emptyList();
35
-			}
29
+		children = new ArrayList<>();
30
+		uut = new ParentController(activity, "uut") {
36
 
31
 
37
 			@NonNull
32
 			@NonNull
38
 			@Override
33
 			@Override
39
 			protected ViewGroup createView() {
34
 			protected ViewGroup createView() {
40
 				return new FrameLayout(activity);
35
 				return new FrameLayout(activity);
41
 			}
36
 			}
42
-		};
43
-
44
-		assertThat(uut.getView()).isInstanceOf(ViewGroup.class);
45
-	}
46
-
47
-	@Test
48
-	public void findControllerById_ReturnsSelfIfSameId() throws Exception {
49
-		ParentController uut = new ParentController(activity, "uut") {
50
-			@Override
51
-			public Collection<ViewController> getChildControllers() {
52
-				return Collections.emptyList();
53
-			}
54
 
37
 
55
 			@NonNull
38
 			@NonNull
56
 			@Override
39
 			@Override
57
-			protected ViewGroup createView() {
58
-				return new FrameLayout(activity);
40
+			public Collection<ViewController> getChildControllers() {
41
+				return children;
59
 			}
42
 			}
60
 		};
43
 		};
61
-
62
-		assertThat(uut.findControllerById("123")).isNull();
63
-		assertThat(uut.findControllerById(uut.getId())).isEqualTo(uut);
64
 	}
44
 	}
65
 
45
 
66
 	@Test
46
 	@Test
67
-	public void findControllerById_DeeplyInOneOfTheChildren() throws Exception {
68
-		ViewController child1 = new SimpleViewController(activity, "child1");
69
-		ViewController child2 = new SimpleViewController(activity, "child2");
70
-
71
-		final StackController someInnerStack = new StackController(activity, "stack1");
72
-		someInnerStack.push(child1);
73
-		someInnerStack.push(child2);
47
+	public void holdsViewGroup() throws Exception {
48
+		assertThat(uut.getView()).isInstanceOf(ViewGroup.class);
49
+	}
74
 
50
 
75
-		ParentController uut = new ParentController(activity, "uut") {
76
-			@Override
77
-			public Collection<ViewController> getChildControllers() {
78
-				return Arrays.<ViewController>asList(someInnerStack);
79
-			}
51
+	@Test
52
+	public void mustHaveChildControllers() throws Exception {
53
+		assertThat(uut.getChildControllers()).isNotNull();
54
+	}
80
 
55
 
81
-			@NonNull
82
-			@Override
83
-			protected ViewGroup createView() {
84
-				return new FrameLayout(activity);
85
-			}
86
-		};
56
+	@Test
57
+	public void findControllerById_ChildById() throws Exception {
58
+		SimpleViewController child1 = new SimpleViewController(activity, "child1");
59
+		SimpleViewController child2 = new SimpleViewController(activity, "child2");
60
+		children.add(child1);
61
+		children.add(child2);
87
 
62
 
88
-		assertThat(uut.findControllerById("stack1")).isEqualTo(someInnerStack);
63
+		assertThat(uut.findControllerById("uut")).isEqualTo(uut);
89
 		assertThat(uut.findControllerById("child1")).isEqualTo(child1);
64
 		assertThat(uut.findControllerById("child1")).isEqualTo(child1);
90
-		assertThat(uut.findControllerById("child2")).isEqualTo(child2);
91
 	}
65
 	}
92
 
66
 
93
 	@Test
67
 	@Test
94
-	public void findParentStackControllerForChildId() throws Exception {
95
-		ViewController child1 = new SimpleViewController(activity, "child1");
96
-		ViewController child2 = new SimpleViewController(activity, "child2");
97
-
98
-		final StackController someInnerStack = new StackController(activity, "stack1");
99
-		someInnerStack.push(child1);
100
-		someInnerStack.push(child2);
68
+	public void findControllerById_Recursive() throws Exception {
69
+		StackController stackController = new StackController(activity, "stack");
70
+		SimpleViewController child1 = new SimpleViewController(activity, "child1");
71
+		SimpleViewController child2 = new SimpleViewController(activity, "child2");
72
+		stackController.push(child1);
73
+		stackController.push(child2);
74
+		children.add(stackController);
101
 
75
 
102
-		ParentController uut = new ParentController(activity, "uut") {
103
-			@Override
104
-			public Collection<ViewController> getChildControllers() {
105
-				return Arrays.<ViewController>asList(someInnerStack);
106
-			}
107
-
108
-			@NonNull
109
-			@Override
110
-			protected ViewGroup createView() {
111
-				return new FrameLayout(activity);
112
-			}
113
-		};
114
-
115
-		assertThat(uut.findParentStackControllerForChildId("not existing child")).isNull();
116
-		assertThat(uut.findParentStackControllerForChildId("child2")).isEqualTo(someInnerStack);
76
+		assertThat(uut.findControllerById("child2")).isEqualTo(child2);
117
 	}
77
 	}
118
 }
78
 }

+ 10
- 3
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ViewControllerTest.java 파일 보기

63
 	}
63
 	}
64
 
64
 
65
 	@Test
65
 	@Test
66
-	public void findControllerById_ReturnsSelfIfSameId() throws Exception {
67
-		assertThat(uut.findControllerById("123")).isNull();
68
-		assertThat(uut.findControllerById(uut.getId())).isEqualTo(uut);
66
+	public void isSameId() throws Exception {
67
+		assertThat(uut.isSameId("")).isFalse();
68
+		assertThat(uut.isSameId(null)).isFalse();
69
+		assertThat(uut.isSameId("uut")).isTrue();
70
+	}
71
+
72
+	@Test
73
+	public void findControllerById_SelfOrNull() throws Exception {
74
+		assertThat(uut.findControllerById("456")).isNull();
75
+		assertThat(uut.findControllerById("uut")).isEqualTo(uut);
69
 	}
76
 	}
70
 }
77
 }