1
1
import 'dart:async' ;
2
2
3
3
import 'package:auto_updater/src/appcast.dart' ;
4
+ import 'package:auto_updater/src/events.dart' ;
4
5
import 'package:auto_updater/src/updater_error.dart' ;
5
6
import 'package:auto_updater/src/updater_listener.dart' ;
7
+ import 'package:auto_updater/src/user_update_choice.dart' ;
6
8
import 'package:auto_updater_platform_interface/auto_updater_platform_interface.dart' ;
7
9
8
10
class AutoUpdater {
@@ -17,84 +19,78 @@ class AutoUpdater {
17
19
18
20
final List <UpdaterListener > _listeners = [];
19
21
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
-
73
22
/// 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);
77
24
78
25
/// 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);
82
27
83
28
/// 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);
87
30
88
31
/// 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);
94
34
95
35
/// 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
+ }
98
94
}
99
95
}
100
96
0 commit comments