| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import 'dart:async';
- import 'package:flutter/foundation.dart';
- import '../../core/app_version.dart';
- import '../../data/models/update_info.dart';
- import '../../data/services/native_bridge.dart';
- import '../../data/services/update_service.dart';
- /// Non-blocking update check against the public Gogs releases API.
- class UpdateController extends ChangeNotifier {
- UpdateController(this._platform, {UpdateService? service})
- : _service = service ?? UpdateService();
- final LauncherPlatform _platform;
- final UpdateService _service;
- UpdateInfo? _result;
- bool _checking = false;
- bool _installing = false;
- bool _started = false;
- bool _disposed = false;
- String? _error;
- String? _installMessage;
- String get currentVersion => appVersion;
- String? get latestVersion => _result?.latestVersion;
- bool get checking => _checking;
- bool get updateAvailable => _result?.available ?? false;
- bool get installing => _installing;
- String? get installMessage => _installMessage;
- String? get error => _error;
- String? get releasePageUrl => _result?.releasePageUrl;
- Future<void> start() async {
- if (_started) return;
- _started = true;
- await check();
- }
- Future<void> check() async {
- if (_checking) return;
- _checking = true;
- _error = null;
- _installMessage = null;
- _notify();
- try {
- _result = await _service.check(appVersion);
- } on UpdateException catch (problem) {
- _error = problem.message;
- } catch (_) {
- _error = 'Could not check for updates.';
- } finally {
- _checking = false;
- _notify();
- }
- }
- Future<void> installUpdate() async {
- final result = _result;
- if (_installing || result == null || !result.available) return;
- _installing = true;
- _installMessage = 'Downloading update…';
- _error = null;
- _notify();
- try {
- final installed = await _platform.downloadAndInstallUpdate(
- result.downloadUrl,
- );
- _installMessage = installed.message;
- if (!installed.started && !installed.permissionRequired) {
- _error = installed.message ?? 'Could not start the update installer.';
- }
- } catch (_) {
- _error = 'Could not download the update.';
- } finally {
- _installing = false;
- _notify();
- }
- }
- Future<bool> openReleasePage() {
- return _platform.openUrl(
- _result?.releasePageUrl ?? UpdateService.releasePageUrl,
- );
- }
- void _notify() {
- if (!_disposed) notifyListeners();
- }
- @override
- void dispose() {
- _disposed = true;
- super.dispose();
- }
- }
|