Skip to content

Commit 03e9b75

Browse files
committed
feat: support skip version and cancel update callback
1 parent 0b9ce2a commit 03e9b75

File tree

15 files changed

+203
-105
lines changed

15 files changed

+203
-105
lines changed
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
1-
include: package:mostly_reasonable_lints/analysis_options.yaml
1+
include: package:flutter_lints/flutter.yaml
2+
3+
analyzer:
4+
exclude:
5+
- "**/*.g.dart"
6+
- "**/*.freezed.dart"
7+
8+
linter:
9+
rules:
10+
- require_trailing_commas
11+
12+
- prefer_collection_literals
13+
- prefer_final_fields
14+
- prefer_final_in_for_each
15+
- prefer_final_locals
16+
17+
- sized_box_for_whitespace
18+
- use_decorated_box
19+
20+
- unnecessary_parenthesis
21+
- unnecessary_await_in_return
22+
- unnecessary_raw_strings
23+
24+
- avoid_unnecessary_containers
25+
- avoid_redundant_argument_values
26+
- avoid_unused_constructor_parameters
27+
28+
- always_declare_return_types
29+
30+
- sort_constructors_first
31+
- unawaited_futures
32+
33+
errors:
34+
invalid_annotation_target: ignore

packages/auto_updater/bin/generate_keys.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Future<void> main(List<String> arguments) async {
77
throw UnsupportedError('auto_updater:generate_keys');
88
}
99

