react-native-navigation的迁移库

RnnToolBar.java 9.9KB

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