-
Notifications
You must be signed in to change notification settings - Fork 0
/
iosrdi.js
95 lines (82 loc) · 2.22 KB
/
iosrdi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { Alert, Linking } from "react-native";
import DeviceInfo from "react-native-device-info";
import apisauce from "apisauce";
const defaultCheckOptions = {
bundleId: DeviceInfo.getBundleId(),
country: undefined,
};
const createAPI = () => {
const api = apisauce.create({
baseURL: "https://itunes.apple.com/",
headers: {
"Cache-Control": "no-cache",
"Content-Type": "application/json",
Accept: "application/json",
},
timeout: 10000,
});
return {
getLatest: (bundleId) => api.get("lookup?bundleId=" + bundleId),
};
};
const performCheck = ({
bundleId = defaultCheckOptions.bundleId,
country,
} = defaultCheckOptions) => {
let updateIsAvailable = false;
const api = createAPI();
return api.getLatest(bundleId, country).then((response) => {
let latestInfo = null;
if (response.data.resultCount === 1) {
latestInfo = response.data.results[0];
updateIsAvailable =
latestInfo.version !== DeviceInfo.getVersion();
}
return { updateIsAvailable, ...latestInfo };
});
};
const attemptUpgrade = (appId) => {
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 = title,
message = message,
buttonUpdateText = buttonUpdateText,
buttonCancelText = buttonCancelText,
forceUpgrade = forceUpgrade,
}
) => {
const buttons = [
{
text: buttonUpdateText,
onPress: () => attemptUpgrade(appId),
},
];
if (forceUpgrade === false || forceUpgrade === undefined) {
buttons.push({ text: buttonCancelText });
}
Alert.alert(title, message, buttons, { cancelable: !!forceUpgrade });
};
export const promptUser = (
versionRules,
bundleId = defaultCheckOptions.bundleId,
country = undefined,
) => {
performCheck({ bundleId, country }).then((result) => {
if (result.updateIsAvailable) {
const options = versionRules
showUpgradePrompt(result.trackId, options);
}
});
};
export default performCheck;