152 lines
6.1 KiB
Dart
152 lines
6.1 KiB
Dart
// lib/services/user_preferences_service.dart
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:environment_monitoring_app/services/api_service.dart'; // Contains DatabaseHelper
|
|
|
|
/// A dedicated service to manage the user's local preferences for
|
|
/// module-specific submission destinations.
|
|
class UserPreferencesService {
|
|
final DatabaseHelper _dbHelper = DatabaseHelper();
|
|
|
|
/// Retrieves a module's master submission preferences.
|
|
/// If no preference has been saved for this module, it returns a default
|
|
/// where both API and FTP are enabled.
|
|
Future<Map<String, dynamic>> getModulePreference(String moduleName) async {
|
|
final preference = await _dbHelper.getModulePreference(moduleName);
|
|
if (preference != null) {
|
|
return preference;
|
|
}
|
|
// Return a default value if no preference is found in the database.
|
|
return {
|
|
'module_name': moduleName,
|
|
'is_api_enabled': true,
|
|
'is_ftp_enabled': true,
|
|
};
|
|
}
|
|
|
|
/// Saves or updates a module's master on/off switches for API and FTP submissions.
|
|
Future<void> saveModulePreference({
|
|
required String moduleName,
|
|
required bool isApiEnabled,
|
|
required bool isFtpEnabled,
|
|
}) async {
|
|
await _dbHelper.saveModulePreference(
|
|
moduleName: moduleName,
|
|
isApiEnabled: isApiEnabled,
|
|
isFtpEnabled: isFtpEnabled,
|
|
);
|
|
}
|
|
|
|
/// Retrieves all available API configurations and merges them with the user's
|
|
/// saved preferences for a specific module.
|
|
///
|
|
/// This is primarily for the Settings UI to display all possible destinations
|
|
/// with their current enabled/disabled state (e.g., checkboxes).
|
|
Future<List<Map<String, dynamic>>> getAllApiConfigsWithModulePreferences(String moduleName) async {
|
|
// 1. Get all possible API destinations that have been synced to the device.
|
|
final allApiConfigs = await _dbHelper.loadApiConfigs() ?? [];
|
|
if (allApiConfigs.isEmpty) return [];
|
|
|
|
// 2. Get the specific links the user has previously saved for this module.
|
|
final savedLinks = await _dbHelper.getAllApiLinksForModule(moduleName);
|
|
|
|
// 3. Merge the two lists.
|
|
return allApiConfigs.map((config) {
|
|
final configId = config['api_config_id'];
|
|
bool isEnabled = false; // Default to disabled
|
|
|
|
try {
|
|
// Find if a link exists for this config ID in the user's saved preferences.
|
|
final matchingLink = savedLinks.firstWhere(
|
|
(link) => link['api_config_id'] == configId,
|
|
// If no link is found, 'orElse' is not triggered, it throws.
|
|
);
|
|
isEnabled = matchingLink['is_enabled'] as bool? ?? false;
|
|
} catch (e) {
|
|
// A 'firstWhere' with no match throws an error. We catch it here.
|
|
// This means no link was saved for this config, so it remains disabled.
|
|
isEnabled = false;
|
|
}
|
|
|
|
// Return a new map containing the original config details plus the 'is_enabled' flag.
|
|
return {
|
|
...config,
|
|
'is_enabled': isEnabled,
|
|
};
|
|
}).toList();
|
|
}
|
|
|
|
/// Retrieves all available FTP configurations and merges them with the user's
|
|
/// saved preferences for a specific module. (For the Settings UI).
|
|
Future<List<Map<String, dynamic>>> getAllFtpConfigsWithModulePreferences(String moduleName) async {
|
|
final allFtpConfigs = await _dbHelper.loadFtpConfigs() ?? [];
|
|
if (allFtpConfigs.isEmpty) return [];
|
|
|
|
final savedLinks = await _dbHelper.getAllFtpLinksForModule(moduleName);
|
|
|
|
return allFtpConfigs.map((config) {
|
|
final configId = config['ftp_config_id'];
|
|
bool isEnabled = false;
|
|
try {
|
|
final matchingLink = savedLinks.firstWhere(
|
|
(link) => link['ftp_config_id'] == configId,
|
|
);
|
|
isEnabled = matchingLink['is_enabled'] as bool? ?? false;
|
|
} catch (e) {
|
|
isEnabled = false;
|
|
}
|
|
return {
|
|
...config,
|
|
'is_enabled': isEnabled,
|
|
};
|
|
}).toList();
|
|
}
|
|
|
|
/// Saves the complete set of enabled/disabled API links for a specific module.
|
|
/// This will replace all previous links for that module.
|
|
Future<void> saveApiLinksForModule(String moduleName, List<Map<String, dynamic>> links) async {
|
|
await _dbHelper.saveApiLinksForModule(moduleName, links);
|
|
}
|
|
|
|
/// Saves the complete set of enabled/disabled FTP links for a specific module.
|
|
Future<void> saveFtpLinksForModule(String moduleName, List<Map<String, dynamic>> links) async {
|
|
await _dbHelper.saveFtpLinksForModule(moduleName, links);
|
|
}
|
|
|
|
/// Retrieves only the API configurations that are actively enabled for a given module.
|
|
///
|
|
/// This is primarily for the submission services to know exactly which
|
|
/// destinations to send data to.
|
|
Future<List<Map<String, dynamic>>> getEnabledApiConfigsForModule(String moduleName) async {
|
|
// 1. Check the master switch for the module.
|
|
final pref = await getModulePreference(moduleName);
|
|
if (!(pref['is_api_enabled'] as bool)) {
|
|
debugPrint("API submissions are disabled for module '$moduleName' via master switch.");
|
|
return []; // Return empty list if API is globally disabled for this module.
|
|
}
|
|
|
|
// 2. Get all configs with their preference flags.
|
|
final allConfigsWithPrefs = await getAllApiConfigsWithModulePreferences(moduleName);
|
|
|
|
// 3. Filter for only those that are enabled.
|
|
final enabledConfigs = allConfigsWithPrefs.where((config) => config['is_enabled'] == true).toList();
|
|
|
|
debugPrint("Found ${enabledConfigs.length} enabled API destinations for module '$moduleName'.");
|
|
return enabledConfigs;
|
|
}
|
|
|
|
/// Retrieves only the FTP configurations that are actively enabled for a given module.
|
|
Future<List<Map<String, dynamic>>> getEnabledFtpConfigsForModule(String moduleName) async {
|
|
final pref = await getModulePreference(moduleName);
|
|
if (!(pref['is_ftp_enabled'] as bool)) {
|
|
debugPrint("FTP submissions are disabled for module '$moduleName' via master switch.");
|
|
return [];
|
|
}
|
|
|
|
final allConfigsWithPrefs = await getAllFtpConfigsWithModulePreferences(moduleName);
|
|
final enabledConfigs = allConfigsWithPrefs.where((config) => config['is_enabled'] == true).toList();
|
|
|
|
debugPrint("Found ${enabledConfigs.length} enabled FTP destinations for module '$moduleName'.");
|
|
return enabledConfigs;
|
|
}
|
|
} |