From 37849b1730b0a649e574ffe1c79dcd052bbb73fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B3=A2=E4=BB=94=E7=B3=95?= Date: Sat, 26 Apr 2025 21:36:02 +0800 Subject: [PATCH] add expoUsePushy demo (#495) * add expoUsePushy demo * update --- Example/expoUsePushy/.gitignore | 36 + Example/expoUsePushy/App.tsx | 221 + Example/expoUsePushy/TestConsole.js | 274 + Example/expoUsePushy/app.json | 29 + Example/expoUsePushy/assets/adaptive-icon.png | Bin 0 -> 17547 bytes Example/expoUsePushy/assets/favicon.png | Bin 0 -> 1466 bytes Example/expoUsePushy/assets/icon.png | Bin 0 -> 22380 bytes Example/expoUsePushy/assets/shezhi.png | Bin 0 -> 696 bytes Example/expoUsePushy/assets/shezhi@2x.png | Bin 0 -> 1257 bytes Example/expoUsePushy/assets/shezhi@3x.png | Bin 0 -> 1859 bytes Example/expoUsePushy/assets/splash-icon.png | Bin 0 -> 17547 bytes Example/expoUsePushy/index.js | 8 + Example/expoUsePushy/package.json | 24 + Example/expoUsePushy/tsconfig.json | 6 + Example/expoUsePushy/update.json | 14 + Example/expoUsePushy/yarn.lock | 5570 +++++++++++++++++ 16 files changed, 6182 insertions(+) create mode 100644 Example/expoUsePushy/.gitignore create mode 100644 Example/expoUsePushy/App.tsx create mode 100644 Example/expoUsePushy/TestConsole.js create mode 100644 Example/expoUsePushy/app.json create mode 100644 Example/expoUsePushy/assets/adaptive-icon.png create mode 100644 Example/expoUsePushy/assets/favicon.png create mode 100644 Example/expoUsePushy/assets/icon.png create mode 100644 Example/expoUsePushy/assets/shezhi.png create mode 100644 Example/expoUsePushy/assets/shezhi@2x.png create mode 100644 Example/expoUsePushy/assets/shezhi@3x.png create mode 100644 Example/expoUsePushy/assets/splash-icon.png create mode 100644 Example/expoUsePushy/index.js create mode 100644 Example/expoUsePushy/package.json create mode 100644 Example/expoUsePushy/tsconfig.json create mode 100644 Example/expoUsePushy/update.json create mode 100644 Example/expoUsePushy/yarn.lock diff --git a/Example/expoUsePushy/.gitignore b/Example/expoUsePushy/.gitignore new file mode 100644 index 0000000..d16e1ef --- /dev/null +++ b/Example/expoUsePushy/.gitignore @@ -0,0 +1,36 @@ +# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files + +# dependencies +node_modules/ + +# Expo +.expo/ +dist/ +web-build/ +expo-env.d.ts + +# Native +*.orig.* +*.jks +*.p8 +*.p12 +*.key +*.mobileprovision + +# Metro +.metro-health-check* + +# debug +npm-debug.* +yarn-debug.* +yarn-error.* + +# macOS +.DS_Store +*.pem + +# local env files +.env*.local + +# typescript +*.tsbuildinfo diff --git a/Example/expoUsePushy/App.tsx b/Example/expoUsePushy/App.tsx new file mode 100644 index 0000000..3059014 --- /dev/null +++ b/Example/expoUsePushy/App.tsx @@ -0,0 +1,221 @@ +/* eslint-disable react/no-unstable-nested-components */ +/* eslint-disable react-native/no-inline-styles */ +import React, {useState} from 'react'; +import {StyleSheet, Text, View, TouchableOpacity, Image} from 'react-native'; + +import TestConsole from './TestConsole'; + +import _updateConfig from './update.json'; +import {PushyProvider, Pushy, usePushy} from 'react-native-update'; +const {appKey} = _updateConfig.android; + +function Home() { + const { + client, + checkUpdate, + downloadUpdate, + switchVersionLater, + switchVersion, + updateInfo, + packageVersion, + currentHash, + progress: {received, total} = {}, + } = usePushy(); + const [useDefaultAlert, setUseDefaultAlert] = useState(false); + const [showTestConsole, setShowTestConsole] = useState(false); + const [showUpdateBanner, setShowUpdateBanner] = useState(false); + const [showUpdateSnackbar, setShowUpdateSnackbar] = useState(false); + // if (updateInfo) { + // updateInfo!.name = 'name'; + // updateInfo!.update = true; + // } + const snackbarVisible = + !useDefaultAlert && showUpdateSnackbar && updateInfo?.update; + + if (showTestConsole) { + return ( + setShowTestConsole(false)} /> + ); + } + + return ( + + 欢迎使用Pushy热更新服务 + {/* 😁hdiffFromAPP更新成功!!! */} + {/* 😁hdiffFromPPk更新成功!!! */} + + { + client?.setOptions({ + updateStrategy: !useDefaultAlert ? null : 'alwaysAlert', + }); + setShowUpdateSnackbar(useDefaultAlert); + setUseDefaultAlert(!useDefaultAlert); + }} + style={{ + flexDirection: 'row', + alignItems: 'center', + }}> + + {useDefaultAlert && } + + + {' '} + {useDefaultAlert ? '当前使用' : '当前不使用'}默认的alert更新提示 + + + + + + 这是版本一 {'\n'} + 当前原生包版本号: {packageVersion} + {'\n'} + 当前热更新版本Hash: {currentHash || '(空)'} + {'\n'} + + + 下载进度:{received} / {total} + + { + checkUpdate(); + setShowUpdateSnackbar(true); + }}> + 点击这里检查更新 + + + { + setShowTestConsole(true); + }}> + + react-native-update版本:{client?.version} + + + {snackbarVisible && ( + + + + 有新版本({updateInfo.name})可用,是否更新? + + + setShowUpdateSnackbar(false)} + style={{marginRight: 10}}> + 取消 + + { + setShowUpdateSnackbar(false); + await downloadUpdate(); + setShowUpdateBanner(true); + }}> + 更新 + + + + + )} + {showUpdateBanner && ( + + + + 更新已完成,是否立即重启? + + + { + switchVersionLater(); + setShowUpdateBanner(false); + }} + style={{marginRight: 20}}> + 下次再说 + + + 立即重启 + + + + + )} + + ); +} + +const styles = StyleSheet.create({ + overlay: { + position: 'absolute', + top: 0, + left: 0, + right: 0, + bottom: 0, + backgroundColor: 'rgba(0, 0, 0, 0.5)', + justifyContent: 'center', + alignItems: 'center', + }, + container: { + flex: 1, + justifyContent: 'center', + alignItems: 'center', + backgroundColor: '#F5FCFF', + }, + welcome: { + fontSize: 20, + textAlign: 'center', + margin: 10, + }, + instructions: { + textAlign: 'center', + color: '#333333', + marginBottom: 5, + }, + image: {}, +}); + +const pushyClient = new Pushy({ + appKey, + debug: true, +}); + +export default function HomeScreen() { + return ( + + + + ); +} diff --git a/Example/expoUsePushy/TestConsole.js b/Example/expoUsePushy/TestConsole.js new file mode 100644 index 0000000..c05fa5d --- /dev/null +++ b/Example/expoUsePushy/TestConsole.js @@ -0,0 +1,274 @@ +/* eslint-disable react-native/no-inline-styles */ +/* eslint-disable react/react-in-jsx-scope */ +import {useCallback, useMemo, useState} from 'react'; +import { + ActivityIndicator, + TextInput, + Button, + StyleSheet, + SafeAreaView, + Text, + View, + TouchableOpacity, +} from 'react-native'; + +import {PushyModule} from 'react-native-update'; +const Hash = '9D5CE6EBA420717BE7E7D308B11F8207681B066C951D68F3994D19828F342474'; +const UUID = '00000000-0000-0000-0000-000000000000'; +const DownloadUrl = 'https://localhost:3000/diff.ppk-patch'; +const AppPatchDownloadUrl = 'https://github.com/bozaigao/test_pushy_server/raw/refs/heads/main/hdiff.app-patch'; +const AppPatchHash = 'f5ba92c7c04250d4b8a446c8267ef459'; +const PPKDownloadUrl = 'https://github.com/bozaigao/test_pushy_server/raw/refs/heads/main/hdiff.ppk-patch'; +const PPKPatchHash = '6b3d26b7d868d1f67aedadb7f0b342d9'; +const OriginHash = 'f5ba92c7c04250d4b8a446c8267ef459'; + + +const CustomDialog = ({title, visible, onConfirm}) => { + if (!visible) { + return null; + } + + return ( + + + {title} + + 确认 + + + + ); +}; +export default function TestConsole({visible, onClose}) { + const [text, setText] = useState(''); + const [running, setRunning] = useState(false); + const [options, setOptions] = useState(); + const [alertVisible, setAlertVisible] = useState(false); + const [alertMsg, setAlertMsg] = useState(''); + const NativeTestMethod = useMemo(() => { + return [ + { + name: 'setLocalHashInfo', + invoke: () => { + setText( + `setLocalHashInfo\n${Hash}\n{\"version\":\"1.0.0\",\"size\":\"19M\"}`, + ); + }, + }, + { + name: 'getLocalHashInfo', + invoke: () => { + setText(`getLocalHashInfo\n${Hash}`); + }, + }, + { + name: 'setUuid', + invoke: () => { + setText(`setUuid\n${UUID}`); + }, + }, + { + name: 'reloadUpdate', + invoke: () => { + setText('reloadUpdate'); + setOptions({hash: Hash}); + }, + }, + { + name: 'setNeedUpdateForApp', + invoke: () => { + setText('setNeedUpdate'); + setOptions({hash: AppPatchHash}); + }, + }, + { + name: 'setNeedUpdateForPPK', + invoke: () => { + setText('setNeedUpdate'); + setOptions({hash: PPKPatchHash}); + }, + }, + { + name: 'markSuccess', + invoke: () => { + setText('markSuccess'); + setOptions(undefined); + }, + }, + { + name: 'downloadPatchFromPpk', + invoke: () => { + setText('downloadPatchFromPpk'); + setOptions({updateUrl: PPKDownloadUrl, hash: PPKPatchHash, originHash: OriginHash}); + }, + }, + { + name: 'downloadPatchFromPackage', + invoke: () => { + setText('downloadPatchFromPackage'); + setOptions({updateUrl: AppPatchDownloadUrl, hash: AppPatchHash}); + }, + }, + { + name: 'downloadFullUpdate', + invoke: () => { + setText('downloadFullUpdate'); + setOptions({updateUrl: DownloadUrl, hash: Hash}); + }, + }, + { + name: 'downloadAndInstallApk', + invoke: () => { + setText('downloadAndInstallApk'); + setOptions({url: DownloadUrl, target: Hash, hash: Hash}); + }, + }, + ]; + }, []); + + const renderTestView = useCallback(() => { + const views = []; + for (let i = 0; i < NativeTestMethod.length; i++) { + views.push( + { + NativeTestMethod[i].invoke(); + }}> + {NativeTestMethod[i].name} + , + ); + } + return {views}; + }, [NativeTestMethod]); + if (!visible) { + return null; + } + + return ( + + + 调试Pushy方法(方法名,参数,值换行) +