settings_repository.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import 'dart:convert';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. import '../../core/theme/accent_choice.dart';
  4. import '../../core/theme/theme_preference.dart';
  5. import '../models/slot_assignment.dart';
  6. import '../models/slot_type.dart';
  7. /// Persists launcher configuration. This is the single source of truth for
  8. /// appearance, home-button visibility, the automatic night window, and pinned
  9. /// apps as well as the three fixed slots.
  10. class SettingsRepository {
  11. SettingsRepository(this._prefs);
  12. final SharedPreferences _prefs;
  13. static const _themeKey = 'settings.theme';
  14. static const _nightStartKey = 'settings.nightStartMinutes';
  15. static const _nightEndKey = 'settings.nightEndMinutes';
  16. static const _accentKey = 'settings.accent';
  17. static const _showBatteryKey = 'settings.showBattery';
  18. static const _slotPrefix = 'slot';
  19. static const _pinnedKey = 'home.pinned';
  20. ThemePreference get themePreference {
  21. return ThemePreference.fromStorage(_prefs.getString(_themeKey));
  22. }
  23. int get nightStartMinutes => _prefs.getInt(_nightStartKey) ?? 19 * 60;
  24. int get nightEndMinutes => _prefs.getInt(_nightEndKey) ?? 7 * 60;
  25. AccentChoice get accent {
  26. return AccentChoice.fromStorage(_prefs.getString(_accentKey));
  27. }
  28. bool get showBattery => _prefs.getBool(_showBatteryKey) ?? true;
  29. Future<void> setThemePreference(ThemePreference preference) {
  30. return _prefs.setString(_themeKey, preference.storageValue);
  31. }
  32. Future<void> setAccent(AccentChoice accent) {
  33. return _prefs.setString(_accentKey, accent.storageValue);
  34. }
  35. Future<void> setShowBattery(bool show) {
  36. return _prefs.setBool(_showBatteryKey, show);
  37. }
  38. Future<void> setNightWindow({
  39. required int startMinutes,
  40. required int endMinutes,
  41. }) async {
  42. await _prefs.setInt(_nightStartKey, startMinutes);
  43. await _prefs.setInt(_nightEndKey, endMinutes);
  44. }
  45. bool slotEnabled(SlotType slot) {
  46. return _prefs.getBool('$_slotPrefix.${slot.key}.enabled') ?? true;
  47. }
  48. Future<void> setSlotEnabled(SlotType slot, bool enabled) {
  49. return _prefs.setBool('$_slotPrefix.${slot.key}.enabled', enabled);
  50. }
  51. SlotAssignment? slot(SlotType slot) {
  52. final packageName = _prefs.getString('$_slotPrefix.${slot.key}.package');
  53. final activityName = _prefs.getString('$_slotPrefix.${slot.key}.activity');
  54. final label = _prefs.getString('$_slotPrefix.${slot.key}.label');
  55. if (packageName == null || activityName == null) return null;
  56. return SlotAssignment(
  57. packageName: packageName,
  58. activityName: activityName,
  59. label: label ?? packageName,
  60. );
  61. }
  62. Future<void> setSlot(SlotType slot, SlotAssignment assignment) async {
  63. await _prefs.setString(
  64. '$_slotPrefix.${slot.key}.package',
  65. assignment.packageName,
  66. );
  67. await _prefs.setString(
  68. '$_slotPrefix.${slot.key}.activity',
  69. assignment.activityName,
  70. );
  71. await _prefs.setString('$_slotPrefix.${slot.key}.label', assignment.label);
  72. }
  73. Future<void> clearSlot(SlotType slot) async {
  74. await _prefs.remove('$_slotPrefix.${slot.key}.package');
  75. await _prefs.remove('$_slotPrefix.${slot.key}.activity');
  76. await _prefs.remove('$_slotPrefix.${slot.key}.label');
  77. }
  78. /// Apps explicitly pinned to the home screen via All Apps. The three
  79. /// predefined slots are stored separately.
  80. List<SlotAssignment> get pinnedApps {
  81. final raw = _prefs.getString(_pinnedKey);
  82. if (raw == null || raw.isEmpty) return const <SlotAssignment>[];
  83. try {
  84. final decoded = jsonDecode(raw);
  85. if (decoded is! List) return const <SlotAssignment>[];
  86. return decoded
  87. .whereType<Map>()
  88. .map((entry) => SlotAssignment.fromMap(entry))
  89. .where(
  90. (entry) =>
  91. entry.packageName.isNotEmpty && entry.activityName.isNotEmpty,
  92. )
  93. .toList(growable: false);
  94. } catch (_) {
  95. return const <SlotAssignment>[];
  96. }
  97. }
  98. Future<void> setPinnedApps(List<SlotAssignment> apps) {
  99. return _prefs.setString(
  100. _pinnedKey,
  101. jsonEncode(apps.map((app) => app.toMap()).toList(growable: false)),
  102. );
  103. }
  104. }