fix some disconnect command ordering issues and unawaited futures

This commit is contained in:
bwees 2025-05-29 11:19:17 -05:00
parent c52e03abfc
commit 4cc5ae472a
No known key found for this signature in database
4 changed files with 17 additions and 5 deletions

View File

@ -83,7 +83,7 @@ class CastNotifier extends StateNotifier<CastManagerState> {
_gCastService.seekTo(position); _gCastService.seekTo(position);
} }
void disconnect() { Future<void> disconnect() async {
_gCastService.disconnect(); await _gCastService.disconnect();
} }
} }

View File

@ -47,6 +47,9 @@ class GCastRepository {
"sessionId": sessionID, "sessionId": sessionID,
}); });
// wait 500ms to ensure the stop command is processed
await Future.delayed(const Duration(milliseconds: 500));
await _castSession?.close(); await _castSession?.close();
} }

View File

@ -56,16 +56,19 @@ class GCastService implements ICastDestinationService {
} }
void _onCastStatusCallback(CastSessionState state) { void _onCastStatusCallback(CastSessionState state) {
print("Cast session state changed: $state");
if (state == CastSessionState.connected) { if (state == CastSessionState.connected) {
onConnectionState?.call(true); onConnectionState?.call(true);
isConnected = true; isConnected = true;
} else if (state == CastSessionState.closed) { } else if (state == CastSessionState.closed) {
onConnectionState?.call(false); onConnectionState?.call(false);
isConnected = false; isConnected = false;
onReceiverName?.call("");
} }
} }
void _onCastMessageCallback(Map<String, dynamic> message) { void _onCastMessageCallback(Map<String, dynamic> message) {
print("Received cast message: $message");
switch (message['type']) { switch (message['type']) {
case "MEDIA_STATUS": case "MEDIA_STATUS":
_handleMediaStatus(message); _handleMediaStatus(message);
@ -143,8 +146,9 @@ class GCastService implements ICastDestinationService {
@override @override
Future<void> disconnect() async { Future<void> disconnect() async {
await _gCastRepository.disconnect();
onReceiverName?.call(""); onReceiverName?.call("");
currentAssetId = null;
await _gCastRepository.disconnect();
} }
bool isSessionValid() { bool isSessionValid() {
@ -166,6 +170,7 @@ class GCastService implements ICastDestinationService {
@override @override
void loadMedia(Asset asset, bool reload) async { void loadMedia(Asset asset, bool reload) async {
print("Loading media for asset: ${asset.remoteId}");
if (!isConnected) { if (!isConnected) {
return; return;
} else if (asset.remoteId == null) { } else if (asset.remoteId == null) {
@ -174,6 +179,8 @@ class GCastService implements ICastDestinationService {
return; return;
} }
print("tried to send it!");
// create a session key // create a session key
if (!isSessionValid()) { if (!isSessionValid()) {
sessionKey = await _sessionsApiService.createSession( sessionKey = await _sessionsApiService.createSession(

View File

@ -110,11 +110,13 @@ class CastDialog extends ConsumerWidget {
: isDeviceConnecting(deviceName) : isDeviceConnecting(deviceName)
? const CircularProgressIndicator() ? const CircularProgressIndicator()
: null, : null,
onTap: () { onTap: () async {
if (isDeviceConnecting(deviceName)) { if (isDeviceConnecting(deviceName)) {
return; return;
} }
ref.read(castProvider.notifier).disconnect();
await ref.read(castProvider.notifier).disconnect();
ref.read(castProvider.notifier).connect(type, deviceObj); ref.read(castProvider.notifier).connect(type, deviceObj);
}, },
); );