mirror of
https://ghfast.top/https://github.com/StarCitizenToolBox/app.git
synced 2025-06-28 11:24:46 +08:00
feat:riverpod 迁移
This commit is contained in:
@ -1,129 +0,0 @@
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:starcitizen_doctor/base/ui.dart';
|
||||
import 'package:starcitizen_doctor/common/conf/app_conf.dart';
|
||||
import 'package:starcitizen_doctor/ui/settings/settings_ui_model.dart';
|
||||
|
||||
class SettingUI extends BaseUI<SettingUIModel> {
|
||||
@override
|
||||
Widget? buildBody(BuildContext context, SettingUIModel model) {
|
||||
return Container(
|
||||
width: MediaQuery.of(context).size.width,
|
||||
height: MediaQuery.of(context).size.height,
|
||||
margin: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
children: [
|
||||
makeSettingsItem(const Icon(FluentIcons.link, size: 20), "创建设置快捷方式",
|
||||
subTitle: "在桌面创建《SC汉化盒子》快捷方式", onTap: model.addShortCut),
|
||||
if (AppConf.isMSE) ...[
|
||||
const SizedBox(height: 12),
|
||||
makeSettingsItem(
|
||||
const Icon(FluentIcons.reset_device, size: 20), "重置自动密码填充",
|
||||
subTitle:
|
||||
"启用:${model.isEnableAutoLogin ? "已启用" : "已禁用"} 设备支持:${model.isDeviceSupportWinHello ? "支持" : "不支持"} 邮箱:${model.autoLoginEmail} 密码:${model.isEnableAutoLoginPwd ? "已加密保存" : "未保存"}",
|
||||
onTap: model.onResetAutoLogin),
|
||||
],
|
||||
const SizedBox(height: 12),
|
||||
makeSettingsItem(const Icon(FontAwesomeIcons.microchip, size: 20),
|
||||
"启动游戏时忽略能效核心( 适用于Intel 12th+ 处理器 ) [实验性功能,请随时反馈]",
|
||||
subTitle:
|
||||
"已设置的核心数量:${model.inputGameLaunchECore} (此功能适用于首页的盒子一键启动 或 工具中的RSI启动器管理员模式,当为 0 时不启用此功能 )",
|
||||
onTap: model.setGameLaunchECore),
|
||||
const SizedBox(height: 12),
|
||||
makeSettingsItem(const Icon(FluentIcons.folder_open, size: 20),
|
||||
"设置启动器文件(RSI Launcher.exe)",
|
||||
subTitle: model.customLauncherPath != null
|
||||
? "${model.customLauncherPath}"
|
||||
: "手动设置启动器位置,建议仅在无法自动扫描安装位置时使用",
|
||||
onTap: model.setLauncherPath, onDel: () {
|
||||
model.delName("custom_launcher_path");
|
||||
}),
|
||||
const SizedBox(height: 12),
|
||||
makeSettingsItem(const Icon(FluentIcons.game, size: 20),
|
||||
"设置游戏文件 (StarCitizen.exe)",
|
||||
subTitle: model.customGamePath != null
|
||||
? "${model.customGamePath}"
|
||||
: "手动设置游戏安装位置,建议仅在无法自动扫描安装位置时使用",
|
||||
onTap: model.setGamePath, onDel: () {
|
||||
model.delName("custom_game_path");
|
||||
}),
|
||||
const SizedBox(height: 12),
|
||||
makeSettingsItem(const Icon(FluentIcons.delete, size: 20), "清理汉化文件缓存",
|
||||
subTitle:
|
||||
"缓存大小 ${(model.locationCacheSize / 1024 / 1024).toStringAsFixed(2)}MB,清理盒子下载的汉化文件缓存,不会影响已安装的汉化",
|
||||
onTap: model.cleanLocationCache),
|
||||
const SizedBox(height: 12),
|
||||
makeSettingsItem(
|
||||
const Icon(FluentIcons.speed_high, size: 20), "工具站访问加速",
|
||||
onTap: () =>
|
||||
model.onChangeToolSiteMirror(!model.isEnableToolSiteMirrors),
|
||||
subTitle:
|
||||
"使用镜像服务器加速访问 Dps Uex 等工具网站,若访问异常请关闭该功能。 为保护账户安全,任何情况下都不会加速RSI官网。",
|
||||
onSwitch: model.onChangeToolSiteMirror,
|
||||
switchStatus: model.isEnableToolSiteMirrors),
|
||||
const SizedBox(height: 12),
|
||||
makeSettingsItem(
|
||||
const Icon(FluentIcons.document_set, size: 20), "查看log",
|
||||
onTap: () => model.showLogs(),
|
||||
subTitle: "查看汉化盒子的 log 文件,以定位盒子的 bug"),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget makeSettingsItem(Widget icon, String title,
|
||||
{String? subTitle,
|
||||
VoidCallback? onTap,
|
||||
VoidCallback? onDel,
|
||||
void Function(bool? b)? onSwitch,
|
||||
bool switchStatus = false}) {
|
||||
return Button(
|
||||
onPressed: onTap,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 12, bottom: 12),
|
||||
child: Row(
|
||||
children: [
|
||||
icon,
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Text(title),
|
||||
const Spacer(),
|
||||
],
|
||||
),
|
||||
if (subTitle != null) ...[
|
||||
const SizedBox(height: 3),
|
||||
Text(
|
||||
subTitle,
|
||||
style: TextStyle(
|
||||
fontSize: 12, color: Colors.white.withOpacity(.6)),
|
||||
)
|
||||
]
|
||||
],
|
||||
),
|
||||
),
|
||||
if (onDel != null) ...[
|
||||
Button(
|
||||
onPressed: onDel,
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.all(6),
|
||||
child: Icon(FluentIcons.delete),
|
||||
)),
|
||||
],
|
||||
if (onSwitch != null) ...[
|
||||
ToggleSwitch(checked: switchStatus, onChanged: onSwitch),
|
||||
],
|
||||
const SizedBox(width: 12),
|
||||
const Icon(FluentIcons.chevron_right),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String getUITitle(BuildContext context, SettingUIModel model) => "SettingUI";
|
||||
}
|
@ -1,197 +0,0 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
import 'package:starcitizen_doctor/base/ui_model.dart';
|
||||
import 'package:starcitizen_doctor/common/conf/app_conf.dart';
|
||||
import 'package:starcitizen_doctor/common/helper/system_helper.dart';
|
||||
import 'package:starcitizen_doctor/common/win32/credentials.dart';
|
||||
|
||||
class SettingUIModel extends BaseUIModel {
|
||||
var isDeviceSupportWinHello = false;
|
||||
|
||||
String autoLoginEmail = "-";
|
||||
bool isEnableAutoLogin = false;
|
||||
bool isEnableAutoLoginPwd = false;
|
||||
bool isEnableToolSiteMirrors = false;
|
||||
String inputGameLaunchECore = "0";
|
||||
|
||||
String? customLauncherPath;
|
||||
String? customGamePath;
|
||||
|
||||
int locationCacheSize = 0;
|
||||
|
||||
@override
|
||||
loadData() async {
|
||||
dPrint("SettingUIModel.loadData");
|
||||
final LocalAuthentication localAuth = LocalAuthentication();
|
||||
isDeviceSupportWinHello = await localAuth.isDeviceSupported();
|
||||
notifyListeners();
|
||||
_updateGameLaunchECore();
|
||||
if (AppConf.isMSE) {
|
||||
_updateAutoLoginAccount();
|
||||
}
|
||||
_loadCustomPath();
|
||||
_loadLocationCacheSize();
|
||||
_loadToolSiteMirrorState();
|
||||
}
|
||||
|
||||
Future<void> onResetAutoLogin() async {
|
||||
final ok = await showConfirmDialogs(context!, "确认重置自动填充?",
|
||||
const Text("这将会删除本地的账号记录,或在下次启动游戏时将自动填充选择 ‘否’ 以禁用自动填充。"));
|
||||
if (ok) {
|
||||
final userBox = await Hive.openBox("rsi_account_data");
|
||||
await userBox.deleteFromDisk();
|
||||
Win32Credentials.delete("SCToolbox_RSI_Account_secret");
|
||||
showToast(context!, "已清理自动填充数据");
|
||||
reloadData();
|
||||
}
|
||||
}
|
||||
|
||||
Future _updateAutoLoginAccount() async {
|
||||
final userBox = await Hive.openBox("rsi_account_data");
|
||||
autoLoginEmail = userBox.get("account_email", defaultValue: "-");
|
||||
isEnableAutoLogin = userBox.get("enable", defaultValue: true);
|
||||
isEnableAutoLoginPwd =
|
||||
userBox.get("account_pwd_encrypted", defaultValue: "") != "";
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> setGameLaunchECore() async {
|
||||
final userBox = await Hive.openBox("app_conf");
|
||||
final defaultInput =
|
||||
userBox.get("gameLaunch_eCore_count", defaultValue: "0");
|
||||
final input = await showInputDialogs(context!,
|
||||
title: "请输入要忽略的 CPU 核心数",
|
||||
content:
|
||||
"Tip:您的设备拥有几个能效核心就输入几,非大小核设备请保持 0\n\n此功能适用于首页的盒子一键启动 或 工具中的 RSI启动器管理员模式,当为 0 时不启用此功能。",
|
||||
initialValue: defaultInput,
|
||||
inputFormatters: [FilteringTextInputFormatter.digitsOnly]);
|
||||
if (input == null) return;
|
||||
userBox.put("gameLaunch_eCore_count", input);
|
||||
reloadData();
|
||||
}
|
||||
|
||||
Future _updateGameLaunchECore() async {
|
||||
final userBox = await Hive.openBox("app_conf");
|
||||
inputGameLaunchECore =
|
||||
userBox.get("gameLaunch_eCore_count", defaultValue: "0");
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> setLauncherPath() async {
|
||||
final r = await FilePicker.platform.pickFiles(
|
||||
dialogTitle: "请选择RSI启动器位置(RSI Launcher.exe)",
|
||||
type: FileType.custom,
|
||||
allowedExtensions: ["exe"]);
|
||||
if (r == null || r.files.firstOrNull?.path == null) return;
|
||||
final fileName = r.files.first.path!;
|
||||
if (fileName.endsWith("\\RSI Launcher.exe")) {
|
||||
await _saveCustomPath("custom_launcher_path", fileName);
|
||||
showToast(context!, "设置成功,在对应页面点击刷新即可扫描出新路径");
|
||||
reloadData();
|
||||
} else {
|
||||
showToast(context!, "文件有误!");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setGamePath() async {
|
||||
final r = await FilePicker.platform.pickFiles(
|
||||
dialogTitle: "请选择游戏安装位置(StarCitizen.exe)",
|
||||
type: FileType.custom,
|
||||
allowedExtensions: ["exe"]);
|
||||
if (r == null || r.files.firstOrNull?.path == null) return;
|
||||
final fileName = r.files.first.path!;
|
||||
dPrint(fileName);
|
||||
final fileNameRegExp =
|
||||
RegExp(r"^(.*\\StarCitizen\\.*\\)Bin64\\StarCitizen\.exe$");
|
||||
if (fileNameRegExp.hasMatch(fileName)) {
|
||||
RegExp pathRegex = RegExp(r"\\[^\\]+\\Bin64\\StarCitizen\.exe$");
|
||||
String extractedPath = fileName.replaceFirst(pathRegex, '');
|
||||
await _saveCustomPath("custom_game_path", extractedPath);
|
||||
showToast(context!, "设置成功,在对应页面点击刷新即可扫描出新路径");
|
||||
reloadData();
|
||||
} else {
|
||||
showToast(context!, "文件有误!");
|
||||
}
|
||||
}
|
||||
|
||||
_saveCustomPath(String pathKey, String dir) async {
|
||||
final confBox = await Hive.openBox("app_conf");
|
||||
await confBox.put(pathKey, dir);
|
||||
}
|
||||
|
||||
_loadCustomPath() async {
|
||||
final confBox = await Hive.openBox("app_conf");
|
||||
customLauncherPath = confBox.get("custom_launcher_path");
|
||||
customGamePath = confBox.get("custom_game_path");
|
||||
}
|
||||
|
||||
Future<void> delName(String key) async {
|
||||
final confBox = await Hive.openBox("app_conf");
|
||||
await confBox.delete(key);
|
||||
reloadData();
|
||||
}
|
||||
|
||||
_loadLocationCacheSize() async {
|
||||
final len = await SystemHelper.getDirLen(
|
||||
"${AppConf.applicationSupportDir}/Localizations");
|
||||
locationCacheSize = len;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> cleanLocationCache() async {
|
||||
final ok = await showConfirmDialogs(
|
||||
context!, "确认清理汉化缓存?", const Text("这不会影响已安装的汉化。"));
|
||||
if (ok == true) {
|
||||
final dir = Directory("${AppConf.applicationSupportDir}/Localizations");
|
||||
await handleError(() => dir.delete(recursive: true));
|
||||
reloadData();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> addShortCut() async {
|
||||
if (AppConf.isMSE) {
|
||||
showToast(context!, "因微软版功能限制,请在接下来打开的窗口中 手动将《SC汉化盒子》拖动到桌面,即可创建快捷方式。");
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
Process.run("explorer.exe", ["shell:AppsFolder"]);
|
||||
return;
|
||||
}
|
||||
dPrint(Platform.resolvedExecutable);
|
||||
final script = """
|
||||
\$targetPath = "${Platform.resolvedExecutable}";
|
||||
\$shortcutPath = [System.IO.Path]::Combine([System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::DesktopDirectory), "SC汉化盒子DEV.lnk");
|
||||
\$shell = New-Object -ComObject WScript.Shell
|
||||
\$shortcut = \$shell.CreateShortcut(\$shortcutPath)
|
||||
if (\$shortcut -eq \$null) {
|
||||
Write-Host "Failed to create shortcut."
|
||||
} else {
|
||||
\$shortcut.TargetPath = \$targetPath
|
||||
\$shortcut.Save()
|
||||
Write-Host "Shortcut created successfully."
|
||||
}
|
||||
""";
|
||||
await Process.run(SystemHelper.powershellPath, [script]);
|
||||
showToast(context!, "创建完毕,请返回桌面查看");
|
||||
}
|
||||
|
||||
_loadToolSiteMirrorState() async {
|
||||
final userBox = await Hive.openBox("app_conf");
|
||||
isEnableToolSiteMirrors =
|
||||
userBox.get("isEnableToolSiteMirrors", defaultValue: false);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void onChangeToolSiteMirror(bool? b) async {
|
||||
final userBox = await Hive.openBox("app_conf");
|
||||
isEnableToolSiteMirrors = b == true;
|
||||
await userBox.put("isEnableToolSiteMirrors", isEnableToolSiteMirrors);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
showLogs() async {
|
||||
SystemHelper.openDir(AppConf.appLogFile?.absolute.path);
|
||||
}
|
||||
}
|
263
lib/ui/settings/upgrade_dialog.dart
Normal file
263
lib/ui/settings/upgrade_dialog.dart
Normal file
@ -0,0 +1,263 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:fluent_ui/fluent_ui.dart';
|
||||
import 'package:flutter/material.dart' show Material;
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:markdown/markdown.dart' as markdown;
|
||||
import 'package:starcitizen_doctor/api/api.dart';
|
||||
import 'package:starcitizen_doctor/app.dart';
|
||||
import 'package:starcitizen_doctor/common/conf/const_conf.dart';
|
||||
import 'package:starcitizen_doctor/common/conf/url_conf.dart';
|
||||
import 'package:starcitizen_doctor/common/helper/system_helper.dart';
|
||||
import 'package:starcitizen_doctor/common/utils/base_utils.dart';
|
||||
import 'package:starcitizen_doctor/common/utils/log.dart';
|
||||
import 'package:starcitizen_doctor/widgets/widgets.dart';
|
||||
import 'package:html/parser.dart' as html_parser;
|
||||
import 'package:url_launcher/url_launcher_string.dart';
|
||||
|
||||
class UpgradeDialogUI extends HookConsumerWidget {
|
||||
const UpgradeDialogUI({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final appState = ref.watch(appGlobalModelProvider);
|
||||
final appModel = ref.read(appGlobalModelProvider.notifier);
|
||||
final description = useState<String?>(null);
|
||||
final isUsingDiversion = useState(false);
|
||||
final isUpgrading = useState(false);
|
||||
final progress = useState(0.0);
|
||||
final downloadUrl = useState("");
|
||||
|
||||
final targetVersion = ConstConf.isMSE
|
||||
? appState.networkVersionData!.mSELastVersion!
|
||||
: appState.networkVersionData!.lastVersion!;
|
||||
|
||||
final minVersionCode = ConstConf.isMSE
|
||||
? appState.networkVersionData?.mSEMinVersionCode
|
||||
: appState.networkVersionData?.minVersionCode;
|
||||
|
||||
useEffect(() {
|
||||
_getUpdateInfo(context, targetVersion, description, downloadUrl);
|
||||
return null;
|
||||
}, []);
|
||||
|
||||
return Material(
|
||||
child: ContentDialog(
|
||||
title: Text("发现新版本 -> $targetVersion"),
|
||||
constraints:
|
||||
BoxConstraints(maxWidth: MediaQuery.of(context).size.width * .55),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Expanded(
|
||||
child: SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 24, right: 24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (description.value == null) ...[
|
||||
const Center(
|
||||
child: Column(
|
||||
children: [
|
||||
ProgressRing(),
|
||||
SizedBox(height: 16),
|
||||
Text("正在获取新版本详情...")
|
||||
],
|
||||
),
|
||||
)
|
||||
] else
|
||||
...makeMarkdownView(description.value!,
|
||||
attachmentsUrl: URLConf.giteaAttachmentsUrl),
|
||||
],
|
||||
),
|
||||
),
|
||||
)),
|
||||
if (isUsingDiversion.value) ...[
|
||||
const SizedBox(height: 24),
|
||||
GestureDetector(
|
||||
onTap: _launchReleaseUrl,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withOpacity(.1),
|
||||
borderRadius: BorderRadius.circular(7)),
|
||||
child: Text(
|
||||
"提示:当前正在使用分流服务器进行更新,可能会出现下载速度下降,但有助于我们进行成本控制,若下载异常请点击这里跳转手动安装。",
|
||||
style: TextStyle(
|
||||
fontSize: 14, color: Colors.white.withOpacity(.7)),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
if (isUpgrading.value) ...[
|
||||
const SizedBox(height: 24),
|
||||
Row(
|
||||
children: [
|
||||
Text(progress.value == 100
|
||||
? "正在安装: "
|
||||
: "正在下载: ${progress.value.toStringAsFixed(2)}% "),
|
||||
Expanded(
|
||||
child: ProgressBar(
|
||||
value: progress.value == 100 ? null : progress.value,
|
||||
)),
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
actions: isUpgrading.value
|
||||
? null
|
||||
: [
|
||||
if (downloadUrl.value.isNotEmpty)
|
||||
FilledButton(
|
||||
onPressed: () => _doUpgrade(
|
||||
context,
|
||||
appState,
|
||||
isUpgrading,
|
||||
appModel,
|
||||
downloadUrl,
|
||||
description,
|
||||
isUsingDiversion,
|
||||
progress),
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.only(
|
||||
top: 4, bottom: 4, left: 8, right: 8),
|
||||
child: Text("立即更新"),
|
||||
)),
|
||||
if (ConstConf.appVersionCode >= (minVersionCode ?? 0))
|
||||
Button(
|
||||
onPressed: () => _doCancel(context),
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.only(
|
||||
top: 4, bottom: 4, left: 8, right: 8),
|
||||
child: Text("下次吧"),
|
||||
)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _getUpdateInfo(
|
||||
BuildContext context,
|
||||
targetVersion,
|
||||
ValueNotifier<String?> description,
|
||||
ValueNotifier<String> downloadUrl) async {
|
||||
try {
|
||||
final r = await Api.getAppReleaseDataByVersionName(targetVersion);
|
||||
description = r["body"];
|
||||
final assets = List.of(r["assets"] ?? []);
|
||||
for (var asset in assets) {
|
||||
if (asset["name"].toString().endsWith("SETUP.exe")) {
|
||||
downloadUrl.value = asset["browser_download_url"];
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
dPrint("UpgradeDialogUIModel.loadData Error : $e");
|
||||
if (!context.mounted) return;
|
||||
Navigator.pop(context, false);
|
||||
}
|
||||
}
|
||||
|
||||
void _launchReleaseUrl() {
|
||||
launchUrlString(URLConf.devReleaseUrl);
|
||||
}
|
||||
|
||||
void _doCancel(BuildContext context) {
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
|
||||
String _getDiversionUrl(String description) {
|
||||
try {
|
||||
final htmlStr = markdown.markdownToHtml(description);
|
||||
final html = html_parser.parse(htmlStr);
|
||||
for (var element in html.querySelectorAll('a')) {
|
||||
String linkText = element.text;
|
||||
String linkUrl = element.attributes['href'] ?? '';
|
||||
if (linkText.trim().endsWith("_SETUP.exe")) {
|
||||
final diversionDownloadUrl = linkUrl.trim();
|
||||
dPrint("diversionDownloadUrl === $diversionDownloadUrl");
|
||||
return diversionDownloadUrl;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
dPrint("_checkDiversionUrl Error:$e");
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
Future<void> _doUpgrade(
|
||||
BuildContext context,
|
||||
AppGlobalState appState,
|
||||
ValueNotifier<bool> isUpgrading,
|
||||
AppGlobalModel appModel,
|
||||
ValueNotifier<String> downloadUrl,
|
||||
ValueNotifier<String?> description,
|
||||
ValueNotifier<bool> isUsingDiversion,
|
||||
ValueNotifier<double> progress) async {
|
||||
if (ConstConf.isMSE) {
|
||||
launchUrlString("ms-windows-store://pdp/?productid=9NF3SWFWNKL1");
|
||||
await Future.delayed(const Duration(seconds: 3));
|
||||
if (ConstConf.appVersionCode <
|
||||
(appState.networkVersionData?.minVersionCode ?? 0)) {
|
||||
exit(0);
|
||||
}
|
||||
if (!context.mounted) return;
|
||||
_doCancel(context);
|
||||
}
|
||||
isUpgrading.value = true;
|
||||
final fileName = "${appModel.getUpgradePath()}/next_SETUP.exe";
|
||||
try {
|
||||
// check diversionDownloadUrl
|
||||
var url = downloadUrl.value;
|
||||
final diversionDownloadUrl = _getDiversionUrl(description.value!);
|
||||
final dio = Dio();
|
||||
if (diversionDownloadUrl.isNotEmpty) {
|
||||
try {
|
||||
final resp = await dio.head(diversionDownloadUrl,
|
||||
options: Options(
|
||||
sendTimeout: const Duration(seconds: 10),
|
||||
receiveTimeout: const Duration(seconds: 10)));
|
||||
if (resp.statusCode == 200) {
|
||||
isUsingDiversion.value = true;
|
||||
url = diversionDownloadUrl;
|
||||
} else {
|
||||
isUsingDiversion.value = false;
|
||||
}
|
||||
dPrint("diversionDownloadUrl head resp == ${resp.headers}");
|
||||
} catch (e) {
|
||||
dPrint("diversionDownloadUrl err:$e");
|
||||
}
|
||||
}
|
||||
await dio.download(url, fileName,
|
||||
onReceiveProgress: (int count, int total) {
|
||||
progress.value = (count / total) * 100;
|
||||
});
|
||||
} catch (_) {
|
||||
isUpgrading.value = false;
|
||||
progress.value = 0;
|
||||
if (!context.mounted) return;
|
||||
showToast(context, "下载失败,请尝试手动安装!");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final r = await (Process.run(
|
||||
SystemHelper.powershellPath, ["start", fileName, "/SILENT"]));
|
||||
if (r.stderr.toString().isNotEmpty) {
|
||||
throw r.stderr;
|
||||
}
|
||||
exit(0);
|
||||
} catch (_) {
|
||||
isUpgrading.value = false;
|
||||
progress.value = 0;
|
||||
if (!context.mounted) return;
|
||||
showToast(context, "运行失败,请尝试手动安装!");
|
||||
Process.run(SystemHelper.powershellPath,
|
||||
["explorer.exe", "/select,\"$fileName\""]);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,104 +0,0 @@
|
||||
import 'package:flutter/material.dart' show Material;
|
||||
import 'package:starcitizen_doctor/base/ui_model.dart';
|
||||
import 'package:starcitizen_doctor/common/conf/app_conf.dart';
|
||||
import 'package:starcitizen_doctor/common/conf/url_conf.dart';
|
||||
|
||||
import 'upgrade_dialog_ui_model.dart';
|
||||
|
||||
class UpgradeDialogUI extends BaseUI<UpgradeDialogUIModel> {
|
||||
@override
|
||||
Widget? buildBody(BuildContext context, UpgradeDialogUIModel model) {
|
||||
return Material(
|
||||
child: ContentDialog(
|
||||
title: Text("发现新版本 -> ${model.targetVersion}"),
|
||||
constraints:
|
||||
BoxConstraints(maxWidth: MediaQuery.of(context).size.width * .55),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Expanded(
|
||||
child: SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 24, right: 24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (model.description == null) ...[
|
||||
const Center(
|
||||
child: Column(
|
||||
children: [
|
||||
ProgressRing(),
|
||||
SizedBox(height: 16),
|
||||
Text("正在获取新版本详情...")
|
||||
],
|
||||
),
|
||||
)
|
||||
] else
|
||||
...makeMarkdownView(model.description!,
|
||||
attachmentsUrl: URLConf.giteaAttachmentsUrl),
|
||||
],
|
||||
),
|
||||
),
|
||||
)),
|
||||
if (model.isUsingDiversion) ...[
|
||||
const SizedBox(height: 24),
|
||||
GestureDetector(
|
||||
onTap: model.launchReleaseUrl,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withOpacity(.1),
|
||||
borderRadius: BorderRadius.circular(7)),
|
||||
child: Text(
|
||||
"提示:当前正在使用分流服务器进行更新,可能会出现下载速度下降,但有助于我们进行成本控制,若下载异常请点击这里跳转手动安装。",
|
||||
style: TextStyle(
|
||||
fontSize: 14, color: Colors.white.withOpacity(.7)),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
if (model.isUpgrading) ...[
|
||||
const SizedBox(height: 24),
|
||||
Row(
|
||||
children: [
|
||||
Text(model.progress == 100
|
||||
? "正在安装: "
|
||||
: "正在下载: ${model.progress?.toStringAsFixed(2) ?? 0}% "),
|
||||
Expanded(
|
||||
child: ProgressBar(
|
||||
value: model.progress == 100 ? null : model.progress,
|
||||
)),
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
actions: model.isUpgrading
|
||||
? null
|
||||
: [
|
||||
if (model.downloadUrl.isNotEmpty)
|
||||
FilledButton(
|
||||
onPressed: model.doUpgrade,
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.only(
|
||||
top: 4, bottom: 4, left: 8, right: 8),
|
||||
child: Text("立即更新"),
|
||||
)),
|
||||
if (AppConf.appVersionCode >=
|
||||
(AppConf.networkVersionData?.minVersionCode ?? 0))
|
||||
Button(
|
||||
onPressed: model.doCancel,
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.only(
|
||||
top: 4, bottom: 4, left: 8, right: 8),
|
||||
child: Text("下次吧"),
|
||||
)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String getUITitle(BuildContext context, UpgradeDialogUIModel model) => "";
|
||||
}
|
@ -1,136 +0,0 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:markdown/markdown.dart';
|
||||
import 'package:starcitizen_doctor/api/api.dart';
|
||||
import 'package:starcitizen_doctor/base/ui_model.dart';
|
||||
import 'package:starcitizen_doctor/common/conf/app_conf.dart';
|
||||
import 'package:starcitizen_doctor/common/conf/url_conf.dart';
|
||||
import 'package:starcitizen_doctor/common/helper/system_helper.dart';
|
||||
import 'package:url_launcher/url_launcher_string.dart';
|
||||
import 'package:html/parser.dart';
|
||||
|
||||
class UpgradeDialogUIModel extends BaseUIModel {
|
||||
String? description;
|
||||
String targetVersion = "";
|
||||
String downloadUrl = "";
|
||||
String? diversionDownloadUrl;
|
||||
bool isUsingDiversion = false;
|
||||
|
||||
bool isUpgrading = false;
|
||||
double? progress;
|
||||
|
||||
@override
|
||||
Future loadData() async {
|
||||
// get download url for gitlab release
|
||||
try {
|
||||
targetVersion = AppConf.isMSE
|
||||
? AppConf.networkVersionData!.mSELastVersion!
|
||||
: AppConf.networkVersionData!.lastVersion!;
|
||||
final r = await Api.getAppReleaseDataByVersionName(targetVersion);
|
||||
description = r["body"];
|
||||
_checkDiversionUrl();
|
||||
final assets = List.of(r["assets"] ?? []);
|
||||
for (var asset in assets) {
|
||||
if (asset["name"].toString().endsWith("SETUP.exe")) {
|
||||
downloadUrl = asset["browser_download_url"];
|
||||
}
|
||||
}
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
dPrint("UpgradeDialogUIModel.loadData Error : $e");
|
||||
Navigator.pop(context!, false);
|
||||
}
|
||||
}
|
||||
|
||||
doUpgrade() async {
|
||||
if (AppConf.isMSE) {
|
||||
launchUrlString("ms-windows-store://pdp/?productid=9NF3SWFWNKL1");
|
||||
await Future.delayed(const Duration(seconds: 3));
|
||||
if (AppConf.appVersionCode <
|
||||
(AppConf.networkVersionData?.minVersionCode ?? 0)) {
|
||||
exit(0);
|
||||
}
|
||||
Navigator.pop(context!);
|
||||
}
|
||||
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,
|
||||
onReceiveProgress: (int count, int total) {
|
||||
progress = (count / total) * 100;
|
||||
notifyListeners();
|
||||
});
|
||||
} catch (_) {
|
||||
isUpgrading = false;
|
||||
progress = null;
|
||||
showToast(context!, "下载失败,请尝试手动安装!");
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user