react-native-navigation的迁移库

Navigation.ts 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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(componentName: string, getComponentClassFunc: ComponentProvider, ReduxProvider: any, reduxStore: any): ComponentType<any> {
  61. return this.componentRegistry.registerComponent(componentName, getComponentClassFunc, ReduxProvider, reduxStore);
  62. }
  63. /**
  64. * Reset the app to a new layout
  65. */
  66. public setRoot(layout: LayoutRoot): Promise<any> {
  67. return this.commands.setRoot(layout);
  68. }
  69. /**
  70. * Set default options to all screens. Useful for declaring a consistent style across the app.
  71. */
  72. public setDefaultOptions(options: Options): void {
  73. this.commands.setDefaultOptions(options);
  74. }
  75. /**
  76. * Change a component's navigation options
  77. */
  78. public mergeOptions(componentId: string, options: Options): void {
  79. this.commands.mergeOptions(componentId, options);
  80. }
  81. /**
  82. * Show a screen as a modal.
  83. */
  84. public showModal(layout: Layout): Promise<any> {
  85. return this.commands.showModal(layout);
  86. }
  87. /**
  88. * Dismiss a modal by componentId. The dismissed modal can be anywhere in the stack.
  89. */
  90. public dismissModal(componentId: string, mergeOptions?): Promise<any> {
  91. return this.commands.dismissModal(componentId, mergeOptions);
  92. }
  93. /**
  94. * Dismiss all Modals
  95. */
  96. public dismissAllModals(mergeOptions?): Promise<any> {
  97. return this.commands.dismissAllModals(mergeOptions);
  98. }
  99. /**
  100. * Push a new layout into this screen's navigation stack.
  101. */
  102. public push(componentId: string, layout: Layout): Promise<any> {
  103. return this.commands.push(componentId, layout);
  104. }
  105. /**
  106. * Pop a component from the stack, regardless of it's position.
  107. */
  108. public pop(componentId: string, mergeOptions?): Promise<any> {
  109. return this.commands.pop(componentId, mergeOptions);
  110. }
  111. /**
  112. * Pop the stack to a given component
  113. */
  114. public popTo(componentId: string, mergeOptions?): Promise<any> {
  115. return this.commands.popTo(componentId, mergeOptions);
  116. }
  117. /**
  118. * Pop the component's stack to root.
  119. */
  120. public popToRoot(componentId: string, mergeOptions?): Promise<any> {
  121. return this.commands.popToRoot(componentId, mergeOptions);
  122. }
  123. /**
  124. * Sets new root component to stack.
  125. */
  126. public setStackRoot(componentId: string, layout: Layout): Promise<any> {
  127. return this.commands.setStackRoot(componentId, layout);
  128. }
  129. /**
  130. * Show overlay on top of the entire app
  131. */
  132. public showOverlay(layout: Layout): Promise<any> {
  133. return this.commands.showOverlay(layout);
  134. }
  135. /**
  136. * dismiss overlay by componentId
  137. */
  138. public dismissOverlay(componentId: string): Promise<any> {
  139. return this.commands.dismissOverlay(componentId);
  140. }
  141. /**
  142. * Resolves arguments passed on launch
  143. */
  144. public getLaunchArgs(): Promise<any> {
  145. return this.commands.getLaunchArgs();
  146. }
  147. /**
  148. * Obtain the events registry instance
  149. */
  150. public events(): EventsRegistry {
  151. return this.eventsRegistry;
  152. }
  153. /**
  154. * Constants coming from native
  155. */
  156. public async constants(): Promise<any> {
  157. return await Constants.get();
  158. }
  159. }