CodePushPage.js 5.7 KB

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