From 8159e9b70e4fb54a59b310264cffbe67fc3e977c Mon Sep 17 00:00:00 2001 From: xkeyC <3334969096@qq.com> Date: Mon, 29 Jan 2024 22:36:04 +0800 Subject: [PATCH] =?UTF-8?q?https://github.com/StarCitizenToolBox/app/issue?= =?UTF-8?q?s/1=20=E5=88=86=E6=B5=81=E4=B8=8B=E8=BD=BD=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=B8=80=E7=A7=8D=E5=BF=AB=E9=80=9F=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E5=8D=A1=E4=BD=8F=E7=9A=84=E4=B8=8B=E8=BD=BD=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/common/utils/base_utils.dart | 5 +- .../downloader/downloader_dialog_ui.dart | 10 +++- .../downloader_dialog_ui_model.dart | 15 ++++- lib/ui/tools/tools_ui_model.dart | 59 +++++++++++++++---- 4 files changed, 73 insertions(+), 16 deletions(-) diff --git a/lib/common/utils/base_utils.dart b/lib/common/utils/base_utils.dart index ae3a10a..c5cde77 100644 --- a/lib/common/utils/base_utils.dart +++ b/lib/common/utils/base_utils.dart @@ -106,7 +106,10 @@ Future showBaseDialog(BuildContext context, builder: (context) => ContentDialog( title: Text(title), content: content, - constraints: constraints ?? kDefaultContentDialogConstraints, + constraints: constraints ?? const BoxConstraints( + maxWidth: 512, + maxHeight: 756.0, + ), actions: actions, ), ); diff --git a/lib/ui/tools/downloader/downloader_dialog_ui.dart b/lib/ui/tools/downloader/downloader_dialog_ui.dart index b70b766..63bb9b3 100644 --- a/lib/ui/tools/downloader/downloader_dialog_ui.dart +++ b/lib/ui/tools/downloader/downloader_dialog_ui.dart @@ -36,13 +36,21 @@ class DownloaderDialogUI extends BaseUI { const SizedBox(width: 24), ], ), + if (model.isP4kDownload) ...[ + const SizedBox(height: 24), + Text( + "提示:因网络波动,若下载进度长时间卡住或速度变慢,可尝试点击暂停下载,之后重新点击P4K分流下载。", + style: + TextStyle(fontSize: 13, color: Colors.white.withOpacity(.7)), + ), + ], ], ), actions: [ FilledButton( child: const Padding( padding: EdgeInsets.only(left: 8, right: 8, top: 2, bottom: 2), - child: Text("取消下载"), + child: Text("暂停下载"), ), onPressed: () => model.doCancel()), ], diff --git a/lib/ui/tools/downloader/downloader_dialog_ui_model.dart b/lib/ui/tools/downloader/downloader_dialog_ui_model.dart index bc0353f..df39f93 100644 --- a/lib/ui/tools/downloader/downloader_dialog_ui_model.dart +++ b/lib/ui/tools/downloader/downloader_dialog_ui_model.dart @@ -1,6 +1,7 @@ import 'dart:io'; import 'package:file_picker/file_picker.dart'; +import 'package:hive/hive.dart'; import 'package:starcitizen_doctor/base/ui_model.dart'; import 'package:starcitizen_doctor/common/rust/ffi.dart'; @@ -10,9 +11,12 @@ class DownloaderDialogUIModel extends BaseUIModel { final String downloadUrl; final bool showChangeSavePathDialog; final int threadCount; + final bool isP4kDownload; DownloaderDialogUIModel(this.fileName, this.savePath, this.downloadUrl, - {this.showChangeSavePathDialog = false, this.threadCount = 1}); + {this.showChangeSavePathDialog = false, + this.threadCount = 1, + this.isP4kDownload = false}); bool isInMerging = false; @@ -52,8 +56,15 @@ class DownloaderDialogUIModel extends BaseUIModel { savePath = savePath.substring(0, savePath.length - fileName.length - 1); } - final downloaderSavePath = "$savePath//$fileName.downloading"; + if (isP4kDownload) { + final box = await Hive.openBox("p4k_cache"); + await box.put( + "last_save_dir", + {"save_path": savePath, "file_name": fileName}, + ); + } + final downloaderSavePath = "$savePath//$fileName.downloading"; try { rustFii .startDownload( diff --git a/lib/ui/tools/tools_ui_model.dart b/lib/ui/tools/tools_ui_model.dart index 692b0c6..249b4b0 100644 --- a/lib/ui/tools/tools_ui_model.dart +++ b/lib/ui/tools/tools_ui_model.dart @@ -4,6 +4,7 @@ import 'dart:io'; import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; +import 'package:hive/hive.dart'; import 'package:starcitizen_doctor/api/analytics.dart'; import 'package:starcitizen_doctor/base/ui_model.dart'; import 'package:starcitizen_doctor/common/conf/app_conf.dart'; @@ -354,6 +355,10 @@ class ToolsUIModel extends BaseUIModel { } Future _downloadP4k() async { + String savePath = scInstalledPath; + String fileName = "Data.p4k"; + bool isResumeDownload = false; + final box = await Hive.openBox("p4k_cache"); var downloadUrl = AppConf.networkVersionData?.p4kDownloadUrl; if (downloadUrl == null || downloadUrl.isEmpty) { showToast(context!, "该功能维护中,请稍后再试!"); @@ -365,14 +370,36 @@ class ToolsUIModel extends BaseUIModel { maxWidth: MediaQuery.of(context!).size.width * .35)); return; } - await showToast( - context!, - "P4k 是星际公民的核心游戏文件,高达近 100GB,盒子提供的离线下载是为了帮助一些p4k文件下载超级慢的用户。" - "\n\n接下来会弹窗询问您保存位置(可以选择星际公民文件夹也可以选择别处),下载完成后请确保 P4K 文件夹位于 LIVE 文件夹内,之后使用星际公民启动器校验更新即可。"); - - AnalyticsApi.touch("p4k_download"); - launchUrlString( - "https://citizenwiki.cn/SC%E6%B1%89%E5%8C%96%E7%9B%92%E5%AD%90#%E5%88%86%E6%B5%81%E4%B8%8B%E8%BD%BD%E6%95%99%E7%A8%8B"); + final lastSavePath = (box.get("last_save_dir", defaultValue: {}) as Map); + dPrint("lastSavePath === $lastSavePath"); + if (lastSavePath.isNotEmpty) { + final s = lastSavePath["save_path"] ?? ""; + final f = lastSavePath["file_name"] ?? ""; + if ((await File("$s/$f.downloading").exists()) && + (await File("$s/$f.downloading.bson").exists())) { + final ok = await showConfirmDialogs(context!, "是否恢复下载?", + const Text("检测到未完成的下载,点击确认即可恢复下载,点击取消将会删除之前的临时文件,并开始一个新的下载。")); + if (ok) { + savePath = s; + fileName = f; + isResumeDownload = true; + } else { + // del last cache and del file + await box.delete("last_save_dir"); + await File("$s/$f.downloading").delete(); + await File("$s/$f.downloading.bson").delete(); + } + } else { + // del last cache + await box.delete("last_save_dir"); + } + } else { + await showToast( + context!, + "P4k 是星际公民的核心游戏文件,高达近 100GB,盒子提供的离线下载是为了帮助一些p4k文件下载超级慢的用户。" + "\n\n接下来会弹窗询问您保存位置(可以选择星际公民文件夹也可以选择别处),下载完成后请确保 P4K 文件夹位于 LIVE 文件夹内,之后使用星际公民启动器校验更新即可。"); + AnalyticsApi.touch("p4k_download"); + } final r = await showDialog( context: context!, dismissWithEsc: false, @@ -380,16 +407,24 @@ class ToolsUIModel extends BaseUIModel { return BaseUIContainer( uiCreate: () => DownloaderDialogUI(), modelCreate: () => DownloaderDialogUIModel( - "Data.p4k", scInstalledPath, downloadUrl, - showChangeSavePathDialog: true, threadCount: 10)); + fileName, savePath, downloadUrl, + showChangeSavePathDialog: !isResumeDownload, + threadCount: 10, + isP4kDownload: true)); }); + if (r != null) { if (r == "cancel") { - return showToast(context!, "下载已取消,下载进度已保留,如果您无需恢复下载,请手动删除下载临时文件。"); + return showToast(context!, "下载进度已保留,您可以再次点击此功能恢复下载。"); } else if (r is DioException) { showToast(context!, "下载失败:$r"); } else { - showToast(context!, "下载完毕,文件已保存到:$r"); + final ok = await showConfirmDialogs( + context!, "下载完毕!", Text("文件已保存到:$r\n\n是否查看P4K操作教程?")); + if (ok == true) { + launchUrlString( + "https://citizenwiki.cn/SC%E6%B1%89%E5%8C%96%E7%9B%92%E5%AD%90#%E5%88%86%E6%B5%81%E4%B8%8B%E8%BD%BD%E6%95%99%E7%A8%8B"); + } } } }