From 4cc5ae472afcbf8065801d49829c9d3a1f7e9c84 Mon Sep 17 00:00:00 2001 From: bwees Date: Thu, 29 May 2025 11:19:17 -0500 Subject: [PATCH] fix some disconnect command ordering issues and unawaited futures --- mobile/lib/providers/cast.provider.dart | 4 ++-- mobile/lib/repositories/gcast.repository.dart | 3 +++ mobile/lib/services/gcast.service.dart | 9 ++++++++- mobile/lib/widgets/asset_viewer/cast_dialog.dart | 6 ++++-- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/mobile/lib/providers/cast.provider.dart b/mobile/lib/providers/cast.provider.dart index 1bab1bef0f..5fa3482a7f 100644 --- a/mobile/lib/providers/cast.provider.dart +++ b/mobile/lib/providers/cast.provider.dart @@ -83,7 +83,7 @@ class CastNotifier extends StateNotifier { _gCastService.seekTo(position); } - void disconnect() { - _gCastService.disconnect(); + Future disconnect() async { + await _gCastService.disconnect(); } } diff --git a/mobile/lib/repositories/gcast.repository.dart b/mobile/lib/repositories/gcast.repository.dart index fbe950d680..11c149ab37 100644 --- a/mobile/lib/repositories/gcast.repository.dart +++ b/mobile/lib/repositories/gcast.repository.dart @@ -47,6 +47,9 @@ class GCastRepository { "sessionId": sessionID, }); + // wait 500ms to ensure the stop command is processed + await Future.delayed(const Duration(milliseconds: 500)); + await _castSession?.close(); } diff --git a/mobile/lib/services/gcast.service.dart b/mobile/lib/services/gcast.service.dart index 79db8d7316..321358a46f 100644 --- a/mobile/lib/services/gcast.service.dart +++ b/mobile/lib/services/gcast.service.dart @@ -56,16 +56,19 @@ class GCastService implements ICastDestinationService { } void _onCastStatusCallback(CastSessionState state) { + print("Cast session state changed: $state"); if (state == CastSessionState.connected) { onConnectionState?.call(true); isConnected = true; } else if (state == CastSessionState.closed) { onConnectionState?.call(false); isConnected = false; + onReceiverName?.call(""); } } void _onCastMessageCallback(Map message) { + print("Received cast message: $message"); switch (message['type']) { case "MEDIA_STATUS": _handleMediaStatus(message); @@ -143,8 +146,9 @@ class GCastService implements ICastDestinationService { @override Future disconnect() async { - await _gCastRepository.disconnect(); onReceiverName?.call(""); + currentAssetId = null; + await _gCastRepository.disconnect(); } bool isSessionValid() { @@ -166,6 +170,7 @@ class GCastService implements ICastDestinationService { @override void loadMedia(Asset asset, bool reload) async { + print("Loading media for asset: ${asset.remoteId}"); if (!isConnected) { return; } else if (asset.remoteId == null) { @@ -174,6 +179,8 @@ class GCastService implements ICastDestinationService { return; } + print("tried to send it!"); + // create a session key if (!isSessionValid()) { sessionKey = await _sessionsApiService.createSession( diff --git a/mobile/lib/widgets/asset_viewer/cast_dialog.dart b/mobile/lib/widgets/asset_viewer/cast_dialog.dart index 5ae27af1e3..1810fd9d94 100644 --- a/mobile/lib/widgets/asset_viewer/cast_dialog.dart +++ b/mobile/lib/widgets/asset_viewer/cast_dialog.dart @@ -110,11 +110,13 @@ class CastDialog extends ConsumerWidget { : isDeviceConnecting(deviceName) ? const CircularProgressIndicator() : null, - onTap: () { + onTap: () async { if (isDeviceConnecting(deviceName)) { return; } - ref.read(castProvider.notifier).disconnect(); + + await ref.read(castProvider.notifier).disconnect(); + ref.read(castProvider.notifier).connect(type, deviceObj); }, );