1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-09-18 23:40:38 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

feat: add progress

This commit is contained in:
sunnylqm
2024-02-08 21:44:21 +08:00
parent d9e4575964
commit 6c1662fce1
4 changed files with 22 additions and 7 deletions

View File

@@ -107,7 +107,7 @@ function App() {
</Snackbar> </Snackbar>
)} )}
<Banner <Banner
style={{position: 'absolute', top: 0}} style={{width: '100%', position: 'absolute', top: 0}}
visible={showUpdateBanner} visible={showUpdateBanner}
actions={[ actions={[
{ {

View File

@@ -261,6 +261,7 @@ export class Pushy {
data: { newVersion: hash }, data: { newVersion: hash },
}); });
} }
log('downloaded hash:', hash);
setLocalHashInfo(hash, { setLocalHashInfo(hash, {
name, name,
description, description,

View File

@@ -12,6 +12,7 @@ export const defaultContext = {
markSuccess: noop, markSuccess: noop,
dismissError: noop, dismissError: noop,
downloadUpdate: noop, downloadUpdate: noop,
downloadAndInstallApk: noop,
currentHash: '', currentHash: '',
packageVersion: '', packageVersion: '',
}; };
@@ -23,6 +24,7 @@ export const PushyContext = createContext<{
markSuccess: () => void; markSuccess: () => void;
dismissError: () => void; dismissError: () => void;
downloadUpdate: () => void; downloadUpdate: () => void;
downloadAndInstallApk: (url: string) => void;
currentHash: string; currentHash: string;
packageVersion: string; packageVersion: string;
client?: Pushy; client?: Pushy;

View File

@@ -14,7 +14,7 @@ import {
} from 'react-native'; } from 'react-native';
import { Pushy } from './client'; import { Pushy } from './client';
import { currentVersion, isFirstTime, packageVersion } from './core'; import { currentVersion, isFirstTime, packageVersion } from './core';
import { CheckResult } from './type'; import { CheckResult, ProgressData } from './type';
import { PushyContext } from './context'; import { PushyContext } from './context';
export const PushyProvider = ({ export const PushyProvider = ({
@@ -27,6 +27,7 @@ export const PushyProvider = ({
const { options } = client; const { options } = client;
const stateListener = useRef<NativeEventSubscription>(); const stateListener = useRef<NativeEventSubscription>();
const [updateInfo, setUpdateInfo] = useState<CheckResult>(); const [updateInfo, setUpdateInfo] = useState<CheckResult>();
const [progress, setProgress] = useState<ProgressData>();
const [lastError, setLastError] = useState<Error>(); const [lastError, setLastError] = useState<Error>();
const dismissError = useCallback(() => { const dismissError = useCallback(() => {
@@ -61,7 +62,7 @@ export const PushyProvider = ({
return; return;
} }
try { try {
const hash = await client.downloadUpdate(updateInfo); const hash = await client.downloadUpdate(updateInfo, setProgress);
if (!hash) { if (!hash) {
return; return;
} }
@@ -88,6 +89,15 @@ export const PushyProvider = ({
} }
}, [client, showAlert, updateInfo]); }, [client, showAlert, updateInfo]);
const downloadAndInstallApk = useCallback(
(downloadUrl: string) => {
if (Platform.OS === 'android' && downloadUrl) {
client.downloadAndInstallApk(downloadUrl, setProgress);
}
},
[client],
);
const checkUpdate = useCallback(async () => { const checkUpdate = useCallback(async () => {
let info: CheckResult; let info: CheckResult;
try { try {
@@ -106,7 +116,7 @@ export const PushyProvider = ({
onPress: () => { onPress: () => {
if (downloadUrl) { if (downloadUrl) {
if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) { if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
client.downloadAndInstallApk(downloadUrl); downloadAndInstallApk(downloadUrl);
} else { } else {
Linking.openURL(downloadUrl); Linking.openURL(downloadUrl);
} }
@@ -130,15 +140,15 @@ export const PushyProvider = ({
], ],
); );
} }
}, [client, downloadUpdate, showAlert]); }, [client, downloadAndInstallApk, downloadUpdate, showAlert]);
const markSuccess = client.markSuccess; const markSuccess = client.markSuccess;
useEffect(() => { useEffect(() => {
if (isFirstTime) { const { strategy, dismissErrorAfter, autoMarkSuccess } = options;
if (isFirstTime && autoMarkSuccess) {
markSuccess(); markSuccess();
} }
const { strategy, dismissErrorAfter } = options;
if (strategy === 'both' || strategy === 'onAppResume') { if (strategy === 'both' || strategy === 'onAppResume') {
stateListener.current = AppState.addEventListener( stateListener.current = AppState.addEventListener(
'change', 'change',
@@ -178,6 +188,8 @@ export const PushyProvider = ({
downloadUpdate, downloadUpdate,
packageVersion, packageVersion,
currentHash: currentVersion, currentHash: currentVersion,
progress,
downloadAndInstallApk,
}} }}
> >
{children} {children}