Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removing react-native-siren #174

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
},
"dependencies": {
"react-native-device-info": "10.3.0",
"react-native-siren": "0.0.5",
"semver": "7.2.1",
"underscore": "1.12.1"
},
Expand Down
107 changes: 107 additions & 0 deletions react-native-siren/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Alert, Linking } from 'react-native';
import DeviceInfo from 'react-native-device-info';
import apisauce from 'apisauce';

const createAPI = (baseURL = 'https://itunes.apple.com/') => {
const api = apisauce.create({
baseURL,
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/json',
'Accept': 'application/json',
},
timeout: 10000,
});

return {
getLatest: (bundleId, country = undefined) =>
api.get('lookup', { bundleId, country }),
};
};

const defaultCheckOptions = {
bundleId: DeviceInfo.getBundleId(),
country: undefined,
};
const performCheck = ({
bundleId = defaultCheckOptions.bundleId,
country,
} = defaultCheckOptions) => {
let updateIsAvailable = false;
const api = createAPI();

// Call API
return api.getLatest(bundleId, country).then((response) => {
let latestInfo = null;
// Did we get our exact result?
if (response.ok && response.data.resultCount === 1) {
latestInfo = response.data.results[0];
// check for version difference

updateIsAvailable = latestInfo.version !== DeviceInfo.getVersion();
}

return { updateIsAvailable, ...latestInfo };
});
};

const attemptUpgrade = (appId) => {
// failover if itunes - a bit excessive
const appStoreURI = `itms-apps://apps.apple.com/app/id${appId}?mt=8`;
const appStoreURL = `https://apps.apple.com/app/id${appId}?mt=8`;

Linking.canOpenURL(appStoreURI).then((supported) => {
if (supported) {
Linking.openURL(appStoreURI);
} else {
Linking.openURL(appStoreURL);
}
});
};

const showUpgradePrompt = (
appId,
{
title = 'Update Available',
message = 'There is an updated version available on the App Store. Would you like to upgrade?',
buttonUpgradeText = 'Upgrade',
buttonCancelText = 'Cancel',
forceUpgrade = false,
}
) => {
const buttons = [
{
text: buttonUpgradeText,
onPress: () => attemptUpgrade(appId),
},
];

if (forceUpgrade === false) {
buttons.push({ text: buttonCancelText });
}

Alert.alert(title, message, buttons, { cancelable: !!forceUpgrade });
};

const promptUser = (
defaultOptions = {},
versionSpecificOptions = [],
bundleId,
country = undefined
) => {
performCheck({ bundleId, country }).then((sirenResult) => {
if (sirenResult.updateIsAvailable) {
const options =
versionSpecificOptions.find(
(o) => o.localVersion === DeviceInfo.getVersion()
) || defaultOptions;

showUpgradePrompt(sirenResult.trackId, options);
}
});
};

export default {
promptUser,
performCheck,
};
2 changes: 1 addition & 1 deletion src/InAppUpdates.ios.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-expect-error
import Siren from 'react-native-siren';
import Siren from '../react-native-siren';

import { compareVersions } from './utils';
import InAppUpdatesBase from './InAppUpdatesBase';
Expand Down