react-native-navigation的迁移库

RnnToolBar.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package com.reactnativenavigation.views;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.res.Resources;
  6. import android.graphics.Color;
  7. import android.graphics.drawable.Drawable;
  8. import android.os.AsyncTask;
  9. import android.support.annotation.ColorInt;
  10. import android.support.annotation.NonNull;
  11. import android.support.annotation.UiThread;
  12. import android.support.v4.content.ContextCompat;
  13. import android.support.v4.content.res.ResourcesCompat;
  14. import android.support.v7.app.ActionBar;
  15. import android.support.v7.app.AppCompatActivity;
  16. import android.support.v7.widget.Toolbar;
  17. import android.util.AttributeSet;
  18. import android.view.Menu;
  19. import android.view.MenuItem;
  20. import android.view.View;
  21. import android.view.ViewTreeObserver;
  22. import android.widget.TextView;
  23. import com.reactnativenavigation.R;
  24. import com.reactnativenavigation.activities.BaseReactActivity;
  25. import com.reactnativenavigation.core.objects.Button;
  26. import com.reactnativenavigation.core.objects.Screen;
  27. import com.reactnativenavigation.utils.ContextProvider;
  28. import com.reactnativenavigation.utils.ImageUtils;
  29. import java.lang.ref.WeakReference;
  30. import java.util.ArrayList;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. /**
  35. * Created by guyc on 09/04/16.
  36. */
  37. public class RnnToolBar extends Toolbar {
  38. private static final int ANIMATE_DURATION = 180;
  39. private List<Screen> mScreens;
  40. private AsyncTask mSetupToolbarTask;
  41. private Drawable mBackground;
  42. private ArrayList<View> mMenuItems;
  43. public RnnToolBar(Context context) {
  44. super(context);
  45. init();
  46. }
  47. public RnnToolBar(Context context, AttributeSet attrs) {
  48. super(context, attrs);
  49. init();
  50. }
  51. public RnnToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
  52. super(context, attrs, defStyleAttr);
  53. init();
  54. }
  55. private void init() {
  56. mMenuItems = new ArrayList<>();
  57. mBackground = getBackground();
  58. }
  59. public void setScreens(List<Screen> screens) {
  60. mScreens = screens;
  61. }
  62. public void setStyle(Screen screen) {
  63. if (screen.toolBarColor != null) {
  64. setBackgroundColor(screen.toolBarColor);
  65. } else {
  66. resetBackground();
  67. }
  68. if (screen.navBarTextColor != null) {
  69. setTitleTextColor(screen.navBarTextColor);
  70. } else {
  71. resetTitleTextColor();
  72. }
  73. if (screen.toolBarHidden != null && screen.toolBarHidden) {
  74. hideToolbar();
  75. } else {
  76. showToolbar();
  77. }
  78. }
  79. private void resetBackground() {
  80. setBackground(mBackground);
  81. }
  82. private void resetTitleTextColor() {
  83. setTitleTextColor(ContextCompat.getColor(getContext(), android.R.color.primary_text_light));
  84. }
  85. public void handleOnCreateOptionsMenuAsync() {
  86. if (mScreens != null) {
  87. setupToolbarButtonsAsync(null, mScreens.get(0));
  88. }
  89. }
  90. public void setupToolbarButtonsAsync(Screen newScreen) {
  91. if (newScreen != null) {
  92. this.setupToolbarButtonsAsync(null, newScreen);
  93. }
  94. }
  95. public void setupToolbarButtonsAsync(Screen oldScreen, Screen newScreen) {
  96. if (mSetupToolbarTask == null) {
  97. mSetupToolbarTask = new SetupToolbarButtonsTask(this, oldScreen, newScreen).execute();
  98. }
  99. }
  100. public void showToolbar(boolean animated) {
  101. ActionBar actionBar = ((AppCompatActivity) getContext()).getSupportActionBar();
  102. if (actionBar != null) {
  103. actionBar.setShowHideAnimationEnabled(animated);
  104. // We hide the ToolBar's parent (AppBarLayout) since this animates the shadow added by AppBar as well
  105. ((View) getParent()).setVisibility(VISIBLE);
  106. }
  107. }
  108. public void hideToolbar(boolean animated) {
  109. ActionBar actionBar = ((AppCompatActivity) getContext()).getSupportActionBar();
  110. if (actionBar != null) {
  111. actionBar.setShowHideAnimationEnabled(animated);
  112. // We hide the ToolBar's parent (AppBarLayout) since this animates the shadow added by AppBar as well
  113. ((View) getParent()).setVisibility(GONE);
  114. }
  115. }
  116. private void showToolbar() {
  117. showToolbar(false);
  118. }
  119. private void hideToolbar() {
  120. hideToolbar(false);
  121. }
  122. @SuppressWarnings({"ConstantConditions"})
  123. public void showBackButton(Screen screen) {
  124. ActionBar actionBar = ContextProvider.getActivityContext().getSupportActionBar();
  125. Drawable backButton = setupBackButton(screen);
  126. actionBar.setHomeAsUpIndicator(backButton);
  127. actionBar.setDisplayHomeAsUpEnabled(true);
  128. }
  129. @SuppressLint("PrivateResource")
  130. @SuppressWarnings({"ConstantConditions"})
  131. private Drawable setupBackButton(Screen screen) {
  132. Resources resources = getResources();
  133. final Drawable backButton = ResourcesCompat.getDrawable(resources,
  134. R.drawable.abc_ic_ab_back_mtrl_am_alpha,
  135. ContextProvider.getActivityContext().getTheme());
  136. int tintColor = screen.navBarButtonColor != null ? screen.navBarButtonColor : Color.BLACK;
  137. ImageUtils.tint(backButton, tintColor);
  138. return backButton;
  139. }
  140. @SuppressWarnings({"ConstantConditions"})
  141. public void hideBackButton() {
  142. ContextProvider.getActivityContext().getSupportActionBar().setDisplayHomeAsUpEnabled(false);
  143. }
  144. /**
  145. * Update the ToolBar from screen. This function sets any properties that are defined
  146. * in the screen.
  147. * @param screen The currently displayed screen
  148. */
  149. @UiThread
  150. public void update(@NonNull Screen screen) {
  151. ((AppCompatActivity) getContext()).setSupportActionBar(this);
  152. setTitle(screen.title);
  153. setStyle(screen);
  154. }
  155. public void updateAndSetButtons(Screen screen) {
  156. update(screen);
  157. setupToolbarButtonsAsync(screen);
  158. }
  159. private static class SetupToolbarButtonsTask extends AsyncTask<Void, Void, Map<String, Drawable>> {
  160. private final List<Button> mOldButtons;
  161. private final List<Button> mNewButtons;
  162. private final WeakReference<RnnToolBar> mToolbarWR;
  163. @ColorInt private final Integer mTintColor;
  164. private final int mIconDimensions;
  165. public SetupToolbarButtonsTask(RnnToolBar toolBar, Screen oldScreen, Screen newScreen) {
  166. mToolbarWR = new WeakReference<>(toolBar);
  167. mOldButtons = oldScreen == null ? null : oldScreen.getButtons();
  168. mNewButtons = newScreen.getButtons();
  169. mTintColor = newScreen.navBarButtonColor;
  170. mIconDimensions = (int) (toolBar.getHeight() * 0.4f);
  171. }
  172. @Override
  173. protected Map<String, Drawable> doInBackground(Void... params) {
  174. Context context = ContextProvider.getActivityContext();
  175. if (context == null) {
  176. return null;
  177. }
  178. Map<String, Drawable> icons = new HashMap<>();
  179. for (Button button : mNewButtons) {
  180. if (button.hasIcon()) {
  181. icons.put(button.id, button.getIcon(context, mIconDimensions));
  182. }
  183. }
  184. return icons;
  185. }
  186. @Override
  187. protected void onPostExecute(Map<String, Drawable> icons) {
  188. final Context context = ContextProvider.getActivityContext();
  189. if (context == null) {
  190. return;
  191. }
  192. Menu menu = ((BaseReactActivity) context).getMenu();
  193. if (menu == null) {
  194. RnnToolBar toolBar = mToolbarWR.get();
  195. if (toolBar != null) {
  196. toolBar.mSetupToolbarTask = null;
  197. }
  198. mToolbarWR.clear();
  199. return;
  200. }
  201. // Remove prev screen buttons
  202. if(mOldButtons == null) {
  203. menu.clear();
  204. } else {
  205. for (Button btn : mOldButtons) {
  206. menu.removeItem(btn.getItemId());
  207. }
  208. }
  209. // Add new screen buttons
  210. final List<String> textButtons = new ArrayList<>();
  211. final int size = mNewButtons.size();
  212. for (int i = 0; i < size; i++) {
  213. Button button = mNewButtons.get(i);
  214. MenuItem item = menu.add(Menu.NONE, button.getItemId(), size - i - 1, button.title);
  215. item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
  216. // Set button icon
  217. if (button.hasIcon()) {
  218. Drawable icon = icons.get(button.id);
  219. if (mTintColor != null) {
  220. ImageUtils.tint(icon, mTintColor);
  221. }
  222. item.setIcon(icon);
  223. } else {
  224. textButtons.add(button.title);
  225. }
  226. // Disable button if needed
  227. if (button.disabled) {
  228. item.setEnabled(false);
  229. }
  230. }
  231. final RnnToolBar toolBar = mToolbarWR.get();
  232. if (toolBar != null) {
  233. // Tint overflow icon which appears when there's not enough space in Toolbar for icons
  234. if (mTintColor != null) {
  235. ImageUtils.tint(toolBar.getOverflowIcon(), mTintColor);
  236. }
  237. // Tint text buttons
  238. if (textButtons.size() > 0 && mTintColor != null) {
  239. final View decorView = ((Activity) context).getWindow().getDecorView();
  240. decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  241. @Override
  242. public void onGlobalLayout() {
  243. decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  244. // Find TextViews
  245. for (String text : textButtons) {
  246. decorView.findViewsWithText(toolBar.mMenuItems, text, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
  247. }
  248. // Set text color
  249. for (View button : toolBar.mMenuItems) {
  250. ((TextView) button).setTextColor(mTintColor);
  251. }
  252. toolBar.mMenuItems.clear();
  253. }
  254. });
  255. }
  256. toolBar.mSetupToolbarTask = null;
  257. mToolbarWR.clear();
  258. }
  259. }
  260. }
  261. }