react-native-navigation的迁移库

NavigationReactModule.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package com.reactnativenavigation.bridge;
  2. import com.facebook.react.bridge.Callback;
  3. import com.facebook.react.bridge.ReactApplicationContext;
  4. import com.facebook.react.bridge.ReactContextBaseJavaModule;
  5. import com.facebook.react.bridge.ReactMethod;
  6. import com.facebook.react.bridge.ReadableArray;
  7. import com.facebook.react.bridge.ReadableMap;
  8. import com.reactnativenavigation.controllers.NavigationCommandsHandler;
  9. import com.reactnativenavigation.params.ContextualMenuParams;
  10. import com.reactnativenavigation.params.FabParams;
  11. import com.reactnativenavigation.params.SnackbarParams;
  12. import com.reactnativenavigation.params.TitleBarButtonParams;
  13. import com.reactnativenavigation.params.TitleBarLeftButtonParams;
  14. import com.reactnativenavigation.params.parsers.ContextualMenuParamsParser;
  15. import com.reactnativenavigation.params.parsers.FabParamsParser;
  16. import com.reactnativenavigation.params.parsers.SnackbarParamsParser;
  17. import com.reactnativenavigation.params.parsers.TitleBarButtonParamsParser;
  18. import com.reactnativenavigation.params.parsers.TitleBarLeftButtonParamsParser;
  19. import com.reactnativenavigation.views.SideMenu.Side;
  20. import java.util.List;
  21. /**
  22. * The basic abstract components we will expose:
  23. * BottomTabs (app) - boolean
  24. * TopBar (per screen)
  25. * - TitleBar
  26. * - - RightButtons
  27. * - - LeftButton
  28. * - TopTabs (segmented control / view pager tabs)
  29. * DeviceStatusBar (app) (colors are per screen)
  30. * AndroidNavigationBar (app) (colors are per screen)
  31. * SideMenu (app) - boolean, (menu icon is screen-based)
  32. */
  33. public class NavigationReactModule extends ReactContextBaseJavaModule {
  34. public static final String NAME = "NavigationReactModule";
  35. public NavigationReactModule(ReactApplicationContext reactContext) {
  36. super(reactContext);
  37. }
  38. @Override
  39. public String getName() {
  40. return NAME;
  41. }
  42. @ReactMethod
  43. public void startApp(final ReadableMap params) {
  44. boolean portraitOnlyMode = false;
  45. boolean landscapeOnlyMode = false;
  46. if (params.hasKey("portraitOnlyMode")) {
  47. portraitOnlyMode = params.getBoolean("portraitOnlyMode");
  48. }
  49. if (params.hasKey(("landscapeOnlyMode"))) {
  50. landscapeOnlyMode = params.getBoolean("landscapeOnlyMode");
  51. }
  52. NavigationCommandsHandler.startApp(BundleConverter.toBundle(params), portraitOnlyMode, landscapeOnlyMode);
  53. }
  54. @ReactMethod
  55. public void setScreenTitleBarTitle(String screenInstanceId, String title) {
  56. NavigationCommandsHandler.setScreenTitleBarTitle(screenInstanceId, title);
  57. }
  58. @ReactMethod
  59. public void setScreenTitleBarSubtitle(String screenInstanceId, String subtitle) {
  60. NavigationCommandsHandler.setScreenTitleBarSubtitle(screenInstanceId, subtitle);
  61. }
  62. @ReactMethod
  63. public void setScreenButtons(String screenInstanceId, String navigatorEventId,
  64. ReadableArray rightButtonsParams, ReadableMap leftButtonParams, ReadableMap fab) {
  65. if (rightButtonsParams != null) {
  66. setScreenTitleBarRightButtons(screenInstanceId, navigatorEventId, rightButtonsParams);
  67. }
  68. if (leftButtonParams != null) {
  69. setScreenTitleBarLeftButton(screenInstanceId, navigatorEventId, leftButtonParams);
  70. }
  71. if (fab != null) {
  72. setScreenFab(screenInstanceId, navigatorEventId, fab);
  73. }
  74. }
  75. private void setScreenTitleBarRightButtons(String screenInstanceId, String navigatorEventId, ReadableArray rightButtonsParams) {
  76. List<TitleBarButtonParams> rightButtons = new TitleBarButtonParamsParser()
  77. .parseButtons(BundleConverter.toBundle(rightButtonsParams));
  78. NavigationCommandsHandler.setScreenTitleBarRightButtons(screenInstanceId, navigatorEventId, rightButtons);
  79. }
  80. private void setScreenTitleBarLeftButton(String screenInstanceId, String navigatorEventId, ReadableMap leftButtonParams) {
  81. TitleBarLeftButtonParams leftButton = new TitleBarLeftButtonParamsParser()
  82. .parseSingleButton(BundleConverter.toBundle(leftButtonParams));
  83. NavigationCommandsHandler.setScreenTitleBarLeftButtons(screenInstanceId, navigatorEventId, leftButton);
  84. }
  85. private void setScreenFab(String screenInstanceId, String navigatorEventId, ReadableMap fab) {
  86. FabParams fabParams = new FabParamsParser().parse(BundleConverter.toBundle(fab), navigatorEventId, screenInstanceId);
  87. NavigationCommandsHandler.setScreenFab(screenInstanceId, navigatorEventId, fabParams);
  88. }
  89. @ReactMethod
  90. public void setBottomTabBadgeByIndex(Integer index, String badge) {
  91. NavigationCommandsHandler.setBottomTabBadgeByIndex(index, badge);
  92. }
  93. @ReactMethod
  94. public void setBottomTabBadgeByNavigatorId(String navigatorId, String badge) {
  95. NavigationCommandsHandler.setBottomTabBadgeByNavigatorId(navigatorId, badge);
  96. }
  97. @ReactMethod
  98. public void selectBottomTabByTabIndex(Integer index) {
  99. NavigationCommandsHandler.selectBottomTabByTabIndex(index);
  100. }
  101. @ReactMethod
  102. public void selectBottomTabByNavigatorId(String navigatorId) {
  103. NavigationCommandsHandler.selectBottomTabByNavigatorId(navigatorId);
  104. }
  105. @ReactMethod
  106. public void toggleSideMenuVisible(boolean animated, String side) {
  107. NavigationCommandsHandler.toggleSideMenuVisible(animated, Side.fromString(side));
  108. }
  109. @ReactMethod
  110. public void setSideMenuVisible(boolean animated, boolean visible, String side) {
  111. NavigationCommandsHandler.setSideMenuVisible(animated, visible, Side.fromString(side));
  112. }
  113. @ReactMethod
  114. public void toggleTopBarVisible(final ReadableMap params) {
  115. }
  116. @ReactMethod
  117. public void setTopBarVisible(String screenInstanceId, boolean hidden, boolean animated) {
  118. NavigationCommandsHandler.setTopBarVisible(screenInstanceId, hidden, animated);
  119. }
  120. @ReactMethod
  121. public void toggleBottomTabsVisible(final ReadableMap params) {
  122. }
  123. @ReactMethod
  124. public void setBottomTabsVisible(boolean hidden, boolean animated) {
  125. NavigationCommandsHandler.setBottomTabsVisible(hidden, animated);
  126. }
  127. @ReactMethod
  128. public void push(final ReadableMap params) {
  129. NavigationCommandsHandler.push(BundleConverter.toBundle(params));
  130. }
  131. @ReactMethod
  132. public void pop(final ReadableMap params) {
  133. NavigationCommandsHandler.pop(BundleConverter.toBundle(params));
  134. }
  135. @ReactMethod
  136. public void popToRoot(final ReadableMap params) {
  137. NavigationCommandsHandler.popToRoot(BundleConverter.toBundle(params));
  138. }
  139. @ReactMethod
  140. public void newStack(final ReadableMap params) {
  141. NavigationCommandsHandler.newStack(BundleConverter.toBundle(params));
  142. }
  143. @ReactMethod
  144. public void showModal(final ReadableMap params) {
  145. NavigationCommandsHandler.showModal(BundleConverter.toBundle(params));
  146. }
  147. @ReactMethod
  148. public void dismissAllModals() {
  149. NavigationCommandsHandler.dismissAllModals();
  150. }
  151. @ReactMethod
  152. public void dismissTopModal() {
  153. NavigationCommandsHandler.dismissTopModal();
  154. }
  155. @ReactMethod
  156. public void showSnackbar(final ReadableMap params) {
  157. SnackbarParams snackbarParams = new SnackbarParamsParser().parse(BundleConverter.toBundle(params));
  158. NavigationCommandsHandler.showSnackbar(snackbarParams);
  159. }
  160. @ReactMethod
  161. public void showContextualMenu(final String screenInstanceId, final ReadableMap params, final Callback onButtonClicked) {
  162. ContextualMenuParams contextualMenuParams =
  163. new ContextualMenuParamsParser().parse(BundleConverter.toBundle(params));
  164. NavigationCommandsHandler.showContextualMenu(screenInstanceId, contextualMenuParams, onButtonClicked);
  165. }
  166. @ReactMethod
  167. public void dismissContextualMenu(String screenInstanceId) {
  168. NavigationCommandsHandler.dismissContextualMenu(screenInstanceId);
  169. }
  170. }