Files
manager/lib/core/connections/connection_storage.dart
T
2026-07-27 14:42:23 +02:00

42 lines
1.3 KiB
Dart

import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'connection.dart';
const _connectionsKey = 'connections';
const _activeConnectionIdKey = 'active_connection_id';
/// Reads/writes the saved connection list and active-connection id to
/// [SharedPreferences] as a plain JSON-encoded array. Not a secret store —
/// the API has no auth, so there's nothing sensitive in a saved base URL.
class ConnectionStorage {
const ConnectionStorage(this._prefs);
final SharedPreferences _prefs;
List<Connection> readConnections() {
final raw = _prefs.getString(_connectionsKey);
if (raw == null || raw.isEmpty) return const [];
final decoded = jsonDecode(raw) as List<dynamic>;
return decoded
.map((e) => Connection.fromJson(e as Map<String, dynamic>))
.toList(growable: false);
}
Future<void> writeConnections(List<Connection> connections) async {
final encoded = jsonEncode(connections.map((c) => c.toJson()).toList());
await _prefs.setString(_connectionsKey, encoded);
}
String? readActiveConnectionId() => _prefs.getString(_activeConnectionIdKey);
Future<void> writeActiveConnectionId(String? id) async {
if (id == null) {
await _prefs.remove(_activeConnectionIdKey);
} else {
await _prefs.setString(_activeConnectionIdKey, id);
}
}
}