|
| 1 | +import { useState } from 'react'; |
| 2 | +import { Alert, Platform } from 'react-native'; |
| 3 | +import * as FileSystem from 'expo-file-system'; |
| 4 | +import * as IntentLauncher from 'expo-intent-launcher'; |
| 5 | +import Constants from 'expo-constants'; |
| 6 | + |
| 7 | +const GITHUB_API = 'https://api.github.com/repos/tiajinsha/JKVideo/releases/latest'; |
| 8 | + |
| 9 | +function compareVersions(a: string, b: string): number { |
| 10 | + const pa = a.replace(/^v/, '').split('.').map(Number); |
| 11 | + const pb = b.replace(/^v/, '').split('.').map(Number); |
| 12 | + for (let i = 0; i < 3; i++) { |
| 13 | + if ((pa[i] ?? 0) > (pb[i] ?? 0)) return 1; |
| 14 | + if ((pa[i] ?? 0) < (pb[i] ?? 0)) return -1; |
| 15 | + } |
| 16 | + return 0; |
| 17 | +} |
| 18 | + |
| 19 | +export function useCheckUpdate() { |
| 20 | + const currentVersion = Constants.expoConfig?.version ?? '0.0.0'; |
| 21 | + const [isChecking, setIsChecking] = useState(false); |
| 22 | + const [downloadProgress, setDownloadProgress] = useState<number | null>(null); |
| 23 | + |
| 24 | + const checkUpdate = async () => { |
| 25 | + setIsChecking(true); |
| 26 | + try { |
| 27 | + const res = await fetch(GITHUB_API, { |
| 28 | + headers: { Accept: 'application/vnd.github+json' }, |
| 29 | + }); |
| 30 | + if (!res.ok) throw new Error(`GitHub API ${res.status}`); |
| 31 | + const data = await res.json(); |
| 32 | + |
| 33 | + const latestVersion: string = data.tag_name ?? ''; |
| 34 | + const apkAsset = (data.assets as any[]).find((a) => |
| 35 | + (a.name as string).endsWith('.apk') |
| 36 | + ); |
| 37 | + const downloadUrl: string = apkAsset?.browser_download_url ?? ''; |
| 38 | + const releaseNotes: string = data.body ?? ''; |
| 39 | + |
| 40 | + if (compareVersions(latestVersion, currentVersion) <= 0) { |
| 41 | + Alert.alert('已是最新版本', `当前版本 v${currentVersion} 已是最新`); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + Alert.alert( |
| 46 | + `发现新版本 ${latestVersion}`, |
| 47 | + releaseNotes || '有新版本可用,是否立即下载?', |
| 48 | + [ |
| 49 | + { text: '取消', style: 'cancel' }, |
| 50 | + { |
| 51 | + text: '下载安装', |
| 52 | + onPress: () => downloadAndInstall(downloadUrl, latestVersion), |
| 53 | + }, |
| 54 | + ] |
| 55 | + ); |
| 56 | + } catch (e: any) { |
| 57 | + Alert.alert('检查失败', e?.message ?? '网络错误,请稍后重试'); |
| 58 | + } finally { |
| 59 | + setIsChecking(false); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + const downloadAndInstall = async (url: string, version: string) => { |
| 64 | + if (Platform.OS !== 'android') { |
| 65 | + Alert.alert('提示', '自动安装仅支持 Android 设备'); |
| 66 | + return; |
| 67 | + } |
| 68 | + const localUri = FileSystem.cacheDirectory + `JKVideo-${version}.apk`; |
| 69 | + try { |
| 70 | + setDownloadProgress(0); |
| 71 | + const downloadResumable = FileSystem.createDownloadResumable( |
| 72 | + url, |
| 73 | + localUri, |
| 74 | + {}, |
| 75 | + ({ totalBytesWritten, totalBytesExpectedToWrite }) => { |
| 76 | + if (totalBytesExpectedToWrite > 0) { |
| 77 | + setDownloadProgress( |
| 78 | + Math.round((totalBytesWritten / totalBytesExpectedToWrite) * 100) |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | + ); |
| 83 | + await downloadResumable.downloadAsync(); |
| 84 | + setDownloadProgress(null); |
| 85 | + |
| 86 | + const contentUri = await FileSystem.getContentUriAsync(localUri); |
| 87 | + await IntentLauncher.startActivityAsync('android.intent.action.VIEW', { |
| 88 | + data: contentUri, |
| 89 | + flags: 1, |
| 90 | + type: 'application/vnd.android.package-archive', |
| 91 | + }); |
| 92 | + } catch (e: any) { |
| 93 | + setDownloadProgress(null); |
| 94 | + Alert.alert('下载失败', e?.message ?? '请稍后重试'); |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + return { currentVersion, isChecking, downloadProgress, checkUpdate }; |
| 99 | +} |
0 commit comments