index.js 7.53 KB
Newer Older
Nguyễn Thị Thúy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import React, {useEffect, useState} from 'react';
import {
    View,
    Text,
    Modal,
    TouchableOpacity,
    StyleSheet,
    Image,
    TouchableWithoutFeedback,
    Linking, Platform,
} from 'react-native';
import R from '../../assets/R';
import {getFontXD, getHeight, getWidth, HEIGHTXD, WIDTHXD} from '../../Config/Functions';
import I18n from '../../helper/i18/i18n';
import DeviceInfo from 'react-native-device-info';
import {getNewestVersionInfo} from '../../apis/Functions/users';
Nguyễn Thị Thúy committed
17
import InAppUpdate from '../../helper/InAppUpdate';
Nguyễn Thị Thúy committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

const VersionChecker = (props) => {
    const [visible, setVisible] = useState(false);
    const [isForceUpdate, setIsForceUpdate] = useState(false);
    const [version, setVersion] = useState('1.0');
    useEffect(() => {
        checkVersion();
    }, []);

    const checkVersion = async () => {
        const verCurrent = DeviceInfo.getVersion();
        const res = await getNewestVersionInfo({
            platform: Platform.OS,
        });
        if ((res.data.code = 200 && res.data.data)) {
Nguyễn Thị Thúy committed
33 34 35 36 37
            console.log(res);
            if (res.data.data[0].version_name !== verCurrent || res.data.data[0].build.toString() !== DeviceInfo.getBuildNumber) {
                setVersion(res.data.data[0].version_name);
                setVisible(true);
                setIsForceUpdate(res.data.data[0].is_require_update == 0 ? false : true);
Nguyễn Thị Thúy committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
            }
        }
    };
    const setVisibleModal = (visible, version, isForceUpdate) => {
        setVisible(visible);
        setVersion(version);
        setIsForceUpdate(isForceUpdate);
    };

    const _renderDivider = () => <View style={styles.dividerStyle}/>;

    const onOutsidePressed = () => {
        setVisible(false);
    };

    const onUpdatePressed = async () => {
        try {
            if (Platform.OS === 'ios') {
Nguyễn Thị Thúy committed
56
                Linking.openURL('itms-apps://itunes.apple.com/us/app/dcv-invest/id1556621903?l=id');
Nguyễn Thị Thúy committed
57
            } else {
Nguyễn Thị Thúy committed
58 59 60 61 62 63
                // try {
                //     InAppUpdate.checkUpdate()
                // } catch(e) {
                //     console.log(e)
                // }
                Linking.openURL('https://play.google.com/store/apps/details?id=com.dcv.invest');
Nguyễn Thị Thúy committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
            }
        } catch (error) {
        }
    };

    const onRequestClose = () => null;

    const renderBackdrop = () => {
        return (
            <View
                style={{
                    backgroundColor: 'rgba(0,0,0,0.30)',
                    // backgroundColor: 'red',
                    position: 'absolute',
                    top: 0,
                    bottom: 0,
                    left: 0,
                    right: 0,

                    width: getWidth(),
                    height: getHeight(),
                }}
            />
        );
    };

    const cancelUpdate = () => {
        setVisible(false);
    };

    return (
        <Modal
            onRequestClose={() => onRequestClose()}
            transparent
            animationType="fade"
            style={{position: 'absolute'}}
            visible={visible}
        >
            {renderBackdrop()}
            <View pointerEvents="box-none" style={styles.containerStyle}>
                <View style={styles.imageUpgradeContainer} zIndex={100}>
                    <Image
Nguyễn Thị Thúy committed
106 107
                        source={R.images.iconUpgrade}
                        style={[styles.imageUpgradeStyle, {tintColor: R.colors.main}]}/>
Nguyễn Thị Thúy committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121
                </View>

                <View style={styles.contentContainerStyle}>
                    <Text style={styles.titleStyle}>{I18n.t('Update')}</Text>
                    <Text style={styles.versionLabelStyle}>
                        {I18n.t('Version')}
                        {': '}
                        {version}
                    </Text>

                    <Text style={styles.descStyle}>{I18n.t('UpdateDescription')}</Text>

                    {_renderDivider()}
                    {isForceUpdate ?
Nguyễn Thị Thúy committed
122
                        <TouchableOpacity onPress={() => onUpdatePressed()} style={styles.notNowContainerStyle}>
Nguyễn Thị Thúy committed
123 124 125 126 127 128 129 130
                            <Text
                                style={[styles.textNotNowStyle, {color: R.colors.main}]}>{I18n.t('Update')}</Text>
                        </TouchableOpacity>
                        :
                        <View style={[styles.notNowContainerStyle, {
                            flexDirection: 'row',
                            marginHorizontal: WIDTHXD(100),
                        }]}>
Nguyễn Thị Thúy committed
131
                            <TouchableOpacity onPress={() => cancelUpdate()}
Giang Tran committed
132
                                              style={[styles.btnButton, {paddingRight: WIDTHXD(60) }]}>
Nguyễn Thị Thúy committed
133
                                <Text
Giang Tran committed
134
                                    style={[styles.textNotNowStyle, {color: R.colors.color777, textAlign: 'right'}]}>{I18n.t('Cancel')}</Text>
Nguyễn Thị Thúy committed
135 136
                            </TouchableOpacity>
                            <View style={styles.dividerStyleVertical}></View>
Nguyễn Thị Thúy committed
137
                            <TouchableOpacity onPress={() => onUpdatePressed()}
Giang Tran committed
138
                                              style={[styles.btnButton, {paddingLeft: WIDTHXD(60)}]}>
Nguyễn Thị Thúy committed
139
                                <Text
Giang Tran committed
140
                                    style={[styles.textNotNowStyle, {color: R.colors.main, textAlign: 'left'}]}>{I18n.t('Update')}</Text>
Nguyễn Thị Thúy committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
                            </TouchableOpacity>
                        </View>
                    }
                </View>
            </View>
        </Modal>
    );
};

