|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { |
| 3 | + AppRegistry, |
| 4 | + Dimensions, |
| 5 | + Image, |
| 6 | + SafeAreaView, |
| 7 | + ScrollView, |
| 8 | + StyleSheet, |
| 9 | + Text, |
| 10 | + TouchableOpacity, |
| 11 | + View, |
| 12 | +} from 'react-native'; |
| 13 | + |
| 14 | +import CodePush from "react-native-code-push"; |
| 15 | + |
| 16 | +class App extends Component<{}> { |
| 17 | + constructor() { |
| 18 | + super(); |
| 19 | + this.state = { restartAllowed: true }; |
| 20 | + } |
| 21 | + |
| 22 | + codePushStatusDidChange(syncStatus) { |
| 23 | + switch(syncStatus) { |
| 24 | + case CodePush.SyncStatus.CHECKING_FOR_UPDATE: |
| 25 | + this.setState({ syncMessage: "Checking for update." }); |
| 26 | + break; |
| 27 | + case CodePush.SyncStatus.DOWNLOADING_PACKAGE: |
| 28 | + this.setState({ syncMessage: "Downloading package." }); |
| 29 | + break; |
| 30 | + case CodePush.SyncStatus.AWAITING_USER_ACTION: |
| 31 | + this.setState({ syncMessage: "Awaiting user action." }); |
| 32 | + break; |
| 33 | + case CodePush.SyncStatus.INSTALLING_UPDATE: |
| 34 | + this.setState({ syncMessage: "Installing update." }); |
| 35 | + break; |
| 36 | + case CodePush.SyncStatus.UP_TO_DATE: |
| 37 | + this.setState({ syncMessage: "App up to date.", progress: false }); |
| 38 | + break; |
| 39 | + case CodePush.SyncStatus.UPDATE_IGNORED: |
| 40 | + this.setState({ syncMessage: "Update cancelled by user.", progress: false }); |
| 41 | + break; |
| 42 | + case CodePush.SyncStatus.UPDATE_INSTALLED: |
| 43 | + this.setState({ syncMessage: "Update installed and will be applied on restart.", progress: false }); |
| 44 | + break; |
| 45 | + case CodePush.SyncStatus.UNKNOWN_ERROR: |
| 46 | + this.setState({ syncMessage: "An unknown error occurred.", progress: false }); |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + codePushDownloadDidProgress(progress) { |
| 52 | + this.setState({ progress }); |
| 53 | + } |
| 54 | + |
| 55 | + toggleAllowRestart() { |
| 56 | + this.state.restartAllowed |
| 57 | + ? CodePush.disallowRestart() |
| 58 | + : CodePush.allowRestart(); |
| 59 | + |
| 60 | + this.setState({ restartAllowed: !this.state.restartAllowed }); |
| 61 | + } |
| 62 | + |
| 63 | + getUpdateMetadata() { |
| 64 | + CodePush.getUpdateMetadata(CodePush.UpdateState.RUNNING) |
| 65 | + .then((metadata: LocalPackage) => { |
| 66 | + this.setState({ syncMessage: metadata ? JSON.stringify(metadata) : "Running binary version", progress: false }); |
| 67 | + }, (error: any) => { |
| 68 | + this.setState({ syncMessage: "Error: " + error, progress: false }); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + /** Update is downloaded silently, and applied on restart (recommended) */ |
| 73 | + sync() { |
| 74 | + CodePush.sync( |
| 75 | + {}, |
| 76 | + this.codePushStatusDidChange.bind(this), |
| 77 | + this.codePushDownloadDidProgress.bind(this) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** Update pops a confirmation dialog, and then immediately reboots the app */ |
| 82 | + syncImmediate() { |
| 83 | + CodePush.sync( |
| 84 | + { installMode: CodePush.InstallMode.IMMEDIATE, updateDialog: true }, |
| 85 | + this.codePushStatusDidChange.bind(this), |
| 86 | + this.codePushDownloadDidProgress.bind(this) |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + restartApp() { |
| 91 | + CodePush.restartApp(); |
| 92 | + } |
| 93 | + |
| 94 | + render() { |
| 95 | + let progressView; |
| 96 | + |
| 97 | + if (this.state.progress) { |
| 98 | + progressView = ( |
| 99 | + <Text style={styles.messages}>{this.state.progress.receivedBytes} of {this.state.progress.totalBytes} bytes received</Text> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + return ( |
| 104 | + <SafeAreaView> |
| 105 | + <ScrollView> |
| 106 | + <View style={styles.container}> |
| 107 | + <Text style={styles.welcome}> |
| 108 | + Welcome to CodePush! |
| 109 | + </Text> |
| 110 | + <Text style={styles.welcome}> |
| 111 | + Demo bundle |
| 112 | + </Text> |
| 113 | + <TouchableOpacity onPress={this.restartApp.bind(this)}> |
| 114 | + <Text style={styles.syncButton}>Press to restart</Text> |
| 115 | + </TouchableOpacity> |
| 116 | + <TouchableOpacity onPress={this.sync.bind(this)}> |
| 117 | + <Text style={styles.syncButton}>Press for background sync</Text> |
| 118 | + </TouchableOpacity> |
| 119 | + <TouchableOpacity onPress={this.syncImmediate.bind(this)}> |
| 120 | + <Text style={styles.syncButton}>Press for dialog-driven sync</Text> |
| 121 | + </TouchableOpacity> |
| 122 | + {progressView} |
| 123 | + <TouchableOpacity onPress={this.toggleAllowRestart.bind(this)}> |
| 124 | + <Text style={styles.restartToggleButton}>Restart { this.state.restartAllowed ? "allowed" : "forbidden"}</Text> |
| 125 | + </TouchableOpacity> |
| 126 | + <TouchableOpacity onPress={this.getUpdateMetadata.bind(this)}> |
| 127 | + <Text style={styles.syncButton}>Press for Update Metadata</Text> |
| 128 | + </TouchableOpacity> |
| 129 | + <Text style={styles.messages}>{this.state.syncMessage || ""}</Text> |
| 130 | + </View> |
| 131 | + </ScrollView> |
| 132 | + </SafeAreaView> |
| 133 | + ); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +const styles = StyleSheet.create({ |
| 138 | + container: { |
| 139 | + flex: 1, |
| 140 | + alignItems: "center", |
| 141 | + backgroundColor: "#F5FCFF", |
| 142 | + paddingTop: 50 |
| 143 | + }, |
| 144 | + image: { |
| 145 | + margin: 30, |
| 146 | + width: Dimensions.get("window").width - 100, |
| 147 | + height: 365 * (Dimensions.get("window").width - 100) / 651, |
| 148 | + }, |
| 149 | + messages: { |
| 150 | + marginTop: 30, |
| 151 | + textAlign: "center", |
| 152 | + }, |
| 153 | + restartToggleButton: { |
| 154 | + color: "blue", |
| 155 | + fontSize: 17 |
| 156 | + }, |
| 157 | + syncButton: { |
| 158 | + color: "green", |
| 159 | + fontSize: 17 |
| 160 | + }, |
| 161 | + welcome: { |
| 162 | + fontSize: 20, |
| 163 | + textAlign: "center", |
| 164 | + margin: 20 |
| 165 | + }, |
| 166 | +}); |
| 167 | + |
| 168 | +/** |
| 169 | + * Configured with a MANUAL check frequency for easy testing. For production apps, it is recommended to configure a |
| 170 | + * different check frequency, such as ON_APP_START, for a 'hands-off' approach where CodePush.sync() does not |
| 171 | + * need to be explicitly called. All options of CodePush.sync() are also available in this decorator. |
| 172 | + */ |
| 173 | +let codePushOptions = { checkFrequency: CodePush.CheckFrequency.MANUAL }; |
| 174 | + |
| 175 | +App = CodePush(codePushOptions)(App); |
| 176 | + |
| 177 | +export default App; |
0 commit comments