11import 'dart:async' ;
22
33import 'package:auto_updater/src/appcast.dart' ;
4+ import 'package:auto_updater/src/events.dart' ;
45import 'package:auto_updater/src/updater_error.dart' ;
56import 'package:auto_updater/src/updater_listener.dart' ;
7+ import 'package:auto_updater/src/user_update_choice.dart' ;
68import 'package:auto_updater_platform_interface/auto_updater_platform_interface.dart' ;
79
810class 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
0 commit comments