checkVersion.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import Constants from 'expo-constants';
  2. import * as Linking from 'expo-linking';
  3. import { Alert, Platform, BackHandler } from 'react-native';
  4. // import * as Updates from 'expo-updates';
  5. export function checkVersion(fetchedVersion: { version: string; isActive: boolean }) {
  6. const currentVersion = Constants.expoConfig?.version;
  7. if (!fetchedVersion.isActive) {
  8. Alert.alert(
  9. '伺服器維護中,敬請見諒',
  10. '',
  11. [
  12. {
  13. text: '確認',
  14. onPress: () => {
  15. if (Platform.OS === 'android') {
  16. BackHandler.exitApp();
  17. } else {
  18. return;
  19. }
  20. }
  21. }
  22. ],
  23. { cancelable: false }
  24. );
  25. // Prevent any further code execution
  26. throw new Error('MAINTENANCE_MODE');
  27. }
  28. function compareVersions(v1: string, v2: string): boolean {
  29. const v1Parts = v1.split('.').map(Number);
  30. const v2Parts = v2.split('.').map(Number);
  31. for (let i = 0; i < v1Parts.length; i++) {
  32. if (v1Parts[i] < v2Parts[i]) return true;
  33. if (v1Parts[i] > v2Parts[i]) return false;
  34. }
  35. return false;
  36. }
  37. // if (currentVersion && currentVersion < fetchedVersion.version) {
  38. if (currentVersion && compareVersions(currentVersion, fetchedVersion.version)) {
  39. Alert.alert(
  40. '請更新到最新版本',
  41. 'Please update to the latest version of the app to continue.',
  42. [
  43. {
  44. text: '現在更新',
  45. onPress: () => {
  46. const url =
  47. Platform.OS === 'ios'
  48. ? 'https://apps.apple.com/hk/app/crazycharge-pro/id6748374363'
  49. : 'https://play.google.com/store/apps/details?id=hk.ayf.crazycharge';
  50. Linking.openURL(url);
  51. }
  52. },
  53. { text: '取消', style: 'cancel' }
  54. ],
  55. { cancelable: false }
  56. );
  57. }
  58. }