From 6c1662fce1d0caaab3996ee40962c3e95fea7410 Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Thu, 8 Feb 2024 21:44:21 +0800 Subject: [PATCH] feat: add progress --- Example/testHotUpdate/src/index.tsx | 2 +- src/client.tsx | 1 + src/context.ts | 2 ++ src/provider.tsx | 24 ++++++++++++++++++------ 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Example/testHotUpdate/src/index.tsx b/Example/testHotUpdate/src/index.tsx index d1ce078..27cb93d 100644 --- a/Example/testHotUpdate/src/index.tsx +++ b/Example/testHotUpdate/src/index.tsx @@ -107,7 +107,7 @@ function App() { )} void; dismissError: () => void; downloadUpdate: () => void; + downloadAndInstallApk: (url: string) => void; currentHash: string; packageVersion: string; client?: Pushy; diff --git a/src/provider.tsx b/src/provider.tsx index 3e1ad09..a9a3404 100644 --- a/src/provider.tsx +++ b/src/provider.tsx @@ -14,7 +14,7 @@ import { } from 'react-native'; import { Pushy } from './client'; import { currentVersion, isFirstTime, packageVersion } from './core'; -import { CheckResult } from './type'; +import { CheckResult, ProgressData } from './type'; import { PushyContext } from './context'; export const PushyProvider = ({ @@ -27,6 +27,7 @@ export const PushyProvider = ({ const { options } = client; const stateListener = useRef(); const [updateInfo, setUpdateInfo] = useState(); + const [progress, setProgress] = useState(); const [lastError, setLastError] = useState(); const dismissError = useCallback(() => { @@ -61,7 +62,7 @@ export const PushyProvider = ({ return; } try { - const hash = await client.downloadUpdate(updateInfo); + const hash = await client.downloadUpdate(updateInfo, setProgress); if (!hash) { return; } @@ -88,6 +89,15 @@ export const PushyProvider = ({ } }, [client, showAlert, updateInfo]); + const downloadAndInstallApk = useCallback( + (downloadUrl: string) => { + if (Platform.OS === 'android' && downloadUrl) { + client.downloadAndInstallApk(downloadUrl, setProgress); + } + }, + [client], + ); + const checkUpdate = useCallback(async () => { let info: CheckResult; try { @@ -106,7 +116,7 @@ export const PushyProvider = ({ onPress: () => { if (downloadUrl) { if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) { - client.downloadAndInstallApk(downloadUrl); + downloadAndInstallApk(downloadUrl); } else { Linking.openURL(downloadUrl); } @@ -130,15 +140,15 @@ export const PushyProvider = ({ ], ); } - }, [client, downloadUpdate, showAlert]); + }, [client, downloadAndInstallApk, downloadUpdate, showAlert]); const markSuccess = client.markSuccess; useEffect(() => { - if (isFirstTime) { + const { strategy, dismissErrorAfter, autoMarkSuccess } = options; + if (isFirstTime && autoMarkSuccess) { markSuccess(); } - const { strategy, dismissErrorAfter } = options; if (strategy === 'both' || strategy === 'onAppResume') { stateListener.current = AppState.addEventListener( 'change', @@ -178,6 +188,8 @@ export const PushyProvider = ({ downloadUpdate, packageVersion, currentHash: currentVersion, + progress, + downloadAndInstallApk, }} > {children}