mirror of
https://github.com/zhigang1992/react-native-code-push.git
synced 2026-05-16 16:31:11 +08:00
update sample app
This commit is contained in:
142
Examples/CodePushDemoApp/demo.js
Normal file
142
Examples/CodePushDemoApp/demo.js
Normal file
@@ -0,0 +1,142 @@
|
||||
import React, { Component } from "react";
|
||||
import {
|
||||
AppRegistry,
|
||||
Dimensions,
|
||||
Image,
|
||||
StyleSheet,
|
||||
Text,
|
||||
TouchableOpacity,
|
||||
View,
|
||||
} from "react-native";
|
||||
|
||||
import CodePush from "react-native-code-push";
|
||||
|
||||
@CodePush({ checkFrequency: CodePush.CheckFrequency.MANUAL })
|
||||
class CodePushDemoApp extends Component {
|
||||
constructor() {
|
||||
super();
|
||||
this.state = { restartAllowed: true };
|
||||
}
|
||||
|
||||
codePushStatusDidChange(syncStatus) {
|
||||
console.log(this.setState);
|
||||
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.", 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 });
|
||||
}
|
||||
|
||||
sync() {
|
||||
CodePush.sync(
|
||||
{
|
||||
installMode: CodePush.InstallMode.IMMEDIATE,
|
||||
updateDialog: true
|
||||
},
|
||||
this.codePushStatusDidChange.bind(this),
|
||||
this.codePushDownloadDidProgress.bind(this)
|
||||
);
|
||||
}
|
||||
|
||||
render() {
|
||||
let syncView, syncButton, progressView;
|
||||
|
||||
if (this.state.syncMessage) {
|
||||
syncView = (
|
||||
<Text style={styles.messages}>{this.state.syncMessage}</Text>
|
||||
);
|
||||
} else {
|
||||
syncButton = (
|
||||
<TouchableOpacity onPress={this.sync.bind(this)}>
|
||||
<Text style={styles.syncButton}>Start Sync!</Text>
|
||||
</TouchableOpacity>
|
||||
);
|
||||
}
|
||||
|
||||
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>
|
||||
{syncButton}
|
||||
{syncView}
|
||||
{progressView}
|
||||
<Image style={styles.image} resizeMode={Image.resizeMode.contain} source={require("./images/laptop_phone_howitworks.png")}/>
|
||||
<TouchableOpacity onPress={this.toggleAllowRestart.bind(this)}>
|
||||
<Text style={styles.restartToggleButton}>Restart { this.state.restartAllowed ? "allowed" : "forbidden"}</Text>
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
container: {
|
||||
flex: 1,
|
||||
alignItems: "center",
|
||||
backgroundColor: "#F5FCFF",
|
||||
paddingTop: 50
|
||||
},
|
||||
image: {
|
||||
marginTop: 50,
|
||||
width: Dimensions.get("window").width - 100,
|
||||
height: 365 * (Dimensions.get("window").width - 100) / 651,
|
||||
},
|
||||
messages: {
|
||||
textAlign: "center",
|
||||
},
|
||||
restartToggleButton: {
|
||||
color: "blue",
|
||||
fontSize: 17
|
||||
},
|
||||
syncButton: {
|
||||
color: "green",
|
||||
fontSize: 17
|
||||
},
|
||||
welcome: {
|
||||
fontSize: 20,
|
||||
textAlign: "center",
|
||||
margin: 10
|
||||
},
|
||||
});
|
||||
|
||||
AppRegistry.registerComponent("CodePushDemoApp", () => CodePushDemoApp);
|
||||
Reference in New Issue
Block a user