Browse Source

detach tab after attaching new tab (#2658)

* detach tab after attaching new tab

* Fix tests
yogevbd 7 years ago
parent
commit
6471fdae30

+ 17
- 15
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/BottomTabsController.java View File

@@ -4,7 +4,6 @@ import android.app.Activity;
4 4
 import android.graphics.Color;
5 5
 import android.graphics.drawable.Drawable;
6 6
 import android.support.annotation.NonNull;
7
-import android.view.View;
8 7
 import android.view.ViewGroup;
9 8
 import android.widget.RelativeLayout;
10 9
 
@@ -32,7 +31,6 @@ import static com.reactnativenavigation.parse.DEFAULT_VALUES.NO_INT_VALUE;
32 31
 public class BottomTabsController extends ParentController implements AHBottomNavigation.OnTabSelectedListener, NavigationOptionsListener {
33 32
 	private BottomTabs bottomTabs;
34 33
 	private List<ViewController> tabs = new ArrayList<>();
35
-	private int selectedIndex = 0;
36 34
     private ImageLoader imageLoader;
37 35
 
38 36
     public BottomTabsController(final Activity activity, ImageLoader imageLoader, final String id, Options initialOptions) {
@@ -54,21 +52,16 @@ public class BottomTabsController extends ParentController implements AHBottomNa
54 52
 
55 53
 	@Override
56 54
 	public boolean handleBack() {
57
-		return !tabs.isEmpty() && tabs.get(selectedIndex).handleBack();
55
+		return !tabs.isEmpty() && tabs.get(bottomTabs.getCurrentItem()).handleBack();
58 56
 	}
59 57
 
60 58
     @Override
61 59
     public boolean onTabSelected(int index, boolean wasSelected) {
60
+        if (wasSelected) return false;
62 61
         selectTabAtIndex(index);
63 62
         return true;
64 63
     }
65 64
 
66
-	void selectTabAtIndex(final int newIndex) {
67
-		tabs.get(selectedIndex).getView().setVisibility(View.GONE);
68
-		selectedIndex = newIndex;
69
-		tabs.get(selectedIndex).getView().setVisibility(View.VISIBLE);
70
-	}
71
-
72 65
 	public void setTabs(final List<ViewController> tabs) {
73 66
 		if (tabs.size() > 5) {
74 67
 			throw new RuntimeException("Too many tabs!");
@@ -76,12 +69,12 @@ public class BottomTabsController extends ParentController implements AHBottomNa
76 69
 		this.tabs = tabs;
77 70
 		getView();
78 71
 		for (int i = 0; i < tabs.size(); i++) {
79
-			createTab(i, tabs.get(i), tabs.get(i).options.bottomTabOptions, tabs.get(i).options.bottomTabsOptions);
72
+			createTab(i, tabs.get(i).options.bottomTabOptions, tabs.get(i).options.bottomTabsOptions);
80 73
 		}
81 74
 		selectTabAtIndex(0);
82 75
 	}
83 76
 
84
-	private void createTab(int index, ViewController tab, final BottomTabOptions tabOptions, final BottomTabsOptions bottomTabsOptions) {
77
+	private void createTab(int index, final BottomTabOptions tabOptions, final BottomTabsOptions bottomTabsOptions) {
85 78
 	    if (!tabOptions.icon.hasValue()) {
86 79
             throw new RuntimeException("BottomTab must have an icon");
87 80
         }
@@ -102,8 +95,6 @@ public class BottomTabsController extends ParentController implements AHBottomNa
102 95
 
103 96
         RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
104 97
         params.addRule(ABOVE, bottomTabs.getId());
105
-        tab.getView().setVisibility(View.GONE);
106
-        getView().addView(tab.getView(), params);
107 98
 	}
108 99
 
109 100
     private void setIconColor(Drawable drawable, BottomTabsOptions options) {
@@ -111,7 +102,7 @@ public class BottomTabsController extends ParentController implements AHBottomNa
111 102
     }
112 103
 
113 104
     int getSelectedIndex() {
114
-		return selectedIndex;
105
+		return bottomTabs.getCurrentItem();
115 106
 	}
116 107
 
117 108
 	@NonNull
@@ -140,7 +131,18 @@ public class BottomTabsController extends ParentController implements AHBottomNa
140 131
         }
141 132
     }
142 133
 
143
-	private boolean hasControlWithId(StackController controller, String id) {
134
+    void selectTabAtIndex(final int newIndex) {
135
+        getView().removeView(getCurrentView());
136
+        bottomTabs.setCurrentItem(newIndex, false);
137
+        getView().addView(getCurrentView());
138
+    }
139
+
140
+    @NonNull
141
+    private ViewGroup getCurrentView() {
142
+        return tabs.get(bottomTabs.getCurrentItem()).getView();
143
+    }
144
+
145
+    private boolean hasControlWithId(StackController controller, String id) {
144 146
 		for (ViewController child : controller.getChildControllers()) {
145 147
 			if (id.equals(child.getId())) {
146 148
 				return true;

+ 4
- 6
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/BottomTabsControllerTest.java View File

@@ -2,7 +2,6 @@ package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4 4
 import android.support.annotation.NonNull;
5
-import android.view.View;
6 5
 import android.widget.RelativeLayout;
7 6
 
8 7
 import com.reactnativenavigation.BaseTest;
@@ -14,7 +13,6 @@ import com.reactnativenavigation.utils.ImageLoader;
14 13
 import com.reactnativenavigation.utils.OptionHelper;
15 14
 import com.reactnativenavigation.views.BottomTabs;
16 15
 
17
-import org.assertj.core.api.iterable.Extractor;
18 16
 import org.junit.Test;
19 17
 
20 18
 import java.util.Arrays;
@@ -65,11 +63,11 @@ public class BottomTabsControllerTest extends BaseTest {
65 63
     }
66 64
 
67 65
     @Test
68
-    public void setTabs_AddAllViewsAsGoneExceptFirst() throws Exception {
66
+    public void setTabs_AddAllViews() throws Exception {
69 67
         List<ViewController> tabs = createTabs();
70 68
         uut.setTabs(tabs);
71
-        assertThat(uut.getView().getChildCount()).isEqualTo(6);
72
-        assertThat(uut.getChildControllers()).extracting((Extractor<ViewController, Integer>) input -> input.getView().getVisibility()).containsExactly(View.VISIBLE, View.GONE, View.GONE, View.GONE, View.GONE);
69
+        assertThat(uut.getView().getChildCount()).isEqualTo(2);
70
+        assertThat(((ViewController) ((List) uut.getChildControllers()).get(0)).getView().getParent()).isNotNull();
73 71
     }
74 72
 
75 73
     @Test
@@ -80,7 +78,7 @@ public class BottomTabsControllerTest extends BaseTest {
80 78
         uut.selectTabAtIndex(3);
81 79
 
82 80
         assertThat(uut.getSelectedIndex()).isEqualTo(3);
83
-        assertThat(uut.getChildControllers()).extracting((Extractor<ViewController, Integer>) input -> input.getView().getVisibility()).containsExactly(View.GONE, View.GONE, View.GONE, View.VISIBLE, View.GONE);
81
+        assertThat(((ViewController) ((List) uut.getChildControllers()).get(0)).getView().getParent()).isNull();
84 82
     }
85 83
 
86 84
     @Test