index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @format
  3. */
  4. import React, { useEffect } from 'react';
  5. import {AppRegistry, KeyboardAvoidingView, StatusBar} from 'react-native';
  6. import codePush from "react-native-code-push";
  7. import 'react-native-gesture-handler';
  8. import './app/utils/themes'
  9. import './app/utils/constant';
  10. import './app/utils/notification';
  11. import './app/utils/vector_icon';
  12. import Router from './app/pages/Router';
  13. import app from './app.json';
  14. //import {ModalPortal} from 'react-native-modals';
  15. import ModalPortal from './app/components/ModalPortal';
  16. import {RootSiblingParent} from 'react-native-root-siblings';
  17. import { SafeAreaView } from 'react-native-safe-area-context';
  18. import Analytics from 'appcenter-analytics';
  19. const Index = () => {
  20. useEffect(() => {
  21. checkUpdate();
  22. })
  23. return (
  24. <RootSiblingParent>
  25. <StatusBar barStyle={themeStatusBar} backgroundColor={colorPrimaryDark}/>
  26. { isIOS
  27. ? <KeyboardAvoidingView style={ui.flex1} behavior="padding">
  28. <Router />
  29. </KeyboardAvoidingView>
  30. : <Router />
  31. }
  32. <ModalPortal />
  33. <SafeAreaView style={{flex: 0, backgroundColor: colorLight}}></SafeAreaView>
  34. </RootSiblingParent>
  35. );
  36. };
  37. const checkUpdate = () => {
  38. console.log("[CodePush]", "Checking Update");
  39. Analytics.trackEvent('checkUpdate', { versionName: app.versionName, versionCode: app.versionCode });
  40. //codePush.disallowRestart();
  41. codePush.sync({
  42. updateDialog: app.debug,
  43. installMode: codePush.InstallMode.IMMEDIATE
  44. }, status => {
  45. //Analytics.trackEvent('checkUpdate-status', status);
  46. switch(status) {
  47. case codePush.SyncStatus.DOWNLOADING_PACKAGE:
  48. if (app.debug) {
  49. toastShort("Downloading update...")
  50. }
  51. console.log("[CodePush]", "Downloading update");
  52. break;
  53. case codePush.SyncStatus.INSTALLING_UPDATE:
  54. if (app.debug) {
  55. toastShort("Installing update")
  56. }
  57. console.log("[CodePush]", "Installing update");
  58. break;
  59. case codePush.SyncStatus.UP_TO_DATE:
  60. if (app.debug) {
  61. toastShort("App is up to date")
  62. }
  63. console.log("[CodePush]", "App is up to date");
  64. codePush.notifyAppReady();
  65. break;
  66. case codePush.SyncStatus.UPDATE_INSTALLED:
  67. if (app.debug) {
  68. toastShort("Update installed")
  69. }
  70. console.log("[CodePush]", "Update installed");
  71. //codePush.notifyAppReady();
  72. //codePush.restartApp(true);
  73. break;
  74. }
  75. }, process => {
  76. });
  77. }
  78. //热更新配置
  79. let codePushOptions = {
  80. updateDialog: isIOS,
  81. //实时检测更新并下载
  82. checkFrequency: codePush.CheckFrequency.MANUAL,
  83. //下载完成后立即安装
  84. installMode: codePush.InstallMode.IMMEDIATE
  85. //下次进入安装
  86. //installMode: codePush.InstallMode.ON_NEXT_RESTART
  87. };
  88. AppRegistry.registerComponent(app.name, () => codePush(codePushOptions)(Index));
  89. //AppRegistry.registerComponent(appName, () => Index);