checkVersion.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // For iOS, we can force a reload of the app
  22. // This will create an infinite loop of the maintenance message
  23. // until maintenance is over
  24. setTimeout(() => {
  25. Updates.reloadAsync();
  26. }, 100);
  27. }
  28. }
  29. }
  30. ],
  31. { cancelable: false }
  32. );
  33. // Prevent any further code execution
  34. throw new Error('MAINTENANCE_MODE');
  35. }
  36. if (currentVersion && currentVersion !== fetchedVersion.version) {
  37. Alert.alert(
  38. '請更新到最新版本',
  39. 'Please update to the latest version of the app to continue.',
  40. [
  41. {
  42. text: '現在更新',
  43. onPress: () => {
  44. const url =
  45. Platform.OS === 'ios'
  46. ? 'https://apps.apple.com/app/crazy-charge/id6639612402'
  47. : 'https://play.google.com/store/apps/details?id=hk.com.crazycharge';
  48. Linking.openURL(url);
  49. }
  50. }
  51. ],
  52. { cancelable: false }
  53. );
  54. }
  55. }