Skip to content

Commit

Permalink
fix: comments solved
Browse files Browse the repository at this point in the history
  • Loading branch information
JuniorSancho14 committed May 6, 2024
1 parent e9dc23b commit 9c9ddcf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 58 deletions.
38 changes: 26 additions & 12 deletions src/features/force-update-modal/force-update-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import { Button, Modal, Text, View } from 'react-native';

import { translate } from 'localization/hooks';
import { isUpdated, openStore } from 'hooks/force-update';

import { isUpdated, openStore } from 'network/services/force-update';
import { translate } from 'localization/hooks';

import { styles } from './force-update-modal.styles';

interface ForceUpdateProps {}

const ForceUpdateModal = ({}: ForceUpdateProps) => (
<Modal animationType="slide" transparent={true} visible={isUpdated()}>
<View style={styles.centeredView}>
<View style={styles.modalView}>
<Text style={styles.modalText}>{translate('alert.update')}</Text>
<Button title={translate('buttons.ok')} onPress={openStore} />
const ForceUpdateModal = ({}: ForceUpdateProps) => {
const [showModal, setShowModal] = useState(false);

useEffect(() => {
const checkAndUpdate = async () => {
const updated = await isUpdated();
if (!updated) {
setShowModal(true);
}
};
checkAndUpdate();
}, []);

return (
<Modal animationType="slide" transparent={true} visible={showModal}>
<View style={styles.centeredView}>
<View style={styles.modalView}>
<Text style={styles.modalText}>{translate('alert.update')}</Text>
<Button title={translate('buttons.ok')} onPress={openStore} />
</View>
</View>
</View>
</Modal>
);
</Modal>
);
};

export { ForceUpdateModal };
44 changes: 44 additions & 0 deletions src/hooks/force-update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Linking, Platform } from 'react-native';
import DeviceInfo from 'react-native-device-info';

import remoteConfig from '@react-native-firebase/remote-config';

export const isUpdated = async (): Promise<boolean> => {
remoteConfig().setDefaults({
minimum_version: '',
});

const fetchedRemotely = await remoteConfig().fetchAndActivate();

if (fetchedRemotely) {
const remoteVersion = remoteConfig().getValue('').asString();
const localVersion = DeviceInfo.getVersion();
return compareVersions(localVersion, remoteVersion);
}
return true;
};

export const openStore = () => {
Platform.select({
ios: Linking.openURL(''),
android: Linking.openURL(''),
});
};

function compareVersions(local: string, remote: string): boolean {
const localVersion = local.split('.').map(versionNumber => parseInt(versionNumber, 10));
const remoteVersion = remote.split('.').map(versionNumber => parseInt(versionNumber, 10));

for (let i = 0; i < Math.max(localVersion.length, remoteVersion.length); i++) {
const localVersionComponent = localVersion[i] || 0;
const remoteVersionComponent = remoteVersion[i] || 0;

if (localVersionComponent < remoteVersionComponent) {
return false;
} else if (localVersionComponent > remoteVersionComponent) {
return true;
}
}

return true;
}
46 changes: 0 additions & 46 deletions src/network/services/force-update.ts

This file was deleted.

0 comments on commit 9c9ddcf

Please sign in to comment.