diff --git a/lib/screen/detail/preview/canvas_device_bloc.dart b/lib/screen/detail/preview/canvas_device_bloc.dart index 5519c098b..52f7fe3d8 100644 --- a/lib/screen/detail/preview/canvas_device_bloc.dart +++ b/lib/screen/detail/preview/canvas_device_bloc.dart @@ -70,6 +70,13 @@ class CanvasDeviceCastListArtworkEvent extends CanvasDeviceEvent { CanvasDeviceCastListArtworkEvent(this.device, this.artwork); } +class CanvasDeviceChangeControllDeviceEvent extends CanvasDeviceEvent { + final CanvasDevice newDevice; + final List artwork; + + CanvasDeviceChangeControllDeviceEvent(this.newDevice, this.artwork); +} + class CanvasDeviceCancelCastingEvent extends CanvasDeviceEvent { final CanvasDevice device; @@ -107,12 +114,6 @@ class CanvasDeviceUpdateDurationEvent extends CanvasDeviceEvent { CanvasDeviceUpdateDurationEvent(this.device, this.artwork); } -class CanvasDeviceChangeDeviceEvent extends CanvasDeviceEvent { - final CanvasDevice device; - - CanvasDeviceChangeDeviceEvent(this.device); -} - class CanvasDeviceState { final List devices; final Map? controllingDeviceStatus; @@ -241,7 +242,12 @@ class CanvasDeviceBloc extends AuBloc { } } catch (_) {} }); - emit(newState.copyWith(controllingDeviceStatus: controllingDeviceStatus)); + + final Map firstControllingDevice = { + controllingDeviceStatus.keys.first: controllingDeviceStatus.values.first + }; + + emit(newState.copyWith(controllingDeviceStatus: firstControllingDevice)); }); on((event, emit) async { @@ -443,14 +449,18 @@ class CanvasDeviceBloc extends AuBloc { } catch (_) {} }); - on((event, emit) async { - final newCanvas = event.device; + on((event, emit) async { + final newCanvas = event.newDevice; try { final canvasStatus = await _canvasClientServiceV2.getDeviceCastingStatus(newCanvas); - emit(state.copyWith( - controllingDeviceStatus: {newCanvas.id: canvasStatus}, - )); + if (canvasStatus.connectedDevice.deviceId == + _canvasClientServiceV2.clientDeviceInfo.deviceId) { + emit(state + .copyWith(controllingDeviceStatus: {newCanvas.id: canvasStatus})); + } else { + add(CanvasDeviceCastListArtworkEvent(newCanvas, event.artwork)); + } } catch (_) {} }); diff --git a/lib/screen/home/collection_home_page.dart b/lib/screen/home/collection_home_page.dart index 1a2cbadea..c9dea51b7 100644 --- a/lib/screen/home/collection_home_page.dart +++ b/lib/screen/home/collection_home_page.dart @@ -228,7 +228,7 @@ class CollectionHomePageState extends State void castToken(CanvasDevice device, String tokenId) { final token = CastAssetToken(id: tokenId); final playArtwork = PlayArtworkV2(token: token); - _canvasDeviceBloc.add(CanvasDeviceCastListArtworkEvent( + _canvasDeviceBloc.add(CanvasDeviceChangeControllDeviceEvent( device, [playArtwork], )); @@ -474,7 +474,7 @@ class CollectionHomePageState extends State PlayArtworkV2(token: CastAssetToken(id: id), duration: duration); final device = _canvasDeviceBloc.state.controllingDevice; if (device != null) { - _canvasDeviceBloc.add(CanvasDeviceCastListArtworkEvent( + _canvasDeviceBloc.add(CanvasDeviceChangeControllDeviceEvent( device, [playArtwork], )); diff --git a/lib/screen/playlists/view_playlist/view_playlist.dart b/lib/screen/playlists/view_playlist/view_playlist.dart index 4c83440c8..84a70d7d9 100644 --- a/lib/screen/playlists/view_playlist/view_playlist.dart +++ b/lib/screen/playlists/view_playlist/view_playlist.dart @@ -395,7 +395,7 @@ class _ViewPlaylistScreenState extends State { .map((e) => PlayArtworkV2( token: CastAssetToken(id: e), duration: duration)) .toList(); - _canvasDeviceBloc.add(CanvasDeviceCastListArtworkEvent( + _canvasDeviceBloc.add(CanvasDeviceChangeControllDeviceEvent( device, listPlayArtwork)); }, ),