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);
}
void disconnect() {
_gCastService.disconnect();
Future<void> disconnect() async {
await _gCastService.disconnect();
}
}

View File

@ -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();
}

View File

@ -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<String, dynamic> message) {
print("Received cast message: $message");
switch (message['type']) {
case "MEDIA_STATUS":
_handleMediaStatus(message);
@ -143,8 +146,9 @@ class GCastService implements ICastDestinationService {
@override
Future<void> 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(

View File

@ -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);
},
);