|
|
@@ -12,10 +12,14 @@ import android.support.annotation.NonNull;
|
|
12
|
12
|
import android.support.annotation.UiThread;
|
|
13
|
13
|
import android.support.v4.content.ContextCompat;
|
|
14
|
14
|
import android.support.v4.content.res.ResourcesCompat;
|
|
|
15
|
+import android.support.v4.widget.DrawerLayout;
|
|
15
|
16
|
import android.support.v7.app.ActionBar;
|
|
|
17
|
+import android.support.v7.app.ActionBarDrawerToggle;
|
|
16
|
18
|
import android.support.v7.app.AppCompatActivity;
|
|
|
19
|
+import android.support.v7.graphics.drawable.DrawerArrowDrawable;
|
|
17
|
20
|
import android.support.v7.widget.Toolbar;
|
|
18
|
21
|
import android.util.AttributeSet;
|
|
|
22
|
+import android.view.Gravity;
|
|
19
|
23
|
import android.view.Menu;
|
|
20
|
24
|
import android.view.MenuItem;
|
|
21
|
25
|
import android.view.View;
|
|
|
@@ -27,6 +31,7 @@ import com.reactnativenavigation.activities.BaseReactActivity;
|
|
27
|
31
|
import com.reactnativenavigation.core.objects.Button;
|
|
28
|
32
|
import com.reactnativenavigation.core.objects.Screen;
|
|
29
|
33
|
import com.reactnativenavigation.utils.ContextProvider;
|
|
|
34
|
+import com.reactnativenavigation.utils.IconUtils;
|
|
30
|
35
|
import com.reactnativenavigation.utils.ImageUtils;
|
|
31
|
36
|
|
|
32
|
37
|
import java.lang.ref.WeakReference;
|
|
|
@@ -43,8 +48,13 @@ public class RnnToolBar extends Toolbar {
|
|
43
|
48
|
private static final int ANIMATE_DURATION = 180;
|
|
44
|
49
|
|
|
45
|
50
|
private List<Screen> mScreens;
|
|
|
51
|
+ private AsyncTask mDrawerIconTask;
|
|
46
|
52
|
private AsyncTask mSetupToolbarTask;
|
|
47
|
53
|
private Drawable mBackground;
|
|
|
54
|
+ private Drawable mDrawerIcon;
|
|
|
55
|
+ private Screen mDrawerScreen;
|
|
|
56
|
+ private DrawerLayout mDrawerLayout;
|
|
|
57
|
+ private ActionBarDrawerToggle mDrawerToggle;
|
|
48
|
58
|
private ArrayList<View> mMenuItems;
|
|
49
|
59
|
|
|
50
|
60
|
public RnnToolBar(Context context) {
|
|
|
@@ -105,6 +115,65 @@ public class RnnToolBar extends Toolbar {
|
|
105
|
115
|
}
|
|
106
|
116
|
}
|
|
107
|
117
|
|
|
|
118
|
+ public ActionBarDrawerToggle setupDrawer(DrawerLayout drawerLayout, Screen drawerScreen, Screen screen) {
|
|
|
119
|
+ if (drawerLayout == null || drawerScreen == null) {
|
|
|
120
|
+ return null;
|
|
|
121
|
+ }
|
|
|
122
|
+
|
|
|
123
|
+ mDrawerLayout = drawerLayout;
|
|
|
124
|
+ mDrawerScreen = drawerScreen;
|
|
|
125
|
+ mDrawerToggle = new ActionBarDrawerToggle(
|
|
|
126
|
+ ContextProvider.getActivityContext(),
|
|
|
127
|
+ mDrawerLayout,
|
|
|
128
|
+ this,
|
|
|
129
|
+ R.string.drawer_open,
|
|
|
130
|
+ R.string.drawer_close
|
|
|
131
|
+ );
|
|
|
132
|
+ mDrawerLayout.setDrawerListener(mDrawerToggle);
|
|
|
133
|
+ setupDrawerIconAsync(drawerScreen.icon, screen);
|
|
|
134
|
+
|
|
|
135
|
+ return mDrawerToggle;
|
|
|
136
|
+ }
|
|
|
137
|
+
|
|
|
138
|
+ public void setDrawerIcon(Drawable icon) {
|
|
|
139
|
+ mDrawerIcon = icon;
|
|
|
140
|
+ }
|
|
|
141
|
+
|
|
|
142
|
+ public void showDrawer(boolean animated) {
|
|
|
143
|
+ if (mDrawerLayout == null) {
|
|
|
144
|
+ return;
|
|
|
145
|
+ }
|
|
|
146
|
+
|
|
|
147
|
+ mDrawerLayout.openDrawer(Gravity.LEFT);
|
|
|
148
|
+ }
|
|
|
149
|
+
|
|
|
150
|
+ public void hideDrawer(boolean animated) {
|
|
|
151
|
+ if (mDrawerLayout == null) {
|
|
|
152
|
+ return;
|
|
|
153
|
+ }
|
|
|
154
|
+
|
|
|
155
|
+ mDrawerLayout.closeDrawer(Gravity.LEFT);
|
|
|
156
|
+ }
|
|
|
157
|
+
|
|
|
158
|
+ public void toggleDrawer(boolean animated) {
|
|
|
159
|
+ if (mDrawerLayout == null) {
|
|
|
160
|
+ return;
|
|
|
161
|
+ }
|
|
|
162
|
+
|
|
|
163
|
+ boolean visible = mDrawerLayout.isDrawerOpen(Gravity.LEFT);
|
|
|
164
|
+ if (visible) {
|
|
|
165
|
+ hideDrawer(animated);
|
|
|
166
|
+ } else {
|
|
|
167
|
+ showDrawer(animated);
|
|
|
168
|
+ }
|
|
|
169
|
+ }
|
|
|
170
|
+
|
|
|
171
|
+ public void setupDrawerIconAsync(String drawerIconSource, Screen screen) {
|
|
|
172
|
+ if (mDrawerIconTask == null) {
|
|
|
173
|
+ mDrawerIconTask = new SetupDrawerIconTask(this, drawerIconSource, screen).execute();
|
|
|
174
|
+ }
|
|
|
175
|
+ }
|
|
|
176
|
+
|
|
108
|
177
|
public void setupToolbarButtonsAsync(Screen newScreen) {
|
|
109
|
178
|
if (newScreen != null) {
|
|
110
|
179
|
this.setupToolbarButtonsAsync(null, newScreen);
|
|
|
@@ -144,29 +213,44 @@ public class RnnToolBar extends Toolbar {
|
|
144
|
213
|
hideToolbar(false);
|
|
145
|
214
|
}
|
|
146
|
215
|
|
|
147
|
|
- @SuppressWarnings({"ConstantConditions"})
|
|
148
|
|
- public void showBackButton(Screen screen) {
|
|
149
|
|
- ActionBar actionBar = ContextProvider.getActivityContext().getSupportActionBar();
|
|
150
|
|
- Drawable backButton = setupBackButton(screen);
|
|
151
|
|
- actionBar.setHomeAsUpIndicator(backButton);
|
|
152
|
|
- actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
216
|
+ public void setNavUpButton() {
|
|
|
217
|
+ setNavUpButton(null);
|
|
153
|
218
|
}
|
|
154
|
219
|
|
|
155
|
|
- @SuppressLint("PrivateResource")
|
|
156
|
220
|
@SuppressWarnings({"ConstantConditions"})
|
|
157
|
|
- private Drawable setupBackButton(Screen screen) {
|
|
158
|
|
- Resources resources = getResources();
|
|
159
|
|
- final Drawable backButton = ResourcesCompat.getDrawable(resources,
|
|
160
|
|
- R.drawable.abc_ic_ab_back_mtrl_am_alpha,
|
|
161
|
|
- ContextProvider.getActivityContext().getTheme());
|
|
162
|
|
- int tintColor = screen.navBarButtonColor != null ? screen.navBarButtonColor : Color.BLACK;
|
|
163
|
|
- ImageUtils.tint(backButton, tintColor);
|
|
164
|
|
- return backButton;
|
|
165
|
|
- }
|
|
|
221
|
+ public void setNavUpButton(Screen screen) {
|
|
|
222
|
+ ActionBar actionBar = ContextProvider.getActivityContext().getSupportActionBar();
|
|
|
223
|
+ if (actionBar == null) {
|
|
|
224
|
+ return;
|
|
|
225
|
+ }
|
|
166
|
226
|
|
|
167
|
|
- @SuppressWarnings({"ConstantConditions"})
|
|
168
|
|
- public void hideBackButton() {
|
|
169
|
|
- ContextProvider.getActivityContext().getSupportActionBar().setDisplayHomeAsUpEnabled(false);
|
|
|
227
|
+ boolean isBack = screen != null;
|
|
|
228
|
+ boolean hasDrawer = mDrawerToggle != null;
|
|
|
229
|
+
|
|
|
230
|
+ Drawable navIcon = null;
|
|
|
231
|
+ DrawerArrowDrawable navArrow = null;
|
|
|
232
|
+ if (hasDrawer && mDrawerIcon == null) {
|
|
|
233
|
+ navArrow = (DrawerArrowDrawable) this.getNavigationIcon();
|
|
|
234
|
+ } else {
|
|
|
235
|
+ if (isBack) {
|
|
|
236
|
+ navArrow = new DrawerArrowDrawable(ContextProvider.getActivityContext());
|
|
|
237
|
+ } else if (hasDrawer) {
|
|
|
238
|
+ navIcon = mDrawerIcon;
|
|
|
239
|
+ }
|
|
|
240
|
+ }
|
|
|
241
|
+
|
|
|
242
|
+ if (navArrow != null) {
|
|
|
243
|
+ navArrow.setProgress(isBack ? 1.0f : 0.0f);
|
|
|
244
|
+ if (screen.navBarButtonColor != null) {
|
|
|
245
|
+ navArrow.setColor(screen.navBarButtonColor);
|
|
|
246
|
+ } else {
|
|
|
247
|
+ navArrow.setColor(Color.BLACK);
|
|
|
248
|
+ }
|
|
|
249
|
+ navIcon = navArrow;
|
|
|
250
|
+ }
|
|
|
251
|
+
|
|
|
252
|
+ actionBar.setHomeAsUpIndicator(navIcon);
|
|
|
253
|
+ actionBar.setDisplayHomeAsUpEnabled(navIcon != null);
|
|
170
|
254
|
}
|
|
171
|
255
|
|
|
172
|
256
|
/**
|
|
|
@@ -186,6 +270,42 @@ public class RnnToolBar extends Toolbar {
|
|
186
|
270
|
setupToolbarButtonsAsync(screen);
|
|
187
|
271
|
}
|
|
188
|
272
|
|
|
|
273
|
+ private static class SetupDrawerIconTask extends AsyncTask<Void, Void, Drawable> {
|
|
|
274
|
+ private final WeakReference<RnnToolBar> mToolbarWR;
|
|
|
275
|
+ private final String mDrawerIconSource;
|
|
|
276
|
+ private final Integer mTintColor;
|
|
|
277
|
+
|
|
|
278
|
+ public SetupDrawerIconTask(RnnToolBar toolBar, String drawerIconSource, Screen screen) {
|
|
|
279
|
+ mToolbarWR = new WeakReference<>(toolBar);
|
|
|
280
|
+ mDrawerIconSource = drawerIconSource;
|
|
|
281
|
+ mTintColor = screen.navBarButtonColor;
|
|
|
282
|
+ }
|
|
|
283
|
+
|
|
|
284
|
+ @Override
|
|
|
285
|
+ protected Drawable doInBackground(Void... params) {
|
|
|
286
|
+ Context context = ContextProvider.getActivityContext();
|
|
|
287
|
+ if (context == null || mDrawerIconSource == null) {
|
|
|
288
|
+ return null;
|
|
|
289
|
+ }
|
|
|
290
|
+
|
|
|
291
|
+ return IconUtils.getIcon(context, mDrawerIconSource);
|
|
|
292
|
+ }
|
|
|
293
|
+
|
|
|
294
|
+ @Override
|
|
|
295
|
+ protected void onPostExecute(Drawable drawerIcon) {
|
|
|
296
|
+ RnnToolBar toolBar = mToolbarWR.get();
|
|
|
297
|
+ if (drawerIcon != null) {
|
|
|
298
|
+ if (mTintColor != null) {
|
|
|
299
|
+ ImageUtils.tint(drawerIcon, mTintColor);
|
|
|
300
|
+ }
|
|
|
301
|
+ toolBar.setDrawerIcon(drawerIcon);
|
|
|
302
|
+ }
|
|
|
303
|
+
|
|
|
304
|
+ toolBar.setNavUpButton();
|
|
|
305
|
+ mToolbarWR.clear();
|
|
|
306
|
+ }
|
|
|
307
|
+ }
|
|
|
308
|
+
|
|
189
|
309
|
private static class SetupToolbarButtonsTask extends AsyncTask<Void, Void, Map<String, Drawable>> {
|
|
190
|
310
|
private final List<Button> mOldButtons;
|
|
191
|
311
|
private final List<Button> mNewButtons;
|