Files
marechai/Marechai.App/Presentation/ViewModels/SettingsViewModel.cs

185 lines
5.1 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Linq;
2025-11-16 18:59:23 +00:00
using System.Threading.Tasks;
using Marechai.App.Services;
2025-11-16 18:59:23 +00:00
using Uno.Extensions.Toolkit;
namespace Marechai.App.Presentation.ViewModels;
public partial class SettingsViewModel : ObservableObject
{
private readonly IColorThemeService _colorThemeService;
2025-11-16 18:59:23 +00:00
private readonly IStringLocalizer _localizer;
private readonly IThemeService _themeService;
[ObservableProperty]
2025-11-16 18:59:23 +00:00
private List<ColorThemeOption> _availableColorThemes = new();
[ObservableProperty]
2025-11-16 18:59:23 +00:00
private List<ThemeOption> _availableThemes = new();
[ObservableProperty]
2025-11-16 18:59:23 +00:00
private ColorThemeOption _selectedColorTheme;
[ObservableProperty]
2025-11-16 18:59:23 +00:00
private ThemeOption _selectedTheme;
2025-11-16 18:59:23 +00:00
public SettingsViewModel(IStringLocalizer localizer, IThemeService themeService,
IColorThemeService colorThemeService)
{
2025-11-16 18:59:23 +00:00
_localizer = localizer;
_themeService = themeService;
_colorThemeService = colorThemeService;
2025-11-16 18:59:23 +00:00
Title = _localizer["Settings"];
// Initialize immediately to ensure UI is populated
InitializeOptions();
2025-11-16 18:59:23 +00:00
// Wait for theme service to initialize
_ = InitializeThemeServiceAsync();
}
2025-11-16 18:59:23 +00:00
public string Title { get; }
private async Task InitializeThemeServiceAsync()
{
try
{
await _themeService.InitializeAsync();
2025-11-16 18:59:23 +00:00
// Ensure the color theme service has a reference to the theme service
_colorThemeService.SetThemeService(_themeService);
}
catch
{
// Theme service might already be initialized
}
}
private void InitializeOptions()
{
// Initialize Light/Dark/System Themes
AvailableThemes = new List<ThemeOption>
{
2025-11-16 18:59:23 +00:00
new()
{
Theme = AppTheme.Light,
DisplayName = _localizer["LightTheme"]
},
new()
{
Theme = AppTheme.Dark,
DisplayName = _localizer["DarkTheme"]
},
new()
{
Theme = AppTheme.System,
DisplayName = _localizer["SystemTheme"]
}
};
// Initialize Color Themes
AvailableColorThemes = new List<ColorThemeOption>
{
2025-11-16 18:59:23 +00:00
new()
{
ThemeName = "Default",
DisplayName = _localizer["DefaultColorTheme"]
},
new()
{
ThemeName = "Windows311",
DisplayName = _localizer["Windows311Theme"]
},
new()
{
ThemeName = "MacOS9",
DisplayName = _localizer["MacOS9Theme"]
2025-11-16 19:11:21 +00:00
},
new()
{
ThemeName = "DOS",
DisplayName = _localizer["DOSTheme"]
2025-11-16 19:18:19 +00:00
},
new()
{
ThemeName = "Amiga",
DisplayName = _localizer["AmigaTheme"]
2025-11-16 18:59:23 +00:00
}
};
// Try to load saved preferences
LoadSavedPreferences();
}
private async void LoadSavedPreferences()
{
try
{
// Load current theme from ThemeService
2025-11-16 18:59:23 +00:00
AppTheme currentTheme = _themeService.Theme;
SelectedTheme = AvailableThemes.FirstOrDefault(t => t.Theme == currentTheme) ??
AvailableThemes.FirstOrDefault(t => t.Theme == AppTheme.System) ?? AvailableThemes.First();
// Load current color theme
2025-11-16 18:59:23 +00:00
string currentColorTheme = _colorThemeService.CurrentColorTheme;
SelectedColorTheme = AvailableColorThemes.FirstOrDefault(t => t.ThemeName == currentColorTheme) ??
AvailableColorThemes.First();
}
catch
{
// If loading fails, use defaults
2025-11-16 18:59:23 +00:00
SelectedTheme = AvailableThemes.FirstOrDefault(t => t.Theme == AppTheme.System) ?? AvailableThemes.First();
SelectedColorTheme = AvailableColorThemes.First();
}
}
partial void OnSelectedThemeChanged(ThemeOption value)
{
2025-11-16 18:59:23 +00:00
if(value != null) ApplyTheme(value);
}
partial void OnSelectedColorThemeChanged(ColorThemeOption value)
{
2025-11-16 18:59:23 +00:00
if(value != null) ApplyColorTheme(value);
}
private async void ApplyTheme(ThemeOption theme)
{
try
{
// Apply theme immediately using ThemeService
await _themeService.SetThemeAsync(theme.Theme);
}
catch
{
// Silently fail
}
}
private void ApplyColorTheme(ColorThemeOption colorTheme)
{
try
{
_colorThemeService.ApplyColorTheme(colorTheme.ThemeName);
}
catch
{
// Silently fail
}
}
}
public class ThemeOption
{
2025-11-16 18:59:23 +00:00
public AppTheme Theme { get; set; }
public string DisplayName { get; set; } = string.Empty;
}
public class ColorThemeOption
{
2025-11-16 18:59:23 +00:00
public string ThemeName { get; set; } = string.Empty;
public string DisplayName { get; set; } = string.Empty;
2025-11-16 18:59:23 +00:00
}