-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathversion-check.js
More file actions
123 lines (115 loc) · 3.98 KB
/
Copy pathversion-check.js
File metadata and controls
123 lines (115 loc) · 3.98 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { validate } from "./validation";
import { checkVersionWithAppUpgrade } from "./api";
import { Platform, Alert, Linking } from "react-native";
import { PreferredAndroidMarket } from "app-upgrade-react-native-sdk";
async function versionCheck(appInfo, xApiKey, alertInfo, baseUrl) {
const isValid = validate(appInfo, xApiKey);
if (isValid) {
const version = await checkVersionWithAppUpgrade(appInfo, xApiKey, baseUrl);
if (!version) {
console.error("App Upgrade Error: Version is null.");
} else if (version && version.found === true) {
if (version.forceUpgrade === true) {
console.info("App Upgrade: Version force upgrade is required.");
// show force upgrade alert
showForceUpgradeAlert(appInfo, alertInfo, version.message);
} else {
console.info(
"App Upgrade: Version force upgrade is not required but upgrade is recommended."
);
// show upgrade alert
showUpgradeAlert(appInfo, alertInfo, version.message);
}
} else {
console.info(
"App Upgrade: Version information not found. No action required."
);
}
}
}
function showForceUpgradeAlert(appInfo, alertInfo, msg) {
Alert.alert(
alertInfo.title ? alertInfo.title : "Please update",
msg,
[
{
text: alertInfo.updateButtonTitle
? alertInfo.updateButtonTitle
: "Update Now",
onPress: () => {
showForceUpgradeAlert(appInfo, alertInfo, msg);
redirectToStore(appInfo);
},
},
],
{
cancelable: false,
}
);
}
function showUpgradeAlert(appInfo, alertInfo, msg) {
Alert.alert(
alertInfo.title ? alertInfo.title : "Please update",
msg,
[
{
text: alertInfo.laterButtonTitle ? alertInfo.laterButtonTitle : "Later",
onPress: () =>
alertInfo.onLaterCallback ? alertInfo.onLaterCallback() : null,
},
{
text: alertInfo.updateButtonTitle
? alertInfo.updateButtonTitle
: "Update Now",
onPress: () => {
alertInfo.onUpdateCallback ? alertInfo.onUpdateCallback() : null;
redirectToStore(appInfo);
},
},
],
{
cancelable: true,
onDismiss: () =>
alertInfo.onDismissCallback ? alertInfo.onDismissCallback() : null,
}
);
}
function redirectToStore(appInfo) {
if (Platform.OS === "android") {
const defaultGooglePlaystoreUrl = `https://play.google.com/store/apps/details?id=${appInfo.appId}`;
if (appInfo.preferredAndroidMarket === PreferredAndroidMarket.GOOGLE) {
const url = `https://play.google.com/store/apps/details?id=${appInfo.appId}`;
openPreferredAndroidMarket(url, defaultGooglePlaystoreUrl);
} else if (
appInfo.preferredAndroidMarket === PreferredAndroidMarket.HUAWEI
) {
const url = `appmarket://details?id=${appInfo.appId}`;
openPreferredAndroidMarket(url, defaultGooglePlaystoreUrl);
} else if (
appInfo.preferredAndroidMarket === PreferredAndroidMarket.AMAZON
) {
const url = `https://www.amazon.com/gp/mas/dl/android?p=${appInfo.appId}`;
openPreferredAndroidMarket(url, defaultGooglePlaystoreUrl);
} else if (
appInfo.preferredAndroidMarket === PreferredAndroidMarket.OTHER
) {
openPreferredAndroidMarket(appInfo.otherAndroidMarketUrl);
} else {
openPreferredAndroidMarket(
`https://play.google.com/store/apps/details?id=${appInfo.appId}`
);
}
} else {
Linking.openURL(`https://apps.apple.com/app/id/${appInfo.appId}`);
}
}
function openPreferredAndroidMarket(url, defaultGooglePlaystoreUrl) {
Linking.openURL(url).catch((err) => {
console.debug(
"App Upgrade Error: Could not open the preferred android market. Defaulting to Google Playstore.",
err.message
);
Linking.openURL(defaultGooglePlaystoreUrl);
});
}
export { versionCheck };