react-native-navigation的迁移库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { NativeCommandsSender } from './adapters/NativeCommandsSender';
  2. import { NativeEventsReceiver } from './adapters/NativeEventsReceiver';
  3. import { UniqueIdProvider } from './adapters/UniqueIdProvider';
  4. import { Store } from './components/Store';
  5. import { ComponentRegistry } from './components/ComponentRegistry';
  6. import { Commands } from './commands/Commands';
  7. import { LayoutTreeParser } from './commands/LayoutTreeParser';
  8. import { LayoutTreeCrawler } from './commands/LayoutTreeCrawler';
  9. import { EventsRegistry } from './events/EventsRegistry';
  10. import { ComponentProvider } from 'react-native';
  11. import { Element } from './adapters/Element';
  12. import { CommandsObserver } from './events/CommandsObserver';
  13. import { Constants } from './adapters/Constants';
  14. import { ComponentType } from 'react';
  15. import { ComponentEventsObserver } from './events/ComponentEventsObserver';
  16. import { TouchablePreview } from './adapters/TouchablePreview';
  17. import { LayoutRoot, Layout } from './interfaces/Layout';
  18. import { Options } from './interfaces/Options';
  19. export class Navigation {
  20. public readonly Element: React.ComponentType<{ elementId: any; resizeMode?: any; }>;
  21. public readonly TouchablePreview: React.ComponentType<any>;
  22. public readonly store: Store;
  23. private readonly nativeEventsReceiver: NativeEventsReceiver;
  24. private readonly uniqueIdProvider: UniqueIdProvider;
  25. private readonly componentRegistry: ComponentRegistry;
  26. private readonly layoutTreeParser: LayoutTreeParser;
  27. private readonly layoutTreeCrawler: LayoutTreeCrawler;
  28. private readonly nativeCommandsSender: NativeCommandsSender;
  29. private readonly commands: Commands;
  30. private readonly eventsRegistry: EventsRegistry;
  31. private readonly commandsObserver: CommandsObserver;
  32. private readonly componentEventsObserver: ComponentEventsObserver;
  33. constructor() {
  34. this.Element = Element;
  35. this.TouchablePreview = TouchablePreview;
  36. this.store = new Store();
  37. this.nativeEventsReceiver = new NativeEventsReceiver();
  38. this.uniqueIdProvider = new UniqueIdProvider();
  39. this.componentEventsObserver = new ComponentEventsObserver(this.nativeEventsReceiver);
  40. this.componentRegistry = new ComponentRegistry(this.store, this.componentEventsObserver);
  41. this.layoutTreeParser = new LayoutTreeParser();
  42. this.layoutTreeCrawler = new LayoutTreeCrawler(this.uniqueIdProvider, this.store);
  43. this.nativeCommandsSender = new NativeCommandsSender();
  44. this.commandsObserver = new CommandsObserver();
  45. this.commands = new Commands(this.nativeCommandsSender, this.layoutTreeParser, this.layoutTreeCrawler, this.commandsObserver, this.uniqueIdProvider);
  46. this.eventsRegistry = new EventsRegistry(this.nativeEventsReceiver, this.commandsObserver, this.componentEventsObserver);
  47. this.componentEventsObserver.registerOnceForAllComponentEvents();
  48. }
  49. /**
  50. * Every navigation component in your app must be registered with a unique name.
  51. * The component itself is a traditional React component extending React.Component.
  52. */
  53. public registerComponent(componentName: string, getComponentClassFunc: ComponentProvider): ComponentType<any> {
  54. return this.componentRegistry.registerComponent(componentName, getComponentClassFunc);
  55. }
  56. /**
  57. * Utility helper function like registerComponent,
  58. * wraps the provided component with a react-redux Provider with the passed redux store
  59. */
  60. public registerComponentWithRedux(
  61. componentName: string,
  62. getComponentClassFunc: ComponentProvider,
  63. ReduxProvider: any,
  64. reduxStore: any
  65. ): ComponentType<any> {
  66. return this.componentRegistry.registerComponent(componentName, getComponentClassFunc, ReduxProvider, reduxStore);
  67. }
  68. /**
  69. * Reset the app to a new layout
  70. */
  71. public setRoot(layout: LayoutRoot): Promise<any> {
  72. return this.commands.setRoot(layout);
  73. }
  74. /**
  75. * Set default options to all screens. Useful for declaring a consistent style across the app.
  76. */
  77. public setDefaultOptions(options: Options): void {
  78. this.commands.setDefaultOptions(options);
  79. }
  80. /**
  81. * Change a component's navigation options
  82. */
  83. public mergeOptions(componentId: string, options: Options): void {
  84. this.commands.mergeOptions(componentId, options);
  85. }
  86. /**
  87. * Show a screen as a modal.
  88. */
  89. public showModal(layout: Layout): Promise<any> {
  90. return this.commands.showModal(layout);
  91. }
  92. /**
  93. * Dismiss a modal by componentId. The dismissed modal can be anywhere in the stack.
  94. */
  95. public dismissModal(componentId: string, mergeOptions?: Options): Promise<any> {
  96. return this.commands.dismissModal(componentId, mergeOptions);
  97. }
  98. /**
  99. * Dismiss all Modals
  100. */
  101. public dismissAllModals(mergeOptions?: Options): Promise<any> {
  102. return this.commands.dismissAllModals(mergeOptions);
  103. }
  104. /**
  105. * Push a new layout into this screen's navigation stack.
  106. */
  107. public push<P>(componentId: string, layout: Layout<P>): Promise<any> {
  108. return this.commands.push(componentId, layout);
  109. }
  110. /**
  111. * Pop a component from the stack, regardless of it's position.
  112. */
  113. public pop(componentId: string, mergeOptions?: Options): Promise<any> {
  114. return this.commands.pop(componentId, mergeOptions);
  115. }
  116. /**
  117. * Pop the stack to a given component
  118. */
  119. public popTo(componentId: string, mergeOptions?: Options): Promise<any> {
  120. return this.commands.popTo(componentId, mergeOptions);
  121. }
  122. /**
  123. * Pop the component's stack to root.
  124. */
  125. public popToRoot(componentId: string, mergeOptions?: Options): Promise<any> {
  126. return this.commands.popToRoot(componentId, mergeOptions);
  127. }
  128. /**
  129. * Sets new root component to stack.
  130. */
  131. public setStackRoot(componentId: string, layout: Layout): Promise<any> {
  132. return this.commands.setStackRoot(componentId, layout);
  133. }
  134. /**
  135. * Show overlay on top of the entire app
  136. */
  137. public showOverlay(layout: Layout): Promise<any> {
  138. return this.commands.showOverlay(layout);
  139. }
  140. /**
  141. * dismiss overlay by componentId
  142. */
  143. public dismissOverlay(componentId: string): Promise<any> {
  144. return this.commands.dismissOverlay(componentId);
  145. }
  146. /**
  147. * Resolves arguments passed on launch
  148. */
  149. public getLaunchArgs(): Promise<any> {
  150. return this.commands.getLaunchArgs();
  151. }
  152. /**
  153. * Obtain the events registry instance
  154. */
  155. public events(): EventsRegistry {
  156. return this.eventsRegistry;
  157. }
  158. /**
  159. * Constants coming from native
  160. */
  161. public async constants(): Promise<any> {
  162. return await Constants.get();
  163. }
  164. }