Daniel Zlotin 7 anni fa
parent
commit
5fbe960885

+ 0
- 83
lib/android/app/src/main/java/com/reactnativenavigation/layout/impl/BottomTabsLayout.java Vedi File

@@ -1,83 +0,0 @@
1
-package com.reactnativenavigation.layout.impl;
2
-
3
-public class BottomTabsLayout {
4
-
5
-//public class BottomTabsLayout implements Layout, BottomNavigationView.OnNavigationItemSelectedListener {
6
-
7
-//	private StackLayout stackLayout;
8
-//
9
-//	public static class TooManyTabs extends RuntimeException {
10
-//		//
11
-//	}
12
-//
13
-//	private final RelativeLayout view;
14
-//	private final BottomNavigationView bottomNavigationView;
15
-//	private final List<Layout> tabs = new ArrayList<>();
16
-//	private int currentTab;
17
-//
18
-//	public BottomTabsLayout(Activity activity) {
19
-//		view = new RelativeLayout(activity);
20
-//		view.setId(CompatUtils.generateViewId());
21
-//
22
-//		bottomNavigationView = new BottomNavigationView(view.getContext());
23
-//		bottomNavigationView.setId(CompatUtils.generateViewId());
24
-//		bottomNavigationView.setBackgroundColor(Color.DKGRAY);
25
-//		bottomNavigationView.setOnNavigationItemSelectedListener(this);
26
-//		LayoutParams lp = new LayoutParams(MATCH_PARENT, WRAP_CONTENT);
27
-//		lp.addRule(ALIGN_PARENT_BOTTOM);
28
-//		bottomNavigationView.setLayoutParams(lp);
29
-//		view.addView(bottomNavigationView, lp);
30
-//	}
31
-//
32
-//	@Override
33
-//	public View getView() {
34
-//		return view;
35
-//	}
36
-//
37
-//	@Override
38
-//	public void destroy() {
39
-//		//
40
-//	}
41
-//
42
-//	@Override
43
-//	public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
44
-//		hideTab(currentTab);
45
-//		currentTab = item.getItemId();
46
-//		showTab(currentTab);
47
-//		return true;
48
-//	}
49
-//
50
-//	public void addTab(String label, Layout tabLayout) {
51
-//		if (tabs.size() >= 5) {
52
-//			throw new TooManyTabs();
53
-//		}
54
-//		int tabId = bottomNavigationView.getMenu().size();
55
-//		bottomNavigationView.getMenu().add(0, tabId, Menu.NONE, label);
56
-//		LayoutParams tabParams = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
57
-//		tabParams.addRule(ABOVE, bottomNavigationView.getId());
58
-//		view.addView(tabLayout.getView(), tabParams);
59
-//		tabs.add(tabLayout);
60
-//
61
-//		if (tabs.size() > 1) {
62
-//			tabLayout.getView().setVisibility(View.GONE);
63
-//		}
64
-//	}
65
-//
66
-//	private void showTab(int tabId) {
67
-//		tabs.get(tabId).getView().setVisibility(View.VISIBLE);
68
-//	}
69
-//
70
-//	private void hideTab(int tabId) {
71
-//		tabs.get(tabId).getView().setVisibility(View.GONE);
72
-//	}
73
-//
74
-//	@Override
75
-//	public void setParentStackLayout(final StackLayout stackLayout) {
76
-//		this.stackLayout = stackLayout;
77
-//	}
78
-//
79
-//	@Override
80
-//	public StackLayout getParentStackLayout() {
81
-//		return stackLayout;
82
-//	}
83
-}

+ 8
- 6
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/BottomTabsController.java Vedi File

@@ -11,6 +11,7 @@ import android.widget.RelativeLayout;
11 11
 
12 12
 import com.reactnativenavigation.utils.CompatUtils;
13 13
 
14
+import java.util.ArrayList;
14 15
 import java.util.List;
15 16
 
16 17
 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
