55 lines
2.1 KiB
Dart
55 lines
2.1 KiB
Dart
// lib/services/server_config_service.dart
|
|
|
|
import 'dart:convert';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class ServerConfigService {
|
|
static const String _activeApiConfigKey = 'active_api_config';
|
|
static const String _activeFtpConfigKey = 'active_ftp_config';
|
|
// A default URL to prevent crashes if no config is set.
|
|
static const String _defaultApiUrl = 'https://dev14.pstw.com.my/v1';
|
|
|
|
// --- API Config Methods ---
|
|
|
|
/// Saves the selected API configuration as the currently active one.
|
|
Future<void> setActiveApiConfig(Map<String, dynamic> config) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(_activeApiConfigKey, jsonEncode(config));
|
|
}
|
|
|
|
/// Retrieves the currently active API configuration from local storage.
|
|
Future<Map<String, dynamic>?> getActiveApiConfig() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final String? configString = prefs.getString(_activeApiConfigKey);
|
|
if (configString != null) {
|
|
return jsonDecode(configString) as Map<String, dynamic>;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Gets the API Base URL from the active configuration.
|
|
/// Falls back to a default URL if none is set.
|
|
Future<String> getActiveApiUrl() async {
|
|
final config = await getActiveApiConfig();
|
|
// The key from the database is 'api_url'.
|
|
return config?['api_url'] as String? ?? _defaultApiUrl;
|
|
}
|
|
|
|
// --- FTP Config Methods ---
|
|
|
|
/// Saves the selected FTP configuration as the currently active one.
|
|
Future<void> setActiveFtpConfig(Map<String, dynamic> config) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(_activeFtpConfigKey, jsonEncode(config));
|
|
}
|
|
|
|
/// Retrieves the currently active FTP configuration from local storage.
|
|
Future<Map<String, dynamic>?> getActiveFtpConfig() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final String? configString = prefs.getString(_activeFtpConfigKey);
|
|
if (configString != null) {
|
|
return jsonDecode(configString) as Map<String, dynamic>;
|
|
}
|
|
return null;
|
|
}
|
|
} |