settings_repository.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. static const _homeRoleHintShownKey = 'onboarding.homeRoleHintShown';
  21. ThemePreference get themePreference {
  22. return ThemePreference.fromStorage(_prefs.getString(_themeKey));
  23. }
  24. int get nightStartMinutes => _prefs.getInt(_nightStartKey) ?? 19 * 60;
  25. int get nightEndMinutes => _prefs.getInt(_nightEndKey) ?? 7 * 60;
  26. AccentChoice get accent {
  27. return AccentChoice.fromStorage(_prefs.getString(_accentKey));
  28. }
  29. bool get showBattery => _prefs.getBool(_showBatteryKey) ?? true;
  30. bool get homeRoleHintShown {
  31. return _prefs.getBool(_homeRoleHintShownKey) ?? false;
  32. }
  33. Future<void> setThemePreference(ThemePreference preference) {
  34. return _prefs.setString(_themeKey, preference.storageValue);
  35. }
  36. Future<void> setAccent(AccentChoice accent) {
  37. return _prefs.setString(_accentKey, accent.storageValue);
  38. }
  39. Future<void> setShowBattery(bool show) {
  40. return _prefs.setBool(_showBatteryKey, show);
  41. }
  42. Future<void> setHomeRoleHintShown(bool shown) {
  43. return _prefs.setBool(_homeRoleHintShownKey, shown);
  44. }
  45. Future<void> setNightWindow({
  46. required int startMinutes,
  47. required int endMinutes,
  48. }) async {
  49. await _prefs.setInt(_nightStartKey, startMinutes);
  50. await _prefs.setInt(_nightEndKey, endMinutes);
  51. }
  52. bool slotEnabled(SlotType slot) {
  53. return _prefs.getBool('$_slotPrefix.${slot.key}.enabled') ?? true;
  54. }
  55. Future<void> setSlotEnabled(SlotType slot, bool enabled) {
  56. return _prefs.setBool('$_slotPrefix.${slot.key}.enabled', enabled);
  57. }
  58. SlotAssignment? slot(SlotType slot) {
  59. final packageName = _prefs.getString('$_slotPrefix.${slot.key}.package');
  60. final activityName = _prefs.getString('$_slotPrefix.${slot.key}.activity');
  61. final label = _prefs.getString('$_slotPrefix.${slot.key}.label');
  62. if (packageName == null || activityName == null) return null;
  63. return SlotAssignment(
  64. packageName: packageName,
  65. activityName: activityName,
  66. label: label ?? packageName,
  67. );
  68. }
  69. Future<void> setSlot(SlotType slot, SlotAssignment assignment) async {
  70. await _prefs.setString(
  71. '$_slotPrefix.${slot.key}.package',
  72. assignment.packageName,
  73. );
  74. await _prefs.setString(
  75. '$_slotPrefix.${slot.key}.activity',
  76. assignment.activityName,
  77. );
  78. await _prefs.setString('$_slotPrefix.${slot.key}.label', assignment.label);
  79. }
  80. Future<void> clearSlot(SlotType slot) async {
  81. await _prefs.remove('$_slotPrefix.${slot.key}.package');
  82. await _prefs.remove('$_slotPrefix.${slot.key}.activity');
  83. await _prefs.remove('$_slotPrefix.${slot.key}.label');
  84. }
  85. /// Apps explicitly pinned to the home screen via All Apps. The three
  86. /// predefined slots are stored separately.
  87. List<SlotAssignment> get pinnedApps {
  88. final raw = _prefs.getString(_pinnedKey);
  89. if (raw == null || raw.isEmpty) return const <SlotAssignment>[];
  90. try {
  91. final decoded = jsonDecode(raw);
  92. if (decoded is! List) return const <SlotAssignment>[];
  93. return decoded
  94. .whereType<Map>()
  95. .map((entry) => SlotAssignment.fromMap(entry))
  96. .where(
  97. (entry) =>
  98. entry.packageName.isNotEmpty && entry.activityName.isNotEmpty,
  99. )
  100. .toList(growable: false);
  101. } catch (_) {
  102. return const <SlotAssignment>[];
  103. }
  104. }
  105. Future<void> setPinnedApps(List<SlotAssignment> apps) {
  106. return _prefs.setString(
  107. _pinnedKey,
  108. jsonEncode(apps.map((app) => app.toMap()).toList(growable: false)),
  109. );
  110. }
  111. }