Farbpalette vorladen lassen
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
This commit is contained in:
+110
-28
@@ -7,55 +7,137 @@ namespace cfs_tracker.Services;
|
||||
|
||||
public class AppearanceService
|
||||
{
|
||||
private readonly AppearanceRepository _repository;
|
||||
private readonly AppearanceRepository _appearanceRepository;
|
||||
|
||||
public event Action? AppearanceChanged;
|
||||
|
||||
public AppearanceService(AppearanceRepository repository)
|
||||
public AppearanceService(AppearanceRepository appearanceRepository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
await _repository.EnsureDefaultsAsync();
|
||||
await ApplyAsync();
|
||||
_appearanceRepository = appearanceRepository;
|
||||
}
|
||||
|
||||
public async Task ApplyAsync()
|
||||
{
|
||||
var items = await _repository.GetAllAsync();
|
||||
var map = await _appearanceRepository.GetMapAsync();
|
||||
var resources = Application.Current?.Resources;
|
||||
|
||||
foreach (var item in items)
|
||||
if (resources is null)
|
||||
return;
|
||||
|
||||
foreach (var pair in AppearanceDefaults.All)
|
||||
{
|
||||
if (item.Key.StartsWith("Status", StringComparison.Ordinal))
|
||||
{
|
||||
Application.Current!.Resources[item.Key] = item.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
Application.Current!.Resources[item.Key] = Color.FromArgb(item.Value);
|
||||
}
|
||||
}
|
||||
var key = pair.Key;
|
||||
var fallback = pair.Value;
|
||||
var value = map.TryGetValue(key, out var stored) && !string.IsNullOrWhiteSpace(stored)
|
||||
? stored
|
||||
: fallback;
|
||||
|
||||
AppearanceChanged?.Invoke();
|
||||
ApplyValue(resources, key, value, fallback);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveColorAsync(string key, string value)
|
||||
{
|
||||
await _repository.UpsertAsync(key, value);
|
||||
await ApplyAsync();
|
||||
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)
|
||||
{
|
||||
await _repository.UpsertAsync(key, value);
|
||||
await ApplyAsync();
|
||||
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 _repository.ResetToDefaultsAsync();
|
||||
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
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user