Files
plezy/lib/services/server_registry.dart
T

38 lines
1.3 KiB
Dart
Raw Normal View History

2025-11-21 23:59:39 +01:00
import 'dart:convert';
import '../utils/app_logger.dart';
import 'plex_auth_service.dart';
import 'storage_service.dart';
2026-04-26 02:34:09 +02:00
/// Per-Plex-account servers list, persisted as JSON in [StorageService].
///
/// Historically this was the single source of truth for "the user's Plex
/// servers". The new pipeline stores servers on
/// [PlexAccountConnection.servers] in [ConnectionRegistry] instead, so the
/// only remaining responsibility here is reading the legacy list during
/// the one-shot bootstrap migration in [ConnectionBootstrap].
2025-11-21 23:59:39 +01:00
class ServerRegistry {
final StorageService _storage;
ServerRegistry(this._storage);
2026-04-26 02:34:09 +02:00
/// Read the legacy servers list from storage. Returns an empty list when
/// there is no legacy data (post-migration installs and fresh installs).
///
/// Called only by [ConnectionBootstrap.migrateLegacyPlexAccount].
2025-11-21 23:59:39 +01:00
Future<List<PlexServer>> getServers() async {
try {
final serversJson = _storage.getServersListJson();
if (serversJson == null || serversJson.isEmpty) {
return [];
}
final List<dynamic> serversList = jsonDecode(serversJson);
2025-12-15 02:29:03 +01:00
return serversList.map((json) => PlexServer.fromJson(json as Map<String, dynamic>)).toList();
2025-11-21 23:59:39 +01:00
} catch (e, stackTrace) {
2025-12-15 02:29:03 +01:00
appLogger.e('Failed to load servers from storage', error: e, stackTrace: stackTrace);
2025-11-21 23:59:39 +01:00
return [];
}
}
}