react-native-navigation的迁移库

Navigation.ts 7.3KB

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