settings_repository.dart 5.0 KB

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