10-
String executable = Platform.isMacOS
10+
final String executable = Platform.isMacOS
1111
? '${Directory.current.path}/macos/Pods/Sparkle/bin/generate_keys'
1212
: p.joinAll(
1313
[
@@ -24,7 +24,7 @@ Future<void> main(List<String> arguments) async {
2424
],
2525
);
2626

27-
Process process = await Process.start(
27+
final Process process = await Process.start(
2828
executable,
2929
arguments,
3030
);

packages/auto_updater/bin/sign_update.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class SignUpdateResult {
1212
}
1313

1414
SignUpdateResult signUpdate(List<String> args) {
15-
String executable = Platform.isMacOS
15+
final String executable = Platform.isMacOS
1616
? '${Directory.current.path}/macos/Pods/Sparkle/bin/sign_update'
1717
: p.joinAll(
1818
[
@@ -28,19 +28,19 @@ SignUpdateResult signUpdate(List<String> args) {
2828
'sign_update.bat',
2929
],
3030
);
31-
List<String> arguments = List<String>.from(args);
31+
final List<String> arguments = List<String>.from(args);
3232
if (Platform.isWindows) {
3333
if (arguments.length == 1) {
3434
arguments.add(p.join('dsa_priv.pem'));
3535
}
3636
}
3737

38-
ProcessResult processResult = Process.runSync(
38+
final ProcessResult processResult = Process.runSync(
3939
executable,
4040
arguments,
4141
);
4242

43-
int exitCode = processResult.exitCode;
43+
final int exitCode = processResult.exitCode;
4444

4545
String? signUpdateOutput;
4646
if (exitCode == 0) {
@@ -54,8 +54,8 @@ SignUpdateResult signUpdate(List<String> args) {
5454
stderr.write(processResult.stderr);
5555
}
5656

57-
RegExp regex = RegExp(r'sparkle:(dsa|ed)Signature="([^"]+)" length="(\d+)"');
58-
RegExpMatch? match = regex.firstMatch(signUpdateOutput!);
57+
final RegExp regex = RegExp(r'sparkle:(dsa|ed)Signature="([^"]+)" length="(\d+)"');
58+
final RegExpMatch? match = regex.firstMatch(signUpdateOutput!);
5959

6060
if (match == null) {
6161
throw Exception('Failed to sign update');

packages/auto_updater/example/windows/flutter/generated_plugin_registrant.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
#include "generated_plugin_registrant.h"
88

99
#include <auto_updater_windows/auto_updater_windows_plugin_c_api.h>
10-
#include <screen_retriever/screen_retriever_plugin.h>
10+
#include <screen_retriever_windows/screen_retriever_windows_plugin_c_api.h>
1111
#include <window_manager/window_manager_plugin.h>
1212

1313
void RegisterPlugins(flutter::PluginRegistry* registry) {
1414
AutoUpdaterWindowsPluginCApiRegisterWithRegistrar(
1515
registry->GetRegistrarForPlugin("AutoUpdaterWindowsPluginCApi"));
16-
ScreenRetrieverPluginRegisterWithRegistrar(
17-
registry->GetRegistrarForPlugin("ScreenRetrieverPlugin"));
16+
ScreenRetrieverWindowsPluginCApiRegisterWithRegistrar(
17+
registry->GetRegistrarForPlugin("ScreenRetrieverWindowsPluginCApi"));
1818
WindowManagerPluginRegisterWithRegistrar(
1919
registry->GetRegistrarForPlugin("WindowManagerPlugin"));
2020
}

packages/auto_updater/example/windows/flutter/generated_plugins.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
list(APPEND FLUTTER_PLUGIN_LIST
66
auto_updater_windows
7-
screen_retriever
7+
screen_retriever_windows
88
window_manager
99
)
1010

packages/auto_updater/lib/auto_updater.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ library auto_updater;
22

33
export 'src/appcast.dart';
44
export 'src/auto_updater.dart';
5+
export 'src/events.dart';
56
export 'src/updater_error.dart';
67
export 'src/updater_listener.dart';
8+
export 'src/user_update_choice.dart';

packages/auto_updater/lib/src/auto_updater.dart

Lines changed: 65 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import 'dart:async';
22

33
import 'package:auto_updater/src/appcast.dart';
4+
import 'package:auto_updater/src/events.dart';
45
import 'package:auto_updater/src/updater_error.dart';
56
import 'package:auto_updater/src/updater_listener.dart';
7+
import 'package:auto_updater/src/user_update_choice.dart';
68
import 'package:auto_updater_platform_interface/auto_updater_platform_interface.dart';
79

810
class AutoUpdater {
@@ -17,84 +19,78 @@ class AutoUpdater {
1719

1820
final List<UpdaterListener> _listeners = [];
1921

20-
void _handleSparkleEvents(event) {
21-
UpdaterError? updaterError;
22-
Appcast? appcast;
23-
AppcastItem? appcastItem;
24-
25-
String type = event['type'] as String;
26-
Map<Object?, Object?>? data;
27-
if (event['data'] != null) {
28-
data = event['data'] as Map;
29-
if (data['error'] != null) {
30-
updaterError = UpdaterError(
31-
data['error'].toString(),
32-
);
33-
}
34-
if (data['appcast'] != null) {
35-
appcast = Appcast.fromJson(
36-
Map<String, dynamic>.from(
37-
(data['appcast'] as Map).cast<String, dynamic>(),
38-
),
39-
);
40-
}
41-
if (data['appcastItem'] != null) {
42-
appcastItem = AppcastItem.fromJson(
43-
Map<String, dynamic>.from(
44-
(data['appcastItem'] as Map).cast<String, dynamic>(),
45-
),
46-
);
47-
}
48-
}
49-
for (var listener in _listeners) {
50-
switch (type) {
51-
case 'error':
52-
listener.onUpdaterError(updaterError);
53-
break;
54-
case 'checking-for-update':
55-
listener.onUpdaterCheckingForUpdate(appcast);
56-
break;
57-
case 'update-available':
58-
listener.onUpdaterUpdateAvailable(appcastItem);
59-
break;
60-
case 'update-not-available':
61-
listener.onUpdaterUpdateNotAvailable(updaterError);
62-
break;
63-
case 'update-downloaded':
64-
listener.onUpdaterUpdateDownloaded(appcastItem);
65-
break;
66-
case 'before-quit-for-update':
67-
listener.onUpdaterBeforeQuitForUpdate(appcastItem);
68-
break;
69-
}
70-
}
71-
}
72-
7322
/// Adds a listener to the auto updater.
74-
void addListener(UpdaterListener listener) {
75-
_listeners.add(listener);
76-
}
23+
void addListener(UpdaterListener listener) => _listeners.add(listener);
7724

7825
/// Removes a listener from the auto updater.
79-
void removeListener(UpdaterListener listener) {
80-
_listeners.remove(listener);
81-
}
26+
void removeListener(UpdaterListener listener) => _listeners.remove(listener);
8227

8328
/// Sets the url and initialize the auto updater.
84-
Future<void> setFeedURL(String feedUrl) {
85-
return _platform.setFeedURL(feedUrl);
86-
}
29+
Future<void> setFeedURL(String feedUrl) => _platform.setFeedURL(feedUrl);
8730

8831
/// Asks the server whether there is an update. You must call setFeedURL before using this API.
89-
Future<void> checkForUpdates({bool? inBackground}) {
90-
return _platform.checkForUpdates(
91-
inBackground: inBackground,
92-
);
93-
}
32+
Future<void> checkForUpdates({bool? inBackground}) =>
33+
_platform.checkForUpdates(inBackground: inBackground);
9434

9535
/// Sets the auto update check interval, default 86400, minimum 3600, 0 to disable update
96-
Future<void> setScheduledCheckInterval(int interval) {
97-
return _platform.setScheduledCheckInterval(interval);
36+
Future<void> setScheduledCheckInterval(int interval) =>
37+
_platform.setScheduledCheckInterval(interval);
38+
39+
/// Checks for update information.
40+
Future<void> checkForUpdateInformation() =>
41+
_platform.checkForUpdateInformation();
42+
43+
void _handleSparkleEvents(dynamic event) {
44+
final type = event['type'] as String;
45+
final eventType = UpdaterEvent.fromString(type);
46+
final eventData = event['data'] as Map<Object?, Object?>?;
47+
48+
if (eventData == null) return;
49+
50+
// Parse event data
51+
final updaterError = eventData['error'] != null
52+
? UpdaterError(eventData['error'].toString())
53+
: null;
54+
55+
final appcast = eventData['appcast'] is Map
56+
? Appcast.fromJson(
57+
Map<String, dynamic>.from(
58+
(eventData['appcast'] as Map).cast<String, dynamic>(),
59+
),
60+
)
61+
: null;
62+
63+
final appcastItem = eventData['appcastItem'] is Map
64+
? AppcastItem.fromJson(
65+
Map<String, dynamic>.from(
66+
(eventData['appcastItem'] as Map).cast<String, dynamic>(),
67+
),
68+
)
69+
: null;
70+
71+
final userUpdateChoice = eventData['choice'] is int
72+
? UserUpdateChoice.values[eventData['choice'] as int]
73+
: null;
74+
75+
// Notify listeners
76+
for (final listener in _listeners) {
77+
switch (eventType) {
78+
case UpdaterEvent.error:
79+
listener.onUpdaterError(updaterError);
80+
case UpdaterEvent.checkingForUpdate:
81+
listener.onUpdaterCheckingForUpdate(appcast);
82+
case UpdaterEvent.updateAvailable:
83+
listener.onUpdaterUpdateAvailable(appcastItem);
84+
case UpdaterEvent.updateNotAvailable:
85+
listener.onUpdaterUpdateNotAvailable(updaterError);
86+
case UpdaterEvent.updateDownloaded:
87+
listener.onUpdaterUpdateDownloaded(appcastItem);
88+
case UpdaterEvent.beforeQuitForUpdate:
89+
listener.onUpdaterBeforeQuitForUpdate(appcastItem);
90+
case UpdaterEvent.userUpdateChoice:
91+
listener.onUpdaterUserUpdateChoice(userUpdateChoice, appcastItem);
92+
}
93+
}
9894
}
9995
}
10096

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
enum UpdaterEvent {
2+
error,
3+
checkingForUpdate,
4+
updateAvailable,
5+
updateNotAvailable,
6+
updateDownloaded,
7+
beforeQuitForUpdate,
8+
userUpdateChoice;
9+
10+
static UpdaterEvent fromString(String name) {
11+
switch (name) {
12+
case 'error':
13+
return UpdaterEvent.error;
14+
case 'checking-for-update':
15+
return UpdaterEvent.checkingForUpdate;
16+
case 'update-available':
17+
return UpdaterEvent.updateAvailable;
18+
case 'update-not-available':
19+
return UpdaterEvent.updateNotAvailable;
20+
case 'update-downloaded':
21+
return UpdaterEvent.updateDownloaded;
22+
case 'before-quit-for-update':
23+
return UpdaterEvent.beforeQuitForUpdate;
24+
case 'user-update-choice':
25+
return UpdaterEvent.userUpdateChoice;
26+
default:
27+
throw ArgumentError('Invalid event name: $name');
28+
}
29+
}
30+
}

packages/auto_updater/lib/src/updater_listener.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ 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+
);
1014
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
enum UserUpdateChoice {
2+
skip,
3+
install,
4+
dismiss,
5+
}

0 commit comments

Comments
 (0)