react-native-navigation的迁移库

SingleScreenLayout.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package com.reactnativenavigation.layouts;
  2. import android.support.annotation.Nullable;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.view.View;
  5. import android.widget.RelativeLayout;
  6. import com.facebook.react.bridge.Callback;
  7. import com.reactnativenavigation.NavigationApplication;
  8. import com.reactnativenavigation.events.EventBus;
  9. import com.reactnativenavigation.events.ScreenChangedEvent;
  10. import com.reactnativenavigation.params.ContextualMenuParams;
  11. import com.reactnativenavigation.params.FabParams;
  12. import com.reactnativenavigation.params.ScreenParams;
  13. import com.reactnativenavigation.params.SideMenuParams;
  14. import com.reactnativenavigation.params.SnackbarParams;
  15. import com.reactnativenavigation.params.TitleBarButtonParams;
  16. import com.reactnativenavigation.params.TitleBarLeftButtonParams;
  17. import com.reactnativenavigation.screens.Screen;
  18. import com.reactnativenavigation.screens.ScreenStack;
  19. import com.reactnativenavigation.views.LeftButtonOnClickListener;
  20. import com.reactnativenavigation.views.SideMenu;
  21. import com.reactnativenavigation.views.SideMenu.Side;
  22. import com.reactnativenavigation.views.SnackbarAndFabContainer;
  23. import java.util.List;
  24. import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
  25. public class SingleScreenLayout extends RelativeLayout implements Layout {
  26. private final AppCompatActivity activity;
  27. protected final ScreenParams screenParams;
  28. private final SideMenuParams leftSideMenuParams;
  29. private final SideMenuParams rightSideMenuParams;
  30. protected ScreenStack stack;
  31. private SnackbarAndFabContainer snackbarAndFabContainer;
  32. protected LeftButtonOnClickListener leftButtonOnClickListener;
  33. private @Nullable SideMenu sideMenu;
  34. public SingleScreenLayout(AppCompatActivity activity, SideMenuParams leftSideMenuParams,
  35. SideMenuParams rightSideMenuParams, ScreenParams screenParams) {
  36. super(activity);
  37. this.activity = activity;
  38. this.screenParams = screenParams;
  39. this.leftSideMenuParams = leftSideMenuParams;
  40. this.rightSideMenuParams = rightSideMenuParams;
  41. createLayout();
  42. }
  43. private void createLayout() {
  44. if (leftSideMenuParams == null && rightSideMenuParams == null) {
  45. createStack(getScreenStackParent());
  46. } else {
  47. sideMenu = createSideMenu();
  48. createStack(getScreenStackParent());
  49. }
  50. createFabAndSnackbarContainer();
  51. sendScreenChangedEventAfterInitialPush();
  52. }
  53. private RelativeLayout getScreenStackParent() {
  54. return sideMenu == null ? this : sideMenu.getContentContainer();
  55. }
  56. private SideMenu createSideMenu() {
  57. SideMenu sideMenu = new SideMenu(getContext(), leftSideMenuParams, rightSideMenuParams);
  58. RelativeLayout.LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
  59. addView(sideMenu, lp);
  60. return sideMenu;
  61. }
  62. private void createStack(RelativeLayout parent) {
  63. if (stack != null) {
  64. stack.destroy();
  65. }
  66. stack = new ScreenStack(activity, parent, screenParams.getNavigatorId(), this);
  67. LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
  68. pushInitialScreen(lp);
  69. }
  70. protected void pushInitialScreen(LayoutParams lp) {
  71. stack.pushInitialScreen(screenParams, lp);
  72. stack.show();
  73. }
  74. private void sendScreenChangedEventAfterInitialPush() {
  75. if (screenParams.topTabParams != null) {
  76. EventBus.instance.post(new ScreenChangedEvent(screenParams.topTabParams.get(0)));
  77. } else {
  78. EventBus.instance.post(new ScreenChangedEvent(screenParams));
  79. }
  80. }
  81. private void createFabAndSnackbarContainer() {
  82. snackbarAndFabContainer = new SnackbarAndFabContainer(getContext(), this);
  83. RelativeLayout.LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
  84. lp.addRule(ALIGN_PARENT_BOTTOM);
  85. snackbarAndFabContainer.setLayoutParams(lp);
  86. getScreenStackParent().addView(snackbarAndFabContainer);
  87. }
  88. @Override
  89. public boolean onBackPressed() {
  90. if (stack.handleBackPressInJs()) {
  91. return true;
  92. }
  93. if (stack.canPop()) {
  94. stack.pop(true);
  95. EventBus.instance.post(new ScreenChangedEvent(stack.peek().getScreenParams()));
  96. return true;
  97. } else {
  98. return false;
  99. }
  100. }
  101. @Override
  102. public void destroy() {
  103. stack.destroy();
  104. snackbarAndFabContainer.destroy();
  105. if (sideMenu != null) {
  106. sideMenu.destroy();
  107. }
  108. }
  109. @Override
  110. public void push(ScreenParams params) {
  111. LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
  112. stack.push(params, lp);
  113. EventBus.instance.post(new ScreenChangedEvent(params));
  114. }
  115. @Override
  116. public void pop(ScreenParams params) {
  117. stack.pop(params.animateScreenTransitions);
  118. EventBus.instance.post(new ScreenChangedEvent(stack.peek().getScreenParams()));
  119. }
  120. @Override
  121. public void popToRoot(ScreenParams params) {
  122. stack.popToRoot(params.animateScreenTransitions);
  123. EventBus.instance.post(new ScreenChangedEvent(stack.peek().getScreenParams()));
  124. }
  125. @Override
  126. public void newStack(ScreenParams params) {
  127. removeView(stack.peek());
  128. stack.destroy();
  129. ScreenStack newStack = new ScreenStack(activity, getScreenStackParent(), params.getNavigatorId(), this);
  130. LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
  131. newStack.pushInitialScreenWithAnimation(params, lp);
  132. stack = newStack;
  133. EventBus.instance.post(new ScreenChangedEvent(params));
  134. }
  135. @Override
  136. public void setTopBarVisible(String screenInstanceID, boolean visible, boolean animate) {
  137. stack.setScreenTopBarVisible(screenInstanceID, visible, animate);
  138. }
  139. @Override
  140. public void setTitleBarTitle(String screenInstanceId, String title) {
  141. stack.setScreenTitleBarTitle(screenInstanceId, title);
  142. }
  143. @Override
  144. public void setTitleBarSubtitle(String screenInstanceId, String subtitle) {
  145. stack.setScreenTitleBarSubtitle(screenInstanceId, subtitle);
  146. }
  147. @Override
  148. public View asView() {
  149. return this;
  150. }
  151. @Override
  152. public void setTitleBarRightButtons(String screenInstanceId, String navigatorEventId,
  153. List<TitleBarButtonParams> titleBarRightButtons) {
  154. stack.setScreenTitleBarRightButtons(screenInstanceId, navigatorEventId, titleBarRightButtons);
  155. }
  156. @Override
  157. public void setTitleBarLeftButton(String screenInstanceId, String navigatorEventId, TitleBarLeftButtonParams titleBarLeftButtonParams) {
  158. stack.setScreenTitleBarLeftButton(screenInstanceId, navigatorEventId, titleBarLeftButtonParams);
  159. }
  160. @Override
  161. public void setFab(String screenInstanceId, String navigatorEventId, FabParams fabParams) {
  162. stack.setFab(screenInstanceId, navigatorEventId, fabParams);
  163. }
  164. @Override
  165. public void toggleSideMenuVisible(boolean animated, Side side) {
  166. if (sideMenu != null) {
  167. sideMenu.toggleVisible(animated, side);
  168. }
  169. }
  170. @Override
  171. public void setSideMenuVisible(boolean animated, boolean visible, Side side) {
  172. if (sideMenu != null) {
  173. sideMenu.setVisible(visible, animated, side);
  174. }
  175. }
  176. @Override
  177. public void showSnackbar(SnackbarParams params) {
  178. final String navigatorEventId = stack.peek().getNavigatorEventId();
  179. snackbarAndFabContainer.showSnackbar(navigatorEventId, params);
  180. }
  181. @Override
  182. public void onModalDismissed() {
  183. EventBus.instance.post(new ScreenChangedEvent(stack.peek().getScreenParams()));
  184. }
  185. @Override
  186. public boolean containsNavigator(String navigatorId) {
  187. return stack.getNavigatorId().equals(navigatorId);
  188. }
  189. @Override
  190. public void showContextualMenu(String screenInstanceId, ContextualMenuParams params, Callback onButtonClicked) {
  191. stack.showContextualMenu(screenInstanceId, params, onButtonClicked);
  192. }
  193. @Override
  194. public void dismissContextualMenu(String screenInstanceId) {
  195. stack.dismissContextualMenu(screenInstanceId);
  196. }
  197. @Override
  198. public Screen getCurrentScreen() {
  199. return stack.peek();
  200. }
  201. @Override
  202. public boolean onTitleBarBackButtonClick() {
  203. if (leftButtonOnClickListener != null) {
  204. return leftButtonOnClickListener.onTitleBarBackButtonClick();
  205. }
  206. return onBackPressed();
  207. }
  208. @Override
  209. public void onSideMenuButtonClick() {
  210. final String navigatorEventId = stack.peek().getNavigatorEventId();
  211. NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("sideMenu", navigatorEventId);
  212. if (sideMenu != null) {
  213. sideMenu.openDrawer(Side.Left);
  214. }
  215. }
  216. }