Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyok committed Mar 21, 2024
1 parent f1cdc4e commit 6e357c1
Showing 1 changed file with 49 additions and 42 deletions.
91 changes: 49 additions & 42 deletions src/lib/hooks/useUpdateCheck.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react'
import {Alert, AppState, AppStateStatus} from 'react-native'
import app from 'react-native-version-number'
import * as Updates from 'expo-updates'
import {useUpdates} from 'expo-updates'

import {logger} from '#/logger'
import {IS_TESTFLIGHT} from '#/env'
import app from 'react-native-version-number'

export function useUpdateCheck() {
const appState = React.useRef<AppStateStatus>('active')
Expand All @@ -16,10 +17,12 @@ export function useUpdateCheck() {
const setCheckTimeout = React.useCallback(() => {
timeout.current = setTimeout(async () => {
try {
Alert.alert('Try')
await Updates.setExtraParamAsync(
'buildNumber',
app.buildVersion.toString(),
)
Alert.alert('Here')
await Updates.setExtraParamAsync(
'channel',
IS_TESTFLIGHT ? 'testflight' : 'production',
Expand All @@ -42,58 +45,62 @@ export function useUpdateCheck() {
}, 15e3)
}, [])

const onIsTestFlight = React.useCallback(() => {
setTimeout(async () => {
await Updates.setExtraParamAsync(
'buildNumber',
app.buildVersion.toString(),
)
await Updates.setExtraParamAsync(
'channel',
IS_TESTFLIGHT ? 'testflight' : 'production',
)

const res = await Updates.checkForUpdateAsync()
console.log(res)
if (res.isAvailable) {
await Updates.fetchUpdateAsync()

Alert.alert(
'Update Available',
'A new version of the app is available. Relaunch now?',
[
{
text: 'No',
style: 'cancel',
},
{
text: 'Relaunch',
style: 'default',
onPress: async () => {
await Updates.reloadAsync()
},
},
],
)
} else {
Alert.alert('No Update Available', 'No update available at this time.')
}
}, 3000)
}, [])

React.useEffect(() => {
// For Testflight users, we can prompt the user to update immediately whenever there's an available update. This
// is suspect however with the Apple App Store guidelines, so we don't want to prompt production users to update
// immediately.
if (IS_TESTFLIGHT) {
;(async () => {
await Updates.setExtraParamAsync(
'buildNumber',
app.buildVersion.toString(),
)
await Updates.setExtraParamAsync(
'channel',
IS_TESTFLIGHT ? 'testflight' : 'production',
)

const res = await Updates.checkForUpdateAsync()
if (res.isAvailable) {
await Updates.fetchUpdateAsync()

Alert.alert(
'Update Available',
'A new version of the app is available. Relaunch now?',
[
{
text: 'No',
style: 'cancel',
},
{
text: 'Relaunch',
style: 'default',
onPress: async () => {
await Updates.reloadAsync()
},
},
],
)
} else {
Alert.alert(
'No Update Available',
'No update available at this time.',
)
}
})()
if (IS_TESTFLIGHT) {
onIsTestFlight()
return
}
if (__DEV__ || ranInitialCheck.current) {
} else if (__DEV__ || ranInitialCheck.current) {
return
} else {
Alert.alert('not testfight')
}

setCheckTimeout()
ranInitialCheck.current = true
}, [setCheckTimeout])
}, [onIsTestFlight, setCheckTimeout])

// After the app has been minimized for 30 minutes, we want to either A. install an update if one has become available
// or B check for an update again.
Expand Down

0 comments on commit 6e357c1

Please sign in to comment.