Skip to content

Commit

Permalink
Merge pull request #11 from Flutter-Chore/develop
Browse files Browse the repository at this point in the history
refactor(appcast_item): let appcast_item doesn't dependence dart:io.
  • Loading branch information
0x7c01 authored May 25, 2023
2 parents 3cfa8ec + 186d1d7 commit 1e61038
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 33 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## 0.0.5
## 0.0.6

* Export Version class.
* Refactor Appcast_Item.
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.4"
version: "0.0.5"
url_launcher:
dependency: transitive
description:
Expand Down
15 changes: 15 additions & 0 deletions lib/core/installer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Installer?> init({
required List<Map<String, dynamic>> configs,
required UpgradeStateChangeNotifier state,
required Map<String, InstallInitializer> 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<InstallInitializer> initializers = [
Expand Down
45 changes: 27 additions & 18 deletions lib/core/upgrade_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, dynamic>>.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<Map<String, dynamic>>.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({
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions lib/core/version.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export 'package:version/version.dart';
12 changes: 1 addition & 11 deletions lib/models/appcast_item.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -15,6 +13,7 @@ class AppcastItem {
final String? maximumSystemVersion;

late List<Map<String, dynamic>> _installersConfig;
List<Map<String, dynamic>> get installersConfig => _installersConfig;

AppcastItem({
this.releaseNotes,
Expand All @@ -41,15 +40,6 @@ class AppcastItem {
return false;
}

Iterable<Installer?> installer({
required UpgradeStateChangeNotifier state,
required Map<String, InstallInitializer> 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<String, dynamic> json) {
return AppcastItem(
releaseNotes: json['release_notes'],
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 1e61038

Please sign in to comment.