update_controller.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'dart:async';
  2. import 'package:flutter/foundation.dart';
  3. import '../../core/app_version.dart';
  4. import '../../data/models/update_info.dart';
  5. import '../../data/services/native_bridge.dart';
  6. import '../../data/services/update_service.dart';
  7. /// Non-blocking update check against the public Gogs releases API.
  8. class UpdateController extends ChangeNotifier {
  9. UpdateController(this._platform, {UpdateService? service})
  10. : _service = service ?? UpdateService();
  11. final LauncherPlatform _platform;
  12. final UpdateService _service;
  13. UpdateInfo? _result;
  14. bool _checking = false;
  15. bool _installing = false;
  16. bool _started = false;
  17. bool _disposed = false;
  18. String? _error;
  19. String? _installMessage;
  20. String get currentVersion => appVersion;
  21. String? get latestVersion => _result?.latestVersion;
  22. bool get checking => _checking;
  23. bool get updateAvailable => _result?.available ?? false;
  24. bool get installing => _installing;
  25. String? get installMessage => _installMessage;
  26. String? get error => _error;
  27. String? get releasePageUrl => _result?.releasePageUrl;
  28. Future<void> start() async {
  29. if (_started) return;
  30. _started = true;
  31. await check();
  32. }
  33. Future<void> check() async {
  34. if (_checking) return;
  35. _checking = true;
  36. _error = null;
  37. _installMessage = null;
  38. _notify();
  39. try {
  40. _result = await _service.check(appVersion);
  41. } on UpdateException catch (problem) {
  42. _error = problem.message;
  43. } catch (_) {
  44. _error = 'Could not check for updates.';
  45. } finally {
  46. _checking = false;
  47. _notify();
  48. }
  49. }
  50. Future<void> installUpdate() async {
  51. final result = _result;
  52. if (_installing || result == null || !result.available) return;
  53. _installing = true;
  54. _installMessage = 'Downloading update…';
  55. _error = null;
  56. _notify();
  57. try {
  58. final installed = await _platform.downloadAndInstallUpdate(
  59. result.downloadUrl,
  60. );
  61. _installMessage = installed.message;
  62. if (!installed.started && !installed.permissionRequired) {
  63. _error = installed.message ?? 'Could not start the update installer.';
  64. }
  65. } catch (_) {
  66. _error = 'Could not download the update.';
  67. } finally {
  68. _installing = false;
  69. _notify();
  70. }
  71. }
  72. Future<bool> openReleasePage() {
  73. return _platform.openUrl(
  74. _result?.releasePageUrl ?? UpdateService.releasePageUrl,
  75. );
  76. }
  77. void _notify() {
  78. if (!_disposed) notifyListeners();
  79. }
  80. @override
  81. void dispose() {
  82. _disposed = true;
  83. super.dispose();
  84. }
  85. }