import 'dart:convert'; import 'package:flutter/foundation.dart'; import '../models/app_settings.dart'; import '../storage/prefs_manager.dart'; import '../utils/app_logger.dart'; class AppSettingsService extends ChangeNotifier { static const String _settingsKey = 'app_settings'; AppSettings _settings = AppSettings(); AppSettings get settings => _settings; String batteryChemistryForDevice(String deviceId) { final stored = _settings.batteryChemistryByDeviceId[deviceId]; if (stored == 'liion') return 'nmc'; return stored ?? 'nmc'; } String batteryChemistryForRepeater(String repeaterPubKeyHex) { final stored = _settings.batteryChemistryByRepeaterId[repeaterPubKeyHex]; if (stored == 'liion') return 'nmc'; return stored ?? 'nmc'; } Future loadSettings() async { final prefs = PrefsManager.instance; final jsonStr = prefs.getString(_settingsKey); if (jsonStr != null) { try { final json = jsonDecode(jsonStr) as Map; _settings = AppSettings.fromJson(json); notifyListeners(); } catch (e) { // If parsing fails, use defaults _settings = AppSettings(); } } } Future updateSettings(AppSettings newSettings) async { _settings = newSettings; notifyListeners(); final prefs = PrefsManager.instance; final jsonStr = jsonEncode(_settings.toJson()); await prefs.setString(_settingsKey, jsonStr); } Future setClearPathOnMaxRetry(bool value) async { await updateSettings(_settings.copyWith(clearPathOnMaxRetry: value)); } Future setMapShowRepeaters(bool value) async { await updateSettings(_settings.copyWith(mapShowRepeaters: value)); } Future setMapShowChatNodes(bool value) async { await updateSettings(_settings.copyWith(mapShowChatNodes: value)); } Future setMapShowOtherNodes(bool value) async { await updateSettings(_settings.copyWith(mapShowOtherNodes: value)); } Future setMapShowOverlaps(bool value) async { await updateSettings(_settings.copyWith(mapShowOverlaps: value)); } Future setMapTimeFilterHours(double value) async { await updateSettings(_settings.copyWith(mapTimeFilterHours: value)); } Future setMapKeyPrefixEnabled(bool value) async { await updateSettings(_settings.copyWith(mapKeyPrefixEnabled: value)); } Future setMapKeyPrefix(String value) async { await updateSettings(_settings.copyWith(mapKeyPrefix: value)); } Future setMapShowMarkers(bool value) async { await updateSettings(_settings.copyWith(mapShowMarkers: value)); } Future setMapShowGuessedLocations(bool value) async { await updateSettings(_settings.copyWith(mapShowGuessedLocations: value)); } Future setEnableMessageTracing(bool value) async { await updateSettings(_settings.copyWith(enableMessageTracing: value)); } Future setMapCacheBounds(Map? value) async { await updateSettings(_settings.copyWith(mapCacheBounds: value)); } Future setMapCacheZoomRange(int minZoom, int maxZoom) async { final safeMin = minZoom <= maxZoom ? minZoom : maxZoom; final safeMax = minZoom <= maxZoom ? maxZoom : minZoom; await updateSettings( _settings.copyWith(mapCacheMinZoom: safeMin, mapCacheMaxZoom: safeMax), ); } Future setNotificationsEnabled(bool value) async { await updateSettings(_settings.copyWith(notificationsEnabled: value)); } Future setNotifyOnNewMessage(bool value) async { await updateSettings(_settings.copyWith(notifyOnNewMessage: value)); } Future setNotifyOnNewChannelMessage(bool value) async { await updateSettings(_settings.copyWith(notifyOnNewChannelMessage: value)); } Future setNotifyOnNewAdvert(bool value) async { await updateSettings(_settings.copyWith(notifyOnNewAdvert: value)); } Future setAutoRouteRotationEnabled(bool value) async { await updateSettings(_settings.copyWith(autoRouteRotationEnabled: value)); } Future setMaxRouteWeight(double value) async { await updateSettings(_settings.copyWith(maxRouteWeight: value)); } Future setInitialRouteWeight(double value) async { await updateSettings(_settings.copyWith(initialRouteWeight: value)); } Future setRouteWeightSuccessIncrement(double value) async { await updateSettings( _settings.copyWith(routeWeightSuccessIncrement: value), ); } Future setRouteWeightFailureDecrement(double value) async { await updateSettings( _settings.copyWith(routeWeightFailureDecrement: value), ); } Future setMaxMessageRetries(int value) async { await updateSettings(_settings.copyWith(maxMessageRetries: value)); } Future setThemeMode(String value) async { await updateSettings(_settings.copyWith(themeMode: value)); } Future setLanguageOverride(String? value) async { await updateSettings(_settings.copyWith(languageOverride: value)); } Future setAppDebugLogEnabled(bool value) async { await updateSettings(_settings.copyWith(appDebugLogEnabled: value)); // Update the global logger appLogger.setEnabled(value); } Future setMapShowDiscoveryContacts(bool value) async { await updateSettings(_settings.copyWith(mapShowDiscoveryContacts: value)); } Future setBatteryChemistryForDevice( String deviceId, String chemistry, ) async { final updated = Map.from( _settings.batteryChemistryByDeviceId, ); updated[deviceId] = chemistry; await updateSettings( _settings.copyWith(batteryChemistryByDeviceId: updated), ); } Future setBatteryChemistryForRepeater( String repeaterPubKeyHex, String chemistry, ) async { final updated = Map.from( _settings.batteryChemistryByRepeaterId, ); updated[repeaterPubKeyHex] = chemistry; await updateSettings( _settings.copyWith(batteryChemistryByRepeaterId: updated), ); } Future setUnitSystem(UnitSystem value) async { await updateSettings(_settings.copyWith(unitSystem: value)); } bool isChannelMuted(String channelName) { return _settings.mutedChannels.contains(channelName); } Future muteChannel(String channelName) async { final updated = Set.from(_settings.mutedChannels)..add(channelName); await updateSettings(_settings.copyWith(mutedChannels: updated)); } Future unmuteChannel(String channelName) async { final updated = Set.from(_settings.mutedChannels) ..remove(channelName); await updateSettings(_settings.copyWith(mutedChannels: updated)); } Future setTcpServerAddress(String value) async { await updateSettings(_settings.copyWith(tcpServerAddress: value)); } Future setTcpServerPort(int value) async { await updateSettings(_settings.copyWith(tcpServerPort: value)); } }