Selaa lähdekoodia

Merge branch 'master' of ssh://10.10.100.21/source/UU_RN_M

huangyp 6 vuotta sitten
vanhempi
commit
62e8fa07b7

+ 13 - 5
App.js

@@ -9,7 +9,7 @@
 
 import React, {Component} from 'react';
 import {Platform, StyleSheet, Text, View} from 'react-native';
-
+import CodePushPage from './units/components/pages/CodePushPage.js'
 const instructions = Platform.select({
     ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
     android:
@@ -19,12 +19,14 @@ const instructions = Platform.select({
 
 
 export default class App extends Component{
+
+
     render() {
+
+
         return (
             <View style={styles.container}>
-              <Text style={styles.welcome}>Welcome to React Native!</Text>
-              <Text style={styles.instructions}>To get started, edit App.js</Text>
-              <Text style={styles.instructions}>{instructions}</Text>
+               <CodePushPage />
             </View>
         );
     }
@@ -34,7 +36,6 @@ const styles = StyleSheet.create({
     container: {
         flex: 1,
         justifyContent: 'center',
-        alignItems: 'center',
         backgroundColor: '#F5FCFF',
     },
     welcome: {
@@ -47,4 +48,11 @@ const styles = StyleSheet.create({
         color: '#333333',
         marginBottom: 5,
     },
+    navBar_leftButton:{
+
+        padding: 8,
+        paddingLeft:12,
+
+
+    },
 });

+ 1 - 0
package.json

@@ -10,6 +10,7 @@
     "axios": "^0.19.0",
     "react": "16.8.3",
     "react-native": "0.59.9",
+    "react-native-code-push": "^5.6.1",
     "react-native-gesture-handler": "^1.3.0",
     "react-navigation": "^3.11.0",
     "react-redux": "^7.1.0",

+ 179 - 0
units/components/pages/CodePushPage.js

@@ -0,0 +1,179 @@
+import React,{ Component } from 'react';
+import {
+    Dimensions,
+    StyleSheet,
+    Text,
+    TouchableOpacity,
+    View,
+} from 'react-native';
+import CodePush from "react-native-code-push";
+
+class CodePushPa extends Component {
+    constructor(props) {
+        super(props);
+
+        this.state = {
+            restartAllowed: true,
+            title:''
+        };
+
+
+    }
+
+    codePushStatusDidChange(syncStatus) {
+        switch(syncStatus) {
+            case CodePush.SyncStatus.CHECKING_FOR_UPDATE:
+                this.setState({ syncMessage: "Checking for update." });
+                break;
+            case CodePush.SyncStatus.DOWNLOADING_PACKAGE:
+                this.setState({ syncMessage: "Downloading package." });
+                break;
+            case CodePush.SyncStatus.AWAITING_USER_ACTION:
+                this.setState({ syncMessage: "Awaiting user action." });
+                break;
+            case CodePush.SyncStatus.INSTALLING_UPDATE:
+                this.setState({ syncMessage: "Installing update." });
+                break;
+            case CodePush.SyncStatus.UP_TO_DATE:
+
+                this.setState({ syncMessage: "App up to date.", progress: false });
+                break;
+            case CodePush.SyncStatus.UPDATE_IGNORED:
+                this.setState({ syncMessage: "Update cancelled by user.", progress: false });
+                break;
+            case CodePush.SyncStatus.UPDATE_INSTALLED:
+
+                this.setState({ syncMessage: "Update installed and will be applied on restart.", progress: false });
+                break;
+            case CodePush.SyncStatus.UNKNOWN_ERROR:
+                this.setState({ syncMessage: "An unknown error occurred.", progress: false });
+                break;
+        }
+    }
+
+    codePushDownloadDidProgress(progress) {
+        this.setState({ progress });
+    }
+
+    toggleAllowRestart() {
+        this.state.restartAllowed
+            ? CodePush.disallowRestart()
+            : CodePush.allowRestart();
+
+        this.setState({ restartAllowed: !this.state.restartAllowed });
+    }
+
+    getUpdateMetadata() {
+        CodePush.getUpdateMetadata(CodePush.UpdateState.RUNNING)
+            .then((metadata) => {
+                this.setState({ syncMessage: metadata ? JSON.stringify(metadata) : "Running binary version", progress: false });
+            }, (error) => {
+                this.setState({ syncMessage: "Error: " + error, progress: false });
+            });
+    }
+
+    /** Update is downloaded silently, and applied on restart (recommended) */
+    sync() {
+        CodePush.sync(
+            {},
+            this.codePushStatusDidChange.bind(this),
+            this.codePushDownloadDidProgress.bind(this)
+        );
+    }
+
+    /** Update pops a confirmation dialog, and then immediately reboots the app */
+    syncImmediate() {
+        CodePush.sync(
+            { installMode: CodePush.InstallMode.IMMEDIATE, updateDialog: true },
+            this.codePushStatusDidChange.bind(this),
+            this.codePushDownloadDidProgress.bind(this)
+        );
+    }
+
+
+
+    render() {
+
+        let progressView;
+
+        if (this.state.progress) {
+            progressView = (
+                <Text style={styles.messages}>{this.state.progress.receivedBytes} of {this.state.progress.totalBytes} bytes received</Text>
+            );
+        }
+
+        return (
+
+            <View style={styles.container} >
+
+                <Text style={styles.welcome}>
+                    Welcome to CodePush!
+                </Text>
+                <TouchableOpacity onPress={this.sync.bind(this)}>
+                    <Text style={styles.syncButton}>Press for background sync</Text>
+                </TouchableOpacity>
+                <TouchableOpacity onPress={this.syncImmediate.bind(this)}>
+                    <Text style={styles.syncButton}>Press for dialog-driven sync</Text>
+                </TouchableOpacity>
+                {progressView}
+                <TouchableOpacity onPress={this.toggleAllowRestart.bind(this)}>
+                    <Text style={styles.restartToggleButton}>Restart { this.state.restartAllowed ? "allowed" : "forbidden"}</Text>
+                </TouchableOpacity>
+                <TouchableOpacity onPress={this.getUpdateMetadata.bind(this)}>
+                    <Text style={styles.syncButton}>Press for Update Metadata</Text>
+                </TouchableOpacity>
+                <Text style={styles.messages}>{this.state.syncMessage || ""}</Text>
+
+                <Text style={styles.messages}></Text>
+
+            </View>
+        );
+    }
+}
+
+const styles = StyleSheet.create({
+    container: {
+
+        flex:1,
+        flexDirection:'column',
+        backgroundColor:'white',
+    },
+    image: {
+        margin: 30,
+        width: Dimensions.get("window").width - 100,
+        height: 365 * (Dimensions.get("window").width - 100) / 651,
+    },
+    messages: {
+        marginTop: 30,
+        margin: 10,
+        textAlign: "center",
+    },
+    restartToggleButton: {
+        color: "blue",
+        textAlign: "center",
+        fontSize: 17,
+        margin: 10
+    },
+    syncButton: {
+        color: "green",
+        textAlign: "center",
+        fontSize: 17,
+        margin: 10,
+    },
+    welcome: {
+        fontSize: 20,
+        textAlign: "center",
+        margin: 20
+    },
+});
+
+/**
+ * Configured with a MANUAL check frequency for easy testing. For production apps, it is recommended to configure a
+ * different check frequency, such as ON_APP_START, for a 'hands-off' approach where CodePush.sync() does not
+ * need to be explicitly called. All options of CodePush.sync() are also available in this decorator.
+ */
+let codePushOptions = { checkFrequency: CodePush.CheckFrequency.MANUAL };
+
+const CodePushPage = CodePush(codePushOptions)(CodePushPa);
+
+export default CodePushPage;

+ 16 - 0
units/components/pages/HomeScreen.js

@@ -4,6 +4,8 @@ import {View, Text, Button,TouchableHighlight,StyleSheet} from "react-native";
 import {changeBtnText} from "../../actions/bi/index";
 import YRHttpRequest from  "../../utils/network/fetch"
 import {API} from "../../utils/network/axios/api.config";
+import {NativeModules} from 'react-native';
+const YRRnBridge = NativeModules.YRRnBridge;
 
 class HomeScreen extends React.Component {
 
@@ -58,6 +60,20 @@ class HomeScreen extends React.Component {
                     <Text style={styles.text} > Touch Here </Text>
                 </TouchableHighlight>
 
+                <Button title="iOS返回测试"
+                        onPress={() => {
+
+                                 YRRnBridge.goBack();
+
+                        }}/>
+
+                <Button title="热更新测试"
+                        onPress={() => {
+
+                            navigation.navigate('CodePushPage', {name: '热更新'});
+
+                        }}/>
+
             </View>
         );
     }

+ 8 - 0
units/containers/AppContainer.js

@@ -1,6 +1,7 @@
 import { createStackNavigator, createAppContainer } from "react-navigation";
 import HomeScreen from "../components/pages/HomeScreen";
 import {DetailsScreen} from "../components/pages/DetailsScreen";
+import CodePushPage from "../components/pages/CodePushPage";
 
 export const AppNavigator = createStackNavigator({
     Home: {
@@ -16,6 +17,13 @@ export const AppNavigator = createStackNavigator({
             title: '详情',
             headerBackTitle:'返回',//设置返回此页面的返回按钮文案,有长度限制
         }
+    },
+    CodePushPage:{
+        screen:CodePushPage,//热更新测试页面
+        navigationOptions : {
+            title: '热更新',
+            headerBackTitle:'热更新',
+        }
     }
 }, {
     initialRouteName: 'Home',