using System.Collections.Generic; using System.Linq; using DownKyi.Core.Settings; using DownKyi.Events; using DownKyi.Models; using DownKyi.Utils; using Prism.Commands; using Prism.Events; using Prism.Regions; namespace DownKyi.ViewModels.Settings; public class ViewBasicViewModel : ViewModelBase { public const string Tag = "PageSettingsBasic"; private bool _isOnNavigatedTo; #region 页面属性申明 private bool _themeLight; public bool ThemeLight { get => _themeLight; set => SetProperty(ref _themeLight, value); } private bool _themeDark; public bool ThemeDark { get => _themeDark; set => SetProperty(ref _themeDark, value); } private bool _themeAuto; public bool ThemeAuto { get => _themeAuto; set => SetProperty(ref _themeAuto, value); } private bool _none; public bool None { get => _none; set => SetProperty(ref _none, value); } private bool _closeApp; public bool CloseApp { get => _closeApp; set => SetProperty(ref _closeApp, value); } private bool _closeSystem; public bool CloseSystem { get => _closeSystem; set => SetProperty(ref _closeSystem, value); } private bool _listenClipboard; public bool ListenClipboard { get => _listenClipboard; set => SetProperty(ref _listenClipboard, value); } private bool _autoParseVideo; public bool AutoParseVideo { get => _autoParseVideo; set => SetProperty(ref _autoParseVideo, value); } private List _parseScopes; public List ParseScopes { get => _parseScopes; set => SetProperty(ref _parseScopes, value); } private ParseScopeDisplay _selectedParseScope; public ParseScopeDisplay SelectedParseScope { get => _selectedParseScope; set => SetProperty(ref _selectedParseScope, value); } private bool _autoDownloadAll; public bool AutoDownloadAll { get => _autoDownloadAll; set => SetProperty(ref _autoDownloadAll, value); } private bool _repeatFileAutoAddNumberSuffix; public bool RepeatFileAutoAddNumberSuffix { get => _repeatFileAutoAddNumberSuffix; set => SetProperty(ref _repeatFileAutoAddNumberSuffix, value); } private List _repeatDownloadStrategy; public List RepeatDownloadStrategy { get => _repeatDownloadStrategy; set => SetProperty(ref _repeatDownloadStrategy, value); } private RepeatDownloadStrategyDisplay _selectedRepeatDownloadStrategy; public RepeatDownloadStrategyDisplay SelectedRepeatDownloadStrategy { get => _selectedRepeatDownloadStrategy; set => SetProperty(ref _selectedRepeatDownloadStrategy, value); } #endregion public ViewBasicViewModel(IEventAggregator eventAggregator) : base(eventAggregator) { #region 属性初始化 // 解析范围 ParseScopes = new List { new() { Name = DictionaryResource.GetString("ParseNone"), ParseScope = ParseScope.NONE }, new() { Name = DictionaryResource.GetString("ParseSelectedItem"), ParseScope = ParseScope.SELECTED_ITEM }, new() { Name = DictionaryResource.GetString("ParseCurrentSection"), ParseScope = ParseScope.CURRENT_SECTION }, new() { Name = DictionaryResource.GetString("ParseAll"), ParseScope = ParseScope.ALL } }; RepeatDownloadStrategy = new List { new() { Name = DictionaryResource.GetString("RepeatDownloadAsk"), RepeatDownloadStrategy = Core.Settings.RepeatDownloadStrategy.Ask }, new() { Name = DictionaryResource.GetString("RepeatDownloadReDownload"), RepeatDownloadStrategy = Core.Settings.RepeatDownloadStrategy.ReDownload }, new() { Name = DictionaryResource.GetString("RepeatDownloadReJumpOver"), RepeatDownloadStrategy = Core.Settings.RepeatDownloadStrategy.JumpOver } }; #endregion } /// /// 导航到页面时执行 /// /// public override void OnNavigatedTo(NavigationContext navigationContext) { base.OnNavigatedTo(navigationContext); _isOnNavigatedTo = true; // 主题 var themeMode = SettingsManager.GetInstance().GetThemeMode(); switch (themeMode) { case ThemeMode.Light: ThemeLight = true; break; case ThemeMode.Dark: ThemeDark = true; break; case ThemeMode.Default: ThemeAuto = true; break; } // 下载完成后的操作 var afterDownload = SettingsManager.GetInstance().GetAfterDownloadOperation(); SetAfterDownloadOperation(afterDownload); // 是否监听剪贴板 var isListenClipboard = SettingsManager.GetInstance().IsListenClipboard(); ListenClipboard = isListenClipboard == AllowStatus.YES; // 是否自动解析视频 var isAutoParseVideo = SettingsManager.GetInstance().IsAutoParseVideo(); AutoParseVideo = isAutoParseVideo == AllowStatus.YES; // 解析范围 var parseScope = SettingsManager.GetInstance().GetParseScope(); SelectedParseScope = ParseScopes.FirstOrDefault(t => { return t.ParseScope == parseScope; }); // 解析后是否自动下载解析视频 var isAutoDownloadAll = SettingsManager.GetInstance().IsAutoDownloadAll(); AutoDownloadAll = isAutoDownloadAll == AllowStatus.YES; // 重复下载策略 var repeatDownloadStrategy = SettingsManager.GetInstance().GetRepeatDownloadStrategy(); SelectedRepeatDownloadStrategy = RepeatDownloadStrategy.FirstOrDefault(t => { return t.RepeatDownloadStrategy == repeatDownloadStrategy; }); // 重复下载文件自动添加数字后缀 var repeatFileAutoAddNumberSuffix = SettingsManager.GetInstance().IsRepeatFileAutoAddNumberSuffix(); RepeatFileAutoAddNumberSuffix = repeatFileAutoAddNumberSuffix; _isOnNavigatedTo = false; } #region 命令申明 // 主题事件 private DelegateCommand? _themeCommand; public DelegateCommand ThemeCommand => _themeCommand ??= new DelegateCommand(ExecuteThemeCommand); /// /// 主题事件 /// private void ExecuteThemeCommand(string parameter) { var themeMode = parameter switch { "Light" => ThemeMode.Light, "Dark" => ThemeMode.Dark, "Default" => ThemeMode.Default, _ => ThemeMode.Default }; var isSucceed = SettingsManager.GetInstance().SetThemeMode(themeMode); PublishTip(isSucceed); ThemeHelper.SetTheme(themeMode); } // 下载完成后的操作事件 private DelegateCommand? _afterDownloadOperationCommand; public DelegateCommand AfterDownloadOperationCommand => _afterDownloadOperationCommand ??= new DelegateCommand(ExecuteAfterDownloadOperationCommand); /// /// 下载完成后的操作事件 /// private void ExecuteAfterDownloadOperationCommand(string parameter) { AfterDownloadOperation afterDownload; switch (parameter) { case "None": afterDownload = AfterDownloadOperation.NONE; break; case "CloseApp": afterDownload = AfterDownloadOperation.CLOSE_APP; break; case "CloseSystem": afterDownload = AfterDownloadOperation.CLOSE_SYSTEM; break; default: afterDownload = AfterDownloadOperation.NONE; break; } var isSucceed = SettingsManager.GetInstance().SetAfterDownloadOperation(afterDownload); PublishTip(isSucceed); } // 是否监听剪贴板事件 private DelegateCommand? _listenClipboardCommand; public DelegateCommand ListenClipboardCommand => _listenClipboardCommand ??= new DelegateCommand(ExecuteListenClipboardCommand); /// /// 是否监听剪贴板事件 /// private void ExecuteListenClipboardCommand() { var isListenClipboard = ListenClipboard ? AllowStatus.YES : AllowStatus.NO; var isSucceed = SettingsManager.GetInstance().IsListenClipboard(isListenClipboard); PublishTip(isSucceed); } private DelegateCommand? _autoParseVideoCommand; public DelegateCommand AutoParseVideoCommand => _autoParseVideoCommand ??= new DelegateCommand(ExecuteAutoParseVideoCommand); /// /// 是否自动解析视频 /// private void ExecuteAutoParseVideoCommand() { var isAutoParseVideo = AutoParseVideo ? AllowStatus.YES : AllowStatus.NO; var isSucceed = SettingsManager.GetInstance().IsAutoParseVideo(isAutoParseVideo); PublishTip(isSucceed); } // 解析范围事件 private DelegateCommand? _parseScopesCommand; public DelegateCommand ParseScopesCommand => _parseScopesCommand ??= new DelegateCommand(ExecuteParseScopesCommand); /// /// 解析范围事件 /// /// private void ExecuteParseScopesCommand(object parameter) { if (parameter is not ParseScopeDisplay parseScope) { return; } var isSucceed = SettingsManager.GetInstance().SetParseScope(parseScope.ParseScope); PublishTip(isSucceed); } // 解析后是否自动下载解析视频 private DelegateCommand? _autoDownloadAllCommand; public DelegateCommand AutoDownloadAllCommand => _autoDownloadAllCommand ??= new DelegateCommand(ExecuteAutoDownloadAllCommand); /// /// 解析后是否自动下载解析视频 /// private void ExecuteAutoDownloadAllCommand() { var isAutoDownloadAll = AutoDownloadAll ? AllowStatus.YES : AllowStatus.NO; var isSucceed = SettingsManager.GetInstance().IsAutoDownloadAll(isAutoDownloadAll); PublishTip(isSucceed); } private DelegateCommand? _repeatFileAutoAddNumberSuffixCommand; public DelegateCommand RepeatFileAutoAddNumberSuffixCommand => _repeatFileAutoAddNumberSuffixCommand ??= new DelegateCommand(ExecuteRepeatFileAutoAddNumberSuffixCommand); private void ExecuteRepeatFileAutoAddNumberSuffixCommand() { var isSucceed = SettingsManager.GetInstance().IsRepeatFileAutoAddNumberSuffix(RepeatFileAutoAddNumberSuffix); PublishTip(isSucceed); } // 重复下载策略事件 private DelegateCommand? _repeatDownloadStrategyCommand; public DelegateCommand RepeatDownloadStrategyCommand => _repeatDownloadStrategyCommand ??= new DelegateCommand(ExecuteRepeatDownloadStrategyCommand); /// /// 重复下载策略事件 /// /// private void ExecuteRepeatDownloadStrategyCommand(object parameter) { if (parameter is not RepeatDownloadStrategyDisplay repeatDownloadStrategy) { return; } var isSucceed = SettingsManager.GetInstance().SetRepeatDownloadStrategy(repeatDownloadStrategy.RepeatDownloadStrategy); PublishTip(isSucceed); } #endregion /// /// 设置下载完成后的操作 /// /// private void SetAfterDownloadOperation(AfterDownloadOperation afterDownload) { switch (afterDownload) { case AfterDownloadOperation.NONE: None = true; break; case AfterDownloadOperation.OPEN_FOLDER: break; case AfterDownloadOperation.CLOSE_APP: CloseApp = true; break; case AfterDownloadOperation.CLOSE_SYSTEM: CloseSystem = true; break; } } /// /// 发送需要显示的tip /// /// private void PublishTip(bool isSucceed) { if (_isOnNavigatedTo) { return; } if (isSucceed) { EventAggregator.GetEvent().Publish(DictionaryResource.GetString("TipSettingUpdated")); } else { EventAggregator.GetEvent().Publish(DictionaryResource.GetString("TipSettingFailed")); } } }