-
Notifications
You must be signed in to change notification settings - Fork 43
/
flutter_app_version_checker.dart
192 lines (172 loc) · 5.54 KB
/
flutter_app_version_checker.dart
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import 'dart:convert';
import 'dart:io';
import 'dart:math' as math;
import 'package:http/http.dart' as http;
import 'package:package_info_plus/package_info_plus.dart';
enum AndroidStore { googlePlayStore, apkPure }
class AppVersionChecker {
/// The current version of the app.
/// if [currentVersion] is null the [currentVersion] will take the Flutter package version
final String? currentVersion;
/// The id of the app (com.exemple.your_app).
/// if [appId] is null the [appId] will take the Flutter package identifier
final String? appId;
/// Select The marketplace of your app
/// default will be `AndroidStore.GooglePlayStore`
final AndroidStore androidStore;
AppVersionChecker({
this.currentVersion,
this.appId,
this.androidStore = AndroidStore.googlePlayStore,
});
Future<AppCheckerResult> checkUpdate() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
final _currentVersion = currentVersion ?? packageInfo.version;
final _packageName = appId ?? packageInfo.packageName;
if (Platform.isAndroid) {
switch (androidStore) {
case AndroidStore.apkPure:
return await _checkApkPureStore(_currentVersion, _packageName);
default:
return await _checkPlayStore(_currentVersion, _packageName);
}
} else if (Platform.isIOS) {
return await _checkAppleStore(_currentVersion, _packageName);
} else {
return AppCheckerResult(_currentVersion, null, "",
'The target platform "${Platform.operatingSystem}" is not yet supported by this package.');
}
}
Future<AppCheckerResult> _checkAppleStore(
String currentVersion, String packageName) async {
String? errorMsg;
String? newVersion;
String? url;
var uri =
Uri.https("itunes.apple.com", "/lookup", {"bundleId": packageName});
try {
final response = await http.get(uri);
if (response.statusCode != 200) {
errorMsg =
"Can't find an app in the Apple Store with the id: $packageName";
} else {
final jsonObj = jsonDecode(response.body);
final List results = jsonObj['results'];
if (results.isEmpty) {
errorMsg =
"Can't find an app in the Apple Store with the id: $packageName";
} else {
newVersion = jsonObj['results'][0]['version'];
url = jsonObj['results'][0]['trackViewUrl'];
}
}
} catch (e) {
errorMsg = "$e";
}
return AppCheckerResult(
currentVersion,
newVersion,
url,
errorMsg,
);
}
Future<AppCheckerResult> _checkPlayStore(
String currentVersion, String packageName) async {
String? errorMsg;
String? newVersion;
String? url;
final uri = Uri.https(
"play.google.com", "/store/apps/details", {"id": packageName});
try {
final response = await http.get(uri);
if (response.statusCode != 200) {
errorMsg =
"Can't find an app in the Google Play Store with the id: $packageName";
} else {
newVersion = RegExp(r',\[\[\["([0-9,\.]*)"]],')
.firstMatch(response.body)!
.group(1);
url = uri.toString();
}
} catch (e) {
errorMsg = "$e";
}
return AppCheckerResult(
currentVersion,
newVersion,
url,
errorMsg,
);
}
}
Future<AppCheckerResult> _checkApkPureStore(
String currentVersion, String packageName) async {
String? errorMsg;
String? newVersion;
String? url;
Uri uri = Uri.https("apkpure.com", "$packageName/$packageName");
try {
final response = await http.get(uri);
if (response.statusCode != 200) {
errorMsg =
"Can't find an app in the ApkPure Store with the id: $packageName";
} else {
newVersion = RegExp(
r'<div class="details-sdk"><span itemprop="version">(.*?)<\/span>for Android<\/div>')
.firstMatch(response.body)!
.group(1)!
.trim();
url = uri.toString();
}
} catch (e) {
errorMsg = "$e";
}
return AppCheckerResult(
currentVersion,
newVersion,
url,
errorMsg,
);
}
class AppCheckerResult {
/// return current app version
final String currentVersion;
/// return the new app version
final String? newVersion;
/// return the app url
final String? appURL;
/// return error message if found else it will return `null`
final String? errorMessage;
AppCheckerResult(
this.currentVersion,
this.newVersion,
this.appURL,
this.errorMessage,
);
/// return `true` if update is available
bool get canUpdate =>
_shouldUpdate(currentVersion, (newVersion ?? currentVersion));
bool _shouldUpdate(String versionA, String versionB) {
final versionNumbersA =
versionA.split(".").map((e) => int.tryParse(e) ?? 0).toList();
final versionNumbersB =
versionB.split(".").map((e) => int.tryParse(e) ?? 0).toList();
final int versionASize = versionNumbersA.length;
final int versionBSize = versionNumbersB.length;
int maxSize = math.max(versionASize, versionBSize);
for (int i = 0; i < maxSize; i++) {
if ((i < versionASize ? versionNumbersA[i] : 0) >
(i < versionBSize ? versionNumbersB[i] : 0)) {
return false;
} else if ((i < versionASize ? versionNumbersA[i] : 0) <
(i < versionBSize ? versionNumbersB[i] : 0)) {
return true;
}
}
return false;
}
@override
String toString() {
return "Current Version: $currentVersion\nNew Version: $newVersion\nApp URL: $appURL\ncan update: $canUpdate\nerror: $errorMessage";
}
}