Files
meshcore-open/lib/services/app_settings_service.dart
T
just_stuff_tm f4b18d97a1 Added Line Of Sight Feature for repeater placement, Added app wide Units Setting (#198)
* feat: add LOS workflow, global units, l10n cleanup, and mobile UI overflow fixes

Squashes prior PR commits into one changeset including: LOS map/service/tests, global metric/imperial unit system adoption, notification/BLE safety fixes, app-wide localization backfill/mojibake cleanup, and responsive UI title/overflow hardening.

* l10n: revert unrelated locale churn for LOS feature

* feat: keep LOS with app-wide unit settings

* fix: resolve post-merge app bar/import analyzer errors

* style: format screen files for CI
2026-02-20 22:08:23 -08:00

146 lines
4.4 KiB
Dart

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';
}
Future<void> loadSettings() async {
final prefs = PrefsManager.instance;
final jsonStr = prefs.getString(_settingsKey);
if (jsonStr != null) {
try {
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
_settings = AppSettings.fromJson(json);
notifyListeners();
} catch (e) {
// If parsing fails, use defaults
_settings = AppSettings();
}
}
}
Future<void> updateSettings(AppSettings newSettings) async {
_settings = newSettings;
notifyListeners();
final prefs = PrefsManager.instance;
final jsonStr = jsonEncode(_settings.toJson());
await prefs.setString(_settingsKey, jsonStr);
}
Future<void> setClearPathOnMaxRetry(bool value) async {
await updateSettings(_settings.copyWith(clearPathOnMaxRetry: value));
}
Future<void> setMapShowRepeaters(bool value) async {
await updateSettings(_settings.copyWith(mapShowRepeaters: value));
}
Future<void> setMapShowChatNodes(bool value) async {
await updateSettings(_settings.copyWith(mapShowChatNodes: value));
}
Future<void> setMapShowOtherNodes(bool value) async {
await updateSettings(_settings.copyWith(mapShowOtherNodes: value));
}
Future<void> setMapTimeFilterHours(double value) async {
await updateSettings(_settings.copyWith(mapTimeFilterHours: value));
}
Future<void> setMapKeyPrefixEnabled(bool value) async {
await updateSettings(_settings.copyWith(mapKeyPrefixEnabled: value));
}
Future<void> setMapKeyPrefix(String value) async {
await updateSettings(_settings.copyWith(mapKeyPrefix: value));
}
Future<void> setMapShowMarkers(bool value) async {
await updateSettings(_settings.copyWith(mapShowMarkers: value));
}
Future<void> setMapCacheBounds(Map<String, double>? value) async {
await updateSettings(_settings.copyWith(mapCacheBounds: value));
}
Future<void> 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<void> setNotificationsEnabled(bool value) async {
await updateSettings(_settings.copyWith(notificationsEnabled: value));
}
Future<void> setNotifyOnNewMessage(bool value) async {
await updateSettings(_settings.copyWith(notifyOnNewMessage: value));
}
Future<void> setNotifyOnNewChannelMessage(bool value) async {
await updateSettings(_settings.copyWith(notifyOnNewChannelMessage: value));
}
Future<void> setNotifyOnNewAdvert(bool value) async {
await updateSettings(_settings.copyWith(notifyOnNewAdvert: value));
}
Future<void> setAutoRouteRotationEnabled(bool value) async {
await updateSettings(_settings.copyWith(autoRouteRotationEnabled: value));
}
Future<void> setThemeMode(String value) async {
await updateSettings(_settings.copyWith(themeMode: value));
}
Future<void> setLanguageOverride(String? value) async {
await updateSettings(_settings.copyWith(languageOverride: value));
}
Future<void> setAppDebugLogEnabled(bool value) async {
await updateSettings(_settings.copyWith(appDebugLogEnabled: value));
// Update the global logger
appLogger.setEnabled(value);
}
Future<void> setBatteryChemistryForDevice(
String deviceId,
String chemistry,
) async {
final updated = Map<String, String>.from(
_settings.batteryChemistryByDeviceId,
);
updated[deviceId] = chemistry;
await updateSettings(
_settings.copyWith(batteryChemistryByDeviceId: updated),
);
}
Future<void> setUnitSystem(UnitSystem value) async {
await updateSettings(_settings.copyWith(unitSystem: value));
}
Future<void> setLosUnitSystem(String value) async {
await setUnitSystem(
value == 'imperial' ? UnitSystem.imperial : UnitSystem.metric,
);
}
}