-
-
Notifications
You must be signed in to change notification settings - Fork 282
/
upgrade_store_controller.dart
281 lines (253 loc) · 8.62 KB
/
upgrade_store_controller.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright (c) 2024 Larry Aasen. All rights reserved.
import 'dart:async';
import 'package:version/version.dart';
import 'appcast.dart';
import 'itunes_search_api.dart';
import 'play_store_search_api.dart';
import 'upgrade_os.dart';
import 'upgrade_state.dart';
import 'upgrader_version_info.dart';
abstract class UpgraderStore {
Future<UpgraderVersionInfo> getVersionInfo(
{required UpgraderState state,
required Version installedVersion,
required String? country,
required String? language});
}
class UpgraderAppStore extends UpgraderStore {
@override
Future<UpgraderVersionInfo> getVersionInfo(
{required UpgraderState state,
required Version installedVersion,
required String? country,
required String? language}) async {
if (state.packageInfo == null) return UpgraderVersionInfo();
String? appStoreListingURL;
Version? appStoreVersion;
bool? isCriticalUpdate;
Version? minAppVersion;
String? releaseNotes;
final iTunes = ITunesSearchAPI();
iTunes.debugLogging = state.debugLogging;
iTunes.client = state.client;
iTunes.clientHeaders = state.clientHeaders;
final response = await (iTunes.lookupByBundleId(
state.packageInfo!.packageName,
country: country,
language: language));
if (response != null) {
final version = iTunes.version(response);
if (version != null) {
try {
appStoreVersion = Version.parse(version);
} catch (e) {
if (state.debugLogging) {
print(
'upgrader: UpgraderAppStore.appStoreVersion "$version" exception: $e');
}
}
}
appStoreListingURL = iTunes.trackViewUrl(response);
releaseNotes ??= iTunes.releaseNotes(response);
minAppVersion = iTunes.minAppVersion(response);
if (minAppVersion != null) {
if (state.debugLogging) {
print('upgrader: UpgraderAppStore.minAppVersion: $minAppVersion');
}
}
}
final versionInfo = UpgraderVersionInfo(
installedVersion: installedVersion,
appStoreListingURL: appStoreListingURL,
appStoreVersion: appStoreVersion,
isCriticalUpdate: isCriticalUpdate,
minAppVersion: minAppVersion,
releaseNotes: releaseNotes,
);
if (state.debugLogging) {
print('upgrader: UpgraderAppStore: version info: $versionInfo');
}
return versionInfo;
}
}
class UpgraderPlayStore extends UpgraderStore {
@override
Future<UpgraderVersionInfo> getVersionInfo(
{required UpgraderState state,
required Version installedVersion,
required String? country,
required String? language}) async {
if (state.packageInfo == null) return UpgraderVersionInfo();
final id = state.packageInfo!.packageName;
final playStore = PlayStoreSearchAPI(
client: state.client, clientHeaders: state.clientHeaders);
playStore.debugLogging = state.debugLogging;
String? appStoreListingURL;
Version? appStoreVersion;
bool? isCriticalUpdate;
Version? minAppVersion;
String? releaseNotes;
final response =
await playStore.lookupById(id, country: country, language: language);
if (response != null) {
final version = playStore.version(response);
if (version != null) {
try {
appStoreVersion = Version.parse(version);
} catch (e) {
if (state.debugLogging) {
print(
'upgrader: UpgraderPlayStore.appStoreVersion "$version" exception: $e');
}
}
}
appStoreListingURL ??=
playStore.lookupURLById(id, language: language, country: country);
releaseNotes ??= playStore.releaseNotes(response);
final mav = playStore.minAppVersion(response);
if (mav != null) {
try {
final minVersion = mav.toString();
minAppVersion = Version.parse(minVersion);
if (state.debugLogging) {
print('upgrader: UpgraderPlayStore.minAppVersion: $minAppVersion');
}
} catch (e) {
if (state.debugLogging) {
print('upgrader: UpgraderPlayStore.minAppVersion exception: $e');
}
}
}
}
final versionInfo = UpgraderVersionInfo(
installedVersion: installedVersion,
appStoreListingURL: appStoreListingURL,
appStoreVersion: appStoreVersion,
isCriticalUpdate: isCriticalUpdate,
minAppVersion: minAppVersion,
releaseNotes: releaseNotes,
);
if (state.debugLogging) {
print('upgrader: UpgraderPlayStore: version info: $versionInfo');
}
return versionInfo;
}
}
class UpgraderAppcastStore extends UpgraderStore {
UpgraderAppcastStore({
required this.appcastURL,
this.appcast,
});
final String appcastURL;
final Appcast? appcast;
@override
Future<UpgraderVersionInfo> getVersionInfo(
{required UpgraderState state,
required Version installedVersion,
required String? country,
required String? language}) async {
String? appStoreListingURL;
Version? appStoreVersion;
bool? isCriticalUpdate;
String? releaseNotes;
final localAppcast = appcast ??
Appcast(
client: state.client,
clientHeaders: state.clientHeaders,
upgraderDevice: state.upgraderDevice,
upgraderOS: state.upgraderOS);
await localAppcast.parseAppcastItemsFromUri(appcastURL);
if (state.debugLogging) {
var count = localAppcast.items == null ? 0 : localAppcast.items!.length;
print('upgrader: UpgraderAppcastStore item count: $count');
}
final criticalUpdateItem = localAppcast.bestCriticalItem();
final criticalVersion = criticalUpdateItem?.versionString ?? '';
final bestItem = localAppcast.bestItem();
if (bestItem != null &&
bestItem.versionString != null &&
bestItem.versionString!.isNotEmpty) {
if (state.debugLogging) {
print('upgrader: UpgraderAppcastStore best item version: '
'${bestItem.versionString}');
print('upgrader: UpgraderAppcastStore critical update item version: '
'${criticalUpdateItem?.versionString}');
}
try {
if (criticalVersion.isNotEmpty &&
installedVersion < Version.parse(criticalVersion)) {
isCriticalUpdate = true;
}
} catch (e) {
if (state.debugLogging) {
print(
'upgrader: UpgraderAppcastStore: getVersionInfo could not parse version info $e');
}
}
if (bestItem.versionString != null) {
try {
appStoreVersion = Version.parse(bestItem.versionString!);
} catch (e) {
if (state.debugLogging) {
print(
'upgrader: UpgraderAppcastStore: best item version could not be parsed: '
'${bestItem.versionString}');
}
}
}
appStoreListingURL = bestItem.fileURL;
releaseNotes = bestItem.itemDescription;
}
final versionInfo = UpgraderVersionInfo(
installedVersion: installedVersion,
appStoreListingURL: appStoreListingURL,
appStoreVersion: appStoreVersion,
isCriticalUpdate: isCriticalUpdate,
releaseNotes: releaseNotes,
);
if (state.debugLogging) {
print('upgrader: UpgraderAppcastStore: version info: $versionInfo');
}
return versionInfo;
}
}
/// A controller that provides the store details for each platform.
class UpgraderStoreController {
/// Creates a controller that provides the store details for each platform.
UpgraderStoreController({
this.onAndroid = onAndroidStore,
this.onFuchsia,
this.oniOS = onIOSStore,
this.onLinux,
this.onMacOS,
this.onWeb,
this.onWindows,
});
final UpgraderStore Function()? onAndroid;
final UpgraderStore Function()? onFuchsia;
final UpgraderStore Function()? oniOS;
final UpgraderStore Function()? onLinux;
final UpgraderStore Function()? onMacOS;
final UpgraderStore Function()? onWeb;
final UpgraderStore Function()? onWindows;
UpgraderStore? getUpgraderStore(UpgraderOS upgraderOS) {
switch (upgraderOS.currentOSType) {
case UpgraderOSType.android:
return onAndroid?.call();
case UpgraderOSType.fuchsia:
return onFuchsia?.call();
case UpgraderOSType.ios:
return oniOS?.call();
case UpgraderOSType.linux:
return onLinux?.call();
case UpgraderOSType.macos:
return onMacOS?.call();
case UpgraderOSType.web:
return onWeb?.call();
case UpgraderOSType.windows:
return onWindows?.call();
}
}
static UpgraderStore onAndroidStore() => UpgraderPlayStore();
static UpgraderStore onIOSStore() => UpgraderAppStore();
}