Преглед на файлове

detach tab after attaching new tab (#2658)

* detach tab after attaching new tab

* Fix tests
yogevbd преди 7 години
родител
ревизия
6471fdae30

+ 17
- 15
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/BottomTabsController.java Целия файл

4
 import android.graphics.Color;
4
 import android.graphics.Color;
5
 import android.graphics.drawable.Drawable;
5
 import android.graphics.drawable.Drawable;
6
 import android.support.annotation.NonNull;
6
 import android.support.annotation.NonNull;
7
-import android.view.View;
8
 import android.view.ViewGroup;
7
 import android.view.ViewGroup;
9
 import android.widget.RelativeLayout;
8
 import android.widget.RelativeLayout;
10
 
9
 
32
 public class BottomTabsController extends ParentController implements AHBottomNavigation.OnTabSelectedListener, NavigationOptionsListener {
31
 public class BottomTabsController extends ParentController implements AHBottomNavigation.OnTabSelectedListener, NavigationOptionsListener {
33
 	private BottomTabs bottomTabs;
32
 	private BottomTabs bottomTabs;
34
 	private List<ViewController> tabs = new ArrayList<>();
33
 	private List<ViewController> tabs = new ArrayList<>();
35
-	private int selectedIndex = 0;
36
     private ImageLoader imageLoader;
34
     private ImageLoader imageLoader;
37
 
35
 
38
     public BottomTabsController(final Activity activity, ImageLoader imageLoader, final String id, Options initialOptions) {
36
     public BottomTabsController(final Activity activity, ImageLoader imageLoader, final String id, Options initialOptions) {
54
 
52
 
55
 	@Override
53
 	@Override
56
 	public boolean handleBack() {
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
     @Override
58
     @Override
61
     public boolean onTabSelected(int index, boolean wasSelected) {
59
     public boolean onTabSelected(int index, boolean wasSelected) {
60
+        if (wasSelected) return false;
62
         selectTabAtIndex(index);
61
         selectTabAtIndex(index);
63
         return true;
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
 	public void setTabs(final List<ViewController> tabs) {
65
 	public void setTabs(final List<ViewController> tabs) {
73
 		if (tabs.size() > 5) {
66
 		if (tabs.size() > 5) {
74
 			throw new RuntimeException("Too many tabs!");
67
 			throw new RuntimeException("Too many tabs!");
76
 		this.tabs = tabs;
69
 		this.tabs = tabs;
77
 		getView();
70
 		getView();
78
 		for (int i = 0; i < tabs.size(); i++) {
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
 		selectTabAtIndex(0);
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
 	    if (!tabOptions.icon.hasValue()) {
78
 	    if (!tabOptions.icon.hasValue()) {
86
             throw new RuntimeException("BottomTab must have an icon");
79
             throw new RuntimeException("BottomTab must have an icon");
87
         }
80
         }
102
 
95
 
103
         RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
96
         RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
104
         params.addRule(ABOVE, bottomTabs.getId());
97
         params.addRule(ABOVE, bottomTabs.getId());
105
-        tab.getView().setVisibility(View.GONE);
106
-        getView().addView(tab.getView(), params);
107
 	}
98
 	}
108
 
99
 
109
     private void setIconColor(Drawable drawable, BottomTabsOptions options) {
100
     private void setIconColor(Drawable drawable, BottomTabsOptions options) {
111
     }
102
     }
112
 
103
 
113
     int getSelectedIndex() {
104
     int getSelectedIndex() {
114
-		return selectedIndex;
105
+		return bottomTabs.getCurrentItem();
115
 	}
106
 	}
116
 
107
 
117
 	@NonNull
108
 	@NonNull
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
 		for (ViewController child : controller.getChildControllers()) {
146
 		for (ViewController child : controller.getChildControllers()) {
145
 			if (id.equals(child.getId())) {
147
 			if (id.equals(child.getId())) {
146
 				return true;
148
 				return true;

+ 4
- 6
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/BottomTabsControllerTest.java Целия файл

2
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
 import android.support.annotation.NonNull;
4
 import android.support.annotation.NonNull;
5
-import android.view.View;
6
 import android.widget.RelativeLayout;
5
 import android.widget.RelativeLayout;
7
 
6
 
8
 import com.reactnativenavigation.BaseTest;
7
 import com.reactnativenavigation.BaseTest;
14
 import com.reactnativenavigation.utils.OptionHelper;
13
 import com.reactnativenavigation.utils.OptionHelper;
15
 import com.reactnativenavigation.views.BottomTabs;
14
 import com.reactnativenavigation.views.BottomTabs;
16
 
15
 
17
-import org.assertj.core.api.iterable.Extractor;
18
 import org.junit.Test;
16
 import org.junit.Test;
19
 
17
 
20
 import java.util.Arrays;
18
 import java.util.Arrays;
65
     }
63
     }
66
 
64
 
67
     @Test
65
     @Test
68
-    public void setTabs_AddAllViewsAsGoneExceptFirst() throws Exception {
66
+    public void setTabs_AddAllViews() throws Exception {
69
         List<ViewController> tabs = createTabs();
67
         List<ViewController> tabs = createTabs();
70
         uut.setTabs(tabs);
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
     @Test
73
     @Test
80
         uut.selectTabAtIndex(3);
78
         uut.selectTabAtIndex(3);
81
 
79
 
82
         assertThat(uut.getSelectedIndex()).isEqualTo(3);
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
     @Test
84
     @Test