| | | 1 | | using Pomodoro.Web.Models; |
| | | 2 | | |
| | | 3 | | namespace Pomodoro.Web.Services.Repositories; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Repository implementation for settings persistence using IndexedDB |
| | | 7 | | /// </summary> |
| | | 8 | | public class SettingsRepository : ISettingsRepository |
| | | 9 | | { |
| | | 10 | | private readonly IIndexedDbService _indexedDb; |
| | | 11 | | |
| | 84 | 12 | | public SettingsRepository(IIndexedDbService indexedDb) |
| | 84 | 13 | | { |
| | 84 | 14 | | _indexedDb = indexedDb; |
| | 84 | 15 | | } |
| | | 16 | | |
| | | 17 | | public async Task<TimerSettings?> GetAsync() |
| | 47 | 18 | | { |
| | 47 | 19 | | var record = await _indexedDb.GetAsync<TimerSettingsRecord>(Constants.Storage.SettingsStore, Constants.Storage.D |
| | 70 | 20 | | if (record == null) return null; |
| | | 21 | | |
| | 14 | 22 | | return new TimerSettings |
| | 14 | 23 | | { |
| | 14 | 24 | | PomodoroMinutes = record.PomodoroMinutes, |
| | 14 | 25 | | ShortBreakMinutes = record.ShortBreakMinutes, |
| | 14 | 26 | | LongBreakMinutes = record.LongBreakMinutes, |
| | 14 | 27 | | SoundEnabled = record.SoundEnabled, |
| | 14 | 28 | | NotificationsEnabled = record.NotificationsEnabled, |
| | 14 | 29 | | AutoStartEnabled = record.AutoStartEnabled, |
| | 14 | 30 | | AutoStartDelaySeconds = record.AutoStartDelaySeconds |
| | 14 | 31 | | }; |
| | 42 | 32 | | } |
| | | 33 | | |
| | | 34 | | public async Task<bool> SaveAsync(TimerSettings settings) |
| | 86 | 35 | | { |
| | 87 | 36 | | if (settings == null) throw new ArgumentNullException(nameof(settings)); |
| | | 37 | | |
| | 85 | 38 | | var record = new TimerSettingsRecord |
| | 85 | 39 | | { |
| | 85 | 40 | | Id = Constants.Storage.DefaultSettingsId, |
| | 85 | 41 | | PomodoroMinutes = settings.PomodoroMinutes, |
| | 85 | 42 | | ShortBreakMinutes = settings.ShortBreakMinutes, |
| | 85 | 43 | | LongBreakMinutes = settings.LongBreakMinutes, |
| | 85 | 44 | | SoundEnabled = settings.SoundEnabled, |
| | 85 | 45 | | NotificationsEnabled = settings.NotificationsEnabled, |
| | 85 | 46 | | AutoStartEnabled = settings.AutoStartEnabled, |
| | 85 | 47 | | AutoStartDelaySeconds = settings.AutoStartDelaySeconds |
| | 85 | 48 | | }; |
| | 85 | 49 | | return await _indexedDb.PutAsync(Constants.Storage.SettingsStore, record); |
| | 76 | 50 | | } |
| | | 51 | | |
| | | 52 | | public async Task ResetToDefaultsAsync() |
| | 29 | 53 | | { |
| | 29 | 54 | | var defaults = new TimerSettings(); |
| | 29 | 55 | | await SaveAsync(defaults); |
| | 27 | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Record for storing timer settings in IndexedDB |
| | | 61 | | /// </summary> |
| | | 62 | | public class TimerSettingsRecord |
| | | 63 | | { |
| | | 64 | | public string Id { get; set; } = Constants.Storage.DefaultSettingsId; |
| | | 65 | | public int PomodoroMinutes { get; set; } |
| | | 66 | | public int ShortBreakMinutes { get; set; } |
| | | 67 | | public int LongBreakMinutes { get; set; } |
| | | 68 | | public bool SoundEnabled { get; set; } |
| | | 69 | | public bool NotificationsEnabled { get; set; } |
| | | 70 | | public bool AutoStartEnabled { get; set; } |
| | | 71 | | public int AutoStartDelaySeconds { get; set; } |
| | | 72 | | } |