Skip to content

Instantly share code, notes, and snippets.

@savayer
Created May 3, 2024 11:30
Show Gist options
  • Save savayer/c2241f0655514a58d5c5a2759250d4bc to your computer and use it in GitHub Desktop.
Save savayer/c2241f0655514a58d5c5a2759250d4bc to your computer and use it in GitHub Desktop.
show notification modal about new version available
const currentVersion = Constants.expoConfig.version;
getAppInfoFromTheStore(country)
.then((appInfo) => {
if (isValidVersionFormat(appInfo?.version)) {
const [currentMajor, currentMinor, currentPatch] = currentVersion
.split('.')
.map(Number);
const [newMajor, newMinor, newPatch] = appInfo.version
.split('.')
.map(Number);
if (
currentMajor < newMajor ||
currentMinor < newMinor ||
currentPatch < newPatch
) {
setStoreUrl(appInfo.storeUrl);
}
if (currentMajor < newMajor || currentMinor < newMinor) {
setShowUpdatingAppModal(true);
}
}
})
.catch((error) => {
console.error(error, 'unable to check app version');
});
import axios from 'axios';
import Constants from 'expo-constants';
import { Platform } from 'react-native';
const bundleInfo = Platform.select({
ios: {
id: Constants.expoConfig?.ios?.bundleIdentifier || 'your.bundle.id',
},
android: {
id: Constants.expoConfig?.android?.package || 'your.bundle.id',
},
});
type AppInfoFromStore = {
version: string;
storeUrl: string;
};
export async function getInfoFromAppStore(
country = '',
): Promise<AppInfoFromStore> {
try {
const { data } = await axios.get('https://itunes.apple.com/lookup', {
params: {
bundleId: bundleInfo.id,
country,
},
});
return {
storeUrl: data.results[0]?.trackViewUrl,
version: data.results[0]?.version,
};
} catch (e) {
console.error(e);
}
}
export async function getInfoFromPlayStore(
country = '',
): Promise<AppInfoFromStore | null> {
try {
const storeUrl = `https://play.google.com/store/apps/details?id=${bundleInfo.id}&hl=en&gl=US`;
const { data } = await axios.get(storeUrl);
const match = data.match(/Current Version.+?>([\d.-]+)<\/span>/);
if (match) {
const latestVersion = match[1].trim();
return { version: latestVersion, storeUrl };
}
const matchNewLayout = data.match(/\[\[\["([\d-.]+?)"\]\]/);
if (matchNewLayout) {
const latestVersion = matchNewLayout[1].trim();
return { version: latestVersion, storeUrl };
}
return null;
} catch (e) {
console.error(e);
}
}
export async function getAppInfoFromTheStore(
country = '',
): Promise<AppInfoFromStore | null> {
return Platform.select({
ios: getInfoFromAppStore,
android: getInfoFromPlayStore,
})(country);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment