You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

App.native.js 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import React from 'react';
  2. import { Platform, StyleSheet, View } from 'react-native';
  3. import { SafeAreaProvider } from 'react-native-safe-area-context';
  4. import SplashScreen from 'react-native-splash-screen';
  5. import { DialogContainer } from '../../base/dialog';
  6. import BottomSheetContainer from '../../base/dialog/components/native/BottomSheetContainer';
  7. import { updateFlags } from '../../base/flags/actions';
  8. import { CALL_INTEGRATION_ENABLED, SERVER_URL_CHANGE_ENABLED } from '../../base/flags/constants';
  9. import { getFeatureFlag } from '../../base/flags/functions';
  10. import { DimensionsDetector, clientResized, setSafeAreaInsets } from '../../base/responsive-ui';
  11. import { updateSettings } from '../../base/settings';
  12. import { _getRouteToRender } from '../getRouteToRender.native';
  13. import logger from '../logger';
  14. import { AbstractApp } from './AbstractApp';
  15. import type { Props as AbstractAppProps } from './AbstractApp';
  16. // Register middlewares and reducers.
  17. import '../middlewares';
  18. import '../reducers';
  19. declare var __DEV__;
  20. const DialogContainerWrapper = Platform.select({
  21. default: View
  22. });
  23. /**
  24. * The type of React {@code Component} props of {@link App}.
  25. */
  26. type Props = AbstractAppProps & {
  27. /**
  28. * An object with the feature flags.
  29. */
  30. flags: Object,
  31. /**
  32. * An object with user information (display name, email, avatar URL).
  33. */
  34. userInfo: ?Object
  35. };
  36. /**
  37. * Root app {@code Component} on mobile/React Native.
  38. *
  39. * @augments AbstractApp
  40. */
  41. export class App extends AbstractApp {
  42. /**
  43. * The deferred for the initialisation {{promise, resolve, reject}}.
  44. */
  45. _init: Object;
  46. /**
  47. * Initializes a new {@code App} instance.
  48. *
  49. * @param {Props} props - The read-only React {@code Component} props with
  50. * which the new instance is to be initialized.
  51. */
  52. constructor(props: Props) {
  53. super(props);
  54. // In the Release configuration, React Native will (intentionally) throw
  55. // an unhandled JavascriptException for an unhandled JavaScript error.
  56. // This will effectively kill the app. In accord with the Web, do not
  57. // kill the app.
  58. this._maybeDisableExceptionsManager();
  59. // Bind event handler so it is only bound once per instance.
  60. this._onDimensionsChanged = this._onDimensionsChanged.bind(this);
  61. this._onSafeAreaInsetsChanged = this._onSafeAreaInsetsChanged.bind(this);
  62. }
  63. /**
  64. * Initializes the color scheme.
  65. *
  66. * @inheritdoc
  67. *
  68. * @returns {void}
  69. */
  70. async componentDidMount() {
  71. await super.componentDidMount();
  72. SplashScreen.hide();
  73. }
  74. /**
  75. * Initializes feature flags and updates settings.
  76. *
  77. * @returns {void}
  78. */
  79. async _extraInit() {
  80. const { dispatch, getState } = this.state.store;
  81. // We set these early enough so then we avoid any unnecessary re-renders.
  82. dispatch(updateFlags(this.props.flags));
  83. const route = await _getRouteToRender();
  84. // We need the root navigator to be set early.
  85. await this._navigate(route);
  86. // HACK ALERT!
  87. // Wait until the root navigator is ready.
  88. // We really need to break the inheritance relationship between App,
  89. // AbstractApp and BaseApp, it's very inflexible and cumbersome right now.
  90. const rootNavigationReady = new Promise(resolve => {
  91. const i = setInterval(() => {
  92. const { ready } = getState()['features/app'] || {};
  93. if (ready) {
  94. clearInterval(i);
  95. resolve();
  96. }
  97. }, 50);
  98. });
  99. await rootNavigationReady;
  100. // Check if serverURL is configured externally and not allowed to change.
  101. const serverURLChangeEnabled = getFeatureFlag(getState(), SERVER_URL_CHANGE_ENABLED, true);
  102. if (!serverURLChangeEnabled) {
  103. // As serverURL is provided externally, so we push it to settings.
  104. if (typeof this.props.url !== 'undefined') {
  105. const { serverURL } = this.props.url;
  106. if (typeof serverURL !== 'undefined') {
  107. dispatch(updateSettings({ serverURL }));
  108. }
  109. }
  110. }
  111. dispatch(updateSettings(this.props.userInfo || {}));
  112. // Update settings with feature-flag.
  113. const callIntegrationEnabled = this.props.flags[CALL_INTEGRATION_ENABLED];
  114. if (typeof callIntegrationEnabled !== 'undefined') {
  115. dispatch(updateSettings({ disableCallIntegration: !callIntegrationEnabled }));
  116. }
  117. }
  118. /**
  119. * Overrides the parent method to inject {@link DimensionsDetector} as
  120. * the top most component.
  121. *
  122. * @override
  123. */
  124. _createMainElement(component, props) {
  125. return (
  126. <SafeAreaProvider>
  127. <DimensionsDetector
  128. onDimensionsChanged = { this._onDimensionsChanged }
  129. onSafeAreaInsetsChanged = { this._onSafeAreaInsetsChanged }>
  130. { super._createMainElement(component, props) }
  131. </DimensionsDetector>
  132. </SafeAreaProvider>
  133. );
  134. }
  135. /**
  136. * Attempts to disable the use of React Native
  137. * {@link ExceptionsManager#handleException} on platforms and in
  138. * configurations on/in which the use of the method in questions has been
  139. * determined to be undesirable. For example, React Native will
  140. * (intentionally) throw an unhandled {@code JavascriptException} for an
  141. * unhandled JavaScript error in the Release configuration. This will
  142. * effectively kill the app. In accord with the Web, do not kill the app.
  143. *
  144. * @private
  145. * @returns {void}
  146. */
  147. _maybeDisableExceptionsManager() {
  148. if (__DEV__) {
  149. // As mentioned above, only the Release configuration was observed
  150. // to suffer.
  151. return;
  152. }
  153. if (Platform.OS !== 'android') {
  154. // A solution based on RTCSetFatalHandler was implemented on iOS and
  155. // it is preferred because it is at a later step of the
  156. // error/exception handling and it is specific to fatal
  157. // errors/exceptions which were observed to kill the app. The
  158. // solution implemented below was tested on Android only so it is
  159. // considered safest to use it there only.
  160. return;
  161. }
  162. const oldHandler = global.ErrorUtils.getGlobalHandler();
  163. const newHandler = _handleException;
  164. if (!oldHandler || oldHandler !== newHandler) {
  165. newHandler.next = oldHandler;
  166. global.ErrorUtils.setGlobalHandler(newHandler);
  167. }
  168. }
  169. _onDimensionsChanged: (width: number, height: number) => void;
  170. /**
  171. * Updates the known available size for the app to occupy.
  172. *
  173. * @param {number} width - The component's current width.
  174. * @param {number} height - The component's current height.
  175. * @private
  176. * @returns {void}
  177. */
  178. _onDimensionsChanged(width: number, height: number) {
  179. const { dispatch } = this.state.store;
  180. dispatch(clientResized(width, height));
  181. }
  182. /**
  183. * Updates the safe are insets values.
  184. *
  185. * @param {Object} insets - The insets.
  186. * @param {number} insets.top - The top inset.
  187. * @param {number} insets.right - The right inset.
  188. * @param {number} insets.bottom - The bottom inset.
  189. * @param {number} insets.left - The left inset.
  190. * @private
  191. * @returns {void}
  192. */
  193. _onSafeAreaInsetsChanged(insets) {
  194. const { dispatch } = this.state.store;
  195. dispatch(setSafeAreaInsets(insets));
  196. }
  197. /**
  198. * Renders the platform specific dialog container.
  199. *
  200. * @returns {React$Element}
  201. */
  202. _renderDialogContainer() {
  203. return (
  204. <DialogContainerWrapper
  205. pointerEvents = 'box-none'
  206. style = { StyleSheet.absoluteFill }>
  207. <BottomSheetContainer />
  208. <DialogContainer />
  209. </DialogContainerWrapper>
  210. );
  211. }
  212. }
  213. /**
  214. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  215. * from converting a fatal error into an unhandled native exception which will
  216. * kill the app.
  217. *
  218. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  219. * @param {boolean} fatal - If the specified error is fatal, {@code true};
  220. * otherwise, {@code false}.
  221. * @private
  222. * @returns {void}
  223. */
  224. function _handleException(error, fatal) {
  225. if (fatal) {
  226. // In the Release configuration, React Native will (intentionally) throw
  227. // an unhandled JavascriptException for an unhandled JavaScript error.
  228. // This will effectively kill the app. In accord with the Web, do not
  229. // kill the app.
  230. logger.error(error);
  231. } else {
  232. // Forward to the next globalHandler of ErrorUtils.
  233. const { next } = _handleException;
  234. typeof next === 'function' && next(error, fatal);
  235. }
  236. }