d85f2e0fb3
Bootstraper läd jetzt die farbeinstellung sofort und nicht nachträglich cleanup and rework PDF switch to HTML export fix Colorsystem Farben für knöpfe hinzugefügt als anpassung button color button color primary and secondary change
143 lines
4.3 KiB
C#
143 lines
4.3 KiB
C#
using cfs_tracker.Data;
|
|
using cfs_tracker.Helpers;
|
|
using Microsoft.Maui.Controls;
|
|
using Microsoft.Maui.Graphics;
|
|
|
|
namespace cfs_tracker.Services;
|
|
|
|
public class AppearanceService
|
|
{
|
|
private readonly AppearanceRepository _appearanceRepository;
|
|
|
|
public AppearanceService(AppearanceRepository appearanceRepository)
|
|
{
|
|
_appearanceRepository = appearanceRepository;
|
|
}
|
|
|
|
public async Task ApplyAsync()
|
|
{
|
|
var map = await _appearanceRepository.GetMapAsync();
|
|
var resources = Application.Current?.Resources;
|
|
|
|
if (resources is null)
|
|
return;
|
|
|
|
foreach (var pair in AppearanceDefaults.All)
|
|
{
|
|
var key = pair.Key;
|
|
var fallback = pair.Value;
|
|
var value = map.TryGetValue(key, out var stored) && !string.IsNullOrWhiteSpace(stored)
|
|
? stored
|
|
: fallback;
|
|
|
|
ApplyValue(resources, key, value, fallback);
|
|
}
|
|
}
|
|
|
|
public async Task SaveColorAsync(string key, string value)
|
|
{
|
|
var fallback = AppearanceDefaults.All.TryGetValue(key, out var defaultValue)
|
|
? defaultValue
|
|
: "#FFFFFF";
|
|
|
|
var safeValue = string.IsNullOrWhiteSpace(value) ? fallback : value;
|
|
|
|
await _appearanceRepository.SaveAsync(key, safeValue);
|
|
|
|
var resources = Application.Current?.Resources;
|
|
if (resources is not null)
|
|
resources[key] = ColorUtility.ParseColor(safeValue, fallback);
|
|
}
|
|
|
|
public async Task SaveTextAsync(string key, string value)
|
|
{
|
|
var fallback = AppearanceDefaults.All.TryGetValue(key, out var defaultValue)
|
|
? defaultValue
|
|
: string.Empty;
|
|
|
|
var safeValue = string.IsNullOrWhiteSpace(value) ? fallback : value;
|
|
|
|
await _appearanceRepository.SaveAsync(key, safeValue);
|
|
|
|
var resources = Application.Current?.Resources;
|
|
if (resources is not null)
|
|
resources[key] = safeValue;
|
|
}
|
|
|
|
public async Task ResetAsync()
|
|
{
|
|
await _appearanceRepository.ResetAsync();
|
|
await _appearanceRepository.EnsureDefaultsAsync();
|
|
await ApplyAsync();
|
|
}
|
|
|
|
public Color GetColor(string key, string fallback)
|
|
{
|
|
var resources = Application.Current?.Resources;
|
|
if (resources is not null && resources.TryGetValue(key, out var value))
|
|
{
|
|
if (value is Color color)
|
|
return color;
|
|
|
|
if (value is string text)
|
|
return ColorUtility.ParseColor(text, fallback);
|
|
}
|
|
|
|
return ColorUtility.ParseColor(fallback, fallback);
|
|
}
|
|
|
|
public string GetText(string key, string fallback)
|
|
{
|
|
var resources = Application.Current?.Resources;
|
|
if (resources is not null && resources.TryGetValue(key, out var value))
|
|
{
|
|
if (value is string text && !string.IsNullOrWhiteSpace(text))
|
|
return text;
|
|
}
|
|
|
|
return fallback;
|
|
}
|
|
|
|
private static void ApplyValue(ResourceDictionary resources, string key, string value, string fallback)
|
|
{
|
|
if (IsColorKey(key))
|
|
{
|
|
resources[key] = ColorUtility.ParseColor(value, fallback);
|
|
return;
|
|
}
|
|
|
|
resources[key] = string.IsNullOrWhiteSpace(value) ? fallback : value;
|
|
}
|
|
|
|
private static bool IsColorKey(string key) => key switch
|
|
{
|
|
AppearanceKeys.AppBackground => true,
|
|
AppearanceKeys.SurfaceBackground => true,
|
|
AppearanceKeys.SurfaceBackgroundAlt => true,
|
|
AppearanceKeys.Border => true,
|
|
AppearanceKeys.TextPrimary => true,
|
|
AppearanceKeys.TextMuted => true,
|
|
|
|
AppearanceKeys.StateGreen => true,
|
|
AppearanceKeys.StateYellow => true,
|
|
AppearanceKeys.StateRed => true,
|
|
AppearanceKeys.StateGray => true,
|
|
|
|
AppearanceKeys.ButtonPrimaryBackground => true,
|
|
AppearanceKeys.ButtonPrimaryText => true,
|
|
AppearanceKeys.ButtonSecondaryBackground => true,
|
|
AppearanceKeys.ButtonSecondaryText => true,
|
|
|
|
AppearanceKeys.ShellBackground => true,
|
|
AppearanceKeys.ShellForeground => true,
|
|
AppearanceKeys.ShellTitle => true,
|
|
AppearanceKeys.ShellUnselected => true,
|
|
|
|
AppearanceKeys.TabBarBackground => true,
|
|
AppearanceKeys.TabBarForeground => true,
|
|
AppearanceKeys.TabBarTitle => true,
|
|
AppearanceKeys.TabBarUnselected => true,
|
|
|
|
_ => false
|
|
};
|
|
} |