CodePushPage.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import React,{ Component } from 'react';
  2. import {
  3. Dimensions, Image,
  4. StyleSheet,
  5. Text,
  6. TouchableOpacity,
  7. View,
  8. } from 'react-native';
  9. import CodePush from "react-native-code-push";
  10. import BasePage from "./BasePage";
  11. class CodePushPa extends BasePage {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. restartAllowed: true,
  16. title:''
  17. };
  18. }
  19. codePushStatusDidChange(syncStatus) {
  20. switch(syncStatus) {
  21. case CodePush.SyncStatus.CHECKING_FOR_UPDATE:
  22. this.setState({ syncMessage: "Checking for update." });
  23. break;
  24. case CodePush.SyncStatus.DOWNLOADING_PACKAGE:
  25. this.setState({ syncMessage: "Downloading package." });
  26. break;
  27. case CodePush.SyncStatus.AWAITING_USER_ACTION:
  28. this.setState({ syncMessage: "Awaiting user action." });
  29. break;
  30. case CodePush.SyncStatus.INSTALLING_UPDATE:
  31. this.setState({ syncMessage: "Installing update." });
  32. break;
  33. case CodePush.SyncStatus.UP_TO_DATE:
  34. this.setState({ syncMessage: "App up to date.", progress: false });
  35. break;
  36. case CodePush.SyncStatus.UPDATE_IGNORED:
  37. this.setState({ syncMessage: "Update cancelled by user.", progress: false });
  38. break;
  39. case CodePush.SyncStatus.UPDATE_INSTALLED:
  40. this.setState({ syncMessage: "Update installed and will be applied on restart.", progress: false });
  41. break;
  42. case CodePush.SyncStatus.UNKNOWN_ERROR:
  43. this.setState({ syncMessage: "An unknown error occurred.", progress: false });
  44. break;
  45. }
  46. }
  47. codePushDownloadDidProgress(progress) {
  48. this.setState({ progress });
  49. }
  50. toggleAllowRestart() {
  51. this.state.restartAllowed
  52. ? CodePush.disallowRestart()
  53. : CodePush.allowRestart();
  54. this.setState({ restartAllowed: !this.state.restartAllowed });
  55. }
  56. getUpdateMetadata() {
  57. // CodePush.getUpdateMetadata(CodePush.UpdateState.RUNNING)
  58. // .then((metadata) => {
  59. // this.setState({ syncMessage: metadata ? JSON.stringify(metadata) : "Running binary version", progress: false });
  60. // }, (error) => {
  61. // this.setState({ syncMessage: "Error: " + error, progress: false });
  62. // });
  63. }
  64. /** Update is downloaded silently, and applied on restart (recommended) */
  65. sync() {
  66. CodePush.sync(
  67. {},
  68. this.codePushStatusDidChange.bind(this),
  69. this.codePushDownloadDidProgress.bind(this)
  70. );
  71. }
  72. /** Update pops a confirmation dialog, and then immediately reboots the app */
  73. syncImmediate() {
  74. CodePush.sync(
  75. { installMode: CodePush.InstallMode.IMMEDIATE, updateDialog: true },
  76. this.codePushStatusDidChange.bind(this),
  77. this.codePushDownloadDidProgress.bind(this)
  78. );
  79. }
  80. render() {
  81. let progressView;
  82. if (this.state.progress) {
  83. progressView = (
  84. <Text style={styles.messages}>{this.state.progress.receivedBytes} of {this.state.progress.totalBytes} bytes received</Text>
  85. );
  86. }
  87. return (
  88. <View style={styles.container} >
  89. <Text style={styles.welcome}>
  90. Welcome to CodePush!
  91. </Text>
  92. <TouchableOpacity onPress={this.sync.bind(this)}>
  93. <Text style={styles.syncButton}>Press for background sync</Text>
  94. </TouchableOpacity>
  95. <TouchableOpacity onPress={this.syncImmediate.bind(this)}>
  96. <Text style={styles.syncButton}>Press for dialog-driven sync</Text>
  97. </TouchableOpacity>
  98. {progressView}
  99. <TouchableOpacity onPress={this.toggleAllowRestart.bind(this)}>
  100. <Text style={styles.restartToggleButton}>Restart { this.state.restartAllowed ? "allowed" : "forbidden"}</Text>
  101. </TouchableOpacity>
  102. <TouchableOpacity onPress={this.getUpdateMetadata.bind(this)}>
  103. <Text style={styles.syncButton}>Press for Update Metadata</Text>
  104. </TouchableOpacity>
  105. <Text style={styles.messages}>{this.state.syncMessage || ""}</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;