Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@

设置检查时间间隔,默认 86400,最少 3600, 0 不更新

##### installUpdateNow

当原生 updater 已准备好下载的更新时,立即安装更新。

<!-- README_DOC_GEN -->

## 相关链接
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ Asks the server whether there is an update. You must call setFeedURL before usin

Sets the auto update check interval, default 86400, minimum 3600, 0 to disable update

##### installUpdateNow

Installs the downloaded update immediately when the native updater has one ready.

<!-- README_DOC_GEN -->

## Related Links
Expand Down
13 changes: 7 additions & 6 deletions packages/auto_updater/lib/src/auto_updater.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ class AutoUpdater {
if (event['data'] != null) {
data = event['data'] as Map;
if (data['error'] != null) {
updaterError = UpdaterError(
data['error'].toString(),
);
updaterError = UpdaterError(data['error'].toString());
}
if (data['appcast'] != null) {
appcast = Appcast.fromJson(
Expand Down Expand Up @@ -87,15 +85,18 @@ class AutoUpdater {

/// Asks the server whether there is an update. You must call setFeedURL before using this API.
Future<void> checkForUpdates({bool? inBackground}) {
return _platform.checkForUpdates(
inBackground: inBackground,
);
return _platform.checkForUpdates(inBackground: inBackground);
}

/// Sets the auto update check interval, default 86400, minimum 3600, 0 to disable update
Future<void> setScheduledCheckInterval(int interval) {
return _platform.setScheduledCheckInterval(interval);
}

/// Installs the downloaded update immediately when the native updater has one ready.
Future<void> installUpdateNow() {
return _platform.installUpdateNow();
}
}

final autoUpdater = AutoUpdater.instance;
17 changes: 17 additions & 0 deletions packages/auto_updater_macos/macos/Classes/AutoUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class AutoUpdater: NSObject, SPUUpdaterDelegate {
var _userDriver: SPUStandardUserDriver?
var _updater: SPUUpdater?
var feedURL: URL?
var immediateInstallHandler: (() -> Void)?
public var onEvent:((String, NSDictionary) -> Void)?

override init() {
Expand Down Expand Up @@ -80,6 +81,21 @@ public class AutoUpdater: NSObject, SPUUpdaterDelegate {
public func setScheduledCheckInterval(_ interval: Int) {
_updater?.updateCheckInterval = TimeInterval(interval)
}

public func installUpdateImmediately() -> Bool {
guard let immediateInstallHandler = immediateInstallHandler else {
let data: NSDictionary = [
"error": "No downloaded update is ready to install immediately.",
]
_emitEvent("error", data)
return false
}

DispatchQueue.main.async {
immediateInstallHandler()
}
return true
}

// SPUUpdaterDelegate

Expand Down Expand Up @@ -119,6 +135,7 @@ public class AutoUpdater: NSObject, SPUUpdaterDelegate {
}

public func updater(_ updater: SPUUpdater, willInstallUpdateOnQuit item: SUAppcastItem, immediateInstallationBlock immediateInstallHandler: @escaping () -> Void) -> Bool {
self.immediateInstallHandler = immediateInstallHandler
let data: NSDictionary = [
"appcastItem": item.toDictionary()
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,19 @@ public class AutoUpdaterMacosPlugin: NSObject, FlutterPlugin,FlutterStreamHandle
autoUpdater.setScheduledCheckInterval(interval)
result(true)
break
case "installUpdateImmediately":
if autoUpdater.installUpdateImmediately() {
result(true)
} else {
result(FlutterError(
code: "UPDATE_NOT_READY",
message: "No downloaded update is ready to install immediately.",
details: nil
))
}
break
default:
result(FlutterMethodNotImplemented)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ class MethodChannelAutoUpdater extends AutoUpdaterPlatform {

@override
Future<void> setFeedURL(String feedUrl) async {
final Map<String, dynamic> arguments = {
'feedURL': feedUrl,
};
final Map<String, dynamic> arguments = {'feedURL': feedUrl};
await methodChannel.invokeMethod('setFeedURL', arguments);
}

Expand All @@ -39,9 +37,12 @@ class MethodChannelAutoUpdater extends AutoUpdaterPlatform {

@override
Future<void> setScheduledCheckInterval(int interval) async {
final Map<String, dynamic> arguments = {
'interval': interval,
};
final Map<String, dynamic> arguments = {'interval': interval};
await methodChannel.invokeMethod('setScheduledCheckInterval', arguments);
}

@override
Future<void> installUpdateNow() async {
await methodChannel.invokeMethod('installUpdateImmediately');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ abstract class AutoUpdaterPlatform extends PlatformInterface {
'setScheduledCheckInterval() has not been implemented.',
);
}

/// Installs the downloaded update immediately when the native updater has one ready.
Future<void> installUpdateNow() async {
throw UnimplementedError('installUpdateNow() has not been implemented.');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:auto_updater_platform_interface/auto_updater_platform_interface.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('MethodChannelAutoUpdater', () {
test('installUpdateNow invokes native immediate install method', () async {
final updater = MethodChannelAutoUpdater();
final calls = <MethodCall>[];

TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(updater.methodChannel, (call) async {
calls.add(call);
return true;
});
addTearDown(() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(updater.methodChannel, null);
});

await updater.installUpdateNow();

expect(calls, hasLength(1));
expect(calls.single.method, 'installUpdateImmediately');
expect(calls.single.arguments, isNull);
});
});
}
5 changes: 5 additions & 0 deletions packages/auto_updater_windows/windows/auto_updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class AutoUpdater {
void AutoUpdater::CheckForUpdates();
void AutoUpdater::CheckForUpdatesWithoutUI();
void AutoUpdater::SetScheduledCheckInterval(int interval);
void AutoUpdater::InstallUpdateNow();

void AutoUpdater::RegisterEventSink(
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> ptr);
Expand Down Expand Up @@ -89,6 +90,10 @@ void AutoUpdater::SetScheduledCheckInterval(int interval) {
win_sparkle_set_update_check_interval(interval);
}

void AutoUpdater::InstallUpdateNow() {
win_sparkle_check_update_with_ui_and_install();
}

void AutoUpdater::RegisterEventSink(
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> ptr) {
event_sink_ = std::move(ptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ void AutoUpdaterWindowsPlugin::HandleMethodCall(
auto_updater.SetScheduledCheckInterval(interval);
result->Success(flutter::EncodableValue(true));

} else if (method_name.compare("installUpdateImmediately") == 0) {
auto_updater.InstallUpdateNow();
result->Success(flutter::EncodableValue(true));

} else {
result->NotImplemented();
}
Expand Down