@@ -18,10 +19,10 @@ import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
18 19
 import static android.widget.RelativeLayout.ABOVE;
19 20
 import static android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM;
20 21
 
21
-public class BottomTabsController extends ViewController implements BottomNavigationView.OnNavigationItemSelectedListener {
22
+public class BottomTabsController extends ParentController implements BottomNavigationView.OnNavigationItemSelectedListener {
22 23
 	private BottomNavigationView bottomNavigationView;
23 24
 	private int selectedIndex = 0;
24
-	private List<ViewController> tabs;
25
+	private List<ViewController> tabs = new ArrayList<>();
25 26
 
26 27
 	public BottomTabsController(final Activity activity, final String id) {
27 28
 		super(activity, id);
@@ -73,11 +74,12 @@ public class BottomTabsController extends ViewController implements BottomNaviga
73 74
 		}
74 75
 	}
75 76
 
76
-	public List<ViewController> getTabs() {
77
-		return tabs;
78
-	}
79
-
80 77
 	public int getSelectedIndex() {
81 78
 		return selectedIndex;
82 79
 	}
80
+
81
+	@Override
82
+	public Iterable<ViewController> getChildControllers() {
83
+		return tabs;
84
+	}
83 85
 }

+ 6
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/IndexedStack.java Vedi File

@@ -3,6 +3,7 @@ package com.reactnativenavigation.viewcontrollers;
3 3
 import com.reactnativenavigation.utils.StringUtils;
4 4
 
5 5
 import java.util.ArrayDeque;
6
+import java.util.Collection;
6 7
 import java.util.HashMap;
7 8
 import java.util.Iterator;
8 9
 
@@ -71,4 +72,9 @@ public class IndexedStack<E> implements Iterable<String> {
71 72
 	public Iterator<String> iterator() {
72 73
 		return deque.iterator();
73 74
 	}
75
+
76
+
77
+	public Collection<E> values() {
78
+		return map.values();
79
+	}
74 80
 }

+ 25
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ParentController.java Vedi File

@@ -0,0 +1,25 @@
1
+package com.reactnativenavigation.viewcontrollers;
2
+
3
+import android.app.Activity;
4
+
5
+public abstract class ParentController extends ViewController {
6
+	public ParentController(final Activity activity, final String id) {
7
+		super(activity, id);
8
+	}
9
+
10
+	public abstract Iterable<ViewController> getChildControllers();
11
+
12
+	public ViewController findControllerById(final String id) {
13
+		ViewController fromSuper = super.findControllerById(id);
14
+		if (fromSuper != null) {
15
+			return fromSuper;
16
+		}
17
+
18
+		for (ViewController child : getChildControllers()) {
19
+			ViewController found = child.findControllerById(id);
20
+			if (found != null) return found;
21
+		}
22
+
23
+		return null;
24
+	}
25
+}

+ 6
- 5
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java Vedi File

@@ -6,7 +6,7 @@ import android.support.annotation.Nullable;
6 6
 import android.view.ViewGroup;
7 7
 import android.widget.FrameLayout;
8 8
 
