mirror of
				https://gitcode.com/gh_mirrors/re/react-native-pushy.git
				synced 2025-11-04 07:13:10 +08:00 
			
		
		
		
	feat: add progress
This commit is contained in:
		@@ -107,7 +107,7 @@ function App() {
 | 
			
		||||
        </Snackbar>
 | 
			
		||||
      )}
 | 
			
		||||
      <Banner
 | 
			
		||||
        style={{position: 'absolute', top: 0}}
 | 
			
		||||
        style={{width: '100%', position: 'absolute', top: 0}}
 | 
			
		||||
        visible={showUpdateBanner}
 | 
			
		||||
        actions={[
 | 
			
		||||
          {
 | 
			
		||||
 
 | 
			
		||||
@@ -261,6 +261,7 @@ export class Pushy {
 | 
			
		||||
        data: { newVersion: hash },
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
    log('downloaded hash:', hash);
 | 
			
		||||
    setLocalHashInfo(hash, {
 | 
			
		||||
      name,
 | 
			
		||||
      description,
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,7 @@ export const defaultContext = {
 | 
			
		||||
  markSuccess: noop,
 | 
			
		||||
  dismissError: noop,
 | 
			
		||||
  downloadUpdate: noop,
 | 
			
		||||
  downloadAndInstallApk: noop,
 | 
			
		||||
  currentHash: '',
 | 
			
		||||
  packageVersion: '',
 | 
			
		||||
};
 | 
			
		||||
@@ -23,6 +24,7 @@ export const PushyContext = createContext<{
 | 
			
		||||
  markSuccess: () => void;
 | 
			
		||||
  dismissError: () => void;
 | 
			
		||||
  downloadUpdate: () => void;
 | 
			
		||||
  downloadAndInstallApk: (url: string) => void;
 | 
			
		||||
  currentHash: string;
 | 
			
		||||
  packageVersion: string;
 | 
			
		||||
  client?: Pushy;
 | 
			
		||||
 
 | 
			
		||||
@@ -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<NativeEventSubscription>();
 | 
			
		||||
  const [updateInfo, setUpdateInfo] = useState<CheckResult>();
 | 
			
		||||
  const [progress, setProgress] = useState<ProgressData>();
 | 
			
		||||
  const [lastError, setLastError] = useState<Error>();
 | 
			
		||||
 | 
			
		||||
  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}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user