2025-05-06 11:11:49 +02:00
|
|
|
import React, { PropsWithChildren, useEffect, useState } from 'react';
|
2020-06-17 14:58:04 +02:00
|
|
|
import { Provider } from 'react-redux';
|
2021-12-31 15:04:37 +01:00
|
|
|
import TrackPlayer, { Capability } from 'react-native-track-player';
|
2020-06-17 14:58:04 +02:00
|
|
|
import { PersistGate } from 'redux-persist/integration/react';
|
2020-06-16 17:51:51 +02:00
|
|
|
import Routes from '../screens';
|
2024-07-22 13:17:26 +02:00
|
|
|
import store, { persistedStore } from '@/store';
|
2020-07-26 14:45:32 +02:00
|
|
|
import {
|
|
|
|
|
NavigationContainer,
|
|
|
|
|
DefaultTheme,
|
2022-05-11 23:57:30 +02:00
|
|
|
DarkTheme as BaseDarkTheme,
|
2020-07-26 14:45:32 +02:00
|
|
|
} from '@react-navigation/native';
|
2024-07-22 13:17:26 +02:00
|
|
|
import { ColorSchemeProvider, themes, useUserOrSystemScheme } from './Colors';
|
2022-01-15 17:25:24 +01:00
|
|
|
import DownloadManager from './DownloadManager';
|
2025-05-06 11:11:49 +02:00
|
|
|
import AppLoading from './AppLoading';
|
2020-06-16 17:51:51 +02:00
|
|
|
|
2022-05-11 23:57:30 +02:00
|
|
|
const LightTheme = {
|
|
|
|
|
...DefaultTheme,
|
|
|
|
|
colors: {
|
|
|
|
|
...DefaultTheme.colors,
|
|
|
|
|
background: themes.light.view.backgroundColor,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const DarkTheme = {
|
|
|
|
|
...BaseDarkTheme,
|
|
|
|
|
colors: {
|
|
|
|
|
...BaseDarkTheme.colors,
|
2022-05-16 22:28:13 +02:00
|
|
|
background: themes.dark.view.backgroundColor,
|
2022-05-11 23:57:30 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-28 21:01:21 +02:00
|
|
|
/**
|
|
|
|
|
* This is a convenience wrapper for NavigationContainer that ensures that the
|
|
|
|
|
* right theme is selected based on OS color scheme settings along with user preferences.
|
|
|
|
|
*/
|
|
|
|
|
function ThemedNavigationContainer({ children }: PropsWithChildren<{}>) {
|
2024-07-22 13:17:26 +02:00
|
|
|
const scheme = useUserOrSystemScheme();
|
2023-04-28 21:01:21 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<NavigationContainer
|
|
|
|
|
theme={scheme === 'dark' ? DarkTheme : LightTheme}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</NavigationContainer>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-06 11:11:49 +02:00
|
|
|
export default function App(): JSX.Element | null {
|
|
|
|
|
// Track whether the player has already been setup, so that we don't
|
|
|
|
|
// accidentally do it twice.
|
|
|
|
|
const [hasSetupPlayer, setHasSetupPlayer] = useState(false);
|
2023-04-27 15:08:10 +02:00
|
|
|
|
2021-02-11 23:43:21 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
async function setupTrackPlayer() {
|
2025-05-06 11:11:49 +02:00
|
|
|
await TrackPlayer.setupPlayer({ autoHandleInterruptions: true });
|
2021-02-11 23:43:21 +01:00
|
|
|
await TrackPlayer.updateOptions({
|
|
|
|
|
capabilities: [
|
2021-12-31 15:04:37 +01:00
|
|
|
Capability.Play,
|
|
|
|
|
Capability.Pause,
|
|
|
|
|
Capability.SkipToNext,
|
|
|
|
|
Capability.SkipToPrevious,
|
|
|
|
|
Capability.Stop,
|
|
|
|
|
Capability.SeekTo,
|
2022-05-05 03:30:51 +02:00
|
|
|
],
|
2023-04-27 15:08:10 +02:00
|
|
|
progressUpdateEventInterval: 5,
|
2021-02-11 23:43:21 +01:00
|
|
|
});
|
2025-05-06 11:11:49 +02:00
|
|
|
setHasSetupPlayer(true);
|
2020-06-16 17:51:51 +02:00
|
|
|
}
|
2023-04-27 15:08:10 +02:00
|
|
|
|
|
|
|
|
if (!hasSetupPlayer) {
|
|
|
|
|
setupTrackPlayer();
|
|
|
|
|
}
|
2025-05-06 11:11:49 +02:00
|
|
|
}, [hasSetupPlayer]);
|
|
|
|
|
|
|
|
|
|
// GUARD: Wait for setup of the player before showing the rest of the app
|
|
|
|
|
if (!hasSetupPlayer) {
|
|
|
|
|
return (<AppLoading />);
|
|
|
|
|
}
|
2020-06-16 17:51:51 +02:00
|
|
|
|
2021-02-11 23:43:21 +01:00
|
|
|
return (
|
|
|
|
|
<Provider store={store}>
|
|
|
|
|
<PersistGate loading={null} persistor={persistedStore}>
|
2023-04-28 21:01:21 +02:00
|
|
|
<ColorSchemeProvider>
|
|
|
|
|
<ThemedNavigationContainer>
|
2022-05-05 22:54:37 +02:00
|
|
|
<Routes />
|
|
|
|
|
<DownloadManager />
|
2023-04-28 21:01:21 +02:00
|
|
|
</ThemedNavigationContainer>
|
|
|
|
|
</ColorSchemeProvider>
|
2021-02-11 23:43:21 +01:00
|
|
|
</PersistGate>
|
|
|
|
|
</Provider>
|
|
|
|
|
);
|
2020-06-16 17:51:51 +02:00
|
|
|
}
|