jellyfin-expo/App.js

135 lines
4.0 KiB
JavaScript
Raw Normal View History

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/.
*/
2021-03-22 19:19:22 +00:00
import { Ionicons } from '@expo/vector-icons';
import { NavigationContainer } from '@react-navigation/native';
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';
import { StatusBar } from 'expo-status-bar';
2021-03-22 19:19:22 +00:00
import { observer } from 'mobx-react';
import { AsyncTrunk } from 'mobx-sync';
2019-08-29 04:38:29 +00:00
import PropTypes from 'prop-types';
2021-03-22 19:19:22 +00:00
import React, { useContext, useEffect, useState } from 'react';
import { AsyncStorage } from 'react-native';
import { AppearanceProvider, useColorScheme } from 'react-native-appearance';
import { ThemeContext, ThemeProvider } from 'react-native-elements';
import { SafeAreaProvider } from 'react-native-safe-area-context';
2019-08-19 06:47:46 +00:00
import ThemeSwitcher from './components/ThemeSwitcher';
2020-06-24 21:01:48 +00:00
import { useStores } from './hooks/useStores';
2021-03-25 18:56:07 +00:00
import RootNavigator from './navigation/RootNavigator';
import StaticScriptLoader from './utils/StaticScriptLoader';
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 }) => {
2021-02-26 23:58:29 +00:00
const [ isSplashReady, setIsSplashReady ] = useState(false);
2020-07-22 03:37:43 +00:00
const { rootStore } = useStores();
2020-12-30 19:22:46 +00:00
const { theme } = useContext(ThemeContext);
2020-07-22 03:37:43 +00:00
rootStore.settingStore.systemThemeId = useColorScheme();
2020-07-22 03:37:43 +00:00
const trunk = new AsyncTrunk(rootStore, {
storage: AsyncStorage
});
const hydrateStores = async () => {
await trunk.init();
2020-07-22 03:37:43 +00:00
rootStore.storeLoaded = true;
};
useEffect(() => {
// Hydrate mobx data stores
hydrateStores();
}, []);
useEffect(() => {
console.info('rotation lock setting changed!', rootStore.settingStore.isRotationLockEnabled);
if (rootStore.settingStore.isRotationLockEnabled) {
2020-07-22 03:37:43 +00:00
ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.PORTRAIT_UP);
} else {
ScreenOrientation.unlockAsync();
}
2021-02-26 23:58:29 +00:00
}, [ rootStore.settingStore.isRotationLockEnabled ]);
2020-07-22 03:37:43 +00:00
const updateScreenOrientation = async () => {
if (rootStore.settingStore.isRotationLockEnabled) {
if (rootStore.isFullscreen) {
// Lock to landscape orientation
// For some reason video apps on iPhone use LANDSCAPE_RIGHT ¯\_(ツ)_/¯
await ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.LANDSCAPE_RIGHT);
// Allow either landscape orientation after forcing initial rotation
ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.LANDSCAPE);
} else {
// Restore portrait orientation lock
ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.PORTRAIT_UP);
}
}
};
useEffect(() => {
// Update the screen orientation
updateScreenOrientation();
2021-02-26 23:58:29 +00:00
}, [ rootStore.isFullscreen ]);
2020-07-22 03:37:43 +00:00
const loadImagesAsync = () => {
const images = [
require('./assets/images/splash.png'),
require('./assets/images/logowhite.png')
];
return images.map(image => Asset.fromModule(image).downloadAsync());
};
const loadResourcesAsync = async () => {
return Promise.all([
Font.loadAsync({
// This is the font that we are using for our tab bar
...Ionicons.font
}),
...loadImagesAsync(),
2021-01-06 19:27:10 +00:00
StaticScriptLoader.load()
2020-07-22 03:37:43 +00:00
]);
};
if (!(isSplashReady && rootStore.storeLoaded) && !skipLoadingScreen) {
2020-07-22 03:37:43 +00:00
return (
<AppLoading
startAsync={loadResourcesAsync}
onError={console.warn}
onFinish={() => setIsSplashReady(true)}
autoHideSplash={false}
/>
);
}
return (
<AppearanceProvider>
<SafeAreaProvider>
<ThemeProvider theme={rootStore.settingStore.theme.Elements}>
<ThemeSwitcher />
<StatusBar
style='light'
backgroundColor={theme.colors.grey0}
hidden={rootStore.isFullscreen}
/>
2021-03-22 19:19:22 +00:00
<NavigationContainer theme={rootStore.settingStore.theme.Navigation}>
2021-03-25 18:56:07 +00:00
<RootNavigator />
2021-03-22 19:19:22 +00:00
</NavigationContainer>
</ThemeProvider>
</SafeAreaProvider>
</AppearanceProvider>
2020-07-22 03:37:43 +00:00
);
2020-06-24 21:01:48 +00:00
});
App.propTypes = {
2020-07-22 03:37:43 +00:00
skipLoadingScreen: PropTypes.bool
};
export default App;