const styles = StyleSheet.create({
    imageUpgradeStyle: {
        width: 50,
        height: 50,
        tintColor: R.colors.primaryColor,
    },
    imageUpgradeContainer: {
        width: 80,
        height: 80,
        borderRadius: 40,
        backgroundColor: 'white',
        alignItems: 'center',
        justifyContent: 'center',
    },
    containerStyle: {
        flex: 1,
        width: getWidth() * 0.8,
        alignSelf: 'center',
        alignItems: 'center',
        justifyContent: 'center',
    },

    contentContainerStyle: {
        marginTop: -40,
        paddingTop: 40,
        width: getWidth() * 0.9,
        backgroundColor: 'white',
        alignItems: 'center',
        justifyContent: 'center',
        borderRadius: 15,
    },

    logoStyle: {
        width: 65,
        height: 65,
        marginVertical: 20,
    },
    titleStyle: {
        fontWeight: '600',
        fontSize: 20,
        color: 'black',
        paddingHorizontal: 8,
        textAlign: 'center',
    },

    versionLabelStyle: {
        fontSize: 14,
        color: R.colors.grey600,
        marginTop: 5,
        marginBottom: 15,
        paddingHorizontal: 8,
        textAlign: 'center',
    },

    descStyle: {
        fontSize: getFontXD(46),
        color: R.colors.grey900,
        marginBottom: 20,
        paddingHorizontal: 10,
        textAlign: 'center',
    },
    notNowContainerStyle: {
        height: HEIGHTXD(160),
        width: '100%',
        alignItems: 'center',
        justifyContent: 'center',
    },

    btnButton: {
Giang Tran committed
219
        flex: 1,
Nguyễn Thị Thúy committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    },

    textNotNowStyle: {
        fontSize: getFontXD(46),
        width: '100%',
        textAlign: 'center',
        color: R.colors.primaryColor,
    },

    starContainer: {
        flexDirection: 'row',
        width: '100%',
        justifyContent: 'center',
        alignItems: 'center',
        height: 45,
    },
    dividerStyle: {
        height: 0.5,
        width: '100%',
        backgroundColor: R.colors.borderC,
    },
    dividerStyleVertical: {
        height: HEIGHTXD(160),
        width: 0.5,
        backgroundColor: R.colors.borderC,
    },
});

export default VersionChecker;