Files
plezy/lib/providers/multi_server_provider.dart
T

157 lines
5.3 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import '../models/livetv_dvr.dart';
import '../mixins/disposable_change_notifier_mixin.dart';
import '../services/plex_client.dart';
import '../services/data_aggregation_service.dart';
import '../services/multi_server_manager.dart';
import '../services/plex_auth_service.dart';
import '../utils/app_logger.dart';
/// Cached info about a DVR-enabled server
class LiveTvServerInfo {
final String serverId;
final String dvrKey;
final String? lineup;
/// Full DVR objects including channel mappings (avoids re-fetching in LiveTvScreen)
final List<LiveTvDvr> dvrs;
LiveTvServerInfo({required this.serverId, required this.dvrKey, this.lineup, this.dvrs = const []});
}
/// Provider for multi-server Plex connections
/// Manages multiple PlexClient instances and provides data aggregation
class MultiServerProvider extends ChangeNotifier with DisposableChangeNotifierMixin {
final MultiServerManager _serverManager;
final DataAggregationService _aggregationService;
StreamSubscription? _statusSubscription;
/// Whether any connected server has Live TV / DVR
bool _hasLiveTv = false;
bool get hasLiveTv => _hasLiveTv;
/// Info about servers with DVR capability
final List<LiveTvServerInfo> _liveTvServers = [];
List<LiveTvServerInfo> get liveTvServers => List.unmodifiable(_liveTvServers);
/// Previously-seen set of online server IDs, used to detect new servers
Set<String> _previousOnlineServerIds = {};
MultiServerProvider(this._serverManager, this._aggregationService) {
// Listen to server status changes
_statusSubscription = _serverManager.statusStream.listen((_) {
final currentOnline = Set<String>.from(onlineServerIds);
final hasNewServer = currentOnline.any((id) => !_previousOnlineServerIds.contains(id));
_previousOnlineServerIds = currentOnline;
safeNotifyListeners();
// Only re-check live TV when a new server came online
if (hasNewServer) {
checkLiveTvAvailability();
}
});
}
/// Get the multi-server manager
MultiServerManager get serverManager => _serverManager;
/// Get the data aggregation service
DataAggregationService get aggregationService => _aggregationService;
/// Get client for specific server
PlexClient? getClientForServer(String serverId) {
return _serverManager.getClient(serverId);
}
/// Get all online server IDs
List<String> get onlineServerIds => _serverManager.onlineServerIds;
/// Get all server IDs
List<String> get serverIds => _serverManager.serverIds;
/// Check if a server is online
bool isServerOnline(String serverId) {
return _serverManager.isServerOnline(serverId);
}
/// Get number of online servers
int get onlineServerCount => _serverManager.onlineServerIds.length;
/// Get number of total servers
int get totalServerCount => _serverManager.serverIds.length;
/// Check if any servers are connected
bool get hasConnectedServers => onlineServerCount > 0;
/// Clear all server connections
void clearAllConnections() {
_serverManager.disconnectAll();
appLogger.d('MultiServerProvider: All connections cleared');
safeNotifyListeners();
}
/// Reconnect all servers after a profile switch
/// Clears existing connections and connects to all provided servers
Future<int> reconnectWithServers(List<PlexServer> servers, {String? clientIdentifier}) async {
// Clear existing connections first
_serverManager.disconnectAll();
appLogger.d('MultiServerProvider: Cleared connections, reconnecting to ${servers.length} servers');
// Connect with new server tokens
final connectedCount = await _serverManager.connectToAllServers(servers, clientIdentifier: clientIdentifier);
appLogger.i('MultiServerProvider: Reconnected to $connectedCount/${servers.length} servers after profile switch');
safeNotifyListeners();
return connectedCount;
}
/// Check server health for all connected servers
Future<void> checkServerHealth() async {
await _serverManager.checkServerHealth();
// notifyListeners() will be called automatically via status stream
}
/// Check all online servers for DVR/Live TV availability
Future<void> checkLiveTvAvailability() async {
final newLiveTvServers = <LiveTvServerInfo>[];
for (final serverId in onlineServerIds) {
final client = getClientForServer(serverId);
if (client == null) continue;
try {
final dvrs = await client.getDvrs();
for (final dvr in dvrs) {
newLiveTvServers.add(LiveTvServerInfo(serverId: serverId, dvrKey: dvr.key, lineup: dvr.lineup, dvrs: dvrs));
}
} catch (e) {
appLogger.d('LiveTV check failed for server $serverId', error: e);
}
}
final hadLiveTv = _hasLiveTv;
final oldServerIds = _liveTvServers.map((s) => s.serverId).toSet();
final newServerIds = newLiveTvServers.map((s) => s.serverId).toSet();
_liveTvServers
..clear()
..addAll(newLiveTvServers);
_hasLiveTv = newLiveTvServers.isNotEmpty;
// Notify when availability changes OR when the server set changes
if (hadLiveTv != _hasLiveTv || !oldServerIds.containsAll(newServerIds) || !newServerIds.containsAll(oldServerIds)) {
safeNotifyListeners();
}
}
@override
void dispose() {
_statusSubscription?.cancel();
_serverManager.dispose();
super.dispose();
}
}