/
App.js
114 lines (100 loc) · 2.96 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// React
import "react-native-gesture-handler"
import React, { useEffect, useMemo, useReducer } from "react"
// React Query
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
const queryClient = new QueryClient()
// Expo
import * as SecureStore from "expo-secure-store"
import * as SplashScreen from "expo-splash-screen"
// Reducer
import ReducedConfig from "./utility/ReducedConfig.js"
// Context
import AuthContext from "./context/auth.js"
import SpotifyContext from "./context/spotify"
// Services
import { getSecureValue } from "./utility/SecureStore"
// Navigation
import AppNavigation from "./navigation/AppNavigation"
import LoginStack from "./navigation/LoginStack.js"
import LoadingStack from "./navigation/LoadingStack.js"
SplashScreen.preventAutoHideAsync()
export default function App() {
const [state, dispatch] = useReducer(ReducedConfig, {
isSignedIn: false,
userToken: null,
currentlyPlaying: {
track: null,
artist: null,
spotifyData: null,
},
})
const authContext = useMemo(
() => ({
signIn: async (loginResults) => {
if (loginResults !== "Login attempt canceled") {
dispatch({ type: "SIGN_IN", token: loginResults })
}
},
signOut: async () => {
await SecureStore.deleteItemAsync("spotifyToken")
await SecureStore.deleteItemAsync("spotifyRefreshToken")
dispatch({ type: "SIGN_OUT" })
},
}),
[]
)
const spotifyContext = useMemo(
() => ({
currentlyPlaying: state.currentlyPlaying,
updateTrack: async ({ track, artist, spotifyData }) => {
dispatch({
type: "UPDATE_TRACK",
track: track,
artist: artist,
spotifyData: spotifyData,
})
},
}),
[state.currentlyPlaying]
)
useEffect(() => {
function timeout(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
const checkToken = async function () {
try {
const accessToken = await getSecureValue("spotifyToken")
if (accessToken !== null) {
dispatch({
type: "UPDATE_TOKEN_BOOTSTRAP",
token: accessToken,
})
await timeout(2000)
await SplashScreen.hideAsync()
} else {
await timeout(2000)
await SplashScreen.hideAsync()
return true
}
} catch (error) {
console.error(error)
}
}
checkToken()
}, [])
const { isSignedIn, currentlyPlaying } = state
return (
<QueryClientProvider client={queryClient}>
<AuthContext.Provider value={authContext}>
<SpotifyContext.Provider value={spotifyContext}>
{!isSignedIn && <LoginStack />}
{isSignedIn && currentlyPlaying.track === null && <LoadingStack />}
{isSignedIn && currentlyPlaying.track !== null && <AppNavigation />}
</SpotifyContext.Provider>
</AuthContext.Provider>
</QueryClientProvider>
)
}