From 7cfb778b2231a4e8185bc0e938496ca62d43bf82 Mon Sep 17 00:00:00 2001 From: Miaoyww Date: Sat, 2 Nov 2024 22:02:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BC=93=E5=AD=98=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Resources/LocalSettings.cs | 23 ++-- .../Services/ExpectionService.cs | 6 +- src/NonsPlayer.Core/Services/FileService.cs | 95 +++++++++++++++- src/NonsPlayer/AppConfig.cs | 2 +- src/NonsPlayer/Helpers/DialogHelper.cs | 17 +++ src/NonsPlayer/Helpers/RequestHelper.cs | 16 --- src/NonsPlayer/Helpers/RuntimeHelper.cs | 20 ---- .../Helpers/SettingsStorageExtensions.cs | 101 ------------------ src/NonsPlayer/Helpers/StartUpHelper.cs | 11 -- src/NonsPlayer/Services/SMTCService.cs | 3 +- src/NonsPlayer/Strings/zh-cn/Resources.resw | 15 +++ src/NonsPlayer/Styles/CustomComponents.xaml | 4 + .../Pages/MainPages/SettingsViewModel.cs | 4 +- .../Views/Pages/MainPages/SettingsPage.xaml | 42 +++++--- .../Pages/MainPages/SettingsPage.xaml.cs | 73 +++++++++++-- src/NonsPlayer/Views/ShellPage.xaml | 2 + src/NonsPlayer/Views/ShellPage.xaml.cs | 29 +++-- 17 files changed, 266 insertions(+), 197 deletions(-) create mode 100644 src/NonsPlayer/Helpers/DialogHelper.cs delete mode 100644 src/NonsPlayer/Helpers/RequestHelper.cs delete mode 100644 src/NonsPlayer/Helpers/RuntimeHelper.cs delete mode 100644 src/NonsPlayer/Helpers/SettingsStorageExtensions.cs delete mode 100644 src/NonsPlayer/Helpers/StartUpHelper.cs diff --git a/src/NonsPlayer.Core/Resources/LocalSettings.cs b/src/NonsPlayer.Core/Resources/LocalSettings.cs index 7e3b9ab..6b390d2 100644 --- a/src/NonsPlayer.Core/Resources/LocalSettings.cs +++ b/src/NonsPlayer.Core/Resources/LocalSettings.cs @@ -11,7 +11,7 @@ namespace NonsPlayer.Core.Resources { public class LocalSettings { - [JsonIgnore] public string DataPath { get; set; } + [JsonIgnore] public string MainPath { get; set; } [JsonIgnore] public string ConfigFilePath; @@ -40,6 +40,12 @@ public class LocalSettings [JsonPropertyName("log")] public string Log { get; set; } + [JsonPropertyName("log_path")] public string LogPath { get; set; } + + [JsonPropertyName("cache_path")] public string CachePath { get; set; } + + [JsonPropertyName("smtc")] public bool SMTCEnable { get; set; } + [JsonPropertyName("today_play_duration")] public Tuple TodayPlayDuration { get; set; } @@ -47,13 +53,15 @@ public class LocalSettings public LocalSettings() { - DataPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), + MainPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NonsPlayer"); - ConfigFilePath = Path.Join(DataPath, "config.json"); - AdapterPath = Path.Join(DataPath, "adapters"); - PluginPath = Path.Join(DataPath, "plugins"); - Data = Path.Join(DataPath, "Data"); - Log = Path.Combine(DataPath, "logs", $"NonsPlayer_{DateTime.Now:yyMMdd_HHmmss}.log"); + ConfigFilePath = Path.Join(MainPath, "config.json"); + AdapterPath = Path.Join(MainPath, "adapters"); + PluginPath = Path.Join(MainPath, "plugins"); + Data = Path.Join(MainPath, "data"); + CachePath = Path.Join(MainPath, "cache"); + LogPath = Path.Join(MainPath, "logs"); + Log = Path.Combine(LogPath, $"NonsPlayer_{DateTime.Now:yyMMdd_HHmmss}.log"); Theme = "Light"; Volume = 50; TotalPlayCount = 0; @@ -63,6 +71,7 @@ public LocalSettings() DisabledPlugins = string.Empty; TodayPlayDuration = new Tuple(DateTime.Now, TimeSpan.Zero); LocalArtistSep = [";", ","]; + SMTCEnable = true; } } } \ No newline at end of file diff --git a/src/NonsPlayer.Core/Services/ExpectionService.cs b/src/NonsPlayer.Core/Services/ExpectionService.cs index ae68939..e35d7bb 100644 --- a/src/NonsPlayer.Core/Services/ExpectionService.cs +++ b/src/NonsPlayer.Core/Services/ExpectionService.cs @@ -12,19 +12,19 @@ public class ExceptionService public void Throw(string content) { - Debug.WriteLine($"抛出了一个异常: {content}"); + Debug.WriteLine($"Threw an Exception: {content}"); ExceptionThrew?.Invoke(content); } public void Throw(Exception exception) { - Debug.WriteLine($"抛出了一个异常: {exception}"); + Debug.WriteLine($"Threw an Exception:: {exception}"); ExceptionThrew?.Invoke(exception.Message); } public void Throw(Exception exception, string content) { - Debug.WriteLine($"抛出了一个异常: {exception}"); + Debug.WriteLine($"Threw an Exception:: {exception}"); ExceptionThrew?.Invoke(content); } } \ No newline at end of file diff --git a/src/NonsPlayer.Core/Services/FileService.cs b/src/NonsPlayer.Core/Services/FileService.cs index 3623547..f2771ea 100644 --- a/src/NonsPlayer.Core/Services/FileService.cs +++ b/src/NonsPlayer.Core/Services/FileService.cs @@ -27,6 +27,7 @@ public string ReadData(string filename) File.Create(path).Dispose(); return string.Empty; } + using var reader = new StreamReader(path); var json = reader.ReadToEnd(); return json; @@ -48,12 +49,42 @@ public void Save(string folderPath, string fileName, T content) File.WriteAllText(Path.Combine(folderPath, fileName), fileContent, Encoding.UTF8); } - public void Delete(string folderPath, string fileName) + public void Delete(string filePath) { - if (fileName != null && File.Exists(Path.Combine(folderPath, fileName))) - File.Delete(Path.Combine(folderPath, fileName)); + if (filePath != null && File.Exists(filePath)) + { + File.Delete(filePath); + } } + public void DeleteFolder(string folderPath) + { + if (!Directory.Exists(folderPath)) + { + return; + } + + try + { + string[] files = Directory.GetFiles(folderPath, "*", SearchOption.TopDirectoryOnly); + foreach (string file in files) + { + Delete(file); + } + } + catch (UnauthorizedAccessException ex) + { + ExceptionService.Instance.Throw(ex); + } + catch (IOException ex) + { + // ignore + } + catch (Exception ex) + { + ExceptionService.Instance.Throw(ex); + } + } public void Move(string targetFile, string folderPath) { } @@ -66,4 +97,60 @@ public void WriteData(string filename, string content) writer.Write(content); } } -} \ No newline at end of file + + public static bool Exist(string path) + { + if (File.Exists(path)) + { + return true; + + } + else if (Directory.Exists(path)) + { + return true; + } + + return false; + } + public static long GetFileSize(string filePath) + { + if (File.Exists(filePath)) + { + FileInfo fileInfo = new FileInfo(filePath); + return fileInfo.Length; + } + + return 0; + + } + public static long GetDirectorySize(string folderPath) + { + if (!Directory.Exists(folderPath)) + { + return 0; + } + + long size = 0; + + foreach (string file in Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories)) + { + size += new FileInfo(file).Length; + } + + return size; + } + public static string FormatSize(long bytes) + { + string[] sizes = { "B", "KB", "MB", "GB", "TB" }; + int order = 0; + double size = bytes; + + while (size >= 1024 && order < sizes.Length - 1) + { + order++; + size /= 1024; + } + + return $"{size:0.##} {sizes[order]}"; + } +} diff --git a/src/NonsPlayer/AppConfig.cs b/src/NonsPlayer/AppConfig.cs index 3ce917b..26ae0d7 100644 --- a/src/NonsPlayer/AppConfig.cs +++ b/src/NonsPlayer/AppConfig.cs @@ -40,7 +40,7 @@ public AppConfig() public string? IgnoreVersion { get; set; } public string? AppVersion { get; set; } - public string ConfigPath = Path.Combine(ConfigManager.Instance.Settings.DataPath, "app_config.json"); + public string ConfigPath = Path.Combine(ConfigManager.Instance.Settings.MainPath, "app_config.json"); private void Initialize() { diff --git a/src/NonsPlayer/Helpers/DialogHelper.cs b/src/NonsPlayer/Helpers/DialogHelper.cs new file mode 100644 index 0000000..89341a7 --- /dev/null +++ b/src/NonsPlayer/Helpers/DialogHelper.cs @@ -0,0 +1,17 @@ +using Microsoft.UI.Xaml.Controls; +using static NonsPlayer.Core.Services.ExceptionService; + +namespace NonsPlayer.Helpers; + +public class DialogHelper +{ + public static DialogHelper Instance { get; } = new(); + public delegate void DialogShowHandle(string content); + public event DialogShowHandle DialogShowing; + + + public void Show(string content) + { + DialogShowing?.Invoke(content); + } +} \ No newline at end of file diff --git a/src/NonsPlayer/Helpers/RequestHelper.cs b/src/NonsPlayer/Helpers/RequestHelper.cs deleted file mode 100644 index 617a2d2..0000000 --- a/src/NonsPlayer/Helpers/RequestHelper.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Windows.Storage.Streams; -using Microsoft.UI.Xaml.Media; -using Microsoft.UI.Xaml.Media.Imaging; - -namespace NonsPlayer.Helpers; - -public static class RequestHelper -{ - public static ImageBrush GetImageBrushAsync(string url) - { - return new ImageBrush - { - ImageSource = new BitmapImage(new Uri(url)) - }; - } -} \ No newline at end of file diff --git a/src/NonsPlayer/Helpers/RuntimeHelper.cs b/src/NonsPlayer/Helpers/RuntimeHelper.cs deleted file mode 100644 index df9fc4d..0000000 --- a/src/NonsPlayer/Helpers/RuntimeHelper.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Runtime.InteropServices; -using System.Text; - -namespace NonsPlayer.Helpers; - -public class RuntimeHelper -{ - public static bool IsMSIX - { - get - { - var length = 0; - - return GetCurrentPackageFullName(ref length, null) != 15700L; - } - } - - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - private static extern int GetCurrentPackageFullName(ref int packageFullNameLength, StringBuilder? packageFullName); -} \ No newline at end of file diff --git a/src/NonsPlayer/Helpers/SettingsStorageExtensions.cs b/src/NonsPlayer/Helpers/SettingsStorageExtensions.cs deleted file mode 100644 index 0d1e01b..0000000 --- a/src/NonsPlayer/Helpers/SettingsStorageExtensions.cs +++ /dev/null @@ -1,101 +0,0 @@ -using Windows.Storage; -using Windows.Storage.Streams; -using NonsPlayer.Core.Helpers; - -namespace NonsPlayer.Helpers; - -// Use these extension methods to store and retrieve local and roaming app data -// More details regarding storing and retrieving app data at https://docs.microsoft.com/windows/apps/design/app-settings/store-and-retrieve-app-data -public static class SettingsStorageExtensions -{ - private const string FileExtension = ".json"; - - public static bool IsRoamingStorageAvailable(this ApplicationData appData) - { - return appData.RoamingStorageQuota == 0; - } - - public static async Task SaveAsync(this StorageFolder folder, string name, T content) - { - var file = await folder.CreateFileAsync(GetFileName(name), CreationCollisionOption.ReplaceExisting); - var fileContent = await JsonUtils.StringifyAsync(content); - - await FileIO.WriteTextAsync(file, fileContent); - } - - public static async Task ReadAsync(this StorageFolder folder, string name) - { - if (!File.Exists(Path.Combine(folder.Path, GetFileName(name)))) return default; - - var file = await folder.GetFileAsync($"{name}.json"); - var fileContent = await FileIO.ReadTextAsync(file); - - return await JsonUtils.ToObjectAsync(fileContent); - } - - public static async Task SaveAsync(this ApplicationDataContainer settings, string key, T value) - { - settings.SaveString(key, await JsonUtils.StringifyAsync(value)); - } - - public static void SaveString(this ApplicationDataContainer settings, string key, string value) - { - settings.Values[key] = value; - } - - public static async Task ReadAsync(this ApplicationDataContainer settings, string key) - { - object? obj; - - if (settings.Values.TryGetValue(key, out obj)) return await JsonUtils.ToObjectAsync((string)obj); - - return default; - } - - public static async Task SaveFileAsync(this StorageFolder folder, byte[] content, string fileName, - CreationCollisionOption options = CreationCollisionOption.ReplaceExisting) - { - if (content == null) throw new ArgumentNullException(nameof(content)); - - if (string.IsNullOrEmpty(fileName)) - throw new ArgumentException("File name is null or empty. Specify a valid file name", nameof(fileName)); - - var storageFile = await folder.CreateFileAsync(fileName, options); - await FileIO.WriteBytesAsync(storageFile, content); - return storageFile; - } - - public static async Task ReadFileAsync(this StorageFolder folder, string fileName) - { - var item = await folder.TryGetItemAsync(fileName).AsTask().ConfigureAwait(false); - - if (item != null && item.IsOfType(StorageItemTypes.File)) - { - var storageFile = await folder.GetFileAsync(fileName); - var content = await storageFile.ReadBytesAsync(); - return content; - } - - return null; - } - - public static async Task ReadBytesAsync(this StorageFile file) - { - if (file != null) - { - using IRandomAccessStream stream = await file.OpenReadAsync(); - using var reader = new DataReader(stream.GetInputStreamAt(0)); - await reader.LoadAsync((uint)stream.Size); - var bytes = new byte[stream.Size]; - reader.ReadBytes(bytes); - return bytes; - } - - return null; - } - - private static string GetFileName(string name) - { - return string.Concat(name, FileExtension); - } -} \ No newline at end of file diff --git a/src/NonsPlayer/Helpers/StartUpHelper.cs b/src/NonsPlayer/Helpers/StartUpHelper.cs deleted file mode 100644 index 7aa6a60..0000000 --- a/src/NonsPlayer/Helpers/StartUpHelper.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace NonsPlayer.Helpers; - -public class StartUpHelper -{ - public static StartUpHelper Instance { get; } = new(); - - public async Task Main() - { - await Task.WhenAll(); - } -} \ No newline at end of file diff --git a/src/NonsPlayer/Services/SMTCService.cs b/src/NonsPlayer/Services/SMTCService.cs index 2e25e2b..4dd0a93 100644 --- a/src/NonsPlayer/Services/SMTCService.cs +++ b/src/NonsPlayer/Services/SMTCService.cs @@ -6,6 +6,7 @@ using NAudio.Wave; using NonsPlayer.Core.Contracts.Models.Music; using NonsPlayer.Core.Nons.Player; +using NonsPlayer.Core.Services; using NonsPlayer.Helpers; using NonsPlayer.ViewModels; using Timer = System.Timers.Timer; @@ -22,7 +23,7 @@ public class SMTCService public SMTCService() { - if (AppConfig.Instance.AppSettings.SMTCEnable) + if (ConfigManager.Instance.Settings.SMTCEnable) { Player.Instance.MusicChanged += MusicChangedHandle; diff --git a/src/NonsPlayer/Strings/zh-cn/Resources.resw b/src/NonsPlayer/Strings/zh-cn/Resources.resw index d3b2918..586a9c7 100644 --- a/src/NonsPlayer/Strings/zh-cn/Resources.resw +++ b/src/NonsPlayer/Strings/zh-cn/Resources.resw @@ -560,4 +560,19 @@ 通用首选项 + + 本地缓存当前共占用了 {0} + + + 清理缓存 + + + 注意 + + + 出错了 + + + 已清理了 {0} 的缓存 + \ No newline at end of file diff --git a/src/NonsPlayer/Styles/CustomComponents.xaml b/src/NonsPlayer/Styles/CustomComponents.xaml index bf91998..832e1c5 100644 --- a/src/NonsPlayer/Styles/CustomComponents.xaml +++ b/src/NonsPlayer/Styles/CustomComponents.xaml @@ -15,6 +15,10 @@ +