settings_repository.dart 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import 'package:shared_preferences/shared_preferences.dart';
  2. import '../../core/theme/theme_preference.dart';
  3. import '../models/slot_assignment.dart';
  4. import '../models/slot_type.dart';
  5. /// Persists launcher configuration. This is the single source of truth for
  6. /// theme choice, the automatic night window, and the three shortcut slots.
  7. class SettingsRepository {
  8. SettingsRepository(this._prefs);
  9. final SharedPreferences _prefs;
  10. static const _themeKey = 'settings.theme';
  11. static const _nightStartKey = 'settings.nightStartMinutes';
  12. static const _nightEndKey = 'settings.nightEndMinutes';
  13. static const _slotPrefix = 'slot';
  14. ThemePreference get themePreference {
  15. return ThemePreference.fromStorage(_prefs.getString(_themeKey));
  16. }
  17. int get nightStartMinutes => _prefs.getInt(_nightStartKey) ?? 19 * 60;
  18. int get nightEndMinutes => _prefs.getInt(_nightEndKey) ?? 7 * 60;
  19. Future<void> setThemePreference(ThemePreference preference) {
  20. return _prefs.setString(_themeKey, preference.storageValue);
  21. }
  22. Future<void> setNightWindow({
  23. required int startMinutes,
  24. required int endMinutes,
  25. }) async {
  26. await _prefs.setInt(_nightStartKey, startMinutes);
  27. await _prefs.setInt(_nightEndKey, endMinutes);
  28. }
  29. SlotAssignment? slot(SlotType slot) {
  30. final packageName = _prefs.getString('$_slotPrefix.${slot.key}.package');
  31. final activityName = _prefs.getString('$_slotPrefix.${slot.key}.activity');
  32. final label = _prefs.getString('$_slotPrefix.${slot.key}.label');
  33. if (packageName == null || activityName == null) return null;
  34. return SlotAssignment(
  35. packageName: packageName,
  36. activityName: activityName,
  37. label: label ?? packageName,
  38. );
  39. }
  40. Future<void> setSlot(SlotType slot, SlotAssignment assignment) async {
  41. await _prefs.setString(
  42. '$_slotPrefix.${slot.key}.package',
  43. assignment.packageName,
  44. );
  45. await _prefs.setString(
  46. '$_slotPrefix.${slot.key}.activity',
  47. assignment.activityName,
  48. );
  49. await _prefs.setString('$_slotPrefix.${slot.key}.label', assignment.label);
  50. }
  51. Future<void> clearSlot(SlotType slot) async {
  52. await _prefs.remove('$_slotPrefix.${slot.key}.package');
  53. await _prefs.remove('$_slotPrefix.${slot.key}.activity');
  54. await _prefs.remove('$_slotPrefix.${slot.key}.label');
  55. }
  56. }