environment_monitoring_app/lib/services/settings_service.dart

74 lines
2.9 KiB
Dart

import 'package:flutter/foundation.dart';
// No longer needs SharedPreferences or BaseApiService for its core logic.
class SettingsService {
// The service no longer manages its own state or makes API calls.
// It is now a utility for parsing the settings list managed by AuthProvider.
/// A private helper method to find a specific setting value from the cached list.
String _getChatId(List<Map<String, dynamic>>? settings, String moduleName) {
if (settings == null) {
debugPrint("SettingsService: Cannot get Chat ID for '$moduleName', settings list is null.");
return '';
}
try {
// Find the specific setting map where the module_name and setting_key match.
final setting = settings.firstWhere(
(s) => s['module_name'] == moduleName && s['setting_key'] == 'telegram_chat_id',
// If no matching setting is found, return an empty map to avoid errors.
orElse: () => {},
);
if (setting.isNotEmpty) {
return setting['setting_value']?.toString() ?? '';
}
} catch (e) {
debugPrint("SettingsService: Error parsing chat ID for '$moduleName': $e");
}
debugPrint("SettingsService: Chat ID for module '$moduleName' not found.");
return '';
}
/// Gets the Chat ID for the Marine In-Situ module from the provided settings list.
String getInSituChatId(List<Map<String, dynamic>>? settings) {
return _getChatId(settings, 'marine_in_situ');
}
/// Gets the Chat ID for the Tarball module from the provided settings list.
String getTarballChatId(List<Map<String, dynamic>>? settings) {
return _getChatId(settings, 'marine_tarball');
}
/// Gets the Chat ID for the River In-Situ module from the provided settings list.
String getRiverInSituChatId(List<Map<String, dynamic>>? settings) {
return _getChatId(settings, 'river_in_situ');
}
/// Gets the Chat ID for the River Triennial module from the provided settings list.
String getRiverTriennialChatId(List<Map<String, dynamic>>? settings) {
return _getChatId(settings, 'river_triennial');
}
/// Gets the Chat ID for the River Investigative module from the provided settings list.
String getRiverInvestigativeChatId(List<Map<String, dynamic>>? settings) {
return _getChatId(settings, 'river_investigative');
}
/// Gets the Chat ID for the Air Manual module from the provided settings list.
String getAirManualChatId(List<Map<String, dynamic>>? settings) {
return _getChatId(settings, 'air_manual');
}
/// Gets the Chat ID for the Air Investigative module from the provided settings list.
String getAirInvestigativeChatId(List<Map<String, dynamic>>? settings) {
return _getChatId(settings, 'air_investigative');
}
/// Gets the Chat ID for the Marine Investigative module from the provided settings list.
String getMarineInvestigativeChatId(List<Map<String, dynamic>>? settings) {
return _getChatId(settings, 'marine_investigative');
}
}