mirror of
				https://gitcode.com/gh_mirrors/re/react-native-pushy.git
				synced 2025-11-04 07:13:10 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			169 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			169 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
/* eslint-disable react/no-unstable-nested-components */
 | 
						||
/* eslint-disable react-native/no-inline-styles */
 | 
						||
import React, {useState} from 'react';
 | 
						||
import {
 | 
						||
  StyleSheet,
 | 
						||
  Platform,
 | 
						||
  Text,
 | 
						||
  View,
 | 
						||
  TouchableOpacity,
 | 
						||
  Image,
 | 
						||
  Switch,
 | 
						||
} from 'react-native';
 | 
						||
import {Icon, PaperProvider, Snackbar, Banner} from 'react-native-paper';
 | 
						||
 | 
						||
import TestConsole from './TestConsole';
 | 
						||
 | 
						||
import _updateConfig from '../update.json';
 | 
						||
import {PushyProvider, Pushy, usePushy} from 'react-native-update';
 | 
						||
const {appKey} = _updateConfig[Platform.OS];
 | 
						||
 | 
						||
function App() {
 | 
						||
  const {
 | 
						||
    client,
 | 
						||
    checkUpdate,
 | 
						||
    downloadUpdate,
 | 
						||
    switchVersionLater,
 | 
						||
    switchVersion,
 | 
						||
    updateInfo,
 | 
						||
    packageVersion,
 | 
						||
    currentHash,
 | 
						||
    progress: {received, total} = {},
 | 
						||
  } = usePushy();
 | 
						||
  const [useDefaultAlert, setUseDefaultAlert] = useState(true);
 | 
						||
  const [showTestConsole, setShowTestConsole] = useState(false);
 | 
						||
  const [showUpdateBanner, setShowUpdateBanner] = useState(false);
 | 
						||
  const [showUpdateSnackbar, setShowUpdateSnackbar] = useState(false);
 | 
						||
  const snackbarVisible =
 | 
						||
    !useDefaultAlert && showUpdateSnackbar && updateInfo?.update;
 | 
						||
 | 
						||
  return (
 | 
						||
    <View style={styles.container}>
 | 
						||
      <Text style={styles.welcome}>欢迎使用Pushy热更新服务</Text>
 | 
						||
      <View style={{flexDirection: 'row'}}>
 | 
						||
        <Text>
 | 
						||
          {useDefaultAlert ? '当前使用' : '当前不使用'}默认的alert更新提示
 | 
						||
        </Text>
 | 
						||
        <Switch
 | 
						||
          value={useDefaultAlert}
 | 
						||
          onValueChange={v => {
 | 
						||
            setUseDefaultAlert(v);
 | 
						||
            client?.setOptions({
 | 
						||
              updateStrategy: v ? null : 'alwaysAlert',
 | 
						||
            });
 | 
						||
            setShowUpdateSnackbar(!v);
 | 
						||
          }}
 | 
						||
        />
 | 
						||
      </View>
 | 
						||
      <Image
 | 
						||
        resizeMode={'contain'}
 | 
						||
        source={require('./assets/shezhi.png')}
 | 
						||
        style={styles.image}
 | 
						||
      />
 | 
						||
      <Text style={styles.instructions}>
 | 
						||
        这是版本一 {'\n'}
 | 
						||
        当前原生包版本号: {packageVersion}
 | 
						||
        {'\n'}
 | 
						||
        当前热更新版本Hash: {currentHash || '(空)'}
 | 
						||
        {'\n'}
 | 
						||
      </Text>
 | 
						||
      <Text>
 | 
						||
        下载进度:{received} / {total}
 | 
						||
      </Text>
 | 
						||
      <TouchableOpacity
 | 
						||
        onPress={() => {
 | 
						||
          checkUpdate();
 | 
						||
          setShowUpdateSnackbar(true);
 | 
						||
        }}>
 | 
						||
        <Text style={styles.instructions}>点击这里检查更新</Text>
 | 
						||
      </TouchableOpacity>
 | 
						||
 | 
						||
      <TouchableOpacity
 | 
						||
        testID="testcase"
 | 
						||
        style={{marginTop: 15}}
 | 
						||
        onLongPress={() => {
 | 
						||
          setShowTestConsole(true);
 | 
						||
        }}>
 | 
						||
        <Text style={styles.instructions}>
 | 
						||
          react-native-update版本:{client?.version}
 | 
						||
        </Text>
 | 
						||
      </TouchableOpacity>
 | 
						||
      <TestConsole visible={showTestConsole} />
 | 
						||
      {snackbarVisible && (
 | 
						||
        <Snackbar
 | 
						||
          visible={snackbarVisible}
 | 
						||
          onDismiss={() => {
 | 
						||
            setShowUpdateSnackbar(false);
 | 
						||
          }}
 | 
						||
          action={{
 | 
						||
            label: '更新',
 | 
						||
            onPress: async () => {
 | 
						||
              setShowUpdateSnackbar(false);
 | 
						||
              await downloadUpdate();
 | 
						||
              setShowUpdateBanner(true);
 | 
						||
            },
 | 
						||
          }}>
 | 
						||
          <Text style={{color: 'white'}}>
 | 
						||
            有新版本({updateInfo.name})可用,是否更新?
 | 
						||
          </Text>
 | 
						||
        </Snackbar>
 | 
						||
      )}
 | 
						||
      <Banner
 | 
						||
        style={{width: '100%', position: 'absolute', top: 0}}
 | 
						||
        visible={showUpdateBanner}
 | 
						||
        actions={[
 | 
						||
          {
 | 
						||
            label: '立即重启',
 | 
						||
            onPress: switchVersion,
 | 
						||
          },
 | 
						||
          {
 | 
						||
            label: '下次再说',
 | 
						||
            onPress: () => {
 | 
						||
              switchVersionLater();
 | 
						||
              setShowUpdateBanner(false);
 | 
						||
            },
 | 
						||
          },
 | 
						||
        ]}
 | 
						||
        icon={({size}) => (
 | 
						||
          <Icon name="checkcircleo" size={size} color="#00f" />
 | 
						||
        )}>
 | 
						||
        更新已完成,是否立即重启?
 | 
						||
      </Banner>
 | 
						||
    </View>
 | 
						||
  );
 | 
						||
}
 | 
						||
 | 
						||
const styles = StyleSheet.create({
 | 
						||
  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,
 | 
						||
});
 | 
						||
 | 
						||
export default function Root() {
 | 
						||
  return (
 | 
						||
    <PushyProvider client={pushyClient}>
 | 
						||
      <PaperProvider>
 | 
						||
        <App />
 | 
						||
      </PaperProvider>
 | 
						||
    </PushyProvider>
 | 
						||
  );
 | 
						||
}
 |