react-native-navigation的迁移库

Screen.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*eslint-disable*/
  2. import React, {Component} from 'react';
  3. import {
  4. NativeAppEventEmitter,
  5. DeviceEventEmitter,
  6. Platform
  7. } from 'react-native';
  8. import platformSpecific from './deprecated/platformSpecificDeprecated';
  9. import Navigation from './Navigation';
  10. const _allNavigatorEventHandlers = {};
  11. const NavigationSpecific = {
  12. push: platformSpecific.navigatorPush,
  13. pop: platformSpecific.navigatorPop,
  14. popToRoot: platformSpecific.navigatorPopToRoot,
  15. resetTo: platformSpecific.navigatorResetTo
  16. };
  17. class Navigator {
  18. constructor(navigatorID, navigatorEventID, screenInstanceID) {
  19. this.navigatorID = navigatorID;
  20. this.screenInstanceID = screenInstanceID;
  21. this.navigatorEventID = navigatorEventID;
  22. this.navigatorEventHandler = null;
  23. this.navigatorEventSubscription = null;
  24. }
  25. push(params = {}) {
  26. return NavigationSpecific.push(this, params);
  27. }
  28. pop(params = {}) {
  29. return NavigationSpecific.pop(this, params);
  30. }
  31. popToRoot(params = {}) {
  32. return NavigationSpecific.popToRoot(this, params);
  33. }
  34. resetTo(params = {}) {
  35. return NavigationSpecific.resetTo(this, params);
  36. }
  37. showModal(params = {}) {
  38. return Navigation.showModal(params);
  39. }
  40. dismissModal(params = {}) {
  41. return Navigation.dismissModal(params);
  42. }
  43. dismissAllModals(params = {}) {
  44. return Navigation.dismissAllModals(params);
  45. }
  46. showLightBox(params = {}) {
  47. return Navigation.showLightBox(params);
  48. }
  49. dismissLightBox(params = {}) {
  50. return Navigation.dismissLightBox(params);
  51. }
  52. showInAppNotification(params = {}) {
  53. return Navigation.showInAppNotification(params);
  54. }
  55. dismissInAppNotification(params = {}) {
  56. return Navigation.dismissInAppNotification(params);
  57. }
  58. setButtons(params = {}) {
  59. return platformSpecific.navigatorSetButtons(this, this.navigatorEventID, params);
  60. }
  61. setTitle(params = {}) {
  62. return platformSpecific.navigatorSetTitle(this, params);
  63. }
  64. setSubTitle(params = {}) {
  65. return platformSpecific.navigatorSetSubtitle(this, params);
  66. }
  67. setTitleImage(params = {}) {
  68. return platformSpecific.navigatorSetTitleImage(this, params);
  69. }
  70. toggleDrawer(params = {}) {
  71. return platformSpecific.navigatorToggleDrawer(this, params);
  72. }
  73. toggleTabs(params = {}) {
  74. return platformSpecific.navigatorToggleTabs(this, params);
  75. }
  76. toggleNavBar(params = {}) {
  77. return platformSpecific.navigatorToggleNavBar(this, params);
  78. }
  79. setTabBadge(params = {}) {
  80. return platformSpecific.navigatorSetTabBadge(this, params);
  81. }
  82. switchToTab(params = {}) {
  83. return platformSpecific.navigatorSwitchToTab(this, params);
  84. }
  85. showSnackbar(params = {}) {
  86. return platformSpecific.showSnackbar(this, params);
  87. }
  88. setOnNavigatorEvent(callback) {
  89. this.navigatorEventHandler = callback;
  90. if (!this.navigatorEventSubscription) {
  91. let Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
  92. this.navigatorEventSubscription = Emitter.addListener(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
  93. _allNavigatorEventHandlers[this.navigatorEventID] = (event) => this.onNavigatorEvent(event);
  94. }
  95. }
  96. handleDeepLink(params = {}) {
  97. if (!params.link) return;
  98. const event = {
  99. type: 'DeepLink',
  100. link: params.link
  101. };
  102. for (let i in _allNavigatorEventHandlers) {
  103. _allNavigatorEventHandlers[i](event);
  104. }
  105. }
  106. onNavigatorEvent(event) {
  107. if (this.navigatorEventHandler) {
  108. this.navigatorEventHandler(event);
  109. }
  110. }
  111. cleanup() {
  112. if (this.navigatorEventSubscription) {
  113. this.navigatorEventSubscription.remove();
  114. delete _allNavigatorEventHandlers[this.navigatorEventID];
  115. }
  116. }
  117. }
  118. export default class Screen extends Component {
  119. static navigatorStyle = {};
  120. static navigatorButtons = {};
  121. constructor(props) {
  122. super(props);
  123. if (props.navigatorID) {
  124. this.navigator = new Navigator(props.navigatorID, props.navigatorEventID, props.screenInstanceID);
  125. }
  126. }
  127. componentWillUnmount() {
  128. if (this.navigator) {
  129. this.navigator.cleanup();
  130. this.navigator = undefined;
  131. }
  132. }
  133. }