settings_repository.dart 5.3 KB

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