App.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* eslint-disable import/no-named-default */
  2. /* eslint-disable react/destructuring-assignment */
  3. /* eslint-disable react/jsx-props-no-spreading */
  4. import * as React from "react";
  5. import * as eva from "@eva-design/eva";
  6. import {
  7. ApplicationProvider,
  8. Layout,
  9. IconRegistry,
  10. } from "@ui-kitten/components";
  11. import { Provider } from "@ant-design/react-native";
  12. import { EvaIconsPack } from "@ui-kitten/eva-icons";
  13. import { SplashScreen } from "expo";
  14. import * as Font from "expo-font";
  15. import { Ionicons } from "@expo/vector-icons";
  16. import { NavigationContainer, CommonActions } from "@react-navigation/native";
  17. import {
  18. createStackNavigator,
  19. CardStyleInterpolators,
  20. } from "@react-navigation/stack";
  21. import { useModel } from "flooks";
  22. import { enableScreens } from "react-native-screens";
  23. // eslint-disable-next-line no-unused-vars
  24. import * as model from "./models";
  25. import { default as theme } from "./theme.json"; // <-- Import app theme
  26. import { default as customMapping } from "./mapping.json"; // <-- Import app theme
  27. import Dialog from "./components/Dialog";
  28. import BottomTabNavigator from "./navigation/BottomTabNavigator";
  29. import LoginStackNavigator from "./navigation/LoginStackNavigator";
  30. import GuideScreens from "./navigation/GuideStackNavigator";
  31. import BasicScreens from "./navigation/BasicNavigator";
  32. import { navigationRef } from "./navigation/RootNavigation";
  33. enableScreens();
  34. const Stack = createStackNavigator();
  35. const fontFile = require("./assets/fonts/SpaceMono-Regular.ttf");
  36. export default function App(props) {
  37. const [isLoadingComplete, setLoadingComplete] = React.useState(false);
  38. const { getWords } = useModel("wordsModel", true);
  39. const { loading, clearLoading } = useModel("loadingModel", true);
  40. // Load any resources or data that we need prior to rendering the app
  41. React.useEffect(() => {
  42. async function loadResourcesAndDataAsync() {
  43. try {
  44. SplashScreen.preventAutoHide();
  45. // Load fonts
  46. await Font.loadAsync({
  47. ...Ionicons.font,
  48. "space-mono": fontFile,
  49. });
  50. await Font.loadAsync(
  51. "antoutline",
  52. // eslint-disable-next-line
  53. require("@ant-design/icons-react-native/fonts/antoutline.ttf")
  54. );
  55. await Font.loadAsync(
  56. "antfill",
  57. // eslint-disable-next-line
  58. require("@ant-design/icons-react-native/fonts/antfill.ttf")
  59. );
  60. getWords();
  61. } catch (e) {
  62. // We might want to provide this error information to an error reporting service
  63. // eslint-disable-next-line no-console
  64. console.log(e);
  65. } finally {
  66. setLoadingComplete(true);
  67. SplashScreen.hide();
  68. }
  69. }
  70. loadResourcesAndDataAsync();
  71. }, []);
  72. const [initRouteName, setInit] = React.useState("");
  73. const {
  74. getUserInfo,
  75. getGuideStep,
  76. initApp,
  77. refreashReason,
  78. changeInIt,
  79. setinitRoute,
  80. } = useModel("userModel");
  81. React.useEffect(() => {
  82. function getLogin() {
  83. if (refreashReason !== "guideStep") {
  84. return getUserInfo();
  85. }
  86. return Promise.resolve();
  87. }
  88. if (initApp && initRouteName) {
  89. loading();
  90. }
  91. if (initApp) {
  92. getLogin()
  93. .then(() => {
  94. return getGuideStep();
  95. })
  96. .then(res => {
  97. switch (res) {
  98. case "finish":
  99. setInit("Root");
  100. break;
  101. case "ComeBack":
  102. setinitRoute("RegisterSe");
  103. setInit("Login");
  104. break;
  105. case "1":
  106. setInit("Guide1");
  107. break;
  108. case "2":
  109. setInit("Guide2");
  110. break;
  111. case "3":
  112. setInit("Guide3");
  113. break;
  114. case "4":
  115. setInit("Guide4");
  116. break;
  117. case "5":
  118. setInit("StoreAudit");
  119. break;
  120. default:
  121. setInit("");
  122. break;
  123. }
  124. })
  125. .catch(() => {
  126. // 未登录
  127. setinitRoute("Login");
  128. setInit("Login");
  129. })
  130. .finally(() => {
  131. changeInIt();
  132. });
  133. }
  134. }, [initApp]);
  135. React.useEffect(() => {
  136. if (navigationRef.current) {
  137. const { name } = navigationRef.current.getCurrentRoute();
  138. if (name !== initRouteName && initRouteName) {
  139. navigationRef.current.dispatch(
  140. CommonActions.reset({
  141. index: 0,
  142. routes: [
  143. {
  144. name: initRouteName,
  145. },
  146. ],
  147. })
  148. );
  149. }
  150. clearLoading();
  151. }
  152. }, [initRouteName]);
  153. if (!isLoadingComplete && !props.skipLoadingScreen && !!initRouteName) {
  154. return null;
  155. }
  156. return (
  157. <>
  158. <IconRegistry icons={EvaIconsPack} />
  159. <Provider>
  160. <ApplicationProvider
  161. {...eva}
  162. theme={{ ...eva.light, ...theme }}
  163. customMapping={customMapping}
  164. >
  165. <Dialog />
  166. <Layout style={{ flex: 1 }}>
  167. <NavigationContainer ref={navigationRef}>
  168. <Stack.Navigator
  169. headerMode="none"
  170. screenOptions={{
  171. gestureEnabled: true,
  172. cardStyleInterpolator:
  173. CardStyleInterpolators.forHorizontalIOS,
  174. }}
  175. initialRouteName={initRouteName}
  176. >
  177. {GuideScreens(Stack.Screen)}
  178. <Stack.Screen
  179. name="Root"
  180. component={BottomTabNavigator}
  181. options={{}}
  182. />
  183. <Stack.Screen name="Login" component={LoginStackNavigator} />
  184. {/* 基础功能页面 */}
  185. {BasicScreens(Stack.Screen)}
  186. </Stack.Navigator>
  187. </NavigationContainer>
  188. </Layout>
  189. </ApplicationProvider>
  190. </Provider>
  191. </>
  192. );
  193. }