9
-public class StackController extends ViewController {
9
+public class StackController extends ParentController {
10 10
 	private final IndexedStack<ViewController> stack = new IndexedStack<>();
11 11
 
12 12
 	public StackController(final Activity activity, String id) {
@@ -103,11 +103,12 @@ public class StackController extends ViewController {
103 103
 		}
104 104
 	}
105 105
 
106
-	public ViewController getChildById(final String id) {
107
-		return stack.get(id);
108
-	}
109
-
110 106
 	boolean containsId(String id) {
111 107
 		return stack.containsId(id);
112 108
 	}
109
+
110
+	@Override
111
+	public Iterable<ViewController> getChildControllers() {
112
+		return stack.values();
113
+	}
113 114
 }

+ 6
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java Vedi File

@@ -5,6 +5,8 @@ import android.support.annotation.NonNull;
5 5
 import android.support.annotation.Nullable;
6 6
 import android.view.View;
7 7
 
8
+import com.reactnativenavigation.utils.StringUtils;
9
+
8 10
 public abstract class ViewController {
9 11
 	private final Activity activity;
10 12
 	private final String id;
@@ -47,4 +49,8 @@ public abstract class ViewController {
47 49
 	public String getId() {
48 50
 		return id;
49 51
 	}
52
+
53
+	public ViewController findControllerById(final String id) {
54
+		return StringUtils.isEqual(this.id, id) ? this : null;
55
+	}
50 56
 }

+ 29
- 13
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/BottomTabsControllerTest.java Vedi File

@@ -10,9 +10,10 @@ import android.widget.RelativeLayout;
10 10
 import com.reactnativenavigation.BaseTest;
11 11
 import com.reactnativenavigation.mocks.SimpleViewController;
12 12
 
13
+import org.assertj.core.api.Condition;
13 14
 import org.junit.Test;
14 15
 
15
-import java.util.ArrayList;
16
+import java.util.Arrays;
16 17
 import java.util.List;
17 18
 
18 19
 import static org.assertj.core.api.Java6Assertions.assertThat;
@@ -26,6 +27,8 @@ public class BottomTabsControllerTest extends BaseTest {
26 27
 	private ViewController child1;
27 28
 	private ViewController child2;
28 29
 	private ViewController child3;
30
+	private ViewController child4;
31
+	private ViewController child5;
29 32
 
30 33
 	@Override
31 34
 	public void beforeEach() {
@@ -35,6 +38,8 @@ public class BottomTabsControllerTest extends BaseTest {
35 38
 		child1 = new SimpleViewController(activity, "child1");
36 39
 		child2 = new SimpleViewController(activity, "child2");
37 40
 		child3 = new SimpleViewController(activity, "child3");
41
+		child4 = new SimpleViewController(activity, "child4");
42
+		child5 = new SimpleViewController(activity, "child5");
38 43
 	}
39 44
 
40 45
 	@Test
@@ -72,21 +77,32 @@ public class BottomTabsControllerTest extends BaseTest {
72 77
 		assertThat(uut.onNavigationItemSelected(menuItem)).isTrue();
73 78
 
74 79
 		assertThat(uut.getSelectedIndex()).isEqualTo(3);
75
-		assertThat(uut.getTabs().get(0).getView().getVisibility()).isEqualTo(View.GONE);
76
-		assertThat(uut.getTabs().get(1).getView().getVisibility()).isEqualTo(View.GONE);
77
-		assertThat(uut.getTabs().get(2).getView().getVisibility()).isEqualTo(View.GONE);
78
-		assertThat(uut.getTabs().get(3).getView().getVisibility()).isEqualTo(View.VISIBLE);
79
-		assertThat(uut.getTabs().get(4).getView().getVisibility()).isEqualTo(View.GONE);
80
+		assertThat(uut.getChildControllers()).areExactly(1, new Condition<ViewController>() {
81
+			@Override
82
+			public boolean matches(final ViewController value) {
83
+				return value.getView().getVisibility() == View.VISIBLE;
84
+			}
85
+		});
86
+//		assertThat(uut.getView().getChildAt(1).getVisibility()).isEqualTo(View.GONE);
87
+//		assertThat(uut.getView().getChildAt(2).getVisibility()).isEqualTo(View.GONE);
88
+//		assertThat(uut.getView().getChildAt(3).getVisibility()).isEqualTo(View.GONE);
89
+//		assertThat(uut.getView().getChildAt(4).getVisibility()).isEqualTo(View.VISIBLE);
90
+//		assertThat(uut.getView().getChildAt(5).getVisibility()).isEqualTo(View.GONE);
91
+	}
92
+
93
+	@Test
94
+	public void findControllerById_ReturnsSelfOrChildren() throws Exception {
95
+		assertThat(uut.findControllerById("123")).isNull();
96
+		assertThat(uut.findControllerById(uut.getId())).isEqualTo(uut);
97
+		StackController inner = new StackController(activity, "inner");
98
+		inner.push(child1);
99
+		assertThat(uut.findControllerById(child1.getId())).isNull();
100
+		uut.setTabs(Arrays.<ViewController>asList(inner));
101
+		assertThat(uut.findControllerById(child1.getId())).isEqualTo(child1);
80 102
 	}
81 103
 
82 104
 	@NonNull
83 105
 	private List<ViewController> createTabs() {
84
-		List<ViewController> tabs = new ArrayList<>();
85
-		tabs.add(new SimpleViewController(activity, "1"));
86
-		tabs.add(new SimpleViewController(activity, "2"));
87
-		tabs.add(new SimpleViewController(activity, "3"));
88
-		tabs.add(new SimpleViewController(activity, "4"));
89
-		tabs.add(new SimpleViewController(activity, "5"));
90
-		return tabs;
106
+		return Arrays.asList(child1, child2, child3, child4, child5);
91 107
 	}
92 108
 }

+ 8
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/IndexedStackTest.java Vedi File

@@ -1,7 +1,6 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import com.reactnativenavigation.BaseTest;
4
-import com.reactnativenavigation.viewcontrollers.IndexedStack;
5 4
 
6 5
 import org.junit.Test;
7 6
 
@@ -104,4 +103,12 @@ public class IndexedStackTest extends BaseTest {
104 103
 		uut.push("456", 456);
105 104
 		assertThat(uut.isTop("123")).isFalse();
106 105
 	}
106
+
107
+	@Test
108
+	public void values() throws Exception {
109
+		assertThat(uut.values()).isNotNull().isEmpty();
110
+		uut.push("123", 123);
111
+		uut.push("456", 456);
112
+		assertThat(uut.values()).isNotNull().containsExactlyInAnyOrder(123, 456);
113
+	}
107 114
 }

+ 18
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java Vedi File

@@ -9,6 +9,8 @@ import com.reactnativenavigation.mocks.SimpleViewController;
9 9
 import org.junit.Test;
10 10
 import org.robolectric.Shadows;
11 11
 
12
+import java.util.Arrays;
13
+
12 14
 import static org.assertj.core.api.Java6Assertions.assertThat;
13 15
 
14 16
 public class NavigatorTest extends BaseTest {
@@ -87,6 +89,22 @@ public class NavigatorTest extends BaseTest {
87 89
 		assertHasSingleChildViewOf(uut, child1);
88 90
 	}
89 91
 
92
+	@Test
93
+	public void push_OnCorrectStackByFindingChildId() throws Exception {
94
+		BottomTabsController bottomTabsController = new BottomTabsController(activity, "tabsController");
95
+		StackController stack1 = new StackController(activity, "stack1");
96
+		StackController stack2 = new StackController(activity, "stack1");
97
+		stack1.push(child1);
98
+		stack2.push(child2);
99
+		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
100
+
101
+		uut.setRoot(bottomTabsController);
102
+		SimpleViewController newChild = new SimpleViewController(activity, "new child");
103
+		uut.push(child2.getId(), newChild);
104
+
105
+
106
+	}
107
+
90 108
 	private void assertHasSingleChildViewOf(ViewController parent, ViewController child) {
91 109
 		assertThat(((ViewGroup) parent.getView()).getChildCount()).isEqualTo(1);
92 110
 		assertThat(((ViewGroup) parent.getView()).getChildAt(0)).isEqualTo(child.getView()).isNotNull();

+ 74
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ParentControllerTest.java Vedi File

@@ -0,0 +1,74 @@
1
+package com.reactnativenavigation.viewcontrollers;
2
+
3
+import android.app.Activity;
4
+import android.support.annotation.NonNull;
5
+import android.view.View;
6
+import android.widget.FrameLayout;
7
+
8
+import com.reactnativenavigation.BaseTest;
9
+import com.reactnativenavigation.mocks.SimpleViewController;
10
+
11
+import org.junit.Test;
12
+
13
+import java.util.Arrays;
14
+import java.util.Collections;
15
+
16
+import static org.assertj.core.api.Java6Assertions.assertThat;
17
+
18
+public class ParentControllerTest extends BaseTest {
19
+
20
+	private Activity activity;
21
+
22
+	@Override
23
+	public void beforeEach() {
24
+		super.beforeEach();
25
+		activity = newActivity();
26
+	}
27
+
28
+
29
+	@Test
30
+	public void findControllerById_ReturnsSelfIfSameId() throws Exception {
31
+		ParentController uut = new ParentController(activity, "uut") {
32
+			@Override
33
+			public Iterable<ViewController> getChildControllers() {
34
+				return Collections.emptyList();
35
+			}
36
+
37
+			@NonNull
38
+			@Override
39
+			protected View createView() {
40
+				return new FrameLayout(activity);
41
+			}
42
+		};
43
+
44
+		assertThat(uut.findControllerById("123")).isNull();
45
+		assertThat(uut.findControllerById(uut.getId())).isEqualTo(uut);
46
+	}
47
+
48
+	@Test
49
+	public void findControllerById_DeeplyInOneOfTheChildren() throws Exception {
50
+		ViewController child1 = new SimpleViewController(activity, "child1");
51
+		ViewController child2 = new SimpleViewController(activity, "child2");
52
+
53
+		final StackController someInnerStack = new StackController(activity, "stack1");
54
+		someInnerStack.push(child1);
55
+		someInnerStack.push(child2);
56
+
57
+		ParentController uut = new ParentController(activity, "uut") {
58
+			@Override
59
+			public Iterable<ViewController> getChildControllers() {
60
+				return Arrays.<ViewController>asList(someInnerStack);
61
+			}
62
+
63
+			@NonNull
64
+			@Override
65
+			protected View createView() {
66
+				return new FrameLayout(activity);
67
+			}
68
+		};
69
+
70
+		assertThat(uut.findControllerById("stack1")).isEqualTo(someInnerStack);
71
+		assertThat(uut.findControllerById("child1")).isEqualTo(child1);
72
+		assertThat(uut.findControllerById("child2")).isEqualTo(child2);
73
+	}
74
+}

+ 12
- 3
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java Vedi File

@@ -226,10 +226,19 @@ public class StackControllerTest extends BaseTest {
226 226
 	}
227 227
 
228 228
 	@Test
229
-	public void holdsChildrenById() throws Exception {
230
-		assertThat(uut.getChildById(child1.getId())).isNull();
229
+	public void findControllerById_ReturnsSelfOrChildrenById() throws Exception {
230
+		assertThat(uut.findControllerById("123")).isNull();
231
+		assertThat(uut.findControllerById(uut.getId())).isEqualTo(uut);
231 232
 		uut.push(child1);
232
-		assertThat(uut.getChildById(child1.getId())).isEqualTo(child1);
233
+		assertThat(uut.findControllerById(child1.getId())).isEqualTo(child1);
234
+	}
235
+
236
+	@Test
237
+	public void findControllerById_Deeply() throws Exception {
238
+		StackController stack = new StackController(activity, "stack2");
239
+		stack.push(child2);
240
+		uut.push(stack);
241
+		assertThat(uut.findControllerById(child2.getId())).isEqualTo(child2);
233 242
 	}
234 243
 
235 244
 	private void assertHasSingleChildViewOfController(ViewController childController) {

+ 6
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ViewControllerTest.java Vedi File

@@ -61,4 +61,10 @@ public class ViewControllerTest extends BaseTest {
61 61
 	public void holdsId() throws Exception {
62 62
 		assertThat(uut.getId()).isEqualTo("uut");
63 63
 	}
64
+
65
+	@Test
66
+	public void findControllerById_ReturnsSelfIfSameId() throws Exception {
67
+		assertThat(uut.findControllerById("123")).isNull();
68
+		assertThat(uut.findControllerById(uut.getId())).isEqualTo(uut);
69
+	}
64 70
 }