2024-03-07 23:01:32 +08:00
|
|
|
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/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(
|
2024-03-16 19:13:49 +08:00
|
|
|
title:
|
|
|
|
Text(S.current.app_upgrade_title_new_version_found(targetVersion)),
|
2024-03-07 23:01:32 +08:00
|
|
|
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) ...[
|
2024-03-15 00:01:06 +08:00
|
|
|
Center(
|
2024-03-07 23:01:32 +08:00
|
|
|
child: Column(
|
|
|
|
children: [
|
2024-03-15 00:01:06 +08:00
|
|
|
const ProgressRing(),
|
|
|
|
const SizedBox(height: 16),
|
2024-03-16 19:13:49 +08:00
|
|
|
Text(S.current
|
|
|
|
.app_upgrade_info_getting_new_version_details)
|
2024-03-07 23:01:32 +08:00
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
] 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(
|
2024-03-15 00:01:06 +08:00
|
|
|
S.current.app_upgrade_info_update_server_tip,
|
2024-03-07 23:01:32 +08:00
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 14, color: Colors.white.withOpacity(.7)),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
if (isUpgrading.value) ...[
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Text(progress.value == 100
|
2024-03-15 00:01:06 +08:00
|
|
|
? S.current.app_upgrade_info_installing
|
2024-03-16 19:13:49 +08:00
|
|
|
: S.current.app_upgrade_info_downloading(
|
|
|
|
progress.value.toStringAsFixed(2))),
|
2024-03-07 23:01:32 +08:00
|
|
|
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),
|
2024-03-15 00:01:06 +08:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.only(
|
2024-03-07 23:01:32 +08:00
|
|
|
top: 4, bottom: 4, left: 8, right: 8),
|
2024-03-15 00:01:06 +08:00
|
|
|
child: Text(S.current.app_upgrade_action_update_now),
|
2024-03-07 23:01:32 +08:00
|
|
|
)),
|
|
|
|
if (ConstConf.appVersionCode >= (minVersionCode ?? 0))
|
|
|
|
Button(
|
|
|
|
onPressed: () => _doCancel(context),
|
2024-03-15 00:01:06 +08:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.only(
|
2024-03-07 23:01:32 +08:00
|
|
|
top: 4, bottom: 4, left: 8, right: 8),
|
2024-03-15 00:01:06 +08:00
|
|
|
child: Text(S.current.app_upgrade_action_next_time),
|
2024-03-07 23:01:32 +08:00
|
|
|
)),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _getUpdateInfo(
|
|
|
|
BuildContext context,
|
2024-03-09 20:22:44 +08:00
|
|
|
String targetVersion,
|
2024-03-07 23:01:32 +08:00
|
|
|
ValueNotifier<String?> description,
|
|
|
|
ValueNotifier<String> downloadUrl) async {
|
|
|
|
try {
|
|
|
|
final r = await Api.getAppReleaseDataByVersionName(targetVersion);
|
2024-03-09 20:22:44 +08:00
|
|
|
description.value = r["body"];
|
2024-03-07 23:01:32 +08:00
|
|
|
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);
|
2024-03-09 20:22:44 +08:00
|
|
|
return;
|
2024-03-07 23:01:32 +08:00
|
|
|
}
|
|
|
|
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;
|
2024-03-15 00:01:06 +08:00
|
|
|
showToast(context, S.current.app_upgrade_info_download_failed);
|
2024-03-07 23:01:32 +08:00
|
|
|
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;
|
2024-03-15 00:01:06 +08:00
|
|
|
showToast(context, S.current.app_upgrade_info_run_failed);
|
2024-03-07 23:01:32 +08:00
|
|
|
Process.run(SystemHelper.powershellPath,
|
|
|
|
["explorer.exe", "/select,\"$fileName\""]);
|
|
|
|
}
|
|
|
|
}
|
2024-03-16 19:13:49 +08:00
|
|
|
}
|