-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(UpdateChecker): 添加最新版本检查功能并优化更新提示
- Loading branch information
Showing
7 changed files
with
193 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,150 @@ | ||
import * as Updates from 'expo-updates'; | ||
import React, { useCallback } from 'react'; | ||
import { Alert, ToastAndroid } from 'react-native'; | ||
import { Alert, Linking, ToastAndroid } from 'react-native'; | ||
import HapticFeedback, { HapticFeedbackTypes } from 'react-native-haptic-feedback'; | ||
import { ActivityIndicator, List, useTheme } from 'react-native-paper'; | ||
import RNRestart from 'react-native-restart'; | ||
import useSWR from 'swr'; | ||
import packageData from '../../package.json'; | ||
import { useAppSelector } from '../hook'; | ||
import { selectDevModeEnabled } from '../redux/slices'; | ||
import type { Main } from '../types/latestRelease'; | ||
|
||
export const UpdateChecker = () => { | ||
const appTheme = useTheme(); | ||
const isDev = useAppSelector(selectDevModeEnabled); | ||
const appTheme = useTheme(); | ||
const isDev = useAppSelector(selectDevModeEnabled); | ||
|
||
const { | ||
currentlyRunning, | ||
isUpdatePending, | ||
isChecking, | ||
isDownloading, | ||
lastCheckForUpdateTimeSinceRestart: lastCheck, | ||
availableUpdate, | ||
} = Updates.useUpdates(); | ||
const userRepo = packageData.homepage.split('/').slice(-2).join('/'); | ||
const { data } = useSWR<Main>(`https://api.github.com/repos/${userRepo}/releases/latest`); | ||
const latestRelease = data?.tag_name; | ||
const isLatest = latestRelease === packageData.version; | ||
|
||
const showCurrent = useCallback(() => { | ||
if (isDev) { | ||
HapticFeedback.trigger(HapticFeedbackTypes.effectClick); | ||
if (availableUpdate) { | ||
Alert.alert( | ||
'Available update info', | ||
JSON.stringify(availableUpdate, null, 2) | ||
); | ||
} else { | ||
Alert.alert( | ||
'Current update info', | ||
JSON.stringify(currentlyRunning, null, 2) | ||
); | ||
} | ||
} | ||
}, [availableUpdate, currentlyRunning, isDev]); | ||
|
||
const fetchUpdateAndRestart = async () => { | ||
try { | ||
await Updates.fetchUpdateAsync(); | ||
RNRestart.Restart(); | ||
} catch (err) { | ||
ToastAndroid.show( | ||
`Error updating app: ${JSON.stringify(err)}`, | ||
ToastAndroid.LONG | ||
); | ||
} | ||
}; | ||
const { | ||
currentlyRunning, | ||
isUpdatePending, | ||
isChecking, | ||
isDownloading, | ||
lastCheckForUpdateTimeSinceRestart: lastCheck, | ||
availableUpdate, | ||
} = Updates.useUpdates(); | ||
|
||
const performUpdateAlert = useCallback(() => { | ||
const showCurrent = useCallback(() => { | ||
if (isDev) { | ||
HapticFeedback.trigger(HapticFeedbackTypes.effectClick); | ||
if (availableUpdate) { | ||
Alert.alert( | ||
availableUpdate?.createdAt | ||
? `New update available:\n${availableUpdate?.createdAt.toLocaleString()}` | ||
: 'New update available', | ||
'Do you want to update now?', | ||
[ | ||
{ text: 'Cancel' }, | ||
{ | ||
text: 'OK', | ||
onPress: isUpdatePending | ||
? () => RNRestart.Restart() | ||
: fetchUpdateAndRestart, | ||
}, | ||
] | ||
'Available update info', | ||
JSON.stringify(availableUpdate, null, 2) | ||
); | ||
}, [availableUpdate?.createdAt, isUpdatePending]); | ||
} else { | ||
Alert.alert( | ||
'Current update info', | ||
JSON.stringify(currentlyRunning, null, 2) | ||
); | ||
} | ||
} | ||
}, [availableUpdate, currentlyRunning, isDev]); | ||
|
||
const checkForUpdate = async () => { | ||
try { | ||
const updateCheckRes = await Updates.checkForUpdateAsync(); | ||
const fetchUpdateAndRestart = async () => { | ||
try { | ||
await Updates.fetchUpdateAsync(); | ||
RNRestart.Restart(); | ||
} catch (err) { | ||
ToastAndroid.show( | ||
`Error updating app: ${JSON.stringify(err)}`, | ||
ToastAndroid.LONG | ||
); | ||
} | ||
}; | ||
|
||
if (updateCheckRes.isAvailable) { | ||
performUpdateAlert(); | ||
} else { | ||
ToastAndroid.show('No updates available', ToastAndroid.SHORT); | ||
} | ||
} catch (err) { | ||
Alert.alert( | ||
'Error checking for updates', | ||
JSON.stringify(err, null, 2) | ||
); | ||
} | ||
}; | ||
const performUpdateAlert = useCallback(() => { | ||
Alert.alert( | ||
availableUpdate?.createdAt | ||
? `New update available:\n${availableUpdate?.createdAt.toLocaleString()}` | ||
: 'New update available', | ||
'Do you want to update now?', | ||
[ | ||
{ text: 'Cancel' }, | ||
{ | ||
text: 'OK', | ||
onPress: isUpdatePending | ||
? () => RNRestart.Restart() | ||
: fetchUpdateAndRestart, | ||
}, | ||
] | ||
); | ||
}, [availableUpdate?.createdAt, isUpdatePending]); | ||
|
||
const handleUpdatePress = isUpdatePending ? performUpdateAlert : checkForUpdate; | ||
const description = isDownloading | ||
? 'Downloading update...' | ||
: isUpdatePending | ||
? 'Update is pending...' | ||
: isChecking | ||
? 'Checking for updates...' | ||
: `Last checked: ${lastCheck?.toLocaleString() || 'Never'}`; | ||
const checkForUpdate = async () => { | ||
try { | ||
const updateCheckRes = await Updates.checkForUpdateAsync(); | ||
|
||
const renderUpdateIcon = useCallback((props: any) => { | ||
const updateIcon = isUpdatePending | ||
? 'progress-download' | ||
: 'cloud-download-outline'; | ||
if (updateCheckRes.isAvailable) { | ||
performUpdateAlert(); | ||
} else { | ||
ToastAndroid.show('No updates available', ToastAndroid.SHORT); | ||
} | ||
} catch (err) { | ||
Alert.alert( | ||
'Error checking for updates', | ||
JSON.stringify(err, null, 2) | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<List.Icon | ||
{...props} | ||
icon={updateIcon} | ||
color={isUpdatePending | ||
? appTheme.colors.primary | ||
: props.color} | ||
/> | ||
); | ||
}, [appTheme.colors.primary, isUpdatePending]); | ||
const handleUpdatePress = isLatest ? | ||
isUpdatePending ? performUpdateAlert : checkForUpdate | ||
: () => Alert.alert( | ||
'New version available', | ||
`Your version: ${packageData.version}\nLatest version: ${latestRelease}`, | ||
[ | ||
{ text: 'Cancel' }, | ||
{ | ||
text: 'OK', onPress: () => { | ||
Linking.openURL(data?.assets_url || 'https://github.com'); | ||
} | ||
} | ||
] | ||
); | ||
|
||
const isProcessing = isChecking || isDownloading; | ||
const renderActivityIndicator = useCallback((props: any) => ( | ||
isProcessing ? <ActivityIndicator {...props} /> : null | ||
), [isProcessing]); | ||
const description = isDownloading | ||
? 'Downloading update...' | ||
: isUpdatePending | ||
? 'Update is pending...' | ||
: isChecking | ||
? 'Checking for updates...' | ||
: `Last checked: ${lastCheck?.toLocaleString() || 'Never'}`; | ||
|
||
const renderUpdateIcon = useCallback((props: any) => { | ||
const updateIcon = isUpdatePending | ||
? 'progress-download' | ||
: 'cloud-download-outline'; | ||
|
||
return ( | ||
<List.Item | ||
title="Check for updates" | ||
description={description} | ||
onPress={handleUpdatePress} | ||
onLongPress={showCurrent} | ||
disabled={isProcessing} | ||
left={renderUpdateIcon} | ||
right={renderActivityIndicator} | ||
/> | ||
<List.Icon | ||
{...props} | ||
icon={updateIcon} | ||
color={isUpdatePending | ||
? appTheme.colors.primary | ||
: props.color} | ||
/> | ||
); | ||
}, [appTheme.colors.primary, isUpdatePending]); | ||
|
||
const isProcessing = isChecking || isDownloading; | ||
const renderActivityIndicator = useCallback((props: any) => ( | ||
isProcessing ? <ActivityIndicator {...props} /> : null | ||
), [isProcessing]); | ||
|
||
return ( | ||
<List.Item | ||
title="Check for updates" | ||
description={description} | ||
onPress={handleUpdatePress} | ||
onLongPress={showCurrent} | ||
disabled={isProcessing} | ||
left={renderUpdateIcon} | ||
right={renderActivityIndicator} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
export interface Main { | ||
url: string; | ||
assets_url: string; | ||
upload_url: string; | ||
html_url: string; | ||
id: number; | ||
author: Author; | ||
node_id: string; | ||
tag_name: string; | ||
target_commitish: string; | ||
name: string; | ||
draft: boolean; | ||
prerelease: boolean; | ||
created_at: Date; | ||
published_at: Date; | ||
assets: Asset[]; | ||
tarball_url: string; | ||
zipball_url: string; | ||
body: string; | ||
} | ||
|
||
export interface Asset { | ||
url: string; | ||
id: number; | ||
node_id: string; | ||
name: string; | ||
label: string; | ||
uploader: Author; | ||
content_type: string; | ||
state: string; | ||
size: number; | ||
download_count: number; | ||
created_at: Date; | ||
updated_at: Date; | ||
browser_download_url: string; | ||
} | ||
|
||
export interface Author { | ||
login: string; | ||
id: number; | ||
node_id: string; | ||
avatar_url: string; | ||
gravatar_id: string; | ||
url: string; | ||
html_url: string; | ||
followers_url: string; | ||
following_url: string; | ||
gists_url: string; | ||
starred_url: string; | ||
subscriptions_url: string; | ||
organizations_url: string; | ||
repos_url: string; | ||
events_url: string; | ||
received_events_url: string; | ||
type: string; | ||
user_view_type: string; | ||
site_admin: boolean; | ||
} |