refresh the ui after subscribing to the script

重构了多个类中的方法为异步方法,并增加了消息注册逻辑:
- `ScriptRepoUpdater.cs`:增加了检查并创建 `ReposPath` 目录的逻辑。
- `RepoWebBridge.cs`:引用新命名空间,重构 `GetRepoJson` 和 `ImportUri` 方法为异步方法,增加了检查和更新本地仓库的逻辑。
- `JsListViewModel.cs` 和 `MapPathingViewModel.cs`:引用新命名空间,增加消息注册逻辑,修正方法中的引用。
- `PathingTask.cs`:修正了日志记录中 `FileName` 的引用。
- `TaskSettingsPage.xaml`:更新了文本内容,增加了关于装备「王树瑞佑」的说明。
- 新增 `RefreshDataMessage.cs` 文件,定义了 `RefreshDataMessage` 类。
This commit is contained in:
辉鸭蛋 2024-10-21 01:32:36 +08:00
parent d23ba65d51
commit 7cc4093079
7 changed files with 69 additions and 22 deletions

View File

@ -54,6 +54,11 @@ public class ScriptRepoUpdater : Singleton<ScriptRepoUpdater>
{
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<ScriptRepoUpdater>
{
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<ScriptRepoUpdater>
{
throw new Exception("本地仓库文件夹不存在");
}
return folderPath;
}

View File

@ -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<string> 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, "订阅脚本链接失败!");
}
}
}

View File

@ -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;
}

View File

@ -177,7 +177,7 @@
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
TextWrapping="Wrap">
需要装备「王树瑞佑」 -<Hyperlink Command="{Binding GoToAutoWoodUrlCommand}" Foreground="{ui:ThemeResource TextFillColorSecondaryBrush}">
装备「王树瑞佑」,通过循环重启游戏刷新并收集木材 -<Hyperlink Command="{Binding GoToAutoWoodUrlCommand}" Foreground="{ui:ThemeResource TextFillColorSecondaryBrush}">
点击查看使用教程
</Hyperlink>
</ui:TextBlock>

View File

@ -0,0 +1,5 @@
using CommunityToolkit.Mvvm.Messaging.Messages;
namespace BetterGenshinImpact.ViewModel.Message;
public class RefreshDataMessage(string value) : ValueChangedMessage<string>(value);

View File

@ -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<ScriptProject> _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<RefreshDataMessage>(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)]);
}

View File

@ -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<MapPathingViewModel> _logger = App.GetLogger<MapPathingViewModel>();
public static readonly string PathJsonPath = Global.Absolute(@"User\AutoPathing");
@ -28,17 +30,26 @@ public partial class MapPathingViewModel(IScriptService scriptService, IConfigSe
private ObservableCollection<FileTreeNode<PathingTask>> _treeList = [];
private MapViewer? _mapViewer;
private readonly IScriptService _scriptService;
public AllConfig Config { get; set; } = configService.Get();
public AllConfig Config { get; set; }
/// <inheritdoc/>
public MapPathingViewModel(IScriptService scriptService, IConfigService configService)
{
_scriptService = scriptService;
Config = configService.Get();
WeakReferenceMessenger.Default.Register<RefreshDataMessage>(this, (r, m) => InitScriptListViewData());
}
private void InitScriptListViewData()
{
_treeList.Clear();
TreeList.Clear();
var root = FileTreeNodeHelper.LoadDirectory<PathingTask>(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]