react-native-navigation的迁移库

RnnToolBar.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. package com.reactnativenavigation.views;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Color;
  5. import android.graphics.drawable.Drawable;
  6. import android.os.AsyncTask;
  7. import android.support.annotation.ColorInt;
  8. import android.support.annotation.NonNull;
  9. import android.support.annotation.UiThread;
  10. import android.support.v4.content.ContextCompat;
  11. import android.support.v4.widget.DrawerLayout;
  12. import android.support.v7.app.ActionBar;
  13. import android.support.v7.app.ActionBarDrawerToggle;
  14. import android.support.v7.app.AppCompatActivity;
  15. import android.support.v7.graphics.drawable.DrawerArrowDrawable;
  16. import android.support.v7.widget.Toolbar;
  17. import android.util.AttributeSet;
  18. import android.view.Gravity;
  19. import android.view.Menu;
  20. import android.view.MenuItem;
  21. import android.view.View;
  22. import android.view.ViewTreeObserver;
  23. import android.widget.TextView;
  24. import com.reactnativenavigation.R;
  25. import com.reactnativenavigation.activities.BaseReactActivity;
  26. import com.reactnativenavigation.core.objects.Button;
  27. import com.reactnativenavigation.core.objects.Screen;
  28. import com.reactnativenavigation.utils.ContextProvider;
  29. import com.reactnativenavigation.utils.IconUtils;
  30. import com.reactnativenavigation.utils.ImageUtils;
  31. import java.lang.ref.WeakReference;
  32. import java.util.ArrayList;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. /**
  37. * Created by guyc on 09/04/16.
  38. */
  39. public class RnnToolBar extends Toolbar {
  40. private List<Screen> mScreens;
  41. private AsyncTask mDrawerIconTask;
  42. private AsyncTask mSetupToolbarTask;
  43. private Drawable mBackground;
  44. private Drawable mDrawerIcon;
  45. private DrawerLayout mDrawerLayout;
  46. private ActionBarDrawerToggle mDrawerToggle;
  47. private ArrayList<View> mMenuItems;
  48. public RnnToolBar(Context context) {
  49. super(context);
  50. init();
  51. }
  52. public RnnToolBar(Context context, AttributeSet attrs) {
  53. super(context, attrs);
  54. init();
  55. }
  56. public RnnToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
  57. super(context, attrs, defStyleAttr);
  58. init();
  59. }
  60. private void init() {
  61. mMenuItems = new ArrayList<>();
  62. mBackground = getBackground();
  63. }
  64. public void setScreens(List<Screen> screens) {
  65. mScreens = screens;
  66. }
  67. public void handleOnCreateOptionsMenuAsync() {
  68. if (mScreens != null) {
  69. setupToolbarButtonsAsync(null, mScreens.get(0));
  70. }
  71. }
  72. public ActionBarDrawerToggle setupDrawer(DrawerLayout drawerLayout, Screen drawerScreen, Screen screen) {
  73. if (drawerLayout == null || drawerScreen == null) {
  74. return null;
  75. }
  76. mDrawerLayout = drawerLayout;
  77. mDrawerToggle = new ActionBarDrawerToggle(
  78. ContextProvider.getActivityContext(),
  79. mDrawerLayout,
  80. this,
  81. R.string.drawer_open,
  82. R.string.drawer_close
  83. );
  84. mDrawerLayout.setDrawerListener(mDrawerToggle);
  85. setupDrawerIconAsync(drawerScreen.icon, screen);
  86. return mDrawerToggle;
  87. }
  88. public void setDrawerIcon(Drawable icon) {
  89. mDrawerIcon = icon;
  90. }
  91. public void showDrawer(boolean animated) {
  92. if (mDrawerLayout == null) {
  93. return;
  94. }
  95. mDrawerLayout.openDrawer(Gravity.LEFT);
  96. }
  97. public void hideDrawer(boolean animated) {
  98. if (mDrawerLayout == null) {
  99. return;
  100. }
  101. mDrawerLayout.closeDrawer(Gravity.LEFT);
  102. }
  103. public void toggleDrawer(boolean animated) {
  104. if (mDrawerLayout == null) {
  105. return;
  106. }
  107. boolean visible = mDrawerLayout.isDrawerOpen(Gravity.LEFT);
  108. if (visible) {
  109. hideDrawer(animated);
  110. } else {
  111. showDrawer(animated);
  112. }
  113. }
  114. public void setupDrawerIconAsync(String drawerIconSource, Screen screen) {
  115. if (mDrawerIconTask == null) {
  116. mDrawerIconTask = new SetupDrawerIconTask(this, drawerIconSource, screen).execute();
  117. }
  118. }
  119. public void setupToolbarButtonsAsync(Screen newScreen) {
  120. if (newScreen != null) {
  121. this.setupToolbarButtonsAsync(null, newScreen);
  122. }
  123. }
  124. public void setupToolbarButtonsAsync(Screen oldScreen, Screen newScreen) {
  125. if (mSetupToolbarTask == null) {
  126. mSetupToolbarTask = new SetupToolbarButtonsTask(this, oldScreen, newScreen).execute();
  127. }
  128. }
  129. public void showToolbar(boolean animated) {
  130. ActionBar actionBar = ((AppCompatActivity) getContext()).getSupportActionBar();
  131. if (actionBar != null) {
  132. actionBar.setShowHideAnimationEnabled(animated);
  133. // We hide the ToolBar's parent (AppBarLayout) since this animates the shadow added by AppBar as well
  134. ((View) getParent()).setVisibility(VISIBLE);
  135. }
  136. }
  137. public void hideToolbar(boolean animated) {
  138. ActionBar actionBar = ((AppCompatActivity) getContext()).getSupportActionBar();
  139. if (actionBar != null) {
  140. actionBar.setShowHideAnimationEnabled(animated);
  141. // We hide the ToolBar's parent (AppBarLayout) since this animates the shadow added by AppBar as well
  142. ((View) getParent()).setVisibility(GONE);
  143. }
  144. }
  145. private void showToolbar() {
  146. showToolbar(false);
  147. }
  148. private void hideToolbar() {
  149. hideToolbar(false);
  150. }
  151. public void setNavUpButton() {
  152. setNavUpButton(null);
  153. }
  154. @SuppressWarnings({"ConstantConditions"})
  155. public void setNavUpButton(Screen screen) {
  156. BaseReactActivity context = ContextProvider.getActivityContext();
  157. if (context == null) {
  158. return;
  159. }
  160. ActionBar actionBar = context.getSupportActionBar();
  161. if (actionBar == null) {
  162. return;
  163. }
  164. boolean isBack = screen != null;
  165. boolean hasDrawer = mDrawerToggle != null;
  166. Drawable navIcon = null;
  167. DrawerArrowDrawable navArrow = null;
  168. if (hasDrawer && mDrawerIcon == null) {
  169. navArrow = (DrawerArrowDrawable) this.getNavigationIcon();
  170. } else {
  171. if (isBack && !screen.backButtonHidden) {
  172. navArrow = new DrawerArrowDrawable(context);
  173. } else if (hasDrawer) {
  174. navIcon = mDrawerIcon;
  175. }
  176. }
  177. if (navArrow != null) {
  178. navArrow.setProgress(isBack ? 1.0f : 0.0f);
  179. if (screen != null && screen.navBarButtonColor != null) {
  180. navArrow.setColor(screen.navBarButtonColor);
  181. } else {
  182. navArrow.setColor(Color.BLACK);
  183. }
  184. navIcon = navArrow;
  185. }
  186. actionBar.setHomeAsUpIndicator(navIcon);
  187. actionBar.setDisplayHomeAsUpEnabled(navIcon != null);
  188. }
  189. /**
  190. * Update the ToolBar from screen. This function sets any properties that are defined
  191. * in the screen.
  192. * @param screen The currently displayed screen
  193. */
  194. @UiThread
  195. public void update(@NonNull Screen screen) {
  196. ((AppCompatActivity) getContext()).setSupportActionBar(this);
  197. setTitle(screen.title);
  198. setStyle(screen);
  199. }
  200. public void updateAndSetButtons(Screen screen) {
  201. update(screen);
  202. setupToolbarButtonsAsync(screen);
  203. }
  204. private static class SetupDrawerIconTask extends AsyncTask<Void, Void, Drawable> {
  205. private final WeakReference<RnnToolBar> mToolbarWR;
  206. private final String mDrawerIconSource;
  207. private final Integer mTintColor;
  208. public SetupDrawerIconTask(RnnToolBar toolBar, String drawerIconSource, Screen screen) {
  209. mToolbarWR = new WeakReference<>(toolBar);
  210. mDrawerIconSource = drawerIconSource;
  211. mTintColor = screen.navBarButtonColor;
  212. }
  213. @Override
  214. protected Drawable doInBackground(Void... params) {
  215. Context context = ContextProvider.getActivityContext();
  216. if (context == null || mDrawerIconSource == null) {
  217. return null;
  218. }
  219. return IconUtils.getIcon(context, mDrawerIconSource);
  220. }
  221. @Override
  222. protected void onPostExecute(Drawable drawerIcon) {
  223. RnnToolBar toolBar = mToolbarWR.get();
  224. if (drawerIcon != null) {
  225. if (mTintColor != null) {
  226. ImageUtils.tint(drawerIcon, mTintColor);
  227. }
  228. toolBar.setDrawerIcon(drawerIcon);
  229. }
  230. toolBar.setNavUpButton();
  231. mToolbarWR.clear();
  232. }
  233. }
  234. private static class SetupToolbarButtonsTask extends AsyncTask<Void, Void, Map<String, Drawable>> {
  235. private final List<Button> mOldButtons;
  236. private final List<Button> mNewButtons;
  237. private final WeakReference<RnnToolBar> mToolbarWR;
  238. @ColorInt private final Integer mTintColor;
  239. private final int mIconDimensions;
  240. public SetupToolbarButtonsTask(RnnToolBar toolBar, Screen oldScreen, Screen newScreen) {
  241. mToolbarWR = new WeakReference<>(toolBar);
  242. mOldButtons = oldScreen == null ? null : oldScreen.getButtons();
  243. mNewButtons = newScreen.getButtons();
  244. mTintColor = newScreen.navBarButtonColor;
  245. mIconDimensions = (int) ImageUtils.convertDpToPixel(48, toolBar.getContext());
  246. }
  247. @Override
  248. protected Map<String, Drawable> doInBackground(Void... params) {
  249. Context context = ContextProvider.getActivityContext();
  250. if (context == null) {
  251. return null;
  252. }
  253. Map<String, Drawable> icons = new HashMap<>();
  254. for (Button button : mNewButtons) {
  255. if (button.hasIcon()) {
  256. icons.put(button.id, button.getIcon(context, mIconDimensions));
  257. }
  258. }
  259. return icons;
  260. }
  261. @Override
  262. protected void onPostExecute(Map<String, Drawable> icons) {
  263. final Context context = ContextProvider.getActivityContext();
  264. if (context == null) {
  265. return;
  266. }
  267. Menu menu = ((BaseReactActivity) context).getMenu();
  268. if (menu == null) {
  269. RnnToolBar toolBar = mToolbarWR.get();
  270. if (toolBar != null) {
  271. toolBar.mSetupToolbarTask = null;
  272. }
  273. mToolbarWR.clear();
  274. return;
  275. }
  276. // Remove prev screen buttons
  277. if(mOldButtons == null) {
  278. menu.clear();
  279. } else {
  280. for (Button btn : mOldButtons) {
  281. menu.removeItem(btn.getItemId());
  282. }
  283. }
  284. // Add new screen buttons
  285. final List<String> textButtons = new ArrayList<>();
  286. final int size = mNewButtons.size();
  287. for (int i = 0; i < size; i++) {
  288. Button button = mNewButtons.get(i);
  289. MenuItem item = menu.add(Menu.NONE, button.getItemId(), size - i - 1, button.title);
  290. item.setShowAsAction(getMenuItemShowAction(button.showAsAction));
  291. // Set button icon
  292. if (button.hasIcon()) {
  293. Drawable icon = icons.get(button.id);
  294. if (mTintColor != null) {
  295. ImageUtils.tint(icon, mTintColor);
  296. }
  297. item.setIcon(icon);
  298. } else {
  299. textButtons.add(button.title);
  300. }
  301. // Disable button if needed
  302. if (button.disabled) {
  303. item.setEnabled(false);
  304. }
  305. }
  306. final RnnToolBar toolBar = mToolbarWR.get();
  307. if (toolBar != null) {
  308. // Tint overflow icon which appears when there's not enough space in Toolbar for icons
  309. if (mTintColor != null) {
  310. ImageUtils.tint(toolBar.getOverflowIcon(), mTintColor);
  311. }
  312. // Tint text buttons
  313. if (textButtons.size() > 0 && mTintColor != null) {
  314. final View decorView = ((Activity) context).getWindow().getDecorView();
  315. decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  316. @Override
  317. public void onGlobalLayout() {
  318. decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  319. // Find TextViews
  320. for (String text : textButtons) {
  321. decorView.findViewsWithText(toolBar.mMenuItems, text, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
  322. }
  323. // Set text color
  324. for (View button : toolBar.mMenuItems) {
  325. ((TextView) button).setTextColor(mTintColor);
  326. }
  327. toolBar.mMenuItems.clear();
  328. }
  329. });
  330. }
  331. toolBar.mSetupToolbarTask = null;
  332. mToolbarWR.clear();
  333. }
  334. }
  335. private int getMenuItemShowAction(String action) {
  336. switch (action) {
  337. case "never":
  338. return MenuItem.SHOW_AS_ACTION_NEVER;
  339. case "always":
  340. return MenuItem.SHOW_AS_ACTION_ALWAYS;
  341. case "withText":
  342. return MenuItem.SHOW_AS_ACTION_WITH_TEXT;
  343. default:
  344. return MenuItem.SHOW_AS_ACTION_IF_ROOM;
  345. }
  346. }
  347. }
  348. }