mirror of
https://github.com/babalae/better-genshin-impact.git
synced 2026-04-01 10:39:50 +08:00
131 lines
3.5 KiB
C#
131 lines
3.5 KiB
C#
using BetterGenshinImpact.Core.Config;
|
|
using BetterGenshinImpact.Core.Script;
|
|
using BetterGenshinImpact.Core.Script.Group;
|
|
using BetterGenshinImpact.Core.Script.Project;
|
|
using BetterGenshinImpact.Service.Interface;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
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;
|
|
|
|
namespace BetterGenshinImpact.ViewModel.Pages;
|
|
|
|
public partial class JsListViewModel : ObservableObject, INavigationAware, IViewModel
|
|
{
|
|
private readonly ILogger<JsListViewModel> _logger = App.GetLogger<JsListViewModel>();
|
|
private readonly string scriptPath = Global.ScriptPath();
|
|
|
|
[ObservableProperty]
|
|
private ObservableCollection<ScriptProject> _scriptItems = [];
|
|
|
|
private readonly IScriptService _scriptService;
|
|
|
|
public AllConfig Config { get; set; }
|
|
|
|
public JsListViewModel(IScriptService scriptService, IConfigService configService)
|
|
{
|
|
_scriptService = scriptService;
|
|
Config = configService.Get();
|
|
|
|
// 注册消息
|
|
WeakReferenceMessenger.Default.Register<RefreshDataMessage>(this, (r, m) => InitScriptListViewData());
|
|
}
|
|
|
|
private void InitScriptListViewData()
|
|
{
|
|
ScriptItems.Clear();
|
|
var directoryInfos = LoadScriptFolder(scriptPath);
|
|
foreach (var f in directoryInfos)
|
|
{
|
|
try
|
|
{
|
|
ScriptItems.Add(new ScriptProject(f.Name));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Toast.Warning($"脚本 {f.Name} 载入失败:{e.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private IEnumerable<DirectoryInfo> LoadScriptFolder(string folder)
|
|
{
|
|
if (!Directory.Exists(folder))
|
|
{
|
|
Directory.CreateDirectory(folder);
|
|
}
|
|
|
|
var di = new DirectoryInfo(folder);
|
|
|
|
return di.GetDirectories();
|
|
}
|
|
|
|
public void OnNavigatedTo()
|
|
{
|
|
InitScriptListViewData();
|
|
}
|
|
|
|
public void OnNavigatedFrom()
|
|
{
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void OnOpenScriptsFolder()
|
|
{
|
|
Process.Start("explorer.exe", scriptPath);
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void OnOpenScriptProjectFolder(ScriptProject? item)
|
|
{
|
|
Process.Start("explorer.exe", item == null ? scriptPath : item.ProjectPath);
|
|
}
|
|
|
|
[RelayCommand]
|
|
public async Task OnStartRun(ScriptProject? item)
|
|
{
|
|
if (item == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(item.Manifest.SettingsUi))
|
|
{
|
|
Toast.Warning("此脚本存在配置,请添加至【调度器】,并右键修改配置后使用!");
|
|
return;
|
|
}
|
|
|
|
await _scriptService.RunMulti([new ScriptGroupProject(item)]);
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void OnRefresh(ScriptProject? item)
|
|
{
|
|
InitScriptListViewData();
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void OnGoToJsScriptUrl()
|
|
{
|
|
Process.Start(new ProcessStartInfo("https://bgi.huiyadan.com/feats/autos/jsscript.html") { UseShellExecute = true });
|
|
}
|
|
|
|
[RelayCommand]
|
|
public void OnOpenLocalScriptRepo()
|
|
{
|
|
Config.ScriptConfig.ScriptRepoHintDotVisible = false;
|
|
ScriptRepoUpdater.Instance.OpenLocalRepoInWebView();
|
|
}
|
|
}
|