Skip to content

Instantly share code, notes, and snippets.

@jwthanh
Created November 30, 2018 08:02
Show Gist options
  • Save jwthanh/f8f7fee089a4c5cfb114575b3e05fc41 to your computer and use it in GitHub Desktop.
Save jwthanh/f8f7fee089a4c5cfb114575b3e05fc41 to your computer and use it in GitHub Desktop.
CodePush
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import codePush from 'react-native-code-push';
class CodePushStatus extends Component {
state = {};
componentDidMount() {
codePush.getUpdateMetadata(codePush.UpdateState.RUNNING).then((update) => {
if (update) {
this.setState({
appVersion: update?.appVersion,
codePushVersion: update?.label,
});
} else {
codePush.getCurrentPackage().then((result) => {
this.setState({
appVersion: result?.appVersion,
codePushVersion: result?.label,
});
});
}
});
}
render() {
let status = false;
switch (this.props.status) {
case codePush.SyncStatus.CHECKING_FOR_UPDATE:
status = 'Đang kiểm tra cập nhật';
break;
case codePush.SyncStatus.DOWNLOADING_PACKAGE:
status = `Đang tải ${this.props.progress}`;
break;
case codePush.SyncStatus.INSTALLING_UPDATE:
status = 'Đang cài đặt';
break;
case codePush.SyncStatus.UPDATE_INSTALLED:
status = 'Đã cài đặt\r\nMở lại ứng dụng để cập nhật';
break;
case codePush.SyncStatus.UP_TO_DATE:
default:
status = `${this.state?.appVersion && this.state?.codePushVersion && 'Phiên bản '}${
this.state?.appVersion ? this.state?.appVersion : ''
}${this.state?.codePushVersion ? ` (${this.state.codePushVersion})` : ''}`;
break;
}
return (
<View style={[{ height: 30, alignItems: 'center' }, this.props.style]}>
<Text style={{ color: 'green', fontSize: 10, textAlign: 'center' }}>
{status !== undefined && status !== 'undefined' ? status : ''}
</Text>
</View>
);
}
}
CodePushStatus.propTypes = {
status: PropTypes.oneOfType([PropTypes.number, PropTypes.bool]),
progress: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
style: PropTypes.oneOfType([PropTypes.number, PropTypes.object, PropTypes.array]),
};
CodePushStatus.defaultProps = {
status: false,
progress: '...',
style: {},
};
const mapStateToProps = state => ({
status: state.codepush.status,
progress: state.codepush.progress,
});
export default connect(mapStateToProps)(CodePushStatus);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment