environment_monitoring_app/lib/services/settings_service.dart

99 lines
4.1 KiB
Dart

import 'package:shared_preferences/shared_preferences.dart';
import 'package:environment_monitoring_app/services/base_api_service.dart';
class SettingsService {
final BaseApiService _baseService = BaseApiService();
// Keys for SharedPreferences
static const _inSituChatIdKey = 'telegram_in_situ_chat_id';
static const _tarballChatIdKey = 'telegram_tarball_chat_id';
static const _riverInSituChatIdKey = 'telegram_river_in_situ_chat_id';
static const _riverTriennialChatIdKey = 'telegram_river_triennial_chat_id';
static const _riverInvestigativeChatIdKey = 'telegram_river_investigative_chat_id';
static const _airManualChatIdKey = 'telegram_air_manual_chat_id';
static const _airInvestigativeChatIdKey = 'telegram_air_investigative_chat_id';
static const _marineInvestigativeChatIdKey = 'telegram_marine_investigative_chat_id';
/// Fetches settings from the server and saves them to local storage.
Future<bool> syncFromServer() async {
try {
final result = await _baseService.get('settings');
if (result['success'] == true && result['data'] is Map) {
final settings = result['data'] as Map<String, dynamic>;
final prefs = await SharedPreferences.getInstance();
// Save all chat IDs from the nested maps
await Future.wait([
_saveChatId(prefs, _inSituChatIdKey, settings['marine_in_situ']),
_saveChatId(prefs, _tarballChatIdKey, settings['marine_tarball']),
_saveChatId(prefs, _riverInSituChatIdKey, settings['river_in_situ']),
_saveChatId(prefs, _riverTriennialChatIdKey, settings['river_triennial']),
_saveChatId(prefs, _riverInvestigativeChatIdKey, settings['river_investigative']),
_saveChatId(prefs, _airManualChatIdKey, settings['air_manual']),
_saveChatId(prefs, _airInvestigativeChatIdKey, settings['air_investigative']),
_saveChatId(prefs, _marineInvestigativeChatIdKey, settings['marine_investigative']),
]);
return true;
}
return false;
} catch (e) {
return false;
}
}
Future<void> _saveChatId(SharedPreferences prefs, String key, dynamic settings) async {
if (settings is Map<String, dynamic>) {
await prefs.setString(key, settings['telegram_chat_id']?.toString() ?? '');
}
}
/// Gets the locally stored Chat ID for the In-Situ module.
Future<String> getInSituChatId() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_inSituChatIdKey) ?? '';
}
/// Gets the locally stored Chat ID for the Tarball module.
Future<String> getTarballChatId() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_tarballChatIdKey) ?? '';
}
/// Gets the locally stored Chat ID for the River In-Situ module.
Future<String> getRiverInSituChatId() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_riverInSituChatIdKey) ?? '';
}
/// Gets the locally stored Chat ID for the River Triennial module.
Future<String> getRiverTriennialChatId() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_riverTriennialChatIdKey) ?? '';
}
/// Gets the locally stored Chat ID for the River Investigative module.
Future<String> getRiverInvestigativeChatId() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_riverInvestigativeChatIdKey) ?? '';
}
/// Gets the locally stored Chat ID for the Air Manual module.
Future<String> getAirManualChatId() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_airManualChatIdKey) ?? '';
}
/// Gets the locally stored Chat ID for the Air Investigative module.
Future<String> getAirInvestigativeChatId() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_airInvestigativeChatIdKey) ?? '';
}
/// Gets the locally stored Chat ID for the Marine Investigative module.
Future<String> getMarineInvestigativeChatId() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_marineInvestigativeChatIdKey) ?? '';
}
}