From 8ccf5da63ed6287c8d98b4a92e76fd0734cfe64f Mon Sep 17 00:00:00 2001 From: status102 <102887808+status102@users.noreply.github.com> Date: Wed, 20 Nov 2024 09:24:19 +0800 Subject: [PATCH] =?UTF-8?q?rft:=20WpfGui=E9=87=8D=E6=9E=84=20=E6=8B=86?= =?UTF-8?q?=E5=88=86`=E8=AE=BE=E7=BD=AE-=E5=AE=9A=E6=97=B6=E6=89=A7?= =?UTF-8?q?=E8=A1=8C`=20(#11192)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rft: 拆分 设置-定时执行 --- .../ViewModels/UI/SettingsViewModel.cs | 162 +--------------- .../ViewModels/UI/TaskQueueViewModel.cs | 26 +-- .../Settings/TimerSettingsUserControlModel.cs | 179 ++++++++++++++++++ src/MaaWpfGui/Views/UI/SettingsView.xaml | 5 +- .../TimerSettingsUserControl.xaml | 45 +++-- .../TimerSettingsUserControl.xaml.cs | 2 +- 6 files changed, 224 insertions(+), 195 deletions(-) create mode 100644 src/MaaWpfGui/ViewModels/UserControl/Settings/TimerSettingsUserControlModel.cs rename src/MaaWpfGui/Views/UserControl/{ => Settings}/TimerSettingsUserControl.xaml (90%) rename src/MaaWpfGui/Views/UserControl/{ => Settings}/TimerSettingsUserControl.xaml.cs (95%) diff --git a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs index f1e154d316..27950b6a2f 100644 --- a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs @@ -103,6 +103,11 @@ namespace MaaWpfGui.ViewModels.UI /// public static GuiSettingsUserControlModel GuiSettings { get; } = new(); + /// + /// Gets 定时设置model + /// + public static TimerSettingsUserControlModel TimerSettings { get; } = new(); + /// /// Gets 软件更新model /// @@ -1963,163 +1968,6 @@ namespace MaaWpfGui.ViewModels.UI #endregion 领取奖励设置 - #region 定时设置 - - public class TimerModel - { - public class TimerProperties : PropertyChangedBase - { - public TimerProperties(int timeId, bool isOn, int hour, int min, string? timerConfig) - { - TimerId = timeId; - _isOn = isOn; - _hour = hour; - _min = min; - if (timerConfig == null || !ConfigurationHelper.GetConfigurationList().Contains(timerConfig)) - { - _timerConfig = ConfigurationHelper.GetCurrentConfiguration(); - } - else - { - _timerConfig = timerConfig; - } - } - - public int TimerId { get; set; } - - private readonly string _timerName = LocalizationHelper.GetString("Timer"); - - public string TimerName - { - get => $"{_timerName} {TimerId + 1}"; - } - - private bool _isOn; - - /// - /// Gets or sets a value indicating whether the timer is set. - /// - public bool IsOn - { - get => _isOn; - set - { - SetAndNotify(ref _isOn, value); - ConfigurationHelper.SetTimer(TimerId, value.ToString()); - } - } - - private int _hour; - - /// - /// Gets or sets the hour of the timer. - /// - public int Hour - { - get => _hour; - set - { - SetAndNotify(ref _hour, value); - ConfigurationHelper.SetTimerHour(TimerId, _hour.ToString()); - } - } - - private int _min; - - /// - /// Gets or sets the minute of the timer. - /// - public int Min - { - get => _min; - set - { - SetAndNotify(ref _min, value); - ConfigurationHelper.SetTimerMin(TimerId, _min.ToString()); - } - } - - private string? _timerConfig; - - /// - /// Gets or sets the config of the timer. - /// - public string? TimerConfig - { - get => _timerConfig; - set - { - SetAndNotify(ref _timerConfig, value ?? ConfigurationHelper.GetCurrentConfiguration()); - ConfigurationHelper.SetTimerConfig(TimerId, _timerConfig); - } - } - } - - public TimerProperties[] Timers { get; set; } = new TimerProperties[8]; - - public TimerModel() - { - for (int i = 0; i < 8; i++) - { - Timers[i] = new TimerProperties( - i, - ConfigurationHelper.GetTimer(i, bool.FalseString) == bool.TrueString, - int.Parse(ConfigurationHelper.GetTimerHour(i, $"{i * 3}")), - int.Parse(ConfigurationHelper.GetTimerMin(i, "0")), - ConfigurationHelper.GetTimerConfig(i, ConfigurationHelper.GetCurrentConfiguration())); - } - } - } - - public TimerModel TimerModels { get; set; } = new(); - - private bool _forceScheduledStart = Convert.ToBoolean(ConfigurationHelper.GetGlobalValue(ConfigurationKeys.ForceScheduledStart, bool.FalseString)); - - /// - /// Gets or sets a value indicating whether to force scheduled start. - /// - public bool ForceScheduledStart - { - get => _forceScheduledStart; - set - { - SetAndNotify(ref _forceScheduledStart, value); - ConfigurationHelper.SetGlobalValue(ConfigurationKeys.ForceScheduledStart, value.ToString()); - } - } - - private bool _showWindowBeforeForceScheduledStart = Convert.ToBoolean(ConfigurationHelper.GetGlobalValue(ConfigurationKeys.ShowWindowBeforeForceScheduledStart, bool.FalseString)); - - /// - /// Gets or sets a value indicating whether show window before force scheduled start. - /// - public bool ShowWindowBeforeForceScheduledStart - { - get => _showWindowBeforeForceScheduledStart; - set - { - SetAndNotify(ref _showWindowBeforeForceScheduledStart, value); - ConfigurationHelper.SetGlobalValue(ConfigurationKeys.ShowWindowBeforeForceScheduledStart, value.ToString()); - } - } - - private bool _customConfig = Convert.ToBoolean(ConfigurationHelper.GetGlobalValue(ConfigurationKeys.CustomConfig, bool.FalseString)); - - /// - /// Gets or sets a value indicating whether to use custom config. - /// - public bool CustomConfig - { - get => _customConfig; - set - { - SetAndNotify(ref _customConfig, value); - ConfigurationHelper.SetGlobalValue(ConfigurationKeys.CustomConfig, value.ToString()); - } - } - - #endregion 定时设置 - #region 刷理智设置 private string _penguinId = ConfigurationHelper.GetValue(ConfigurationKeys.PenguinId, string.Empty); diff --git a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs index afe373eb90..812005a255 100644 --- a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs @@ -433,7 +433,7 @@ namespace MaaWpfGui.ViewModels.UI for (int i = 0; i < 8; ++i) { - if (!Instances.SettingsViewModel.TimerModels.Timers[i].IsOn) + if (!SettingsViewModel.TimerSettings.TimerModels.Timers[i].IsOn) { continue; } @@ -441,8 +441,8 @@ namespace MaaWpfGui.ViewModels.UI DateTime startTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, - Instances.SettingsViewModel.TimerModels.Timers[i].Hour, - Instances.SettingsViewModel.TimerModels.Timers[i].Min, + SettingsViewModel.TimerSettings.TimerModels.Timers[i].Hour, + SettingsViewModel.TimerSettings.TimerModels.Timers[i].Min, 0); DateTime restartDateTime = startTime.AddMinutes(-2); @@ -453,7 +453,7 @@ namespace MaaWpfGui.ViewModels.UI } if (currentTime == restartDateTime && - Instances.SettingsViewModel.CurrentConfiguration != Instances.SettingsViewModel.TimerModels.Timers[i].TimerConfig) + Instances.SettingsViewModel.CurrentConfiguration != SettingsViewModel.TimerSettings.TimerModels.Timers[i].TimerConfig) { timeToChangeConfig = true; configIndex = i; @@ -474,7 +474,7 @@ namespace MaaWpfGui.ViewModels.UI private async Task HandleTimerLogic(DateTime currentTime) { - if (!_runningState.GetIdle() && !Instances.SettingsViewModel.ForceScheduledStart) + if (!_runningState.GetIdle() && !SettingsViewModel.TimerSettings.ForceScheduledStart) { return; } @@ -497,25 +497,25 @@ namespace MaaWpfGui.ViewModels.UI private void HandleConfigChange(int configIndex) { - if (Instances.SettingsViewModel.CustomConfig && - (_runningState.GetIdle() || Instances.SettingsViewModel.ForceScheduledStart)) + if (SettingsViewModel.TimerSettings.CustomConfig && + (_runningState.GetIdle() || SettingsViewModel.TimerSettings.ForceScheduledStart)) { - Instances.SettingsViewModel.CurrentConfiguration = Instances.SettingsViewModel.TimerModels.Timers[configIndex].TimerConfig; + Instances.SettingsViewModel.CurrentConfiguration = SettingsViewModel.TimerSettings.TimerModels.Timers[configIndex].TimerConfig; } } private async Task HandleScheduledStart(int configIndex) { - if (Instances.SettingsViewModel.ForceScheduledStart) + if (SettingsViewModel.TimerSettings.ForceScheduledStart) { - if (Instances.SettingsViewModel.CustomConfig && - Instances.SettingsViewModel.CurrentConfiguration != Instances.SettingsViewModel.TimerModels.Timers[configIndex].TimerConfig) + if (SettingsViewModel.TimerSettings.CustomConfig && + Instances.SettingsViewModel.CurrentConfiguration != SettingsViewModel.TimerSettings.TimerModels.Timers[configIndex].TimerConfig) { - _logger.Warning($"Scheduled start skipped: Custom configuration is enabled, but the current configuration does not match the scheduled timer configuration (Timer Index: {configIndex}). Current Configuration: {Instances.SettingsViewModel.CurrentConfiguration}, Scheduled Configuration: {Instances.SettingsViewModel.TimerModels.Timers[configIndex].TimerConfig}"); + _logger.Warning($"Scheduled start skipped: Custom configuration is enabled, but the current configuration does not match the scheduled timer configuration (Timer Index: {configIndex}). Current Configuration: {Instances.SettingsViewModel.CurrentConfiguration}, Scheduled Configuration: {SettingsViewModel.TimerSettings.TimerModels.Timers[configIndex].TimerConfig}"); return; } - if (Instances.SettingsViewModel.ShowWindowBeforeForceScheduledStart) + if (SettingsViewModel.TimerSettings.ShowWindowBeforeForceScheduledStart) { await Execute.OnUIThreadAsync(() => Instances.MainWindowManager?.Show()); } diff --git a/src/MaaWpfGui/ViewModels/UserControl/Settings/TimerSettingsUserControlModel.cs b/src/MaaWpfGui/ViewModels/UserControl/Settings/TimerSettingsUserControlModel.cs new file mode 100644 index 0000000000..2d5b63f7cd --- /dev/null +++ b/src/MaaWpfGui/ViewModels/UserControl/Settings/TimerSettingsUserControlModel.cs @@ -0,0 +1,179 @@ +// +// MaaWpfGui - A part of the MaaCoreArknights project +// Copyright (C) 2021 MistEO and Contributors +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License v3.0 only as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY +// + +#nullable enable +using System; +using MaaWpfGui.Constants; +using MaaWpfGui.Helper; +using Stylet; + +namespace MaaWpfGui.ViewModels.UserControl.Settings; + +/// +/// 定时设置 +/// +public class TimerSettingsUserControlModel : PropertyChangedBase +{ + private bool _forceScheduledStart = Convert.ToBoolean(ConfigurationHelper.GetGlobalValue(ConfigurationKeys.ForceScheduledStart, bool.FalseString)); + + /// + /// Gets or sets a value indicating whether to force scheduled start. + /// + public bool ForceScheduledStart + { + get => _forceScheduledStart; + set + { + SetAndNotify(ref _forceScheduledStart, value); + ConfigurationHelper.SetGlobalValue(ConfigurationKeys.ForceScheduledStart, value.ToString()); + } + } + + private bool _showWindowBeforeForceScheduledStart = Convert.ToBoolean(ConfigurationHelper.GetGlobalValue(ConfigurationKeys.ShowWindowBeforeForceScheduledStart, bool.FalseString)); + + /// + /// Gets or sets a value indicating whether show window before force scheduled start. + /// + public bool ShowWindowBeforeForceScheduledStart + { + get => _showWindowBeforeForceScheduledStart; + set + { + SetAndNotify(ref _showWindowBeforeForceScheduledStart, value); + ConfigurationHelper.SetGlobalValue(ConfigurationKeys.ShowWindowBeforeForceScheduledStart, value.ToString()); + } + } + + private bool _customConfig = Convert.ToBoolean(ConfigurationHelper.GetGlobalValue(ConfigurationKeys.CustomConfig, bool.FalseString)); + + /// + /// Gets or sets a value indicating whether to use custom config. + /// + public bool CustomConfig + { + get => _customConfig; + set + { + SetAndNotify(ref _customConfig, value); + ConfigurationHelper.SetGlobalValue(ConfigurationKeys.CustomConfig, value.ToString()); + } + } + + public TimerModel TimerModels { get; set; } = new(); + + public class TimerModel + { + public class TimerProperties : PropertyChangedBase + { + public TimerProperties(int timeId, bool isOn, int hour, int min, string? timerConfig) + { + TimerId = timeId; + _isOn = isOn; + _hour = hour; + _min = min; + if (timerConfig == null || !ConfigurationHelper.GetConfigurationList().Contains(timerConfig)) + { + _timerConfig = ConfigurationHelper.GetCurrentConfiguration(); + } + else + { + _timerConfig = timerConfig; + } + } + + public int TimerId { get; set; } + + private readonly string _timerName = LocalizationHelper.GetString("Timer"); + + public string TimerName + { + get => $"{_timerName} {TimerId + 1}"; + } + + private bool _isOn; + + /// + /// Gets or sets a value indicating whether the timer is set. + /// + public bool IsOn + { + get => _isOn; + set + { + SetAndNotify(ref _isOn, value); + ConfigurationHelper.SetTimer(TimerId, value.ToString()); + } + } + + private int _hour; + + /// + /// Gets or sets the hour of the timer. + /// + public int Hour + { + get => _hour; + set + { + SetAndNotify(ref _hour, value); + ConfigurationHelper.SetTimerHour(TimerId, _hour.ToString()); + } + } + + private int _min; + + /// + /// Gets or sets the minute of the timer. + /// + public int Min + { + get => _min; + set + { + SetAndNotify(ref _min, value); + ConfigurationHelper.SetTimerMin(TimerId, _min.ToString()); + } + } + + private string? _timerConfig; + + /// + /// Gets or sets the config of the timer. + /// + public string? TimerConfig + { + get => _timerConfig; + set + { + SetAndNotify(ref _timerConfig, value ?? ConfigurationHelper.GetCurrentConfiguration()); + ConfigurationHelper.SetTimerConfig(TimerId, _timerConfig); + } + } + } + + public TimerProperties[] Timers { get; set; } = new TimerProperties[8]; + + public TimerModel() + { + for (int i = 0; i < 8; i++) + { + Timers[i] = new TimerProperties( + i, + ConfigurationHelper.GetTimer(i, bool.FalseString) == bool.TrueString, + int.Parse(ConfigurationHelper.GetTimerHour(i, $"{i * 3}")), + int.Parse(ConfigurationHelper.GetTimerMin(i, "0")), + ConfigurationHelper.GetTimerConfig(i, ConfigurationHelper.GetCurrentConfiguration())); + } + } + } +} diff --git a/src/MaaWpfGui/Views/UI/SettingsView.xaml b/src/MaaWpfGui/Views/UI/SettingsView.xaml index 745347b1d4..f6f8bb0b9c 100644 --- a/src/MaaWpfGui/Views/UI/SettingsView.xaml +++ b/src/MaaWpfGui/Views/UI/SettingsView.xaml @@ -49,7 +49,10 @@ - + @@ -116,8 +115,8 @@ HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False" - IsEnabled="{Binding Idle}" - ItemsSource="{Binding ConfigurationList}" + IsEnabled="{Binding Idle, Source={x:Static helper:Instances.SettingsViewModel}}" + ItemsSource="{Binding ConfigurationList, Source={x:Static helper:Instances.SettingsViewModel}}" SelectedValue="{Binding TimerModels.Timers[0].TimerConfig}" SelectedValuePath="Value" ToolTip="{Binding TimerModels.Timers[0].TimerConfig}" @@ -162,8 +161,8 @@ HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False" - IsEnabled="{Binding Idle}" - ItemsSource="{Binding ConfigurationList}" + IsEnabled="{Binding Idle, Source={x:Static helper:Instances.SettingsViewModel}}" + ItemsSource="{Binding ConfigurationList, Source={x:Static helper:Instances.SettingsViewModel}}" SelectedValue="{Binding TimerModels.Timers[1].TimerConfig}" SelectedValuePath="Value" ToolTip="{Binding TimerModels.Timers[1].TimerConfig}" @@ -208,8 +207,8 @@ HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False" - IsEnabled="{Binding Idle}" - ItemsSource="{Binding ConfigurationList}" + IsEnabled="{Binding Idle, Source={x:Static helper:Instances.SettingsViewModel}}" + ItemsSource="{Binding ConfigurationList, Source={x:Static helper:Instances.SettingsViewModel}}" SelectedValue="{Binding TimerModels.Timers[2].TimerConfig}" SelectedValuePath="Value" ToolTip="{Binding TimerModels.Timers[2].TimerConfig}" @@ -254,8 +253,8 @@ HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False" - IsEnabled="{Binding Idle}" - ItemsSource="{Binding ConfigurationList}" + IsEnabled="{Binding Idle, Source={x:Static helper:Instances.SettingsViewModel}}" + ItemsSource="{Binding ConfigurationList, Source={x:Static helper:Instances.SettingsViewModel}}" SelectedValue="{Binding TimerModels.Timers[3].TimerConfig}" SelectedValuePath="Value" ToolTip="{Binding TimerModels.Timers[3].TimerConfig}" @@ -300,8 +299,8 @@ HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False" - IsEnabled="{Binding Idle}" - ItemsSource="{Binding ConfigurationList}" + IsEnabled="{Binding Idle, Source={x:Static helper:Instances.SettingsViewModel}}" + ItemsSource="{Binding ConfigurationList, Source={x:Static helper:Instances.SettingsViewModel}}" SelectedValue="{Binding TimerModels.Timers[4].TimerConfig}" SelectedValuePath="Value" ToolTip="{Binding TimerModels.Timers[4].TimerConfig}" @@ -346,8 +345,8 @@ HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False" - IsEnabled="{Binding Idle}" - ItemsSource="{Binding ConfigurationList}" + IsEnabled="{Binding Idle, Source={x:Static helper:Instances.SettingsViewModel}}" + ItemsSource="{Binding ConfigurationList, Source={x:Static helper:Instances.SettingsViewModel}}" SelectedValue="{Binding TimerModels.Timers[5].TimerConfig}" SelectedValuePath="Value" ToolTip="{Binding TimerModels.Timers[5].TimerConfig}" @@ -392,8 +391,8 @@ HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False" - IsEnabled="{Binding Idle}" - ItemsSource="{Binding ConfigurationList}" + IsEnabled="{Binding Idle, Source={x:Static helper:Instances.SettingsViewModel}}" + ItemsSource="{Binding ConfigurationList, Source={x:Static helper:Instances.SettingsViewModel}}" SelectedValue="{Binding TimerModels.Timers[6].TimerConfig}" SelectedValuePath="Value" ToolTip="{Binding TimerModels.Timers[6].TimerConfig}" @@ -438,8 +437,8 @@ HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False" - IsEnabled="{Binding Idle}" - ItemsSource="{Binding ConfigurationList}" + IsEnabled="{Binding Idle, Source={x:Static helper:Instances.SettingsViewModel}}" + ItemsSource="{Binding ConfigurationList, Source={x:Static helper:Instances.SettingsViewModel}}" SelectedValue="{Binding TimerModels.Timers[7].TimerConfig}" SelectedValuePath="Value" ToolTip="{Binding TimerModels.Timers[7].TimerConfig}" diff --git a/src/MaaWpfGui/Views/UserControl/TimerSettingsUserControl.xaml.cs b/src/MaaWpfGui/Views/UserControl/Settings/TimerSettingsUserControl.xaml.cs similarity index 95% rename from src/MaaWpfGui/Views/UserControl/TimerSettingsUserControl.xaml.cs rename to src/MaaWpfGui/Views/UserControl/Settings/TimerSettingsUserControl.xaml.cs index 052a2fd937..0de36af742 100644 --- a/src/MaaWpfGui/Views/UserControl/TimerSettingsUserControl.xaml.cs +++ b/src/MaaWpfGui/Views/UserControl/Settings/TimerSettingsUserControl.xaml.cs @@ -14,7 +14,7 @@ using System.Text.RegularExpressions; using System.Windows.Input; -namespace MaaWpfGui.Views.UserControl +namespace MaaWpfGui.Views.UserControl.Settings { /// /// TimerSettingsUserControl.xaml 的交互逻辑