using ChaoXingAutoClass.Interface.Services; using XFEExtension.NetCore.WinUIHelper.Utilities; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Microsoft.Win32; using System.Reflection; using XFEExtension.NetCore.FileExtension; using XFEExtension.NetCore.WinUIHelper.Interface.Services; using XFEExtension.NetCore.WinUIHelper.Utilities.Helper; using ChaoXingAutoClass.Core.Utilities.Helpers; using ChaoXingAutoClass.Profiles.CrossVersionProfiles; namespace ChaoXingAutoClass.ViewModels { public partial class SettingPageViewModel : ViewModelBase { public ILearningService LearningService { get; } = ServiceManager.GetGlobalService()!; public AppSettings Settings => LearningService.Settings; [ObservableProperty] public partial string AgentStatus { get; set; } = ""; [RelayCommand] private void SaveAgent() { try { LearningService.SaveSettings(); AgentStatus = "设置已保存"; } catch (Exception error) { AgentStatus = error.Message; } } [RelayCommand] private async Task TestAgentAsync() { try { AgentStatus = "正在连接…"; AgentStatus = await LearningService.TestAgentAsync(CancellationToken.None); } catch (Exception error) { AgentStatus = error.Message; } } [ObservableProperty] public partial bool IsAutoStartEnable { get; set; } = SystemProfile.AutoStart; [ObservableProperty] public partial string AppCacheDirectory { get; set; } = AppPathHelper.AppCache; [ObservableProperty] public partial string AppCacheSize { get; set; } = FileHelper.GetDirectorySize(new(AppPathHelper.AppCache)).FileSize(); [ObservableProperty] public partial string AppDataDirectory { get; set; } = AppPathHelper.AppLocalData; [ObservableProperty] public partial string AppDataSize { get; set; } = FileHelper.GetDirectorySize(new(AppPathHelper.AppLocalData)).FileSize(); public ISettingService SettingService { get; set; } = ServiceManager.GetService(); public IDialogService DialogService { get; set; } = ServiceManager.GetService(); partial void OnIsAutoStartEnableChanged(bool value) { SystemProfile.AutoStart = value; SetAutoStart(value); } private static void SetAutoStart(bool enable) => SetAutoStart(enable, Assembly.GetExecutingAssembly().GetName().Name ?? "ChaoXingAutoClass"); private static void SetAutoStart(bool enable, string appName, string exePath = "") { const string runKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; using var key = Registry.CurrentUser.OpenSubKey(runKey, true); if (enable) { if (exePath == string.Empty) exePath = Environment.ProcessPath ?? string.Empty; key?.SetValue(appName, $"\"{exePath}\""); } else { if (key?.GetValue(appName) != null) { key.DeleteValue(appName); } } } [RelayCommand] private static void OpenPath(string originalPath) => Helper.OpenPath(originalPath); [RelayCommand] private async Task ClearCache() { if (await DialogService.ShowDialog("cleanCacheContentDialog") == ContentDialogResult.Primary) { Directory.Delete(AppPathHelper.AppCache, true); AppCacheSize = FileHelper.GetDirectorySize(new(AppPathHelper.AppCache)).FileSize(); } } } }