react-native-navigation的迁移库

Navigation.ts 7.2KB

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