react-native-navigation的迁移库

NavigationCommandsHandler.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package com.reactnativenavigation.controllers;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import com.facebook.react.bridge.Callback;
  5. import com.reactnativenavigation.NavigationApplication;
  6. import com.reactnativenavigation.params.ActivityParams;
  7. import com.reactnativenavigation.params.ContextualMenuParams;
  8. import com.reactnativenavigation.params.FabParams;
  9. import com.reactnativenavigation.params.ScreenParams;
  10. import com.reactnativenavigation.params.SnackbarParams;
  11. import com.reactnativenavigation.params.TitleBarButtonParams;
  12. import com.reactnativenavigation.params.TitleBarLeftButtonParams;
  13. import com.reactnativenavigation.params.parsers.ActivityParamsParser;
  14. import com.reactnativenavigation.params.parsers.ScreenParamsParser;
  15. import com.reactnativenavigation.views.SideMenu.Side;
  16. import java.util.List;
  17. public class NavigationCommandsHandler {
  18. private static final String ACTIVITY_PARAMS_BUNDLE = "ACTIVITY_PARAMS_BUNDLE";
  19. static ActivityParams parseActivityParams(Intent intent) {
  20. return ActivityParamsParser.parse(intent.getBundleExtra(NavigationCommandsHandler.ACTIVITY_PARAMS_BUNDLE));
  21. }
  22. /**
  23. * start a new activity with CLEAR_TASK | NEW_TASK
  24. *
  25. * @param params ActivityParams as bundle
  26. */
  27. public static void startApp(Bundle params, boolean portraitOnlyMode, boolean landscapeOnlyMode) {
  28. Intent intent;
  29. if (portraitOnlyMode) {
  30. intent = new Intent(NavigationApplication.instance, PortraitNavigationActivity.class);
  31. } else if (landscapeOnlyMode) {
  32. intent = new Intent(NavigationApplication.instance, LandscapeNavigationActivity.class);
  33. } else {
  34. intent = new Intent(NavigationApplication.instance, NavigationActivity.class);
  35. }
  36. IntentDataHandler.setIntentData(intent);
  37. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
  38. intent.putExtra(ACTIVITY_PARAMS_BUNDLE, params);
  39. NavigationApplication.instance.startActivity(intent);
  40. }
  41. public static void push(Bundle screenParams) {
  42. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  43. if (currentActivity == null) {
  44. return;
  45. }
  46. final ScreenParams params = ScreenParamsParser.parse(screenParams);
  47. NavigationApplication.instance.runOnMainThread(new Runnable() {
  48. @Override
  49. public void run() {
  50. currentActivity.push(params);
  51. }
  52. });
  53. }
  54. public static void pop(Bundle screenParams) {
  55. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  56. if (currentActivity == null) {
  57. return;
  58. }
  59. final ScreenParams params = ScreenParamsParser.parse(screenParams);
  60. NavigationApplication.instance.runOnMainThread(new Runnable() {
  61. @Override
  62. public void run() {
  63. currentActivity.pop(params);
  64. }
  65. });
  66. }
  67. public static void popToRoot(Bundle screenParams) {
  68. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  69. if (currentActivity == null) {
  70. return;
  71. }
  72. final ScreenParams params = ScreenParamsParser.parse(screenParams);
  73. NavigationApplication.instance.runOnMainThread(new Runnable() {
  74. @Override
  75. public void run() {
  76. currentActivity.popToRoot(params);
  77. }
  78. });
  79. }
  80. public static void newStack(Bundle screenParams) {
  81. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  82. if (currentActivity == null) {
  83. return;
  84. }
  85. final ScreenParams params = ScreenParamsParser.parse(screenParams);
  86. NavigationApplication.instance.runOnMainThread(new Runnable() {
  87. @Override
  88. public void run() {
  89. currentActivity.newStack(params);
  90. }
  91. });
  92. }
  93. public static void setTopBarVisible(final String screenInstanceID, final boolean hidden, final boolean animated) {
  94. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  95. if (currentActivity == null) {
  96. return;
  97. }
  98. NavigationApplication.instance.runOnMainThread(new Runnable() {
  99. @Override
  100. public void run() {
  101. currentActivity.setTopBarVisible(screenInstanceID, hidden, animated);
  102. }
  103. });
  104. }
  105. public static void setBottomTabsVisible(final boolean hidden, final boolean animated) {
  106. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  107. if (currentActivity == null) {
  108. return;
  109. }
  110. NavigationApplication.instance.runOnMainThread(new Runnable() {
  111. @Override
  112. public void run() {
  113. currentActivity.setBottomTabsVisible(hidden, animated);
  114. }
  115. });
  116. }
  117. public static void setScreenTitleBarTitle(final String screenInstanceId, final String title) {
  118. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  119. if (currentActivity == null) {
  120. return;
  121. }
  122. NavigationApplication.instance.runOnMainThread(new Runnable() {
  123. @Override
  124. public void run() {
  125. currentActivity.setTitleBarTitle(screenInstanceId, title);
  126. }
  127. });
  128. }
  129. public static void setScreenTitleBarSubtitle(final String screenInstanceId, final String subtitle) {
  130. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  131. if (currentActivity == null) {
  132. return;
  133. }
  134. NavigationApplication.instance.runOnMainThread(new Runnable() {
  135. @Override
  136. public void run() {
  137. currentActivity.setTitleBarSubtitle(screenInstanceId, subtitle);
  138. }
  139. });
  140. }
  141. public static void showModal(Bundle params) {
  142. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  143. if (currentActivity == null) {
  144. return;
  145. }
  146. final ScreenParams screenParams = ScreenParamsParser.parse(params);
  147. NavigationApplication.instance.runOnMainThread(new Runnable() {
  148. @Override
  149. public void run() {
  150. currentActivity.showModal(screenParams);
  151. }
  152. });
  153. }
  154. public static void setScreenTitleBarRightButtons(final String screenInstanceId,
  155. final String navigatorEventId,
  156. final List<TitleBarButtonParams> titleBarButtons) {
  157. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  158. if (currentActivity == null) {
  159. return;
  160. }
  161. NavigationApplication.instance.runOnMainThread(new Runnable() {
  162. @Override
  163. public void run() {
  164. currentActivity.setTitleBarButtons(screenInstanceId, navigatorEventId, titleBarButtons);
  165. }
  166. });
  167. }
  168. public static void setScreenTitleBarLeftButtons(final String screenInstanceId,
  169. final String navigatorEventId,
  170. final TitleBarLeftButtonParams titleBarButtons) {
  171. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  172. if (currentActivity == null) {
  173. return;
  174. }
  175. NavigationApplication.instance.runOnMainThread(new Runnable() {
  176. @Override
  177. public void run() {
  178. currentActivity.setTitleBarLeftButton(screenInstanceId, navigatorEventId, titleBarButtons);
  179. }
  180. });
  181. }
  182. public static void setScreenFab(final String screenInstanceId, final String navigatorEventId, final FabParams fab) {
  183. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  184. if (currentActivity == null) {
  185. return;
  186. }
  187. NavigationApplication.instance.runOnMainThread(new Runnable() {
  188. @Override
  189. public void run() {
  190. currentActivity.setScreenFab(screenInstanceId, navigatorEventId, fab);
  191. }
  192. });
  193. }
  194. public static void dismissTopModal() {
  195. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  196. if (currentActivity == null) {
  197. return;
  198. }
  199. NavigationApplication.instance.runOnMainThread(new Runnable() {
  200. @Override
  201. public void run() {
  202. currentActivity.dismissTopModal();
  203. }
  204. });
  205. }
  206. public static void dismissAllModals() {
  207. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  208. if (currentActivity == null) {
  209. return;
  210. }
  211. NavigationApplication.instance.runOnMainThread(new Runnable() {
  212. @Override
  213. public void run() {
  214. currentActivity.dismissAllModals();
  215. }
  216. });
  217. }
  218. public static void toggleSideMenuVisible(final boolean animated, final Side side) {
  219. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  220. if (currentActivity == null) {
  221. return;
  222. }
  223. NavigationApplication.instance.runOnMainThread(new Runnable() {
  224. @Override
  225. public void run() {
  226. currentActivity.toggleSideMenuVisible(animated, side);
  227. }
  228. });
  229. }
  230. public static void setSideMenuVisible(final boolean animated, final boolean visible, final Side side) {
  231. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  232. if (currentActivity == null) {
  233. return;
  234. }
  235. NavigationApplication.instance.runOnMainThread(new Runnable() {
  236. @Override
  237. public void run() {
  238. currentActivity.setSideMenuVisible(animated, visible, side);
  239. }
  240. });
  241. }
  242. public static void selectBottomTabByTabIndex(final Integer index) {
  243. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  244. if (currentActivity == null) {
  245. return;
  246. }
  247. NavigationApplication.instance.runOnMainThread(new Runnable() {
  248. @Override
  249. public void run() {
  250. currentActivity.selectBottomTabByTabIndex(index);
  251. }
  252. });
  253. }
  254. public static void selectBottomTabByNavigatorId(final String navigatorId) {
  255. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  256. if (currentActivity == null) {
  257. return;
  258. }
  259. NavigationApplication.instance.runOnMainThread(new Runnable() {
  260. @Override
  261. public void run() {
  262. currentActivity.selectBottomTabByNavigatorId(navigatorId);
  263. }
  264. });
  265. }
  266. public static void setBottomTabBadgeByIndex(final Integer index, final String badge) {
  267. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  268. if (currentActivity == null) {
  269. return;
  270. }
  271. NavigationApplication.instance.runOnMainThread(new Runnable() {
  272. @Override
  273. public void run() {
  274. currentActivity.setBottomTabBadgeByIndex(index, badge);
  275. }
  276. });
  277. }
  278. public static void setBottomTabBadgeByNavigatorId(final String navigatorId, final String badge) {
  279. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  280. if (currentActivity == null) {
  281. return;
  282. }
  283. NavigationApplication.instance.runOnMainThread(new Runnable() {
  284. @Override
  285. public void run() {
  286. currentActivity.setBottomTabBadgeByNavigatorId(navigatorId, badge);
  287. }
  288. });
  289. }
  290. public static void showSnackbar(final SnackbarParams params) {
  291. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  292. if (currentActivity == null) {
  293. return;
  294. }
  295. NavigationApplication.instance.runOnMainThread(new Runnable() {
  296. @Override
  297. public void run() {
  298. currentActivity.showSnackbar(params);
  299. }
  300. });
  301. }
  302. public static void showContextualMenu(final String screenInstanceId, final ContextualMenuParams params, final Callback onButtonClicked) {
  303. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  304. if (currentActivity == null) {
  305. return;
  306. }
  307. NavigationApplication.instance.runOnMainThread(new Runnable() {
  308. @Override
  309. public void run() {
  310. currentActivity.showContextualMenu(screenInstanceId, params, onButtonClicked);
  311. }
  312. });
  313. }
  314. public static void dismissContextualMenu(final String screenInstanceId) {
  315. final NavigationActivity currentActivity = NavigationActivity.currentActivity;
  316. if (currentActivity == null) {
  317. return;
  318. }
  319. NavigationApplication.instance.runOnMainThread(new Runnable() {
  320. @Override
  321. public void run() {
  322. currentActivity.dismissContextualMenu(screenInstanceId);
  323. }
  324. });
  325. }
  326. }