app/lib/ui/settings/upgrade_dialog_ui_model.dart

137 lines
4.1 KiB
Dart
Raw Normal View History

2023-10-09 09:32:07 +08:00
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:markdown/markdown.dart';
2023-10-09 09:32:07 +08:00
import 'package:starcitizen_doctor/api/api.dart';
import 'package:starcitizen_doctor/base/ui_model.dart';
2024-01-29 20:44:00 +08:00
import 'package:starcitizen_doctor/common/conf/app_conf.dart';
import 'package:starcitizen_doctor/common/conf/url_conf.dart';
2024-01-29 23:14:55 +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';
import 'package:html/parser.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 = "";
String? diversionDownloadUrl;
bool isUsingDiversion = false;
2023-10-09 09:32:07 +08:00
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-12-05 21:26:46 +08:00
description = r["body"];
_checkDiversionUrl();
2023-12-05 21:26:46 +08:00
final assets = List.of(r["assets"] ?? []);
for (var asset in assets) {
if (asset["name"].toString().endsWith("SETUP.exe")) {
downloadUrl = asset["browser_download_url"];
2023-10-09 09:32:07 +08:00
}
}
notifyListeners();
} catch (e) {
2024-02-07 23:14:02 +08:00
dPrint("UpgradeDialogUIModel.loadData Error : $e");
2023-10-09 09:32:07 +08:00
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));
if (AppConf.appVersionCode <
(AppConf.networkVersionData?.minVersionCode ?? 0)) {
exit(0);
}
Navigator.pop(context!);
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 {
// check diversionDownloadUrl
var url = downloadUrl;
final dio = Dio();
if (diversionDownloadUrl != null) {
try {
final resp = await dio.head(diversionDownloadUrl!,
options: Options(
sendTimeout: const Duration(seconds: 10),
receiveTimeout: const Duration(seconds: 10)));
if (resp.statusCode == 200) {
isUsingDiversion = true;
url = diversionDownloadUrl!;
notifyListeners();
} else {
isUsingDiversion = false;
notifyListeners();
}
dPrint("diversionDownloadUrl head resp == ${resp.headers}");
} catch (e) {
dPrint("diversionDownloadUrl err:$e");
}
}
await dio.download(url, fileName,
2023-10-09 09:32:07 +08:00
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
}
2024-01-29 23:14:55 +08:00
try {
final r = await (Process.run(
SystemHelper.powershellPath, ["start", fileName, "/SILENT"]));
if (r.stderr.toString().isNotEmpty) {
throw r.stderr;
}
exit(0);
} catch (_) {
isUpgrading = false;
progress = null;
showToast(context!, "运行失败,请尝试手动安装!");
Process.run(SystemHelper.powershellPath,
["explorer.exe", "/select,\"$fileName\""]);
notifyListeners();
}
2023-10-09 09:32:07 +08:00
}
void doCancel() {
Navigator.pop(context!, true);
}
void _checkDiversionUrl() {
try {
final htmlStr = markdownToHtml(description!);
final html = parse(htmlStr);
html.querySelectorAll('a').forEach((element) {
String linkText = element.text;
String linkUrl = element.attributes['href'] ?? '';
if (linkText.trim().endsWith("_SETUP.exe")) {
diversionDownloadUrl = linkUrl.trim();
dPrint("diversionDownloadUrl === $diversionDownloadUrl");
}
});
} catch (e) {
dPrint("_checkDiversionUrl Error:$e");
}
}
void launchReleaseUrl() {
launchUrlString(URLConf.devReleaseUrl);
}
2023-10-09 09:32:07 +08:00
}