environment_monitoring_app/lib/services/settings_service.dart
2025-08-04 15:11:24 +08:00

42 lines
1.6 KiB
Dart

// lib/services/settings_service.dart
import 'package:shared_preferences/shared_preferences.dart';
import 'package:environment_monitoring_app/services/base_api_service.dart';
class SettingsService {
final BaseApiService _baseService = BaseApiService();
static const _inSituChatIdKey = 'telegram_in_situ_chat_id';
static const _tarballChatIdKey = 'telegram_tarball_chat_id';
/// Fetches settings from the server and saves them to local storage.
Future<bool> syncFromServer() async {
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 the chat IDs from the nested map
final inSituSettings = settings['marine_in_situ'] as Map<String, dynamic>?;
await prefs.setString(_inSituChatIdKey, inSituSettings?['telegram_chat_id'] ?? '');
final tarballSettings = settings['marine_tarball'] as Map<String, dynamic>?;
await prefs.setString(_tarballChatIdKey, tarballSettings?['telegram_chat_id'] ?? '');
return true;
}
return false;
}
/// 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) ?? '';
}
}