CodePushPage.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. goBack() {
  80. YR
  81. }
  82. render() {
  83. let progressView;
  84. if (this.state.progress) {
  85. progressView = (
  86. <Text style={styles.messages}>{this.state.progress.receivedBytes} of {this.state.progress.totalBytes} bytes received</Text>
  87. );
  88. }
  89. return (
  90. <View style={styles.container} >
  91. <TouchableOpacity onPress={this.getUpdateMetadata.bind(this)}>
  92. <Text style={styles.syncButton}>Welcome to CodePush!</Text>
  93. </TouchableOpacity>
  94. <TouchableOpacity onPress={this.sync.bind(this)}>
  95. <Text style={styles.syncButton}>Press for background sync</Text>
  96. </TouchableOpacity>
  97. <TouchableOpacity onPress={this.syncImmediate.bind(this)}>
  98. <Text style={styles.syncButton}>Press for dialog-driven sync</Text>
  99. </TouchableOpacity>
  100. {progressView}
  101. <TouchableOpacity onPress={this.toggleAllowRestart.bind(this)}>
  102. <Text style={styles.restartToggleButton}>Restart { this.state.restartAllowed ? "allowed" : "forbidden"}</Text>
  103. </TouchableOpacity>
  104. <TouchableOpacity onPress={this.getUpdateMetadata.bind(this)}>
  105. <Text style={styles.syncButton}>Press for Update Metadata</Text>
  106. </TouchableOpacity>
  107. <Text style={styles.messages}>{this.state.syncMessage || ""}</Text>
  108. </View>
  109. );
  110. }
  111. }
  112. const styles = StyleSheet.create({
  113. container: {
  114. flex:1,
  115. flexDirection:'column',
  116. backgroundColor:'white',
  117. },
  118. image: {
  119. margin: 30,
  120. width: Dimensions.get("window").width - 100,
  121. height: 365 * (Dimensions.get("window").width - 100) / 651,
  122. },
  123. messages: {
  124. marginTop: 30,
  125. margin: 10,
  126. textAlign: "center",
  127. },
  128. restartToggleButton: {
  129. color: "blue",
  130. textAlign: "center",
  131. fontSize: 17,
  132. margin: 10
  133. },
  134. syncButton: {
  135. color: "green",
  136. textAlign: "center",
  137. fontSize: 17,
  138. margin: 10,
  139. },
  140. welcome: {
  141. fontSize: 20,
  142. textAlign: "center",
  143. margin: 20
  144. },
  145. });
  146. /**
  147. * Configured with a MANUAL check frequency for easy testing. For production apps, it is recommended to configure a
  148. * different check frequency, such as ON_APP_START, for a 'hands-off' approach where CodePush.sync() does not
  149. * need to be explicitly called. All options of CodePush.sync() are also available in this decorator.
  150. */
  151. let codePushOptions = { checkFrequency: CodePush.CheckFrequency.MANUAL };
  152. const CodePushPage = CodePush(codePushOptions)(CodePushPa);
  153. export default CodePushPage;