XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

ChaoXingAutoClass

【WinUI】基于 XFEstudio 的 XFEWinUIApplication 模板重建的 WinUI 3 课程工作台。二维码和账号密码登录、课程 GridView、章节、播放进度、学习笔记及日志均使用原生 WinUI 控件。网页播放器运行在独立后台进程中,界面不显示课程网站。

公开
关注 0 Fork 0 Star 0
UTF-8
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<ILearningService>()!;

        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<ISettingService>();
        public IDialogService DialogService { get; set; } = ServiceManager.GetService<IDialogService>();

        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();
            }
        }
    }
}