Files

114 lines
3.3 KiB
Dart

import 'dart:convert';
import 'app_updates.dart';
typedef UpdateInstaller =
Future<void> Function(
AppUpdate update, {
void Function(double progress)? onProgress,
});
sealed class LaunchUpdate {
const LaunchUpdate();
}
class WebLaunchUpdate extends LaunchUpdate {
const WebLaunchUpdate(this.buildId);
final String buildId;
}
class AndroidLaunchUpdate extends LaunchUpdate {
const AndroidLaunchUpdate(this.update);
final AppUpdate update;
}
LaunchUpdate? webUpdateFor(String compiled, String body) {
final current = compiled.trim();
if (current.isEmpty) return null;
try {
final value = jsonDecode(body);
if (value is! Map || value['schema'] != 1 || value['platform'] != 'web') {
return null;
}
final latest = '${value['buildId'] ?? ''}'.trim();
return latest.isEmpty || latest == current ? null : WebLaunchUpdate(latest);
} on FormatException {
return null;
}
}
AndroidLaunchUpdate? androidUpdateFor(AppUpdate latest, int? installed) {
if (installed == null || latest.versionCode <= installed) return null;
return AndroidLaunchUpdate(latest);
}
/// Page hosting and RPC servers may differ; web releases belong to the page.
Uri webReleaseUri(Uri page) => Uri(
scheme: page.scheme,
host: page.host,
port: page.hasPort ? page.port : null,
path: '/release.json',
);
/// One opportunistic request per foreground visit and feed. Declined releases
/// stay dismissed for this application session, while a newer release can ask.
class LaunchUpdateChecker {
String? _attempt;
final _offered = <String>{};
void beginVisit() => _attempt = null;
Future<LaunchUpdate?> check({
required bool web,
required bool android,
required String webBuildId,
required Uri page,
required Uri? endpoint,
required Future<String?> Function(Uri) readWeb,
required Future<int?> Function() installedVersion,
required Future<AppUpdate> Function(Uri) readAndroid,
Future<AppUpdate> Function()? readNative,
String nativeScope = '',
}) async {
final feed = web
? webReleaseUri(page)
: endpoint == null
? null
: updatesBaseFor(endpoint);
if (feed == null || (web ? webBuildId.trim().isEmpty : !android)) {
return null;
}
final key = '${web ? 'web' : 'native'}:$feed:$nativeScope';
if (_attempt == key) return null;
_attempt = key;
LaunchUpdate? update;
if (web) {
final body = await readWeb(feed).timeout(const Duration(seconds: 15));
if (body != null) update = webUpdateFor(webBuildId, body);
} else {
final installed = await installedVersion().timeout(
const Duration(seconds: 15),
);
if (installed != null) {
update = androidUpdateFor(
await (readNative == null ? readAndroid(feed) : readNative()).timeout(
const Duration(seconds: 30),
),
installed,
);
}
}
return update == null || _offered.contains(_identity(update))
? null
: update;
}
void markOffered(LaunchUpdate update) => _offered.add(_identity(update));
String _identity(LaunchUpdate update) => switch (update) {
WebLaunchUpdate(:final buildId) => 'web:$buildId',
AndroidLaunchUpdate(:final update) =>
'android:${update.apk}:${update.versionCode}',
};
}