mirror of
https://github.com/wwiinnddyy/LanMountainDesktop.git
synced 2026-06-21 16:14:28 +08:00
154 lines
4.9 KiB
C#
154 lines
4.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Controls.Primitives;
|
|
using Avalonia.Interactivity;
|
|
using LanMountainDesktop.Models;
|
|
using LanMountainDesktop.Services;
|
|
|
|
namespace LanMountainDesktop.Views.Components;
|
|
|
|
public partial class DailyWordSettingsWindow : UserControl
|
|
{
|
|
private static readonly IReadOnlyList<int> SupportedIntervals = RefreshIntervalCatalog.SupportedIntervalsMinutes;
|
|
|
|
private readonly AppSettingsService _appSettingsService = new();
|
|
private readonly ComponentSettingsService _componentSettingsService = new();
|
|
private readonly LocalizationService _localizationService = new();
|
|
private bool _suppressEvents;
|
|
private string _languageCode = "zh-CN";
|
|
|
|
public event EventHandler? SettingsChanged;
|
|
|
|
public DailyWordSettingsWindow()
|
|
{
|
|
InitializeComponent();
|
|
InitializeFrequencyOptions();
|
|
LoadState();
|
|
ApplyLocalization();
|
|
}
|
|
|
|
private void LoadState()
|
|
{
|
|
var appSnapshot = _appSettingsService.Load();
|
|
var componentSnapshot = _componentSettingsService.Load();
|
|
_languageCode = _localizationService.NormalizeLanguageCode(appSnapshot.LanguageCode);
|
|
|
|
var enabled = componentSnapshot.DailyWordAutoRefreshEnabled;
|
|
var interval = NormalizeInterval(componentSnapshot.DailyWordAutoRefreshIntervalMinutes);
|
|
|
|
_suppressEvents = true;
|
|
AutoRefreshCheckBox.IsChecked = enabled;
|
|
SelectInterval(interval);
|
|
FrequencyCardBorder.IsVisible = enabled;
|
|
_suppressEvents = false;
|
|
}
|
|
|
|
private void ApplyLocalization()
|
|
{
|
|
TitleTextBlock.Text = L("dailyword.settings.title", "Daily word settings");
|
|
DescriptionTextBlock.Text = L("dailyword.settings.desc", "Configure auto refresh and refresh interval.");
|
|
AutoRefreshLabelTextBlock.Text = L("dailyword.settings.auto_refresh_label", "Auto refresh");
|
|
AutoRefreshCheckBox.Content = L("dailyword.settings.auto_refresh_enabled", "Enable auto refresh");
|
|
FrequencyLabelTextBlock.Text = L("dailyword.settings.frequency_label", "Refresh interval");
|
|
ApplyFrequencyLocalization();
|
|
}
|
|
|
|
private void OnAutoRefreshChanged(object? sender, RoutedEventArgs e)
|
|
{
|
|
_ = sender;
|
|
_ = e;
|
|
if (_suppressEvents)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var enabled = AutoRefreshCheckBox.IsChecked == true;
|
|
FrequencyCardBorder.IsVisible = enabled;
|
|
SaveState();
|
|
}
|
|
|
|
private void OnFrequencySelectionChanged(object? sender, SelectionChangedEventArgs e)
|
|
{
|
|
_ = sender;
|
|
_ = e;
|
|
if (_suppressEvents)
|
|
{
|
|
return;
|
|
}
|
|
|
|
SaveState();
|
|
}
|
|
|
|
private void SaveState()
|
|
{
|
|
var snapshot = _componentSettingsService.Load();
|
|
snapshot.DailyWordAutoRefreshEnabled = AutoRefreshCheckBox.IsChecked == true;
|
|
snapshot.DailyWordAutoRefreshIntervalMinutes = GetSelectedInterval();
|
|
_componentSettingsService.Save(snapshot);
|
|
SettingsChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
private int GetSelectedInterval()
|
|
{
|
|
if (FrequencyComboBox.SelectedItem is ComboBoxItem item &&
|
|
item.Tag is string tagText &&
|
|
int.TryParse(tagText, out var minutes))
|
|
{
|
|
return NormalizeInterval(minutes);
|
|
}
|
|
|
|
return 360;
|
|
}
|
|
|
|
private void SelectInterval(int intervalMinutes)
|
|
{
|
|
var selected = FrequencyComboBox.Items
|
|
.OfType<ComboBoxItem>()
|
|
.FirstOrDefault(item =>
|
|
item.Tag is string tagText &&
|
|
int.TryParse(tagText, out var minutes) &&
|
|
minutes == intervalMinutes);
|
|
FrequencyComboBox.SelectedItem = selected ?? FrequencyComboBox.Items.OfType<ComboBoxItem>().FirstOrDefault();
|
|
}
|
|
|
|
private static int NormalizeInterval(int minutes)
|
|
{
|
|
return RefreshIntervalCatalog.Normalize(minutes, 360);
|
|
}
|
|
|
|
private void InitializeFrequencyOptions()
|
|
{
|
|
FrequencyComboBox.Items.Clear();
|
|
foreach (var minutes in SupportedIntervals)
|
|
{
|
|
FrequencyComboBox.Items.Add(new ComboBoxItem
|
|
{
|
|
Tag = minutes.ToString(),
|
|
Content = RefreshIntervalCatalog.ToEnglishFallbackLabel(minutes)
|
|
});
|
|
}
|
|
}
|
|
|
|
private void ApplyFrequencyLocalization()
|
|
{
|
|
foreach (var item in FrequencyComboBox.Items.OfType<ComboBoxItem>())
|
|
{
|
|
if (item.Tag is not string tagText ||
|
|
!int.TryParse(tagText, out var minutes))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var key = $"refresh.frequency.{RefreshIntervalCatalog.ToLocalizationKeySuffix(minutes)}";
|
|
item.Content = L(key, RefreshIntervalCatalog.ToEnglishFallbackLabel(minutes));
|
|
}
|
|
}
|
|
|
|
private string L(string key, string fallback)
|
|
{
|
|
return _localizationService.GetString(_languageCode, key, fallback);
|
|
}
|
|
}
|