Skip to content

Commit

Permalink
feat(mobile): force ota updates on load (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdev98 committed Sep 11, 2024
1 parent d88e674 commit 010dc74
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 132 deletions.
3 changes: 2 additions & 1 deletion apps/mobile/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@
"owner": "sixpm",
"runtimeVersion": "1.0.0",
"updates": {
"url": "https://u.expo.dev/5853e031-3bfb-41fe-bfb6-c49f746c4adc"
"url": "https://u.expo.dev/5853e031-3bfb-41fe-bfb6-c49f746c4adc",
"checkAutomatically": "ON_ERROR_RECOVERY"
}
}
}
53 changes: 40 additions & 13 deletions apps/mobile/app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import * as WebBrowser from 'expo-web-browser'

import 'react-native-reanimated'
import { ToastRoot } from '@/components/common/toast'
import { UpdateLoader } from '@/components/common/update-loader'
import { useNotificationObserver } from '@/hooks/use-schedule-notification'
import { useWarmUpBrowser } from '@/hooks/use-warm-up-browser'
import { useColorScheme } from '@/hooks/useColorScheme'
Expand All @@ -47,9 +48,10 @@ import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persi
import { focusManager, onlineManager } from '@tanstack/react-query'
import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client'
import { LinearGradient } from 'expo-linear-gradient'
import * as Updates from 'expo-updates'
import { cssInterop } from 'nativewind'
import { PostHogProvider } from 'posthog-react-native'
import { useEffect } from 'react'
import { useCallback, useEffect, useState } from 'react'
import { AppState, type AppStateStatus, Platform } from 'react-native'
import { GestureHandlerRootView } from 'react-native-gesture-handler'
import { SafeAreaProvider } from 'react-native-safe-area-context'
Expand Down Expand Up @@ -130,8 +132,29 @@ function RootLayout() {
})
const ref = useNavigationContainerRef()
useNotificationObserver()
const [updating, setUpdating] = useState<boolean>(false)

const checkAndForceUpdates = useCallback(async () => {
if (__DEV__) {
return
}
setUpdating(true)
try {
const update = await Updates.checkForUpdateAsync()
if (update.isAvailable) {
await Updates.fetchUpdateAsync()
await Updates.reloadAsync()
}
} catch (error) {
Sentry.captureException(error)
}
setUpdating(false)
}, [])

// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
useEffect(() => {
checkAndForceUpdates()

setTimeout(() => SplashScreen.hideAsync(), 2000)

const subscription = AppState.addEventListener('change', onAppStateChange)
Expand Down Expand Up @@ -175,18 +198,22 @@ function RootLayout() {
>
<SafeAreaProvider>
<GestureHandlerRootView>
<BottomSheetModalProvider>
<Stack screenOptions={{ headerShown: false }}>
<Stack.Screen
name="(aux)"
options={{
presentation: 'modal',
}}
/>
</Stack>
<ToastRoot />
<PortalHost />
</BottomSheetModalProvider>
{updating ? (
<UpdateLoader />
) : (
<BottomSheetModalProvider>
<Stack screenOptions={{ headerShown: false }}>
<Stack.Screen
name="(aux)"
options={{
presentation: 'modal',
}}
/>
</Stack>
<ToastRoot />
<PortalHost />
</BottomSheetModalProvider>
)}
</GestureHandlerRootView>
</SafeAreaProvider>
</ThemeProvider>
Expand Down
17 changes: 17 additions & 0 deletions apps/mobile/components/common/update-loader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { t } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import { View } from 'react-native'
import UpdateIllustration from '../svg-assets/update-illustration'
import { Text } from '../ui/text'

export function UpdateLoader() {
const { i18n } = useLingui()
return (
<View className="flex-1 items-center justify-center gap-12 bg-background">
<UpdateIllustration className="h-72 text-primary" />
<Text className="text-muted-foreground">{t(
i18n,
)`6pm is updating, please wait...`}</Text>
</View>
)
}
44 changes: 44 additions & 0 deletions apps/mobile/components/svg-assets/update-illustration.tsx

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions apps/mobile/hooks/use-ota-updates.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useQuery } from '@tanstack/react-query'
import * as Updates from 'expo-updates'

export function useOTAUpdates() {
const { isLoading, refetch } = useQuery({
queryKey: ['updates'],
queryFn: async () => {
await Updates.fetchUpdateAsync()
await Updates.reloadAsync()
},
enabled: false,
})

async function checkForUpdate() {
return await Updates.checkForUpdateAsync()
}

return {
checkForUpdate,
startUpdate: refetch,
updating: isLoading,
}
}
Loading

0 comments on commit 010dc74

Please sign in to comment.