87 lines
2.7 KiB
Dart
87 lines
2.7 KiB
Dart
import 'dart:async';
|
|
|
|
import '../diagnostics/app_diagnostics.dart';
|
|
import 'app_update_configuration.dart';
|
|
|
|
import 'package:deskup/deskup.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
export 'package:deskup/deskup.dart' show UpdateStatus, UpdatePhase;
|
|
|
|
part 'app_update_bridge.g.dart';
|
|
|
|
const appUpdatesChannel = MethodChannel('deskup');
|
|
const appLifecycleChannel = MethodChannel('cutdex/lifecycle');
|
|
|
|
bool get supportsAppUpdates => Deskup.isSupported;
|
|
|
|
@Riverpod(keepAlive: true)
|
|
AppUpdateBridge appUpdateBridge(Ref ref) {
|
|
final bridge = AppUpdateBridge();
|
|
ref.onDispose(bridge.dispose);
|
|
return bridge;
|
|
}
|
|
|
|
// Application adapter: Riverpod ownership and the macOS workspace quit gate.
|
|
class AppUpdateBridge {
|
|
AppUpdateBridge([MethodChannel channel = appUpdatesChannel])
|
|
: _updater = Deskup(
|
|
channel: channel,
|
|
windows: windowsUpdateConfiguration(),
|
|
macos: macosUpdateConfiguration(),
|
|
);
|
|
|
|
final Deskup _updater;
|
|
Stream<UpdateStatus> get events => _updater.events;
|
|
bool _started = false;
|
|
bool _disposed = false;
|
|
Future<void> Function()? _prepareToQuit;
|
|
|
|
void start(Future<void> Function() prepareToQuit) {
|
|
if (_disposed) return;
|
|
_prepareToQuit = prepareToQuit;
|
|
if (_started) return;
|
|
_started = true;
|
|
if (defaultTargetPlatform == TargetPlatform.macOS) {
|
|
appLifecycleChannel.setMethodCallHandler((call) async {
|
|
if (_disposed) return null;
|
|
if (call.method != 'prepareToQuit') throw MissingPluginException();
|
|
await _prepareToQuit!();
|
|
return true;
|
|
});
|
|
unawaited(
|
|
appLifecycleChannel
|
|
.invokeMethod<void>('ready')
|
|
.catchError(_initializationError),
|
|
);
|
|
}
|
|
unawaited(_updater.initialize().catchError(_initializationError));
|
|
}
|
|
|
|
void _initializationError(Object error, StackTrace stack) {
|
|
if (!_disposed && error is! MissingPluginException) {
|
|
AppDiagnostics.failure('updates', 'initialize', error, stack);
|
|
}
|
|
}
|
|
|
|
Future<UpdateStatus> status() => _updater.status();
|
|
Future<void> check() => _updater.check();
|
|
Future<void> download() => _updater.download();
|
|
Future<void> validateInstall() => _updater.validateInstall();
|
|
Future<void> install() => _updater.install(silent: true, restart: true);
|
|
Future<void> setAutomaticChecks(bool enabled) =>
|
|
_updater.setAutomaticChecks(enabled);
|
|
|
|
void dispose() {
|
|
if (_disposed) return;
|
|
_disposed = true;
|
|
_prepareToQuit = null;
|
|
if (_started && defaultTargetPlatform == TargetPlatform.macOS) {
|
|
appLifecycleChannel.setMethodCallHandler(null);
|
|
}
|
|
_updater.dispose();
|
|
}
|
|
}
|