checkVersion.tsx 2.4 KB

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