Build APK / build (push) Successful in 2m33s
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
47 lines
1.6 KiB
Dart
47 lines
1.6 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
/// Vérifie s'il existe une version plus récente (API `/version`) et, si oui,
|
|
/// ouvre l'URL de l'APK (le navigateur télécharge, l'utilisateur installe —
|
|
/// robuste, sans dépendance de packaging fragile).
|
|
class UpdateService {
|
|
UpdateService(this._baseUrl);
|
|
final Future<String> _baseUrl;
|
|
|
|
Future<UpdateInfo?> check() async {
|
|
try {
|
|
final base = await _baseUrl;
|
|
final res = await http.get(Uri.parse('$base/version')).timeout(const Duration(seconds: 6));
|
|
if (res.statusCode != 200) return null;
|
|
final data = jsonDecode(res.body) as Map<String, dynamic>;
|
|
final remote = data['version_code'] as int? ?? 0;
|
|
final info = await PackageInfo.fromPlatform();
|
|
final current = int.tryParse(info.buildNumber) ?? 0;
|
|
if (remote > current) {
|
|
return UpdateInfo(
|
|
version: data['version'] as String? ?? '?',
|
|
apkUrl: data['apk_url'] as String? ?? '$base/download/apk',
|
|
notes: data['notes'] as String? ?? '',
|
|
);
|
|
}
|
|
} catch (_) {/* pas de réseau / pas d'update → on ignore */}
|
|
return null;
|
|
}
|
|
|
|
Future<bool> apply(UpdateInfo info) async {
|
|
final uri = Uri.parse(info.apkUrl);
|
|
if (!await canLaunchUrl(uri)) return false;
|
|
return launchUrl(uri, mode: LaunchMode.externalApplication);
|
|
}
|
|
}
|
|
|
|
class UpdateInfo {
|
|
UpdateInfo({required this.version, required this.apkUrl, required this.notes});
|
|
final String version;
|
|
final String apkUrl;
|
|
final String notes;
|
|
}
|