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-10-19 18:06:04 +00:00
|
|
|
|
|
|
|
// polyfill whatwg URL globals
|
|
|
|
import 'react-native-url-polyfill/auto';
|
|
|
|
|
2021-03-22 19:19:22 +00:00
|
|
|
import { Ionicons } from '@expo/vector-icons';
|
2022-12-01 15:53:06 +00:00
|
|
|
import { getPlaystateApi } from '@jellyfin/sdk/lib/utils/api/playstate-api';
|
2021-10-19 04:30:21 +00:00
|
|
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
2021-03-22 19:19:22 +00:00
|
|
|
import { NavigationContainer } from '@react-navigation/native';
|
2019-08-19 06:47:46 +00:00
|
|
|
import { Asset } from 'expo-asset';
|
2022-01-31 20:16:15 +00:00
|
|
|
import * as FileSystem from 'expo-file-system';
|
2019-08-19 06:47:46 +00:00
|
|
|
import * as Font from 'expo-font';
|
2020-04-03 16:57:51 +00:00
|
|
|
import * as ScreenOrientation from 'expo-screen-orientation';
|
2022-12-01 16:52:36 +00:00
|
|
|
import * as SplashScreen from 'expo-splash-screen';
|
2020-07-16 14:07:08 +00:00
|
|
|
import { StatusBar } from 'expo-status-bar';
|
2021-10-20 04:43:02 +00:00
|
|
|
import { observer } from 'mobx-react-lite';
|
2021-10-27 05:02:38 +00:00
|
|
|
import { AsyncTrunk } from 'mobx-sync-lite';
|
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';
|
2022-12-01 15:53:06 +00:00
|
|
|
import { Alert, useColorScheme } from 'react-native';
|
2021-03-22 19:19:22 +00:00
|
|
|
import { ThemeContext, ThemeProvider } from 'react-native-elements';
|
|
|
|
import { SafeAreaProvider } from 'react-native-safe-area-context';
|
2019-08-19 06:47:46 +00:00
|
|
|
|
2021-02-01 17:15:16 +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';
|
2022-01-31 20:16:15 +00:00
|
|
|
import { ensurePathExists } from './utils/File';
|
2021-03-01 15:08:22 +00:00
|
|
|
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
|
|
|
|
2021-02-01 17:15:16 +00:00
|
|
|
rootStore.settingStore.systemThemeId = useColorScheme();
|
|
|
|
|
2022-12-01 16:52:36 +00:00
|
|
|
SplashScreen.preventAutoHideAsync();
|
|
|
|
|
2020-07-22 03:37:43 +00:00
|
|
|
const trunk = new AsyncTrunk(rootStore, {
|
|
|
|
storage: AsyncStorage
|
|
|
|
});
|
|
|
|
|
|
|
|
const hydrateStores = async () => {
|
2020-12-07 22:22:27 +00:00
|
|
|
await trunk.init();
|
2020-07-22 03:37:43 +00:00
|
|
|
|
|
|
|
rootStore.storeLoaded = true;
|
|
|
|
};
|
|
|
|
|
2022-12-01 16:52:36 +00:00
|
|
|
const loadImages = () => {
|
|
|
|
const images = [
|
|
|
|
require('./assets/images/splash.png'),
|
|
|
|
require('./assets/images/logowhite.png')
|
|
|
|
];
|
|
|
|
return images.map(image => Asset.fromModule(image).downloadAsync());
|
|
|
|
};
|
|
|
|
|
|
|
|
const loadResources = async () => {
|
|
|
|
try {
|
|
|
|
await Promise.all([
|
|
|
|
Font.loadAsync({
|
|
|
|
// This is the font that we are using for our tab bar
|
|
|
|
...Ionicons.font
|
|
|
|
}),
|
|
|
|
...loadImages(),
|
|
|
|
StaticScriptLoader.load()
|
|
|
|
]);
|
|
|
|
} catch (err) {
|
|
|
|
console.warn('[App] Failed loading resources', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsSplashReady(true);
|
|
|
|
};
|
|
|
|
|
2020-07-22 03:37:43 +00:00
|
|
|
useEffect(() => {
|
|
|
|
// Hydrate mobx data stores
|
|
|
|
hydrateStores();
|
2022-12-01 16:52:36 +00:00
|
|
|
|
|
|
|
// Load app resources
|
|
|
|
loadResources();
|
2020-07-22 03:37:43 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-08-18 05:24:35 +00:00
|
|
|
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
|
|
|
|
2020-07-22 18:56:33 +00:00
|
|
|
const updateScreenOrientation = async () => {
|
2020-08-18 05:24:35 +00:00
|
|
|
if (rootStore.settingStore.isRotationLockEnabled) {
|
2020-07-22 18:56:33 +00:00
|
|
|
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 18:56:33 +00:00
|
|
|
|
2022-01-31 20:16:15 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const downloadFile = async (download) => {
|
|
|
|
console.debug('[App] downloading "%s"', download.filename);
|
|
|
|
await ensurePathExists(download.localPath);
|
|
|
|
|
|
|
|
const url = download.getStreamUrl(rootStore.deviceId);
|
|
|
|
|
|
|
|
const resumable = FileSystem.createDownloadResumable(
|
|
|
|
url.toString(),
|
|
|
|
download.uri,
|
|
|
|
{},
|
|
|
|
(/*{ totalBytesWritten }*/) => {
|
|
|
|
// FIXME: We should save the download progress in the model for display
|
|
|
|
// but this needs throttling
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: The resumable should be saved to allow pausing/resuming downloads
|
|
|
|
|
2022-12-01 19:19:02 +00:00
|
|
|
// Download the file
|
2022-01-31 20:16:15 +00:00
|
|
|
try {
|
|
|
|
download.isDownloading = true;
|
|
|
|
await resumable.downloadAsync();
|
|
|
|
download.isComplete = true;
|
|
|
|
download.isDownloading = false;
|
|
|
|
} catch (e) {
|
|
|
|
console.error('[App] Download failed', e);
|
2022-12-01 15:53:06 +00:00
|
|
|
Alert.alert('Download Failed', `"${download.title}" failed to download.`);
|
|
|
|
|
|
|
|
// TODO: If a download fails, we should probably remove it from the queue
|
2022-01-31 20:16:15 +00:00
|
|
|
download.isDownloading = false;
|
|
|
|
}
|
2022-12-01 19:19:02 +00:00
|
|
|
|
|
|
|
// Report download has stopped
|
|
|
|
const serverUrl = download.serverUrl.endsWith('/') ? download.serverUrl.slice(0, -1) : download.serverUrl;
|
|
|
|
const api = rootStore.sdk.createApi(serverUrl, download.apiKey);
|
|
|
|
console.log('[App] Reporting download stopped', download.sessionId);
|
|
|
|
getPlaystateApi(api)
|
|
|
|
.reportPlaybackStopped({
|
|
|
|
playbackStopInfo: {
|
|
|
|
PlaySessionId: download.sessionId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error('[App] Failed reporting download stopped', err.response || err.request || err.message);
|
|
|
|
});
|
2022-01-31 20:16:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
rootStore.downloadStore.downloads
|
|
|
|
.forEach(download => {
|
|
|
|
if (!download.isComplete && !download.isDownloading) {
|
|
|
|
downloadFile(download);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, [ rootStore.deviceId, rootStore.downloadStore.downloads.size ]);
|
|
|
|
|
2020-08-13 16:31:18 +00:00
|
|
|
if (!(isSplashReady && rootStore.storeLoaded) && !skipLoadingScreen) {
|
2022-12-01 16:52:36 +00:00
|
|
|
return null;
|
2020-07-22 03:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-11-04 16:50:16 +00:00
|
|
|
<SafeAreaProvider>
|
|
|
|
<ThemeProvider theme={rootStore.settingStore.theme.Elements}>
|
|
|
|
<ThemeSwitcher />
|
|
|
|
<StatusBar
|
|
|
|
style='light'
|
|
|
|
backgroundColor={theme.colors.grey0}
|
|
|
|
hidden={rootStore.isFullscreen}
|
|
|
|
/>
|
|
|
|
<NavigationContainer theme={rootStore.settingStore.theme.Navigation}>
|
|
|
|
<RootNavigator />
|
|
|
|
</NavigationContainer>
|
|
|
|
</ThemeProvider>
|
|
|
|
</SafeAreaProvider>
|
2020-07-22 03:37:43 +00:00
|
|
|
);
|
2020-06-24 21:01:48 +00:00
|
|
|
});
|
2020-06-19 18:40:17 +00:00
|
|
|
|
|
|
|
App.propTypes = {
|
2020-07-22 03:37:43 +00:00
|
|
|
skipLoadingScreen: PropTypes.bool
|
2020-06-19 18:40:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|