|
@@ -0,0 +1,185 @@
|
|
1
|
+package com.reactnativenavigation.activities;
|
|
2
|
+
|
|
3
|
+import android.graphics.Color;
|
|
4
|
+import android.graphics.drawable.Drawable;
|
|
5
|
+import android.os.AsyncTask;
|
|
6
|
+import android.os.Bundle;
|
|
7
|
+import android.view.Menu;
|
|
8
|
+import android.widget.FrameLayout;
|
|
9
|
+
|
|
10
|
+import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
|
|
11
|
+import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
|
|
12
|
+import com.reactnativenavigation.R;
|
|
13
|
+import com.reactnativenavigation.core.RctManager;
|
|
14
|
+import com.reactnativenavigation.core.objects.Screen;
|
|
15
|
+import com.reactnativenavigation.views.RnnToolBar;
|
|
16
|
+import com.reactnativenavigation.views.ScreenStack;
|
|
17
|
+
|
|
18
|
+import java.util.ArrayList;
|
|
19
|
+import java.util.HashMap;
|
|
20
|
+import java.util.Map;
|
|
21
|
+
|
|
22
|
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
|
|
23
|
+
|
|
24
|
+/**
|
|
25
|
+ * Created by guyc on 02/04/16.
|
|
26
|
+ */
|
|
27
|
+public class BottomTabActivity extends BaseReactActivity implements AHBottomNavigation.OnTabSelectedListener {
|
|
28
|
+ public static final String EXTRA_SCREENS = "extraScreens";
|
|
29
|
+
|
|
30
|
+ private static final String TAB_STYLE_BUTTON_COLOR = "tabBarButtonColor";
|
|
31
|
+ private static final String TAB_STYLE_SELECTED_COLOR = "tabBarSelectedButtonColor";
|
|
32
|
+ private static final String TAB_STYLE_BAR_BG_COLOR = "tabBarBackgroundColor";
|
|
33
|
+ private static final String TAB_STYLE_INACTIVE_TITLES = "tabShowInactiveTitles";
|
|
34
|
+
|
|
35
|
+ private static int DEFAULT_TAB_BAR_BG_COLOR = 0xFFFFFFFF;
|
|
36
|
+ private static int DEFAULT_TAB_BUTTON_COLOR = Color.GRAY;
|
|
37
|
+ private static int DEFAULT_TAB_SELECTED_COLOR = 0xFF0000FF;
|
|
38
|
+ private static boolean DEFAULT_TAB_INACTIVE_TITLES = true;
|
|
39
|
+
|
|
40
|
+ private AHBottomNavigation mBottomNavigation;
|
|
41
|
+ private FrameLayout mContentFrame;
|
|
42
|
+ private ArrayList<ScreenStack> mScreenStacks;
|
|
43
|
+ private int mCurrentStackPosition = 0;
|
|
44
|
+
|
|
45
|
+ @Override
|
|
46
|
+ protected void handleOnCreate() {
|
|
47
|
+ mReactInstanceManager = RctManager.getInstance().getReactInstanceManager();
|
|
48
|
+
|
|
49
|
+ setContentView(R.layout.bottom_tab_activity);
|
|
50
|
+ mToolbar = (RnnToolBar) findViewById(R.id.toolbar);
|
|
51
|
+ mBottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_tab_bar);
|
|
52
|
+ mContentFrame = (FrameLayout) findViewById(R.id.contentFrame);
|
|
53
|
+
|
|
54
|
+ ArrayList<Screen> screens = (ArrayList<Screen>) getIntent().getSerializableExtra(EXTRA_SCREENS);
|
|
55
|
+ mBottomNavigation.setForceTint(true);
|
|
56
|
+ setupToolbar(screens);
|
|
57
|
+ setupTabs(getIntent().getExtras());
|
|
58
|
+ setupPages(screens);
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ private void setupPages(ArrayList<Screen> screens) {
|
|
62
|
+ new SetupTabsTask(this, screens).execute();
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ private void setupToolbar(ArrayList<Screen> screens) {
|
|
66
|
+ Screen initialScreen = screens.get(0);
|
|
67
|
+ setNavigationStyle(initialScreen);
|
|
68
|
+ mToolbar.setScreens(screens);
|
|
69
|
+ mToolbar.setTitle(initialScreen.title);
|
|
70
|
+ setSupportActionBar(mToolbar);
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ @Override
|
|
74
|
+ public void setNavigationStyle(Screen screen) {
|
|
75
|
+ super.setNavigationStyle(screen);
|
|
76
|
+ mToolbar.setTitle(screen.title);
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ private void setupTabs(Bundle style) {
|
|
80
|
+
|
|
81
|
+ mBottomNavigation.setForceTitlesDisplay(style.getBoolean(TAB_STYLE_INACTIVE_TITLES, DEFAULT_TAB_INACTIVE_TITLES));
|
|
82
|
+ mBottomNavigation.setForceTint(true);
|
|
83
|
+ mBottomNavigation.setDefaultBackgroundColor(getColor(style, TAB_STYLE_BAR_BG_COLOR, DEFAULT_TAB_BAR_BG_COLOR));
|
|
84
|
+ mBottomNavigation.setInactiveColor(getColor(style, TAB_STYLE_BUTTON_COLOR, DEFAULT_TAB_BUTTON_COLOR));
|
|
85
|
+ mBottomNavigation.setAccentColor(getColor(style, TAB_STYLE_SELECTED_COLOR, DEFAULT_TAB_SELECTED_COLOR));
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ private static int getColor(Bundle bundle, String key, int defaultColor) {
|
|
89
|
+ if (bundle.containsKey(key)) {
|
|
90
|
+ return Color.parseColor(bundle.getString(key));
|
|
91
|
+ }
|
|
92
|
+ else {
|
|
93
|
+ return defaultColor;
|
|
94
|
+ }
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+ @Override
|
|
99
|
+ public boolean onCreateOptionsMenu(Menu menu) {
|
|
100
|
+ boolean ret = super.onCreateOptionsMenu(menu);
|
|
101
|
+ mToolbar.handleOnCreateOptionsMenuAsync();
|
|
102
|
+ return ret;
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ @Override
|
|
106
|
+ public void push(Screen screen) {
|
|
107
|
+ super.push(screen);
|
|
108
|
+ mScreenStacks.get(mCurrentStackPosition).push(screen);
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ @Override
|
|
112
|
+ public Screen pop(String navigatorId) {
|
|
113
|
+ super.pop(navigatorId);
|
|
114
|
+ Screen screen = mScreenStacks.get(mCurrentStackPosition).pop();
|
|
115
|
+ setNavigationStyle(screen);
|
|
116
|
+ return screen;
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ @Override
|
|
120
|
+ protected Screen getCurrentScreen() {
|
|
121
|
+ return mScreenStacks.get(mCurrentStackPosition).peek();
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ @Override
|
|
125
|
+ protected String getCurrentNavigatorId() {
|
|
126
|
+ return mScreenStacks.get(mCurrentStackPosition).peek().navigatorId;
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ @Override
|
|
130
|
+ public int getScreenStackSize() {
|
|
131
|
+ return mScreenStacks.get(mCurrentStackPosition).getStackSize();
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ @Override
|
|
135
|
+ public void onTabSelected(int position, boolean wasSelected) {
|
|
136
|
+ if (wasSelected) {
|
|
137
|
+ return;
|
|
138
|
+ }
|
|
139
|
+ mContentFrame.removeAllViews();
|
|
140
|
+ mContentFrame.addView(mScreenStacks.get(position), new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
|
|
141
|
+ mCurrentStackPosition = position;
|
|
142
|
+ setNavigationStyle(mScreenStacks.get(mCurrentStackPosition).peek());
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+ private static class SetupTabsTask extends AsyncTask<Void, Void, Map<Screen, Drawable>> {
|
|
147
|
+
|
|
148
|
+ private BottomTabActivity activity;
|
|
149
|
+ private ArrayList<Screen> screens;
|
|
150
|
+
|
|
151
|
+ public SetupTabsTask(BottomTabActivity context, ArrayList<Screen> screens) {
|
|
152
|
+ this.activity = context;
|
|
153
|
+ this.screens = screens;
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ @Override
|
|
157
|
+ protected Map<Screen, Drawable> doInBackground(Void... params) {
|
|
158
|
+ Map<Screen, Drawable> icons = new HashMap<>();
|
|
159
|
+ for (Screen screen : this.screens) {
|
|
160
|
+ if (screen.icon != null) {
|
|
161
|
+ icons.put(screen, screen.getIcon(this.activity));
|
|
162
|
+ }
|
|
163
|
+ }
|
|
164
|
+ return icons;
|
|
165
|
+ }
|
|
166
|
+
|
|
167
|
+ @Override
|
|
168
|
+ protected void onPostExecute(Map<Screen, Drawable> icons) {
|
|
169
|
+ activity.setTabsWithIcons(this.screens, icons);
|
|
170
|
+ }
|
|
171
|
+ }
|
|
172
|
+
|
|
173
|
+ private void setTabsWithIcons(ArrayList<Screen> screens, Map<Screen, Drawable> icons) {
|
|
174
|
+ mScreenStacks = new ArrayList<>();
|
|
175
|
+ for(Screen screen: screens) {
|
|
176
|
+ ScreenStack stack = new ScreenStack(this);
|
|
177
|
+ stack.push(screen);
|
|
178
|
+ mScreenStacks.add(stack);
|
|
179
|
+ AHBottomNavigationItem item = new AHBottomNavigationItem(screen.label, icons.get(screen), Color.GRAY);
|
|
180
|
+ mBottomNavigation.addItem(item);
|
|
181
|
+ mBottomNavigation.setOnTabSelectedListener(this);
|
|
182
|
+ }
|
|
183
|
+ this.onTabSelected(0, false);
|
|
184
|
+ }
|
|
185
|
+}
|