From 7cc4093079e162abb53f5b50f38f88ac098ecc1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Mon, 21 Oct 2024 01:32:36 +0800 Subject: [PATCH] refresh the ui after subscribing to the script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构了多个类中的方法为异步方法,并增加了消息注册逻辑: - `ScriptRepoUpdater.cs`:增加了检查并创建 `ReposPath` 目录的逻辑。 - `RepoWebBridge.cs`:引用新命名空间,重构 `GetRepoJson` 和 `ImportUri` 方法为异步方法,增加了检查和更新本地仓库的逻辑。 - `JsListViewModel.cs` 和 `MapPathingViewModel.cs`:引用新命名空间,增加消息注册逻辑,修正方法中的引用。 - `PathingTask.cs`:修正了日志记录中 `FileName` 的引用。 - `TaskSettingsPage.xaml`:更新了文本内容,增加了关于装备「王树瑞佑」的说明。 - 新增 `RefreshDataMessage.cs` 文件,定义了 `RefreshDataMessage` 类。 --- .../Core/Script/ScriptRepoUpdater.cs | 7 ++++ .../Core/Script/WebView/RepoWebBridge.cs | 37 ++++++++++++++----- .../GameTask/AutoPathing/Model/PathingTask.cs | 2 +- .../View/Pages/TaskSettingsPage.xaml | 2 +- .../ViewModel/Message/RefreshDataMessage.cs | 5 +++ .../ViewModel/Pages/JsListViewModel.cs | 17 ++++++--- .../ViewModel/Pages/MapPathingViewModel.cs | 21 ++++++++--- 7 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 BetterGenshinImpact/ViewModel/Message/RefreshDataMessage.cs diff --git a/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs b/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs index 6a09ba8b..c81fd7cd 100644 --- a/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs +++ b/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs @@ -54,6 +54,11 @@ public class ScriptRepoUpdater : Singleton { var scriptConfig = TaskContext.Instance().Config.ScriptConfig; + if (!Directory.Exists(ReposPath)) + { + Directory.CreateDirectory(ReposPath); + } + // 判断更新周期是否到达 if (DateTime.Now - scriptConfig.LastUpdateScriptRepoTime >= TimeSpan.FromDays(scriptConfig.AutoUpdateScriptRepoPeriod)) { @@ -130,6 +135,7 @@ public class ScriptRepoUpdater : Singleton { throw new Exception("本地仓库缺少 repo.json"); } + // 获取与 localRepoJsonPath 同名(无扩展名)的文件夹路径 var folderName = Path.GetFileNameWithoutExtension(localRepoJsonPath); var folderPath = Path.Combine(Path.GetDirectoryName(localRepoJsonPath)!, folderName); @@ -137,6 +143,7 @@ public class ScriptRepoUpdater : Singleton { throw new Exception("本地仓库文件夹不存在"); } + return folderPath; } diff --git a/BetterGenshinImpact/Core/Script/WebView/RepoWebBridge.cs b/BetterGenshinImpact/Core/Script/WebView/RepoWebBridge.cs index 67dddd73..f4587484 100644 --- a/BetterGenshinImpact/Core/Script/WebView/RepoWebBridge.cs +++ b/BetterGenshinImpact/Core/Script/WebView/RepoWebBridge.cs @@ -2,6 +2,10 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; +using System.Security.Policy; +using System.Threading.Tasks; +using BetterGenshinImpact.ViewModel.Message; +using CommunityToolkit.Mvvm.Messaging; namespace BetterGenshinImpact.Core.Script.WebView; @@ -13,14 +17,28 @@ namespace BetterGenshinImpact.Core.Script.WebView; [ComVisible(true)] public class RepoWebBridge { - public string GetRepoJson() + public async Task GetRepoJson() { try { - var localRepoJsonPath = Directory.GetFiles(ScriptRepoUpdater.CenterRepoPath, "repo.json", SearchOption.AllDirectories).FirstOrDefault(); - if (localRepoJsonPath is null) + var needUpdate = false; + string? localRepoJsonPath = null; + if (Directory.Exists(ScriptRepoUpdater.CenterRepoPath)) { - _ = ScriptRepoUpdater.Instance.UpdateCenterRepo().ConfigureAwait(false); + localRepoJsonPath = Directory.GetFiles(ScriptRepoUpdater.CenterRepoPath, "repo.json", SearchOption.AllDirectories).FirstOrDefault(); + if (localRepoJsonPath is null) + { + needUpdate = true; + } + } + else + { + needUpdate = true; + } + + if (needUpdate) + { + await ScriptRepoUpdater.Instance.UpdateCenterRepo(); localRepoJsonPath = Directory.GetFiles(ScriptRepoUpdater.CenterRepoPath, "repo.json", SearchOption.AllDirectories).FirstOrDefault(); if (localRepoJsonPath is null) { @@ -28,25 +46,26 @@ public class RepoWebBridge } } - var json = File.ReadAllText(localRepoJsonPath); + var json = await File.ReadAllTextAsync(localRepoJsonPath); return json; } catch (Exception e) { - MessageBox.Show(e.Message, "获取仓库信息失败!"); + await MessageBox.ShowAsync(e.Message, "获取仓库信息失败!"); return ""; } } - public void ImportUri(string url) + public async void ImportUri(string url) { try { - ScriptRepoUpdater.Instance.ImportScriptFromUri(url, false).ConfigureAwait(false); + await ScriptRepoUpdater.Instance.ImportScriptFromUri(url, false); + WeakReferenceMessenger.Default.Send(new RefreshDataMessage("Refresh")); } catch (Exception e) { - MessageBox.Show(e.Message, "订阅脚本链接失败!"); + await MessageBox.ShowAsync(e.Message, "订阅脚本链接失败!"); } } } diff --git a/BetterGenshinImpact/GameTask/AutoPathing/Model/PathingTask.cs b/BetterGenshinImpact/GameTask/AutoPathing/Model/PathingTask.cs index 5261c925..2b918d31 100644 --- a/BetterGenshinImpact/GameTask/AutoPathing/Model/PathingTask.cs +++ b/BetterGenshinImpact/GameTask/AutoPathing/Model/PathingTask.cs @@ -39,7 +39,7 @@ public class PathingTask // 比较版本号大小 BgiVersion if (!string.IsNullOrWhiteSpace(task.Info.BgiVersion) && Global.IsNewVersion(task.Info.BgiVersion)) { - TaskControl.Logger.LogError("路径追踪任务 {Name} 版本号要求 {BgiVersion} 大于当前 BetterGI 版本号 {CurrentVersion} , 脚本可能无法正常工作,请更新 BetterGI 版本!", FileName, task.Info.BgiVersion, Global.Version); + TaskControl.Logger.LogError("路径追踪任务 {Name} 版本号要求 {BgiVersion} 大于当前 BetterGI 版本号 {CurrentVersion} , 脚本可能无法正常工作,请更新 BetterGI 版本!", task.FileName, task.Info.BgiVersion, Global.Version); } return task; } diff --git a/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml b/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml index ec109448..e041fa30 100644 --- a/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml +++ b/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml @@ -177,7 +177,7 @@ Grid.Column="0" Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}" TextWrapping="Wrap"> - 需要装备「王树瑞佑」 - + 装备「王树瑞佑」,通过循环重启游戏刷新并收集木材 - 点击查看使用教程 diff --git a/BetterGenshinImpact/ViewModel/Message/RefreshDataMessage.cs b/BetterGenshinImpact/ViewModel/Message/RefreshDataMessage.cs new file mode 100644 index 00000000..ec6cec28 --- /dev/null +++ b/BetterGenshinImpact/ViewModel/Message/RefreshDataMessage.cs @@ -0,0 +1,5 @@ +using CommunityToolkit.Mvvm.Messaging.Messages; + +namespace BetterGenshinImpact.ViewModel.Message; + +public class RefreshDataMessage(string value) : ValueChangedMessage(value); diff --git a/BetterGenshinImpact/ViewModel/Pages/JsListViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/JsListViewModel.cs index cd3c54ca..6e3d1981 100644 --- a/BetterGenshinImpact/ViewModel/Pages/JsListViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/JsListViewModel.cs @@ -12,6 +12,9 @@ using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; using System.Threading.Tasks; +using BetterGenshinImpact.Helpers; +using BetterGenshinImpact.ViewModel.Message; +using CommunityToolkit.Mvvm.Messaging; using Wpf.Ui; using Wpf.Ui.Controls; using Wpf.Ui.Violeta.Controls; @@ -26,27 +29,28 @@ public partial class JsListViewModel : ObservableObject, INavigationAware, IView [ObservableProperty] private ObservableCollection _scriptItems = []; - private ISnackbarService _snackbarService; - private IScriptService _scriptService; + private readonly IScriptService _scriptService; public AllConfig Config { get; set; } - public JsListViewModel(ISnackbarService snackbarService, IScriptService scriptService, IConfigService configService) + public JsListViewModel(IScriptService scriptService, IConfigService configService) { - _snackbarService = snackbarService; _scriptService = scriptService; Config = configService.Get(); + + // 注册消息 + WeakReferenceMessenger.Default.Register(this, (r, m) => InitScriptListViewData()); } private void InitScriptListViewData() { - _scriptItems.Clear(); + ScriptItems.Clear(); var directoryInfos = LoadScriptFolder(scriptPath); foreach (var f in directoryInfos) { try { - _scriptItems.Add(new ScriptProject(f.Name)); + ScriptItems.Add(new ScriptProject(f.Name)); } catch (Exception e) { @@ -95,6 +99,7 @@ public partial class JsListViewModel : ObservableObject, INavigationAware, IView { return; } + await _scriptService.RunMulti([new ScriptGroupProject(item)]); } diff --git a/BetterGenshinImpact/ViewModel/Pages/MapPathingViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/MapPathingViewModel.cs index c5254f70..152c93d4 100644 --- a/BetterGenshinImpact/ViewModel/Pages/MapPathingViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/MapPathingViewModel.cs @@ -14,12 +14,14 @@ using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; using BetterGenshinImpact.Core.Script; +using BetterGenshinImpact.ViewModel.Message; +using CommunityToolkit.Mvvm.Messaging; using Wpf.Ui.Controls; using Wpf.Ui.Violeta.Controls; namespace BetterGenshinImpact.ViewModel.Pages; -public partial class MapPathingViewModel(IScriptService scriptService, IConfigService configService) : ObservableObject, INavigationAware, IViewModel +public partial class MapPathingViewModel : ObservableObject, INavigationAware, IViewModel { private readonly ILogger _logger = App.GetLogger(); public static readonly string PathJsonPath = Global.Absolute(@"User\AutoPathing"); @@ -28,17 +30,26 @@ public partial class MapPathingViewModel(IScriptService scriptService, IConfigSe private ObservableCollection> _treeList = []; private MapViewer? _mapViewer; + private readonly IScriptService _scriptService; - public AllConfig Config { get; set; } = configService.Get(); + public AllConfig Config { get; set; } + + /// + public MapPathingViewModel(IScriptService scriptService, IConfigService configService) + { + _scriptService = scriptService; + Config = configService.Get(); + WeakReferenceMessenger.Default.Register(this, (r, m) => InitScriptListViewData()); + } private void InitScriptListViewData() { - _treeList.Clear(); + TreeList.Clear(); var root = FileTreeNodeHelper.LoadDirectory(PathJsonPath); // 循环写入 root.Children foreach (var item in root.Children) { - _treeList.Add(item); + TreeList.Add(item); } } @@ -93,7 +104,7 @@ public partial class MapPathingViewModel(IScriptService scriptService, IConfigSe var fileInfo = new FileInfo(item.FilePath); var project = ScriptGroupProject.BuildPathingProject(fileInfo.Name, fileInfo.DirectoryName!); - await scriptService.RunMulti([project]); + await _scriptService.RunMulti([project]); } [RelayCommand]