Skip to content

Commit 1d81a82

Browse files
committed
feat: add user event callback on Windows
1 parent b61573a commit 1d81a82

File tree

6 files changed

+57
-11
lines changed

6 files changed

+57
-11
lines changed

packages/auto_updater/example/lib/pages/home.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,14 @@ class _HomePageState extends State<HomePage> with UpdaterListener {
138138
}
139139
windowManager.setPreventClose(false);
140140
}
141+
142+
@override
143+
void onUpdaterUserUpdateChoice(
144+
UserUpdateChoice? choice,
145+
AppcastItem? appcastItem,
146+
) {
147+
if (kDebugMode) {
148+
print('onUpdaterUserUpdateChoice: $choice, ${appcastItem?.toJson()}');
149+
}
150+
}
141151
}

packages/auto_updater/lib/src/auto_updater.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,26 @@ class AutoUpdater {
8888
case UpdaterEvent.beforeQuitForUpdate:
8989
listener.onUpdaterBeforeQuitForUpdate(appcastItem);
9090
case UpdaterEvent.userUpdateChoice:
91-
listener.onUpdaterUserUpdateChoice(userUpdateChoice, appcastItem);
91+
// this function is only available on macOS
92+
final choice = userUpdateChoice;
93+
if (choice == null) return;
94+
switch (choice) {
95+
case UserUpdateChoice.skip:
96+
listener.onUpdaterUpdateSkipped(appcastItem);
97+
case UserUpdateChoice.install:
98+
listener.onUpdaterUpdateInstalled(appcastItem);
99+
case UserUpdateChoice.dismiss:
100+
listener.onUpdaterUpdateCancelled(appcastItem);
101+
}
102+
case UpdaterEvent.updateCancelled:
103+
// this event is only available on Windows
104+
listener.onUpdaterUpdateCancelled(appcastItem);
105+
case UpdaterEvent.updateSkipped:
106+
// this event is only available on Windows
107+
listener.onUpdaterUpdateSkipped(appcastItem);
108+
case UpdaterEvent.updateInstalled:
109+
// this event is only available on Windows
110+
listener.onUpdaterUpdateInstalled(appcastItem);
92111
}
93112
}
94113
}

packages/auto_updater/lib/src/events.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ enum UpdaterEvent {
55
updateNotAvailable,
66
updateDownloaded,
77
beforeQuitForUpdate,
8-
userUpdateChoice;
8+
9+
// this event is only available on macOS
10+
userUpdateChoice,
11+
12+
// these events are only available on Windows
13+
updateCancelled,
14+
updateSkipped,
15+
updateInstalled;
916

1017
static UpdaterEvent fromString(String name) {
1118
switch (name) {
@@ -23,6 +30,12 @@ enum UpdaterEvent {
2330
return UpdaterEvent.beforeQuitForUpdate;
2431
case 'user-update-choice':
2532
return UpdaterEvent.userUpdateChoice;
33+
case 'update-cancelled':
34+
return UpdaterEvent.updateCancelled;
35+
case 'update-skipped':
36+
return UpdaterEvent.updateSkipped;
37+
case 'update-installed':
38+
return UpdaterEvent.updateInstalled;
2639
default:
2740
throw ArgumentError('Invalid event name: $name');
2841
}

packages/auto_updater/lib/src/updater_listener.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ abstract mixin class UpdaterListener {
77
void onUpdaterUpdateNotAvailable(UpdaterError? error);
88
void onUpdaterUpdateDownloaded(AppcastItem? appcastItem);
99
void onUpdaterBeforeQuitForUpdate(AppcastItem? appcastItem);
10-
void onUpdaterUserUpdateChoice(
11-
UserUpdateChoice? choice,
12-
AppcastItem? appcastItem,
13-
);
10+
void onUpdaterUpdateSkipped(AppcastItem? appcastItem);
11+
void onUpdaterUpdateCancelled(AppcastItem? appcastItem);
12+
void onUpdaterUpdateInstalled(AppcastItem? appcastItem);
1413
}

packages/auto_updater_windows/windows/auto_updater.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,34 +136,34 @@ void __onUpdateCancelledCallback() {
136136
AutoUpdater* autoUpdater = AutoUpdater::GetInstance();
137137
if (autoUpdater == nullptr)
138138
return;
139-
autoUpdater->OnWinSparkleEvent("updateCancelled");
139+
autoUpdater->OnWinSparkleEvent("update-cancelled");
140140
}
141141

142142
void __onUpdateSkippedCallback() {
143143
AutoUpdater* autoUpdater = AutoUpdater::GetInstance();
144144
if (autoUpdater == nullptr)
145145
return;
146-
autoUpdater->OnWinSparkleEvent("updateSkipped");
146+
autoUpdater->OnWinSparkleEvent("update-skipped");
147147
}
148148

149149
void __onUpdatePostponedCallback() {
150150
AutoUpdater* autoUpdater = AutoUpdater::GetInstance();
151151
if (autoUpdater == nullptr)
152152
return;
153-
autoUpdater->OnWinSparkleEvent("updatePostponed");
153+
autoUpdater->OnWinSparkleEvent("update-postponed");
154154
}
155155

156156
void __onUpdateDismissedCallback() {
157157
AutoUpdater* autoUpdater = AutoUpdater::GetInstance();
158158
if (autoUpdater == nullptr)
159159
return;
160-
autoUpdater->OnWinSparkleEvent("updateDismissed");
160+
autoUpdater->OnWinSparkleEvent("update-dismissed");
161161
}
162162

163163
void __onUserRunInstallerCallback() {
164164
AutoUpdater* autoUpdater = AutoUpdater::GetInstance();
165165
if (autoUpdater == nullptr)
166166
return;
167-
autoUpdater->OnWinSparkleEvent("userRunInstaller");
167+
autoUpdater->OnWinSparkleEvent("user-run-installer");
168168
}
169169
} // namespace

packages/auto_updater_windows/windows/auto_updater_windows_plugin.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ void AutoUpdaterWindowsPlugin::HandleMethodCall(
8686
auto_updater.SetScheduledCheckInterval(interval);
8787
result->Success(flutter::EncodableValue(true));
8888

89+
} else if (method_name.compare("checkForUpdateInformation") == 0) {
90+
const flutter::EncodableMap& args =
91+
std::get<flutter::EncodableMap>(*method_call.arguments());
92+
auto_updater.CheckForUpdatesWithoutUI();
93+
result->Success(flutter::EncodableValue(true));
8994
} else {
9095
result->NotImplemented();
9196
}

0 commit comments

Comments
 (0)