app/lib/ui/settings/upgrade_dialog_ui_model.dart

85 lines
2.4 KiB
Dart
Raw Normal View History

2023-10-09 09:32:07 +08:00
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:starcitizen_doctor/api/api.dart';
import 'package:starcitizen_doctor/base/ui_model.dart';
import 'package:starcitizen_doctor/common/conf.dart';
2023-11-07 22:35:25 +08:00
import 'package:starcitizen_doctor/common/helper/system_helper.dart';
2023-11-11 01:41:06 +08:00
import 'package:url_launcher/url_launcher_string.dart';
2023-10-09 09:32:07 +08:00
class UpgradeDialogUIModel extends BaseUIModel {
String? description;
2023-10-16 23:26:33 +08:00
String targetVersion = "";
2023-10-09 09:32:07 +08:00
String downloadUrl = "";
bool isUpgrading = false;
double? progress;
@override
Future loadData() async {
// get download url for gitlab release
try {
2023-11-11 01:41:06 +08:00
targetVersion = AppConf.isMSE
? AppConf.networkVersionData!.mSELastVersion!
: AppConf.networkVersionData!.lastVersion!;
final r = await Api.getAppReleaseDataByVersionName(targetVersion);
2023-10-09 09:32:07 +08:00
description = r["description"];
final assetsLinks = List.of(r["assets"]?["links"] ?? []);
for (var link in assetsLinks) {
if (link["name"].toString().contains("SETUP.exe")) {
downloadUrl = link["direct_asset_url"];
break;
}
}
notifyListeners();
} catch (e) {
Navigator.pop(context!, false);
}
}
doUpgrade() async {
2023-11-11 01:41:06 +08:00
if (AppConf.isMSE) {
launchUrlString("ms-windows-store://pdp/?productid=9NF3SWFWNKL1");
2023-11-11 01:44:15 +08:00
await Future.delayed(const Duration(seconds: 3));
exit(0);
2023-11-11 01:41:06 +08:00
}
2023-10-09 09:32:07 +08:00
isUpgrading = true;
notifyListeners();
final fileName = "${AppConf.getUpgradePath()}/next_SETUP.exe";
try {
await Dio().download(downloadUrl, fileName,
onReceiveProgress: (int count, int total) {
progress = (count / total) * 100;
notifyListeners();
});
} catch (_) {
isUpgrading = false;
progress = null;
showToast(context!, "下载失败,请尝试手动安装!");
notifyListeners();
2023-10-12 21:03:49 +08:00
return;
2023-10-09 09:32:07 +08:00
}
try {
2023-11-10 21:58:29 +08:00
final r = await (Process.run(
SystemHelper.powershellPath, ["start", fileName, "/SILENT"]));
2023-10-09 09:32:07 +08:00
if (r.stderr.toString().isNotEmpty) {
throw r.stderr;
}
exit(0);
} catch (_) {
isUpgrading = false;
progress = null;
showToast(context!, "运行失败,请尝试手动安装!");
2023-11-07 22:35:25 +08:00
Process.run(SystemHelper.powershellPath,
["explorer.exe", "/select,\"$fileName\""]);
2023-10-09 09:32:07 +08:00
notifyListeners();
}
}
void doCancel() {
Navigator.pop(context!, true);
}
}