rft: WpfGui重构 拆分设置-定时执行 (#11192)

rft: 拆分 设置-定时执行
This commit is contained in:
status102
2024-11-20 09:24:19 +08:00
committed by GitHub
parent 5445ca5c53
commit 8ccf5da63e
6 changed files with 224 additions and 195 deletions

View File

@@ -103,6 +103,11 @@ namespace MaaWpfGui.ViewModels.UI
/// </summary>
public static GuiSettingsUserControlModel GuiSettings { get; } = new();
/// <summary>
/// Gets 定时设置model
/// </summary>
public static TimerSettingsUserControlModel TimerSettings { get; } = new();
/// <summary>
/// Gets 软件更新model
/// </summary>
@@ -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;
/// <summary>
/// Gets or sets a value indicating whether the timer is set.
/// </summary>
public bool IsOn
{
get => _isOn;
set
{
SetAndNotify(ref _isOn, value);
ConfigurationHelper.SetTimer(TimerId, value.ToString());
}
}
private int _hour;
/// <summary>
/// Gets or sets the hour of the timer.
/// </summary>
public int Hour
{
get => _hour;
set
{
SetAndNotify(ref _hour, value);
ConfigurationHelper.SetTimerHour(TimerId, _hour.ToString());
}
}
private int _min;
/// <summary>
/// Gets or sets the minute of the timer.
/// </summary>
public int Min
{
get => _min;
set
{
SetAndNotify(ref _min, value);
ConfigurationHelper.SetTimerMin(TimerId, _min.ToString());
}
}
private string? _timerConfig;
/// <summary>
/// Gets or sets the config of the timer.
/// </summary>
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));
/// <summary>
/// Gets or sets a value indicating whether to force scheduled start.
/// </summary>
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));
/// <summary>
/// Gets or sets a value indicating whether show window before force scheduled start.
/// </summary>
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));
/// <summary>
/// Gets or sets a value indicating whether to use custom config.
/// </summary>
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);

View File

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

View File

@@ -0,0 +1,179 @@
// <copyright file="TimerSettingsUserControlModel.cs" company="MaaAssistantArknights">
// 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
// </copyright>
#nullable enable
using System;
using MaaWpfGui.Constants;
using MaaWpfGui.Helper;
using Stylet;
namespace MaaWpfGui.ViewModels.UserControl.Settings;
/// <summary>
/// 定时设置
/// </summary>
public class TimerSettingsUserControlModel : PropertyChangedBase
{
private bool _forceScheduledStart = Convert.ToBoolean(ConfigurationHelper.GetGlobalValue(ConfigurationKeys.ForceScheduledStart, bool.FalseString));
/// <summary>
/// Gets or sets a value indicating whether to force scheduled start.
/// </summary>
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));
/// <summary>
/// Gets or sets a value indicating whether show window before force scheduled start.
/// </summary>
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));
/// <summary>
/// Gets or sets a value indicating whether to use custom config.
/// </summary>
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;
/// <summary>
/// Gets or sets a value indicating whether the timer is set.
/// </summary>
public bool IsOn
{
get => _isOn;
set
{
SetAndNotify(ref _isOn, value);
ConfigurationHelper.SetTimer(TimerId, value.ToString());
}
}
private int _hour;
/// <summary>
/// Gets or sets the hour of the timer.
/// </summary>
public int Hour
{
get => _hour;
set
{
SetAndNotify(ref _hour, value);
ConfigurationHelper.SetTimerHour(TimerId, _hour.ToString());
}
}
private int _min;
/// <summary>
/// Gets or sets the minute of the timer.
/// </summary>
public int Min
{
get => _min;
set
{
SetAndNotify(ref _min, value);
ConfigurationHelper.SetTimerMin(TimerId, _min.ToString());
}
}
private string? _timerConfig;
/// <summary>
/// Gets or sets the config of the timer.
/// </summary>
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()));
}
}
}
}

View File

@@ -49,7 +49,10 @@
<userControl:ConfigurationMgrUserControl Margin="0,20" HorizontalAlignment="Center" />
<hc:Divider Content="{Binding ListTitle[1]}" />
<userControl:TimerSettingsUserControl Margin="0,20" HorizontalAlignment="Center" />
<settingsViews:TimerSettingsUserControl
Margin="0,20"
HorizontalAlignment="Center"
DataContext="{Binding TimerSettings}" />
<hc:Divider Content="{Binding ListTitle[2]}" />
<userControl:PerformanceUserControl

View File

@@ -1,5 +1,5 @@
<UserControl
x:Class="MaaWpfGui.Views.UserControl.TimerSettingsUserControl"
x:Class="MaaWpfGui.Views.UserControl.Settings.TimerSettingsUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:CalcBinding;assembly=CalcBinding"
@@ -7,15 +7,14 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dd="urn:gong-wpf-dragdrop"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:helper="clr-namespace:MaaWpfGui.Helper"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:s="https://github.com/canton7/Stylet"
xmlns:styles="clr-namespace:MaaWpfGui.Styles"
xmlns:ui="clr-namespace:MaaWpfGui.ViewModels.UI"
xmlns:viewModels="clr-namespace:MaaWpfGui.ViewModels"
xmlns:vm="clr-namespace:MaaWpfGui"
d:DataContext="{d:DesignInstance {x:Type ui:SettingsViewModel}}"
xmlns:viewModels="clr-namespace:MaaWpfGui.ViewModels.UserControl.Settings"
d:Background="White"
d:DataContext="{d:DesignInstance {x:Type viewModels:TimerSettingsUserControlModel}}"
d:DesignHeight="450"
d:DesignWidth="800"
s:View.ActionTarget="{Binding}"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
@@ -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}"

View File

@@ -14,7 +14,7 @@
using System.Text.RegularExpressions;
using System.Windows.Input;
namespace MaaWpfGui.Views.UserControl
namespace MaaWpfGui.Views.UserControl.Settings
{
/// <summary>
/// TimerSettingsUserControl.xaml 的交互逻辑