react-native-navigation的迁移库

Screen.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package com.reactnativenavigation.screens;
  2. import android.annotation.TargetApi;
  3. import android.app.Activity;
  4. import android.graphics.Color;
  5. import android.os.Build;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.view.Window;
  8. import android.widget.RelativeLayout;
  9. import com.facebook.react.bridge.Callback;
  10. import com.reactnativenavigation.animation.VisibilityAnimator;
  11. import com.reactnativenavigation.events.ContextualMenuHiddenEvent;
  12. import com.reactnativenavigation.events.Event;
  13. import com.reactnativenavigation.events.EventBus;
  14. import com.reactnativenavigation.events.FabSetEvent;
  15. import com.reactnativenavigation.events.Subscriber;
  16. import com.reactnativenavigation.events.ViewPagerScreenChangedEvent;
  17. import com.reactnativenavigation.params.BaseScreenParams;
  18. import com.reactnativenavigation.params.ContextualMenuParams;
  19. import com.reactnativenavigation.params.FabParams;
  20. import com.reactnativenavigation.params.ScreenParams;
  21. import com.reactnativenavigation.params.StyleParams;
  22. import com.reactnativenavigation.params.TitleBarButtonParams;
  23. import com.reactnativenavigation.params.TitleBarLeftButtonParams;
  24. import com.reactnativenavigation.utils.ViewUtils;
  25. import com.reactnativenavigation.views.LeftButtonOnClickListener;
  26. import com.reactnativenavigation.views.TopBar;
  27. import java.util.List;
  28. import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
  29. import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
  30. public abstract class Screen extends RelativeLayout implements Subscriber {
  31. public interface OnDisplayListener {
  32. void onDisplay();
  33. }
  34. protected final AppCompatActivity activity;
  35. protected final ScreenParams screenParams;
  36. protected TopBar topBar;
  37. private final LeftButtonOnClickListener leftButtonOnClickListener;
  38. private VisibilityAnimator topBarVisibilityAnimator;
  39. private ScreenAnimator screenAnimator;
  40. protected final StyleParams styleParams;
  41. public Screen(AppCompatActivity activity, ScreenParams screenParams, LeftButtonOnClickListener leftButtonOnClickListener) {
  42. super(activity);
  43. this.activity = activity;
  44. this.screenParams = screenParams;
  45. styleParams = screenParams.styleParams;
  46. this.leftButtonOnClickListener = leftButtonOnClickListener;
  47. screenAnimator = new ScreenAnimator(this);
  48. createViews();
  49. EventBus.instance.register(this);
  50. }
  51. @Override
  52. public void onEvent(Event event) {
  53. if (ContextualMenuHiddenEvent.TYPE.equals(event.getType()) && isShown()) {
  54. setStyle();
  55. topBar.onContextualMenuHidden();
  56. }
  57. if (ViewPagerScreenChangedEvent.TYPE.equals(event.getType()) && isShown() ) {
  58. setStyle();
  59. topBar.dismissContextualMenu();
  60. }
  61. }
  62. public void setStyle() {
  63. setStatusBarColor(styleParams.statusBarColor);
  64. setNavigationBarColor(styleParams.navigationBarColor);
  65. topBar.setStyle(styleParams);
  66. if (styleParams.screenBackgroundColor.hasColor()) {
  67. setBackgroundColor(styleParams.screenBackgroundColor.getColor());
  68. }
  69. }
  70. private void createViews() {
  71. createAndAddTopBar();
  72. createTitleBar();
  73. createContent();
  74. }
  75. protected abstract void createContent();
  76. private void createTitleBar() {
  77. addTitleBarButtons();
  78. topBar.setTitle(screenParams.title);
  79. topBar.setSubtitle(screenParams.subtitle);
  80. }
  81. private void addTitleBarButtons() {
  82. setButtonColorFromScreen(screenParams.rightButtons);
  83. if (screenParams.leftButton != null) {
  84. screenParams.leftButton.setColorFromScreenStyle(screenParams.styleParams.titleBarButtonColor);
  85. }
  86. topBar.addTitleBarAndSetButtons(screenParams.rightButtons,
  87. screenParams.leftButton,
  88. leftButtonOnClickListener,
  89. screenParams.getNavigatorEventId(),
  90. screenParams.overrideBackPressInJs);
  91. }
  92. private void createAndAddTopBar() {
  93. createTopBar();
  94. addTopBar();
  95. }
  96. protected void createTopBar() {
  97. topBar = new TopBar(getContext());
  98. }
  99. private void addTopBar() {
  100. createTopBarVisibilityAnimator();
  101. addView(topBar, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
  102. }
  103. private void createTopBarVisibilityAnimator() {
  104. ViewUtils.runOnPreDraw(topBar, new Runnable() {
  105. @Override
  106. public void run() {
  107. if (topBarVisibilityAnimator == null) {
  108. topBarVisibilityAnimator = new VisibilityAnimator(topBar,
  109. VisibilityAnimator.HideDirection.Up,
  110. topBar.getHeight());
  111. }
  112. }
  113. });
  114. }
  115. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  116. private void setStatusBarColor(StyleParams.Color statusBarColor) {
  117. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
  118. final Activity context = (Activity) getContext();
  119. final Window window = context.getWindow();
  120. if (statusBarColor.hasColor()) {
  121. window.setStatusBarColor(statusBarColor.getColor());
  122. } else {
  123. window.setStatusBarColor(Color.BLACK);
  124. }
  125. }
  126. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  127. public void setNavigationBarColor(StyleParams.Color navigationBarColor) {
  128. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
  129. final Activity context = (Activity) getContext();
  130. final Window window = context.getWindow();
  131. if (navigationBarColor.hasColor()) {
  132. window.setNavigationBarColor(navigationBarColor.getColor());
  133. } else {
  134. window.setNavigationBarColor(Color.BLACK);
  135. }
  136. }
  137. public abstract void unmountReactView();
  138. public String getScreenInstanceId() {
  139. return screenParams.getScreenInstanceId();
  140. }
  141. public abstract String getNavigatorEventId();
  142. public BaseScreenParams getScreenParams() {
  143. return screenParams;
  144. }
  145. public void setTopBarVisible(boolean visible, boolean animate) {
  146. topBarVisibilityAnimator.setVisible(visible, animate);
  147. }
  148. public void setTitleBarTitle(String title) {
  149. topBar.setTitle(title);
  150. }
  151. public void setTitleBarSubtitle(String subtitle) {
  152. topBar.setSubtitle(subtitle);
  153. }
  154. public void setTitleBarRightButtons(String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
  155. setButtonColorFromScreen(titleBarButtons);
  156. topBar.setTitleBarRightButtons(navigatorEventId, titleBarButtons);
  157. }
  158. public void setTitleBarLeftButton(String navigatorEventId, LeftButtonOnClickListener backButtonListener,
  159. TitleBarLeftButtonParams titleBarLeftButtonParams) {
  160. topBar.setTitleBarLeftButton(navigatorEventId,
  161. backButtonListener,
  162. titleBarLeftButtonParams,
  163. screenParams.overrideBackPressInJs);
  164. }
  165. public void setFab(FabParams fabParams) {
  166. screenParams.fabParams = fabParams;
  167. if (isShown()) {
  168. EventBus.instance.post(new FabSetEvent(fabParams));
  169. }
  170. }
  171. public StyleParams getStyleParams() {
  172. return screenParams.styleParams;
  173. }
  174. private void setButtonColorFromScreen(List<TitleBarButtonParams> titleBarButtonParams) {
  175. if (titleBarButtonParams == null) {
  176. return;
  177. }
  178. for (TitleBarButtonParams titleBarButtonParam : titleBarButtonParams) {
  179. titleBarButtonParam.setColorFromScreenStyle(screenParams.styleParams.titleBarButtonColor);
  180. }
  181. }
  182. public abstract void setOnDisplayListener(OnDisplayListener onContentViewDisplayedListener);
  183. public void show() {
  184. screenAnimator.show(screenParams.animateScreenTransitions);
  185. }
  186. public void show(boolean animated) {
  187. screenAnimator.show(animated);
  188. }
  189. public void show(boolean animated, Runnable onAnimationEnd) {
  190. setStyle();
  191. screenAnimator.show(animated, onAnimationEnd);
  192. }
  193. public void hide(boolean animated, Runnable onAnimatedEnd) {
  194. screenAnimator.hide(animated, onAnimatedEnd);
  195. }
  196. public void showContextualMenu(ContextualMenuParams params, Callback onButtonClicked) {
  197. topBar.showContextualMenu(params, styleParams, onButtonClicked);
  198. setStatusBarColor(styleParams.contextualMenuStatusBarColor);
  199. }
  200. public void dismissContextualMenu() {
  201. topBar.dismissContextualMenu();
  202. }
  203. public void destroy() {
  204. unmountReactView();
  205. EventBus.instance.unregister(this);
  206. }
  207. }