|
| 1 | +import * as React from 'react' |
| 2 | +import { Linking, Platform } from 'react-native' |
| 3 | +import { getVersion } from 'react-native-device-info' |
| 4 | + |
| 5 | +import { ButtonsModal } from '../components/modals/ButtonsModal' |
| 6 | +import { Airship } from '../components/services/AirshipInstance' |
| 7 | +import { getLocaleOrDefaultString } from '../locales/intl' |
| 8 | +import { lstrings } from '../locales/strings' |
| 9 | +import { config } from '../theme/appConfig' |
| 10 | +import { infoServerData } from './network' |
| 11 | + |
| 12 | +const compareVersions = (v1: string, v2: string): number => { |
| 13 | + const v1Parts = v1.split('-')[0].split('.').map(Number) |
| 14 | + const v2Parts = v2.split('-')[0].split('.').map(Number) |
| 15 | + |
| 16 | + for (let i = 0; i < 3; i++) { |
| 17 | + if (v1Parts[i] > v2Parts[i]) return 1 |
| 18 | + if (v1Parts[i] < v2Parts[i]) return -1 |
| 19 | + } |
| 20 | + return 0 |
| 21 | +} |
| 22 | + |
| 23 | +export const checkAppVersion = async (): Promise<void> => { |
| 24 | + const currentVersion = getVersion() |
| 25 | + const platform = Platform.OS |
| 26 | + |
| 27 | + // Get latest version from info server |
| 28 | + const updateInfo = infoServerData.rollup?.updateInfo |
| 29 | + if (updateInfo == null || updateInfo[platform] == null) return |
| 30 | + const { updateVersion, localeMessage } = updateInfo[platform] |
| 31 | + |
| 32 | + const message = getLocaleOrDefaultString(localeMessage) |
| 33 | + |
| 34 | + // Compare versions |
| 35 | + if (compareVersions(updateVersion, currentVersion) > 0) { |
| 36 | + // Show update modal |
| 37 | + const updateRes = await Airship.show<'update' | undefined>(bridge => ( |
| 38 | + <ButtonsModal |
| 39 | + bridge={bridge} |
| 40 | + title={lstrings.update_available} |
| 41 | + message={message} |
| 42 | + buttons={{ |
| 43 | + update: { |
| 44 | + label: lstrings.update_now, |
| 45 | + type: 'primary' |
| 46 | + } |
| 47 | + }} |
| 48 | + /> |
| 49 | + )) |
| 50 | + if (updateRes === 'update') { |
| 51 | + const url = Platform.OS === 'android' ? config.playStore : config.appStore |
| 52 | + await Linking.openURL(url) |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments