2019-12-12 21:22:47 +00:00
|
|
|
/**
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
2020-06-24 21:01:48 +00:00
|
|
|
import 'mobx-react-lite/batchingForReactNative';
|
|
|
|
|
2020-06-19 18:40:17 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-06-24 21:01:48 +00:00
|
|
|
import { AsyncStorage, Platform, StatusBar } from 'react-native';
|
2019-08-25 03:26:31 +00:00
|
|
|
import { ThemeProvider } from 'react-native-elements';
|
2020-06-19 21:51:13 +00:00
|
|
|
import { SafeAreaProvider } from 'react-native-safe-area-context';
|
2020-06-24 21:01:48 +00:00
|
|
|
import { observer } from 'mobx-react';
|
2020-06-30 15:27:02 +00:00
|
|
|
import { AsyncTrunk } from 'mobx-sync';
|
2020-04-03 16:57:51 +00:00
|
|
|
import { AppLoading } from 'expo';
|
2019-08-19 06:47:46 +00:00
|
|
|
import { Asset } from 'expo-asset';
|
|
|
|
import * as Font from 'expo-font';
|
2020-04-03 16:57:51 +00:00
|
|
|
import * as ScreenOrientation from 'expo-screen-orientation';
|
2019-08-19 06:47:46 +00:00
|
|
|
import { Ionicons } from '@expo/vector-icons';
|
2019-08-29 04:38:29 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-08-19 06:47:46 +00:00
|
|
|
|
2020-06-24 21:01:48 +00:00
|
|
|
import { useStores } from './hooks/useStores';
|
|
|
|
import StorageKeys from './constants/Storage';
|
2019-08-19 06:47:46 +00:00
|
|
|
import AppNavigator from './navigation/AppNavigator';
|
2020-06-24 21:01:48 +00:00
|
|
|
import CachingStorage from './utils/CachingStorage';
|
2019-08-25 03:26:31 +00:00
|
|
|
import Theme from './utils/Theme';
|
2019-08-19 06:47:46 +00:00
|
|
|
|
2020-07-13 16:30:38 +00:00
|
|
|
// Import i18n configuration
|
|
|
|
import './i18n';
|
|
|
|
|
2020-06-24 21:01:48 +00:00
|
|
|
const App = observer(({ skipLoadingScreen }) => {
|
2020-06-19 18:40:17 +00:00
|
|
|
const [isSplashReady, setIsSplashReady] = useState(false);
|
2020-06-30 15:27:02 +00:00
|
|
|
const { rootStore } = useStores();
|
|
|
|
|
|
|
|
const trunk = new AsyncTrunk(rootStore, {
|
|
|
|
storage: AsyncStorage
|
|
|
|
});
|
2020-06-24 21:01:48 +00:00
|
|
|
|
|
|
|
const hydrateStores = async () => {
|
2020-06-30 15:27:02 +00:00
|
|
|
// Migrate servers and settings
|
2020-06-24 21:01:48 +00:00
|
|
|
// TODO: Remove this for next release
|
|
|
|
const servers = await CachingStorage.getInstance().getItem(StorageKeys.Servers);
|
2020-06-30 15:27:02 +00:00
|
|
|
if (servers) {
|
|
|
|
const activeServer = await CachingStorage.getInstance().getItem(StorageKeys.ActiveServer) || 0;
|
|
|
|
|
|
|
|
// Initialize the store with the existing servers and settings
|
|
|
|
await trunk.init({
|
|
|
|
serverStore: { servers },
|
|
|
|
settingStore: { activeServer }
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove old data values
|
|
|
|
AsyncStorage.multiRemove(Object.values(StorageKeys));
|
|
|
|
} else {
|
|
|
|
// No servers saved in the old method, initialize normally
|
|
|
|
await trunk.init();
|
|
|
|
}
|
2020-06-24 21:01:48 +00:00
|
|
|
|
2020-06-30 15:27:02 +00:00
|
|
|
rootStore.storeLoaded = true;
|
2019-08-19 06:47:46 +00:00
|
|
|
|
2020-07-02 20:24:14 +00:00
|
|
|
if (typeof rootStore.settingStore.isRotationEnabled === 'undefined') {
|
|
|
|
rootStore.settingStore.isRotationEnabled = Platform.OS === 'ios' && !Platform.isPad;
|
|
|
|
console.info('Initializing rotation lock setting', rootStore.settingStore.isRotationEnabled);
|
2020-04-13 15:20:05 +00:00
|
|
|
}
|
2020-07-02 20:24:14 +00:00
|
|
|
};
|
2020-06-24 21:01:48 +00:00
|
|
|
|
2020-07-02 20:24:14 +00:00
|
|
|
useEffect(() => {
|
2020-06-24 21:01:48 +00:00
|
|
|
// Hydrate mobx data stores
|
|
|
|
hydrateStores();
|
2020-06-19 18:40:17 +00:00
|
|
|
}, []);
|
2019-08-19 06:47:46 +00:00
|
|
|
|
2020-07-02 20:24:14 +00:00
|
|
|
useEffect(() => {
|
|
|
|
console.info('rotation lock setting changed!', rootStore.settingStore.isRotationEnabled);
|
|
|
|
if (rootStore.settingStore.isRotationEnabled) {
|
|
|
|
ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.PORTRAIT_UP);
|
|
|
|
} else {
|
|
|
|
ScreenOrientation.unlockAsync();
|
|
|
|
}
|
|
|
|
}, [rootStore.settingStore.isRotationEnabled]);
|
|
|
|
|
2020-06-19 18:40:17 +00:00
|
|
|
const loadImagesAsync = () => {
|
2020-04-13 15:20:05 +00:00
|
|
|
const images = [
|
|
|
|
require('./assets/images/splash.png'),
|
|
|
|
require('./assets/images/logowhite.png')
|
|
|
|
];
|
|
|
|
return images.map(image => Asset.fromModule(image).downloadAsync());
|
2020-06-19 18:40:17 +00:00
|
|
|
};
|
2019-08-19 06:47:46 +00:00
|
|
|
|
2020-06-19 18:40:17 +00:00
|
|
|
const loadResourcesAsync = async () => {
|
2020-04-13 15:20:05 +00:00
|
|
|
return Promise.all([
|
|
|
|
Font.loadAsync({
|
|
|
|
// This is the font that we are using for our tab bar
|
|
|
|
...Ionicons.font
|
|
|
|
}),
|
2020-06-19 18:40:17 +00:00
|
|
|
...loadImagesAsync()
|
2020-04-13 15:20:05 +00:00
|
|
|
]);
|
2019-08-19 06:47:46 +00:00
|
|
|
};
|
|
|
|
|
2020-06-19 18:40:17 +00:00
|
|
|
if (!isSplashReady && !skipLoadingScreen) {
|
|
|
|
return (
|
|
|
|
<AppLoading
|
|
|
|
startAsync={loadResourcesAsync}
|
|
|
|
onError={console.warn}
|
|
|
|
onFinish={() => setIsSplashReady(true)}
|
|
|
|
autoHideSplash={false}
|
|
|
|
/>
|
|
|
|
);
|
2020-04-13 15:20:05 +00:00
|
|
|
}
|
2020-06-19 18:40:17 +00:00
|
|
|
|
|
|
|
return (
|
2020-06-19 21:51:13 +00:00
|
|
|
<SafeAreaProvider>
|
|
|
|
<ThemeProvider theme={Theme}>
|
|
|
|
{Platform.OS === 'ios' && <StatusBar barStyle="light-content" />}
|
|
|
|
<AppNavigator />
|
|
|
|
</ThemeProvider>
|
|
|
|
</SafeAreaProvider>
|
2020-06-19 18:40:17 +00:00
|
|
|
);
|
2020-06-24 21:01:48 +00:00
|
|
|
});
|
2020-06-19 18:40:17 +00:00
|
|
|
|
|
|
|
App.propTypes = {
|
|
|
|
skipLoadingScreen: PropTypes.bool
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|