update_controller.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 _started = false;
  16. bool _disposed = false;
  17. String? _error;
  18. String get currentVersion => appVersion;
  19. String? get latestVersion => _result?.latestVersion;
  20. bool get checking => _checking;
  21. bool get updateAvailable => _result?.available ?? false;
  22. String? get error => _error;
  23. String? get releasePageUrl => _result?.releasePageUrl;
  24. Future<void> start() async {
  25. if (_started) return;
  26. _started = true;
  27. await check();
  28. }
  29. Future<void> check() async {
  30. if (_checking) return;
  31. _checking = true;
  32. _error = null;
  33. _notify();
  34. try {
  35. _result = await _service.check(appVersion);
  36. } on UpdateException catch (problem) {
  37. _error = problem.message;
  38. } catch (_) {
  39. _error = 'Could not check for updates.';
  40. } finally {
  41. _checking = false;
  42. _notify();
  43. }
  44. }
  45. Future<bool> openReleasePage() {
  46. return _platform.openUrl(
  47. _result?.releasePageUrl ?? UpdateService.releasePageUrl,
  48. );
  49. }
  50. void _notify() {
  51. if (!_disposed) notifyListeners();
  52. }
  53. @override
  54. void dispose() {
  55. _disposed = true;
  56. super.dispose();
  57. }
  58. }