| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- import 'dart:convert';
- import 'package:shared_preferences/shared_preferences.dart';
- import '../../core/theme/accent_choice.dart';
- import '../../core/theme/theme_preference.dart';
- import '../models/slot_assignment.dart';
- import '../models/slot_type.dart';
- /// Persists launcher configuration. This is the single source of truth for
- /// appearance, home-button visibility, the automatic night window, and pinned
- /// apps as well as the three fixed slots.
- class SettingsRepository {
- SettingsRepository(this._prefs);
- final SharedPreferences _prefs;
- static const _themeKey = 'settings.theme';
- static const _nightStartKey = 'settings.nightStartMinutes';
- static const _nightEndKey = 'settings.nightEndMinutes';
- static const _accentKey = 'settings.accent';
- static const _showBatteryKey = 'settings.showBattery';
- static const _slotPrefix = 'slot';
- static const _pinnedKey = 'home.pinned';
- static const _homeRoleHintShownKey = 'onboarding.homeRoleHintShown';
- static const _obdAddressKey = 'obd.adapterAddress';
- static const _weatherTemperatureKey = 'weather.locationTemperature';
- ThemePreference get themePreference {
- return ThemePreference.fromStorage(_prefs.getString(_themeKey));
- }
- int get nightStartMinutes => _prefs.getInt(_nightStartKey) ?? 19 * 60;
- int get nightEndMinutes => _prefs.getInt(_nightEndKey) ?? 7 * 60;
- AccentChoice get accent {
- return AccentChoice.fromStorage(_prefs.getString(_accentKey));
- }
- bool get showBattery => _prefs.getBool(_showBatteryKey) ?? true;
- bool get homeRoleHintShown {
- return _prefs.getBool(_homeRoleHintShownKey) ?? false;
- }
- String? get obdAdapterAddress {
- final value = _prefs.getString(_obdAddressKey)?.trim();
- return value == null || value.isEmpty ? null : value;
- }
- bool get weatherLocationTemperature {
- return _prefs.getBool(_weatherTemperatureKey) ?? false;
- }
- Future<void> setThemePreference(ThemePreference preference) {
- return _prefs.setString(_themeKey, preference.storageValue);
- }
- Future<void> setAccent(AccentChoice accent) {
- return _prefs.setString(_accentKey, accent.storageValue);
- }
- Future<void> setShowBattery(bool show) {
- return _prefs.setBool(_showBatteryKey, show);
- }
- Future<void> setHomeRoleHintShown(bool shown) {
- return _prefs.setBool(_homeRoleHintShownKey, shown);
- }
- Future<void> setObdAdapterAddress(String? address) async {
- if (address == null || address.isEmpty) {
- await _prefs.remove(_obdAddressKey);
- } else {
- await _prefs.setString(_obdAddressKey, address);
- }
- }
- Future<void> setWeatherLocationTemperature(bool enabled) {
- return _prefs.setBool(_weatherTemperatureKey, enabled);
- }
- Future<void> setNightWindow({
- required int startMinutes,
- required int endMinutes,
- }) async {
- await _prefs.setInt(_nightStartKey, startMinutes);
- await _prefs.setInt(_nightEndKey, endMinutes);
- }
- bool slotEnabled(SlotType slot) {
- return _prefs.getBool('$_slotPrefix.${slot.key}.enabled') ?? true;
- }
- Future<void> setSlotEnabled(SlotType slot, bool enabled) {
- return _prefs.setBool('$_slotPrefix.${slot.key}.enabled', enabled);
- }
- SlotAssignment? slot(SlotType slot) {
- final packageName = _prefs.getString('$_slotPrefix.${slot.key}.package');
- final activityName = _prefs.getString('$_slotPrefix.${slot.key}.activity');
- final label = _prefs.getString('$_slotPrefix.${slot.key}.label');
- if (packageName == null || activityName == null) return null;
- return SlotAssignment(
- packageName: packageName,
- activityName: activityName,
- label: label ?? packageName,
- );
- }
- Future<void> setSlot(SlotType slot, SlotAssignment assignment) async {
- await _prefs.setString(
- '$_slotPrefix.${slot.key}.package',
- assignment.packageName,
- );
- await _prefs.setString(
- '$_slotPrefix.${slot.key}.activity',
- assignment.activityName,
- );
- await _prefs.setString('$_slotPrefix.${slot.key}.label', assignment.label);
- }
- Future<void> clearSlot(SlotType slot) async {
- await _prefs.remove('$_slotPrefix.${slot.key}.package');
- await _prefs.remove('$_slotPrefix.${slot.key}.activity');
- await _prefs.remove('$_slotPrefix.${slot.key}.label');
- }
- /// Apps explicitly pinned to the home screen via All Apps. The three
- /// predefined slots are stored separately.
- List<SlotAssignment> get pinnedApps {
- final raw = _prefs.getString(_pinnedKey);
- if (raw == null || raw.isEmpty) return const <SlotAssignment>[];
- try {
- final decoded = jsonDecode(raw);
- if (decoded is! List) return const <SlotAssignment>[];
- return decoded
- .whereType<Map>()
- .map((entry) => SlotAssignment.fromMap(entry))
- .where(
- (entry) =>
- entry.packageName.isNotEmpty && entry.activityName.isNotEmpty,
- )
- .toList(growable: false);
- } catch (_) {
- return const <SlotAssignment>[];
- }
- }
- Future<void> setPinnedApps(List<SlotAssignment> apps) {
- return _prefs.setString(
- _pinnedKey,
- jsonEncode(apps.map((app) => app.toMap()).toList(growable: false)),
- );
- }
- }
|