From 186d1d7e667ea7dad7c1b741148fc7aea5b04a91 Mon Sep 17 00:00:00 2001 From: 0x7c01 Date: Thu, 25 May 2023 20:04:52 +0800 Subject: [PATCH] refactor(appcast_item): let appcast_item doesn't dependence dart:io. --- CHANGELOG.md | 4 ++-- example/pubspec.lock | 2 +- lib/core/installer.dart | 15 ++++++++++++ lib/core/upgrade_manager.dart | 45 +++++++++++++++++++++-------------- lib/core/version.dart | 2 ++ lib/models/appcast_item.dart | 12 +--------- pubspec.yaml | 2 +- 7 files changed, 49 insertions(+), 33 deletions(-) create mode 100644 lib/core/version.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index eafcf7b..35b6afc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,3 @@ -## 0.0.5 +## 0.0.6 -* Export Version class. +* Refactor Appcast_Item. diff --git a/example/pubspec.lock b/example/pubspec.lock index 143992f..ebd4f40 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -315,7 +315,7 @@ packages: path: ".." relative: true source: path - version: "0.0.4" + version: "0.0.5" url_launcher: dependency: transitive description: diff --git a/lib/core/installer.dart b/lib/core/installer.dart index 0c92031..ec69e5c 100644 --- a/lib/core/installer.dart +++ b/lib/core/installer.dart @@ -10,6 +10,21 @@ import 'package:upgrade/installer/ios_app_store_installer.dart'; import 'package:upgrade/installer/macos_app_store_installer.dart'; import 'package:upgrade/models/upgrade_status.dart'; +class InstallerHelper { + + static Iterable init({ + required List> configs, + required UpgradeStateChangeNotifier state, + required Map initializers, + }) sync* { + for (int i = 0; i < configs.length; i++) { + final config = configs[i]; + yield initializers[config['initializer']]?.init(state: state, data: config); + } + } + +} + class SystemInstaller { static List initializers = [ diff --git a/lib/core/upgrade_manager.dart b/lib/core/upgrade_manager.dart index 0cc4846..f9950cd 100644 --- a/lib/core/upgrade_manager.dart +++ b/lib/core/upgrade_manager.dart @@ -62,23 +62,28 @@ class UpgradeManager { state.updateUpgradeStatus(status: UpgradeStatus.checking); - final uri = Uri.parse(_url); - final response = await Client().get(uri, headers: {'Content-Type': 'application/json;charset=utf-8'}); - if (response.statusCode != 200) { + Client().get( + Uri.parse(_url), + headers: {'Content-Type': 'application/json;charset=utf-8'}, + ).then((response) { + if (response.statusCode != 200) { + state.updateUpgradeStatus(status: UpgradeStatus.error); + debugPrint("[UpgradeManager] Download Appcast from $_url error."); + return; + } + + final appcast = Appcast.fromJson(List>.from(json.decode(response.body))); + final best = appcast.best(); + if (best != null) { + state.updateLatestVersion(version: best); + initInstallers(); + nextInstaller(); + } else { + state.updateUpgradeStatus(status: UpgradeStatus.upToDate); + } + }).catchError((_) { state.updateUpgradeStatus(status: UpgradeStatus.error); - debugPrint("[UpgradeManager] Download Appcast from $_url error."); - return; - } - - final appcast = Appcast.fromJson(List>.from(json.decode(response.body))); - final best = appcast.best(); - if (best != null) { - state.updateLatestVersion(version: best); - initInstallers(); - nextInstaller(); - } else { - state.updateUpgradeStatus(status: UpgradeStatus.upToDate); - } + }); } void download({ @@ -118,8 +123,12 @@ class UpgradeManager { void initInstallers() { if (status != UpgradeStatus.available) { return; } - _installers = state.latest?.installer( - state: _stateChangeNotifier, initializers: _installInitializers).iterator; + _installers = InstallerHelper.init( + configs: state.latest!.installersConfig, + state: _stateChangeNotifier, + initializers: _installInitializers, + ).iterator; + } bool nextInstaller() { diff --git a/lib/core/version.dart b/lib/core/version.dart new file mode 100644 index 0000000..2e86aeb --- /dev/null +++ b/lib/core/version.dart @@ -0,0 +1,2 @@ + +export 'package:version/version.dart'; \ No newline at end of file diff --git a/lib/models/appcast_item.dart b/lib/models/appcast_item.dart index b482f12..9cfdbf0 100644 --- a/lib/models/appcast_item.dart +++ b/lib/models/appcast_item.dart @@ -1,8 +1,6 @@ import 'dart:io'; -import 'package:upgrade/core/installer.dart'; -import 'package:upgrade/core/upgrade_state_change_notifier.dart'; import 'package:version/version.dart'; class AppcastItem { @@ -15,6 +13,7 @@ class AppcastItem { final String? maximumSystemVersion; late List> _installersConfig; + List> get installersConfig => _installersConfig; AppcastItem({ this.releaseNotes, @@ -41,15 +40,6 @@ class AppcastItem { return false; } - Iterable installer({ - required UpgradeStateChangeNotifier state, - required Map initializers}) sync* { - for (int i = 0; i < _installersConfig.length; i++) { - final config = _installersConfig[i]; - yield initializers[config['initializer']]?.init(state: state, data: config); - } - } - factory AppcastItem.fromJson(Map json) { return AppcastItem( releaseNotes: json['release_notes'], diff --git a/pubspec.yaml b/pubspec.yaml index eae84b5..5cf7918 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: upgrade description: A Flutter plugin for prompting and help users to upgrade when there is a newer version of this app in the app store or server repository. -version: 0.0.5 +version: 0.0.6 homepage: https://github.com/Flutter-Chore/Flutter-InAppUpgrade