react-native-navigation的迁移库

Screen.js 3.8KB

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