From 2a09cd499a03f4bb9cfe9d8835dcb312645285ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E5=BD=AA?= <1315508912@qq.com> Date: Thu, 14 Dec 2023 22:04:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E7=9A=84=E8=B7=A8=E5=B9=B3=E5=8F=B0=E5=A4=84=E7=90=86=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DownKyi/Services/Download/DownloadService.cs | 18 +++---- DownKyi/Utils/PlatformHelper.cs | 41 ++++++++++++++ .../DownloadManager/DownloadedItem.cs | 53 +++++++++++-------- .../ViewModels/Settings/ViewAboutViewModel.cs | 23 ++++---- .../Toolbox/ViewBiliHelperViewModel.cs | 39 +++++--------- 5 files changed, 104 insertions(+), 70 deletions(-) create mode 100644 DownKyi/Utils/PlatformHelper.cs diff --git a/DownKyi/Services/Download/DownloadService.cs b/DownKyi/Services/Download/DownloadService.cs index ad8c33b..6e5477e 100644 --- a/DownKyi/Services/Download/DownloadService.cs +++ b/DownKyi/Services/Download/DownloadService.cs @@ -36,7 +36,7 @@ public abstract class DownloadService protected Task workTask; protected CancellationTokenSource tokenSource; protected CancellationToken cancellationToken; - protected List downloadingTasks = new List(); + protected List downloadingTasks = new(); protected readonly int retry = 5; protected readonly string nullMark = ""; @@ -453,7 +453,7 @@ public abstract class DownloadService } await Task.WhenAny(Task.WhenAll(downloadingTasks), Task.Delay(30000)); - foreach (Task tsk in downloadingTasks.FindAll((m) => !m.IsCompleted)) + foreach (var tsk in downloadingTasks.FindAll((m) => !m.IsCompleted)) { Console.PrintLine($"{Tag}.DoWork() 任务结束超时"); LogManager.Debug($"{Tag}.DoWork()", "任务结束超时"); @@ -485,8 +485,8 @@ public abstract class DownloadService Console.PrintLine(Tag, e.ToString()); LogManager.Debug(Tag, e.Message); - AlertService alertService = new AlertService(dialogService); - ButtonResult result = + var alertService = new AlertService(dialogService); + var result = await alertService.ShowError($"{path}{DictionaryResource.GetString("DirectoryError")}"); return; @@ -781,7 +781,7 @@ public abstract class DownloadService break; case AfterDownloadOperation.CLOSE_SYSTEM: // 关机 - Process.Start("shutdown.exe", "-s"); + // Process.Start("shutdown.exe", "-s"); break; default: break; @@ -801,7 +801,7 @@ public abstract class DownloadService //先简单等待一下 // 下载数据存储服务 - // DownloadStorageService downloadStorageService = new DownloadStorageService(); + var downloadStorageService = new DownloadStorageService(); // 保存数据 foreach (DownloadingItem item in downloadingList) { @@ -829,12 +829,12 @@ public abstract class DownloadService item.Progress = 0; - // downloadStorageService.UpdateDownloading(item); + downloadStorageService.UpdateDownloading(item); } - foreach (DownloadedItem item in downloadedList) + foreach (var item in downloadedList) { - // downloadStorageService.UpdateDownloaded(item); + downloadStorageService.UpdateDownloaded(item); } } diff --git a/DownKyi/Utils/PlatformHelper.cs b/DownKyi/Utils/PlatformHelper.cs new file mode 100644 index 0000000..cfb3c78 --- /dev/null +++ b/DownKyi/Utils/PlatformHelper.cs @@ -0,0 +1,41 @@ +using System; +using System.Diagnostics; + +namespace DownKyi.Utils; + +public static class PlatformHelper +{ + /// + /// 打开文件夹 + /// + /// 路径 + public static void OpenFolder(string folder) + { + if (OperatingSystem.IsWindows()) + { + Process.Start("explorer.exe", $"/select,{folder}"); + } + + if (OperatingSystem.IsMacOS()) + { + Process.Start("open", $"\"{folder}\""); + } + } + + /// + /// 打开各种 (文件、url) + /// + /// 文件名 + public static void Open(string filename) + { + if (OperatingSystem.IsWindows()) + { + Process.Start(filename); + } + + if (OperatingSystem.IsMacOS()) + { + Process.Start("open", $"\"{filename}\""); + } + } +} \ No newline at end of file diff --git a/DownKyi/ViewModels/DownloadManager/DownloadedItem.cs b/DownKyi/ViewModels/DownloadManager/DownloadedItem.cs index 3304e5a..8f5e82b 100644 --- a/DownKyi/ViewModels/DownloadManager/DownloadedItem.cs +++ b/DownKyi/ViewModels/DownloadManager/DownloadedItem.cs @@ -1,6 +1,4 @@ -using System; -using System.Diagnostics; -using System.IO; +using System.IO; using System.Linq; using DownKyi.Images; using DownKyi.Models; @@ -57,9 +55,10 @@ namespace DownKyi.ViewModels.DownloadManager } } - #region 控制按钮 + #region 控制按钮 private VectorImage openFolder; + public VectorImage OpenFolder { get => openFolder; @@ -67,6 +66,7 @@ namespace DownKyi.ViewModels.DownloadManager } private VectorImage openVideo; + public VectorImage OpenVideo { get => openVideo; @@ -74,6 +74,7 @@ namespace DownKyi.ViewModels.DownloadManager } private VectorImage removeVideo; + public VectorImage RemoveVideo { get => removeVideo; @@ -86,19 +87,25 @@ namespace DownKyi.ViewModels.DownloadManager // 打开文件夹事件 private DelegateCommand openFolderCommand; - public DelegateCommand OpenFolderCommand => openFolderCommand ?? (openFolderCommand = new DelegateCommand(ExecuteOpenFolderCommand)); + + public DelegateCommand OpenFolderCommand => + openFolderCommand ?? (openFolderCommand = new DelegateCommand(ExecuteOpenFolderCommand)); /// /// 打开文件夹事件 /// private void ExecuteOpenFolderCommand() { - if (DownloadBase == null) { return; } + if (DownloadBase == null) + { + return; + } + //TODO:这里不光有mp4视频文件,也可能存在音频文件、字幕,或者其他文件类型 //fix bug:Issues #709 //这里根据需要下载的类型判断,具体对应的文件后缀名 var downLoadContents = DownloadBase.NeedDownloadContent.Where(e => e.Value == true).Select(e => e.Key); - string fileSuffix = string.Empty; + var fileSuffix = string.Empty; if (downLoadContents.Contains("downloadVideo")) { fileSuffix = ".mp4"; @@ -111,14 +118,12 @@ namespace DownKyi.ViewModels.DownloadManager { fileSuffix = ".jpg"; } - string videoPath = $"{DownloadBase.FilePath}{fileSuffix}"; - FileInfo fileInfo = new FileInfo(videoPath); + + var videoPath = $"{DownloadBase.FilePath}{fileSuffix}"; + var fileInfo = new FileInfo(videoPath); if (File.Exists(fileInfo.FullName)) { - if (OperatingSystem.IsWindows()) - { - Process.Start("Explorer", "/select," + fileInfo.FullName); - } + PlatformHelper.OpenFolder(fileInfo.FullName); } else { @@ -135,16 +140,16 @@ namespace DownKyi.ViewModels.DownloadManager /// private void ExecuteOpenVideoCommand() { - if (DownloadBase == null) { return; } + if (DownloadBase == null) + { + return; + } - string videoPath = $"{DownloadBase.FilePath}.mp4"; + var videoPath = $"{DownloadBase.FilePath}.mp4"; var fileInfo = new FileInfo(videoPath); if (File.Exists(fileInfo.FullName)) { - if (OperatingSystem.IsWindows()) - { - Process.Start(fileInfo.FullName); - } + PlatformHelper.Open(fileInfo.FullName); } else { @@ -155,7 +160,10 @@ namespace DownKyi.ViewModels.DownloadManager // 删除事件 private DelegateCommand removeVideoCommand; - public DelegateCommand RemoveVideoCommand => removeVideoCommand ?? (removeVideoCommand = new DelegateCommand(ExecuteRemoveVideoCommand)); + + public DelegateCommand RemoveVideoCommand => removeVideoCommand ?? + (removeVideoCommand = + new DelegateCommand(ExecuteRemoveVideoCommand)); /// /// 删除事件 @@ -168,11 +176,10 @@ namespace DownKyi.ViewModels.DownloadManager { return; } - + App.DownloadedList.Remove(this); } #endregion - } -} +} \ No newline at end of file diff --git a/DownKyi/ViewModels/Settings/ViewAboutViewModel.cs b/DownKyi/ViewModels/Settings/ViewAboutViewModel.cs index 8520a42..a52924a 100644 --- a/DownKyi/ViewModels/Settings/ViewAboutViewModel.cs +++ b/DownKyi/ViewModels/Settings/ViewAboutViewModel.cs @@ -1,5 +1,4 @@ -using System.Diagnostics; -using DownKyi.Core.Settings; +using DownKyi.Core.Settings; using DownKyi.Events; using DownKyi.Models; using DownKyi.Utils; @@ -97,7 +96,7 @@ public class ViewAboutViewModel : ViewModelBase /// private void ExecuteAppNameCommand() { - Process.Start("https://github.com/leiurayer/downkyi"); + PlatformHelper.Open("https://github.com/leiurayer/downkyi"); } // 检查更新事件 @@ -124,7 +123,7 @@ public class ViewAboutViewModel : ViewModelBase /// private void ExecuteFeedbackCommand() { - Process.Start("https://github.com/leiurayer/downkyi/issues"); + PlatformHelper.Open("https://github.com/leiurayer/downkyi/issues"); } // 是否接收测试版更新事件 @@ -173,7 +172,7 @@ public class ViewAboutViewModel : ViewModelBase /// private void ExecuteBrotliLicenseCommand() { - Process.Start("https://licenses.nuget.org/MIT"); + PlatformHelper.Open("https://licenses.nuget.org/MIT"); } // Google.Protobuf许可证查看事件 @@ -188,7 +187,7 @@ public class ViewAboutViewModel : ViewModelBase /// private void ExecuteProtobufLicenseCommand() { - Process.Start("https://github.com/protocolbuffers/protobuf/blob/master/LICENSE"); + PlatformHelper.Open("https://github.com/protocolbuffers/protobuf/blob/master/LICENSE"); } // Newtonsoft.Json许可证查看事件 @@ -203,7 +202,7 @@ public class ViewAboutViewModel : ViewModelBase /// private void ExecuteNewtonsoftLicenseCommand() { - Process.Start("https://licenses.nuget.org/MIT"); + PlatformHelper.Open("https://licenses.nuget.org/MIT"); } // Prism.DryIoc许可证查看事件 @@ -218,7 +217,7 @@ public class ViewAboutViewModel : ViewModelBase /// private void ExecutePrismLicenseCommand() { - Process.Start("https://www.nuget.org/packages/Prism.DryIoc/8.1.97/license"); + PlatformHelper.Open("https://www.nuget.org/packages/Prism.DryIoc/8.1.97/license"); } // QRCoder许可证查看事件 @@ -233,7 +232,7 @@ public class ViewAboutViewModel : ViewModelBase /// private void ExecuteQRCoderLicenseCommand() { - Process.Start("https://licenses.nuget.org/MIT"); + PlatformHelper.Open("https://licenses.nuget.org/MIT"); } // System.Data.SQLite.Core许可证查看事件 @@ -248,7 +247,7 @@ public class ViewAboutViewModel : ViewModelBase /// private void ExecuteSQLiteLicenseCommand() { - Process.Start("https://www.sqlite.org/copyright.html"); + PlatformHelper.Open("https://www.sqlite.org/copyright.html"); } // Aria2c许可证查看事件 @@ -262,7 +261,7 @@ public class ViewAboutViewModel : ViewModelBase /// private void ExecuteAriaLicenseCommand() { - Process.Start("aria2_COPYING.txt"); + PlatformHelper.Open("aria2_COPYING.txt"); } // FFmpeg许可证查看事件 @@ -277,7 +276,7 @@ public class ViewAboutViewModel : ViewModelBase /// private void ExecuteFFmpegLicenseCommand() { - Process.Start("FFmpeg_LICENSE.txt"); + PlatformHelper.Open("FFmpeg_LICENSE.txt"); } #endregion diff --git a/DownKyi/ViewModels/Toolbox/ViewBiliHelperViewModel.cs b/DownKyi/ViewModels/Toolbox/ViewBiliHelperViewModel.cs index fc300e7..769892a 100644 --- a/DownKyi/ViewModels/Toolbox/ViewBiliHelperViewModel.cs +++ b/DownKyi/ViewModels/Toolbox/ViewBiliHelperViewModel.cs @@ -1,9 +1,8 @@ using System; -using System.Diagnostics; -using System.Runtime.InteropServices; using System.Threading.Tasks; using DownKyi.Core.BiliApi.BiliUtils; using DownKyi.Core.Logging; +using DownKyi.Utils; using Prism.Commands; using Prism.Events; using Console = DownKyi.Core.Utils.Debugging.Console; @@ -20,32 +19,32 @@ public class ViewBiliHelperViewModel : ViewModelBase public string Avid { - get { return avid; } - set { SetProperty(ref avid, value); } + get => avid; + set => SetProperty(ref avid, value); } private string bvid; public string Bvid { - get { return bvid; } - set { SetProperty(ref bvid, value); } + get => bvid; + set => SetProperty(ref bvid, value); } private string danmakuUserID; public string DanmakuUserID { - get { return danmakuUserID; } - set { SetProperty(ref danmakuUserID, value); } + get => danmakuUserID; + set => SetProperty(ref danmakuUserID, value); } private string userMid; public string UserMid { - get { return userMid; } - set { SetProperty(ref userMid, value); } + get => userMid; + set => SetProperty(ref userMid, value); } #endregion @@ -129,20 +128,8 @@ public class ViewBiliHelperViewModel : ViewModelBase /// private void ExecuteGotoWebCommand() { - var baseUrl = "https://www.bilibili.com/video/"; - if (OperatingSystem.IsWindows()) - { - Process.Start(baseUrl + Bvid); - } - - if (OperatingSystem.IsMacOS()) - { - Process.Start(new ProcessStartInfo - { - FileName = "open", - ArgumentList = { baseUrl + Bvid }, - }); - } + var url = $"https://www.bilibili.com/video/{Bvid}"; + PlatformHelper.Open(url); } // 查询弹幕发送者事件 @@ -190,8 +177,8 @@ public class ViewBiliHelperViewModel : ViewModelBase return; } - string baseUrl = "https://space.bilibili.com/"; - Process.Start(baseUrl + UserMid); + var userSpace = $"https://space.bilibili.com/{UserMid}"; + PlatformHelper.Open(userSpace); } #endregion