|
@@ -1,293 +0,0 @@
|
1
|
|
-package com.reactnativenavigation.managers;
|
2
|
|
-
|
3
|
|
-import android.content.Context;
|
4
|
|
-import android.graphics.drawable.Drawable;
|
5
|
|
-import android.support.design.widget.TabLayout;
|
6
|
|
-import android.support.v4.view.ViewPager;
|
7
|
|
-import android.util.Log;
|
8
|
|
-import android.view.View;
|
9
|
|
-import android.view.ViewGroup;
|
10
|
|
-
|
11
|
|
-import com.facebook.infer.annotation.Assertions;
|
12
|
|
-import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
|
13
|
|
-import com.facebook.react.bridge.ReadableArray;
|
14
|
|
-import com.facebook.react.bridge.ReadableMap;
|
15
|
|
-import com.facebook.react.common.MapBuilder;
|
16
|
|
-import com.facebook.react.uimanager.ThemedReactContext;
|
17
|
|
-import com.facebook.react.uimanager.UIManagerModule;
|
18
|
|
-import com.facebook.react.uimanager.ViewGroupManager;
|
19
|
|
-import com.facebook.react.uimanager.annotations.ReactProp;
|
20
|
|
-import com.facebook.react.uimanager.events.EventDispatcher;
|
21
|
|
-import com.reactnativenavigation.layouts.ReactTabLayout;
|
22
|
|
-import com.reactnativenavigation.layouts.ReactTabLayout.InitialState;
|
23
|
|
-import com.reactnativenavigation.utils.ResourceUtils;
|
24
|
|
-
|
25
|
|
-import java.util.Map;
|
26
|
|
-
|
27
|
|
-import static android.support.design.widget.TabLayout.Tab;
|
28
|
|
-
|
29
|
|
-/**
|
30
|
|
- *
|
31
|
|
- * Created by guyc on 06/03/16.
|
32
|
|
- */
|
33
|
|
-@Deprecated
|
34
|
|
-public class TabLayoutManager extends ViewGroupManager<ReactTabLayout> {
|
35
|
|
- private static final String REACT_CLASS = "TabLayout";
|
36
|
|
- public static final int COMMAND_SET_VIEW_PAGER_TYPE = 1;
|
37
|
|
- public static final String PARAM_ICON = "icon";
|
38
|
|
- public static final String PARAM_TEXT = "text";
|
39
|
|
- public static final String REGISTRATION_NAME = "registrationName";
|
40
|
|
- public static final String EVENT_ON_TAB_SELECTED = "onTabSelected";
|
41
|
|
- public static final String COMMAND_SET_VIEW_PAGER = "setViewPager";
|
42
|
|
- private static final String TAG = TabLayoutManager.class.getSimpleName();
|
43
|
|
-
|
44
|
|
- private enum TabMode {
|
45
|
|
- SCROLLABLE(TabLayout.MODE_SCROLLABLE),
|
46
|
|
- FIXED(TabLayout.MODE_FIXED);
|
47
|
|
-
|
48
|
|
- int mode;
|
49
|
|
-
|
50
|
|
- TabMode(int mode) {
|
51
|
|
- this.mode = mode;
|
52
|
|
- }
|
53
|
|
-
|
54
|
|
- public static TabMode fromString(String text) {
|
55
|
|
- return text != null ? Enum.valueOf(TabMode.class, text.trim().toUpperCase()) : null;
|
56
|
|
- }
|
57
|
|
- }
|
58
|
|
-
|
59
|
|
- private enum TabGravity {
|
60
|
|
- FILL(TabLayout.GRAVITY_FILL),
|
61
|
|
- CENTER(TabLayout.GRAVITY_CENTER);
|
62
|
|
-
|
63
|
|
- int gravity;
|
64
|
|
-
|
65
|
|
- TabGravity(int gravity) {
|
66
|
|
- this.gravity = gravity;
|
67
|
|
- }
|
68
|
|
-
|
69
|
|
- public static TabGravity fromString(String text) {
|
70
|
|
- return text != null ? Enum.valueOf(TabGravity.class, text.trim().toUpperCase()) : null;
|
71
|
|
- }
|
72
|
|
- }
|
73
|
|
-
|
74
|
|
- private EventDispatcher mDispatcher;
|
75
|
|
-
|
76
|
|
- @Override
|
77
|
|
- public String getName() {
|
78
|
|
- return REACT_CLASS;
|
79
|
|
- }
|
80
|
|
-
|
81
|
|
- @Override
|
82
|
|
- protected ReactTabLayout createViewInstance(ThemedReactContext reactContext) {
|
83
|
|
- ReactTabLayout tabLayout = new ReactTabLayout(reactContext);
|
84
|
|
-// setupWithViewPager(tabLayout);
|
85
|
|
- return tabLayout;
|
86
|
|
- }
|
87
|
|
-
|
88
|
|
- private void setupWithViewPager(final ReactTabLayout tabLayout, final ReadableArray tabs) {
|
89
|
|
- tabLayout.post(new Runnable() {
|
90
|
|
- @Override
|
91
|
|
- public void run() {
|
92
|
|
- if (tabLayout.getParent() == null) {
|
93
|
|
- Log.e(TAG, "Tried to setupViewPager but parent is null");
|
94
|
|
- return;
|
95
|
|
- }
|
96
|
|
-
|
97
|
|
- ViewGroup parent = (ViewGroup) tabLayout.getParent().getParent();
|
98
|
|
- if (parent == null) {
|
99
|
|
- return;
|
100
|
|
- }
|
101
|
|
-
|
102
|
|
- for (int i = 0; i < parent.getChildCount(); i++) {
|
103
|
|
- View child = parent.getChildAt(i);
|
104
|
|
- if (child instanceof ViewPager) {
|
105
|
|
- // Setup TabLayout with ViewPager
|
106
|
|
- ViewPager viewPager = (ViewPager) child;
|
107
|
|
- tabLayout.setupWithViewPager(viewPager);
|
108
|
|
-
|
109
|
|
- // Add tabs
|
110
|
|
- if (tabs != null) {
|
111
|
|
- tabLayout.removeAllTabs();
|
112
|
|
- populateTabLayoutWithTabs(tabLayout, tabs);
|
113
|
|
- tabLayout.setOnTabSelectedListener(new OnTabSelectedListener(viewPager, tabLayout, TabLayoutManager.this));
|
114
|
|
- }
|
115
|
|
- }
|
116
|
|
- }
|
117
|
|
-
|
118
|
|
- Log.i("GUY", "childCount" + parent.getChildCount());
|
119
|
|
- for (int i = 0; i < parent.getChildCount(); i++) {
|
120
|
|
- Log.d("GUY", "[" + 1 + "] " + parent.getChildAt(i).getClass().getSimpleName());
|
121
|
|
- }
|
122
|
|
- }
|
123
|
|
- });
|
124
|
|
-
|
125
|
|
- }
|
126
|
|
-
|
127
|
|
- @Override
|
128
|
|
- public void addView(ReactTabLayout tabLayout, View child, int index) {
|
129
|
|
- Tab tab = tabLayout.newTab();
|
130
|
|
- tabLayout.addTab(tab);
|
131
|
|
-
|
132
|
|
- // when initial position was stored, update tab selection
|
133
|
|
- if (tabLayout.getInitialState() == InitialState.TAB_POSITION_SET &&
|
134
|
|
- tabLayout.getInitialTabPosition() == index) {
|
135
|
|
- tabLayout.setInitialState(InitialState.TAB_SELECTED);
|
136
|
|
- tab.select();
|
137
|
|
- }
|
138
|
|
- }
|
139
|
|
-
|
140
|
|
- @ReactProp(name = "tabs")
|
141
|
|
- public void setTabs(ReactTabLayout tabLayout, ReadableArray tabs) {
|
142
|
|
- setupWithViewPager(tabLayout, tabs);
|
143
|
|
- }
|
144
|
|
-
|
145
|
|
-
|
146
|
|
- @ReactProp(name = "selectedTab", defaultInt = 0)
|
147
|
|
- public void setSelectedTab(ReactTabLayout view, int selectedTab) {
|
148
|
|
- selectTab(view, selectedTab);
|
149
|
|
- }
|
150
|
|
-
|
151
|
|
- private void selectTab(ReactTabLayout tabLayout, int position) {
|
152
|
|
- if (position < 0 || position >= tabLayout.getTabCount()) {
|
153
|
|
- Log.w(REACT_CLASS, "Tried to select tab " + position + " but index is out of bounds");
|
154
|
|
- return;
|
155
|
|
- }
|
156
|
|
-
|
157
|
|
- Tab tab = tabLayout.getTabAt(position);
|
158
|
|
- if (tab != null) {
|
159
|
|
- tab.select();
|
160
|
|
- }
|
161
|
|
- }
|
162
|
|
-
|
163
|
|
- @ReactProp(name = "selectedTabIndicatorColor")
|
164
|
|
- public void setSelectedTabIndicatorColor(ReactTabLayout view, int indicatorColor) {
|
165
|
|
- view.setSelectedTabIndicatorColor(indicatorColor);
|
166
|
|
- }
|
167
|
|
-
|
168
|
|
- @ReactProp(name = "tabMode")
|
169
|
|
- public void setTabMode(ReactTabLayout view, String mode) {
|
170
|
|
- TabMode tabMode = TabMode.fromString(mode);
|
171
|
|
- if (tabMode == null) {
|
172
|
|
- Log.w(REACT_CLASS, "Invalid tabMode [" + tabMode + "]");
|
173
|
|
- } else {
|
174
|
|
- view.setTabMode(tabMode.mode);
|
175
|
|
- }
|
176
|
|
- }
|
177
|
|
-
|
178
|
|
- @ReactProp(name = "tabGravity")
|
179
|
|
- public void setTabGravity(ReactTabLayout view, String gravity) {
|
180
|
|
- TabGravity tabGravity = TabGravity.fromString(gravity);
|
181
|
|
- if (tabGravity == null) {
|
182
|
|
- Log.w(REACT_CLASS, "Invalid tabGravity [" + gravity + "]");
|
183
|
|
- } else {
|
184
|
|
- view.setTabGravity(tabGravity.gravity);
|
185
|
|
- }
|
186
|
|
- }
|
187
|
|
-
|
188
|
|
- @Override
|
189
|
|
- public boolean needsCustomLayoutForChildren() {
|
190
|
|
- return true;
|
191
|
|
- }
|
192
|
|
-
|
193
|
|
- @Override
|
194
|
|
- protected void addEventEmitters(ThemedReactContext reactContext, ReactTabLayout view) {
|
195
|
|
- mDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
|
196
|
|
- }
|
197
|
|
-
|
198
|
|
-// @Override
|
199
|
|
-// public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
|
200
|
|
-// return MapBuilder.of(
|
201
|
|
-// TabSelectedEvent.EVENT_NAME, (Object) MapBuilder.of(REGISTRATION_NAME, EVENT_ON_TAB_SELECTED)
|
202
|
|
-// );
|
203
|
|
-// }
|
204
|
|
-
|
205
|
|
- /*
|
206
|
|
- * This method allows us to expose an interface which we can use from react.
|
207
|
|
- * If we obtain a reference to this native view, we can call `setViewPager` method and handle
|
208
|
|
- * the call in `receiveCommand`
|
209
|
|
- * */
|
210
|
|
- @Override
|
211
|
|
- public Map<String, Integer> getCommandsMap() {
|
212
|
|
- return MapBuilder.of(COMMAND_SET_VIEW_PAGER, COMMAND_SET_VIEW_PAGER_TYPE);
|
213
|
|
- }
|
214
|
|
-
|
215
|
|
- @Override
|
216
|
|
- public void receiveCommand(ReactTabLayout tabLayout, int commandType, ReadableArray args) {
|
217
|
|
- Assertions.assertNotNull(tabLayout);
|
218
|
|
- Assertions.assertNotNull(args);
|
219
|
|
-
|
220
|
|
- switch (commandType) {
|
221
|
|
- case COMMAND_SET_VIEW_PAGER_TYPE: {
|
222
|
|
- // Setup TabLayout with ViewPager
|
223
|
|
- int viewPagerId = args.getInt(0);
|
224
|
|
- ViewPager viewPager = (ViewPager) tabLayout.getRootView().findViewById(viewPagerId);
|
225
|
|
- tabLayout.setupWithViewPager(viewPager);
|
226
|
|
-
|
227
|
|
- // Add tabs
|
228
|
|
- ReadableArray tabs = args.getArray(1);
|
229
|
|
- if (tabs != null) {
|
230
|
|
- tabLayout.removeAllTabs();
|
231
|
|
- this.populateTabLayoutWithTabs(tabLayout, tabs);
|
232
|
|
-// tabLayout.setOnTabSelectedListener(new OnTabSelectedListener(viewPager, tabLayout, this));
|
233
|
|
- }
|
234
|
|
-
|
235
|
|
- return;
|
236
|
|
- }
|
237
|
|
-
|
238
|
|
- default:
|
239
|
|
- throw new JSApplicationIllegalArgumentException(String.format(
|
240
|
|
- "Unsupported command %d received by %s.", commandType, REACT_CLASS));
|
241
|
|
- }
|
242
|
|
- }
|
243
|
|
-
|
244
|
|
- private void populateTabLayoutWithTabs(ReactTabLayout tabLayout, ReadableArray tabs) {
|
245
|
|
- for (int i = 0; i < tabs.size(); i++) {
|
246
|
|
- ReadableMap tabMap = tabs.getMap(i);
|
247
|
|
- TabLayout.Tab tab = tabLayout.newTab();
|
248
|
|
-
|
249
|
|
- // Set tab text
|
250
|
|
- if (tabMap.hasKey(PARAM_TEXT)) {
|
251
|
|
- tab.setText(tabMap.getString(PARAM_TEXT));
|
252
|
|
- }
|
253
|
|
-
|
254
|
|
- // Set tab icon
|
255
|
|
- if (tabMap.hasKey(PARAM_ICON)) {
|
256
|
|
- Context ctx = tabLayout.getContext();
|
257
|
|
- Drawable icon = ResourceUtils.getDrawable(ctx, tabMap.getString(PARAM_ICON));
|
258
|
|
- if (icon != null) {
|
259
|
|
- tab.setIcon(icon);
|
260
|
|
- }
|
261
|
|
- }
|
262
|
|
-
|
263
|
|
- tabLayout.addTab(tab);
|
264
|
|
- }
|
265
|
|
- }
|
266
|
|
-
|
267
|
|
- private static class OnTabSelectedListener extends TabLayout.ViewPagerOnTabSelectedListener {
|
268
|
|
- private final ReactTabLayout mTabLayout;
|
269
|
|
- private TabLayoutManager mTabLayoutManager;
|
270
|
|
-
|
271
|
|
- public OnTabSelectedListener(ViewPager viewPager, ReactTabLayout tabLayout, TabLayoutManager manager) {
|
272
|
|
- super(viewPager);
|
273
|
|
- mTabLayout = tabLayout;
|
274
|
|
- mTabLayoutManager = manager;
|
275
|
|
- }
|
276
|
|
-
|
277
|
|
- @Override
|
278
|
|
- public void onTabSelected(Tab tab) {
|
279
|
|
- super.onTabSelected(tab);
|
280
|
|
- int position = mTabLayout.indexOf(tab);
|
281
|
|
-// mTabLayoutManager.mDispatcher.dispatchEvent(new TabSelectedEvent(mTabLayout.getId(position), position));
|
282
|
|
-// mTabLayoutManager.mDispatcher.dispatchEvent(new TabSelectedEvent(mTabLayout.getId(), position));
|
283
|
|
- }
|
284
|
|
-
|
285
|
|
- @Override
|
286
|
|
- public void onTabUnselected(Tab tab) {
|
287
|
|
- }
|
288
|
|
-
|
289
|
|
- @Override
|
290
|
|
- public void onTabReselected(Tab tab) {
|
291
|
|
- }
|
292
|
|
- }
|
293
|
|
-}
|