mirror of
				https://gitcode.com/gh_mirrors/re/react-native-pushy.git
				synced 2025-11-04 15:23:11 +08:00 
			
		
		
		
	feat: add progress
This commit is contained in:
		@@ -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={[
 | 
				
			||||||
          {
 | 
					          {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user