File size: 2,126 Bytes
68465df | 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 | import {FC, ReactNode, createContext, useCallback, useContext, useEffect, useState} from "react"
import Animated, {useAnimatedStyle, useSharedValue, withTiming} from "react-native-reanimated"
import {scheduleOnRN} from "react-native-worklets"
import {Screen} from "@/components/ignite"
import {SplashVideo} from "@/components/splash/SplashVideo"
import {SETTINGS, useSetting} from "@mentra/engine"
interface SplashLoaderContextType {
splashEnabled: boolean
setSplashEnabled: (v: boolean) => void
}
const SplashLoaderContext = createContext<SplashLoaderContextType | null>(null)
export const useSplashLoader = (): SplashLoaderContextType => {
const ctx = useContext(SplashLoaderContext)
if (!ctx) {
throw new Error("useSplashLoader must be used within a SplashLoaderProvider")
}
return ctx
}
const FADE_MS = 250
export const SplashLoaderProvider: FC<{children: ReactNode}> = ({children}) => {
const [appBootExtraInfo] = useSetting(SETTINGS.app_boot_extra_info.key)
const [splashEnabled, setSplashEnabledState] = useState(false)
const [visible, setVisible] = useState(false)
const opacity = useSharedValue(0)
const setSplashEnabled = useCallback((v: boolean) => {
setSplashEnabledState(v)
}, [])
useEffect(() => {
if (splashEnabled) {
setVisible(true)
opacity.value = withTiming(1, {duration: FADE_MS})
return
}
opacity.value = withTiming(0, {duration: FADE_MS}, (finished) => {
if (finished) {
scheduleOnRN(setVisible, false)
}
})
}, [splashEnabled, opacity])
const overlayStyle = useAnimatedStyle(() => ({
opacity: opacity.value,
}))
return (
<SplashLoaderContext.Provider value={{splashEnabled, setSplashEnabled}}>
{children}
{visible && (
<Animated.View className="absolute inset-0 z-50" style={overlayStyle}>
<Screen preset="fixed">
<SplashVideo label={appBootExtraInfo ? "Opening link…" : undefined} />
</Screen>
</Animated.View>
)}
</SplashLoaderContext.Provider>
)
}
|