| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Reflection; |
| | | 3 | | |
| | | 4 | | namespace UIBlazor.Services.Settings; |
| | | 5 | | |
| | | 6 | | public abstract class BaseSettingsProvider<TOptions> : IBaseSettingsProvider where TOptions : BaseOptions, new() |
| | | 7 | | { |
| | | 8 | | private readonly ILocalStorageService _storage; |
| | | 9 | | private readonly string _storageKey; |
| | | 10 | | protected readonly Debouncer Debouncer; |
| | | 11 | | private bool _isInitializing; |
| | | 12 | | private readonly ILogger _logger; |
| | | 13 | | |
| | 312 | 14 | | public TOptions Current { get; } = new(); |
| | | 15 | | |
| | 72 | 16 | | protected BaseSettingsProvider( |
| | 72 | 17 | | ILocalStorageService storage, |
| | 72 | 18 | | ILogger logger, |
| | 72 | 19 | | string storageKey, |
| | 72 | 20 | | TimeSpan? debounceDelay = null) |
| | | 21 | | { |
| | 72 | 22 | | _storage = storage; |
| | 72 | 23 | | _logger = logger; |
| | 72 | 24 | | _storageKey = storageKey; |
| | 72 | 25 | | Debouncer = new Debouncer(debounceDelay ?? TimeSpan.FromMilliseconds(750), SaveAsync); |
| | 72 | 26 | | } |
| | | 27 | | |
| | | 28 | | private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) |
| | | 29 | | { |
| | 3 | 30 | | if (_isInitializing) |
| | 2 | 31 | | return; |
| | | 32 | | |
| | 1 | 33 | | Debouncer.Trigger(); |
| | 1 | 34 | | } |
| | | 35 | | |
| | | 36 | | public void CallSaveTrigger() |
| | | 37 | | { |
| | 0 | 38 | | if (_isInitializing) |
| | 0 | 39 | | return; |
| | 0 | 40 | | Debouncer.Trigger(); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public event Action? OnSaved; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Немедленное сохранение Current объекта.<br/> |
| | | 47 | | /// Автоматически вызывается при изменении любого свойства, которое использует <see cref="BaseOptions.SetIfChanged"/ |
| | | 48 | | /// </summary> |
| | | 49 | | public virtual async Task SaveAsync() |
| | | 50 | | { |
| | 20 | 51 | | await _storage.SetItemAsync(_storageKey, Current); |
| | 20 | 52 | | OnSaved?.Invoke(); |
| | 20 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Загрузка настроек |
| | | 57 | | /// </summary> |
| | | 58 | | public async Task InitializeAsync() |
| | | 59 | | { |
| | 38 | 60 | | _isInitializing = true; |
| | | 61 | | try |
| | | 62 | | { |
| | 38 | 63 | | var saved = await _storage.TryGetItemAsync<TOptions>(_storageKey); |
| | 37 | 64 | | if (saved != null) |
| | | 65 | | { |
| | 9 | 66 | | CopyProperties(saved, Current); |
| | | 67 | | } |
| | | 68 | | |
| | 37 | 69 | | Current.PropertyChanged += OnPropertyChanged; |
| | 37 | 70 | | await AfterInitAsync(); |
| | 37 | 71 | | } |
| | 1 | 72 | | catch (Exception ex) |
| | | 73 | | { |
| | 1 | 74 | | _logger.LogError($"Failed to initialize settings for {_storageKey}: {ex.Message}"); |
| | 1 | 75 | | } |
| | | 76 | | finally |
| | | 77 | | { |
| | 38 | 78 | | _isInitializing = false; |
| | | 79 | | } |
| | 38 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Вызывается сразу после загрузки настроек |
| | | 84 | | /// Сюда надо добавлять первичные манипуляции с настройками |
| | | 85 | | /// </summary> |
| | 0 | 86 | | protected virtual Task AfterInitAsync() => Task.CompletedTask; |
| | | 87 | | |
| | | 88 | | private void CopyProperties(TOptions from, TOptions to) |
| | | 89 | | { |
| | 9 | 90 | | var properties = typeof(TOptions).GetProperties(BindingFlags.Public | BindingFlags.Instance) |
| | 27 | 91 | | .Where(p => p is { CanRead: true, CanWrite: true }); |
| | | 92 | | |
| | 54 | 93 | | foreach (var prop in properties) |
| | | 94 | | { |
| | 18 | 95 | | var value = prop.GetValue(from); |
| | 18 | 96 | | prop.SetValue(to, value); |
| | | 97 | | } |
| | 9 | 98 | | } |
| | | 99 | | |
| | | 100 | | public abstract Task ResetAsync(); |
| | | 101 | | |
| | | 102 | | public virtual void Dispose() |
| | | 103 | | { |
| | 0 | 104 | | Current.PropertyChanged -= OnPropertyChanged; |
| | 0 | 105 | | Debouncer.Dispose(); |
| | 0 | 106 | | } |
| | | 107 | | } |