Skip to content

Commit

Permalink
ChangeDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
ppupha committed May 8, 2024
1 parent 4cfddf9 commit f5cfcd3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
34 changes: 22 additions & 12 deletions lib/screen/detail/preview/canvas_device_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ class CanvasDeviceCastListArtworkEvent extends CanvasDeviceEvent {
CanvasDeviceCastListArtworkEvent(this.device, this.artwork);
}

class CanvasDeviceChangeControllDeviceEvent extends CanvasDeviceEvent {
final CanvasDevice newDevice;
final List<PlayArtworkV2> artwork;

CanvasDeviceChangeControllDeviceEvent(this.newDevice, this.artwork);
}

class CanvasDeviceCancelCastingEvent extends CanvasDeviceEvent {
final CanvasDevice device;

Expand Down Expand Up @@ -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<DeviceState> devices;
final Map<String, CheckDeviceStatusReply>? controllingDeviceStatus;
Expand Down Expand Up @@ -241,7 +242,12 @@ class CanvasDeviceBloc extends AuBloc<CanvasDeviceEvent, CanvasDeviceState> {
}
} catch (_) {}
});
emit(newState.copyWith(controllingDeviceStatus: controllingDeviceStatus));

final Map<String, CheckDeviceStatusReply> firstControllingDevice = {
controllingDeviceStatus.keys.first: controllingDeviceStatus.values.first
};

emit(newState.copyWith(controllingDeviceStatus: firstControllingDevice));
});

on<CanvasDeviceAddEvent>((event, emit) async {
Expand Down Expand Up @@ -443,14 +449,18 @@ class CanvasDeviceBloc extends AuBloc<CanvasDeviceEvent, CanvasDeviceState> {
} catch (_) {}
});

on<CanvasDeviceChangeDeviceEvent>((event, emit) async {
final newCanvas = event.device;
on<CanvasDeviceChangeControllDeviceEvent>((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 (_) {}
});

Expand Down
4 changes: 2 additions & 2 deletions lib/screen/home/collection_home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class CollectionHomePageState extends State<CollectionHomePage>
void castToken(CanvasDevice device, String tokenId) {
final token = CastAssetToken(id: tokenId);
final playArtwork = PlayArtworkV2(token: token);
_canvasDeviceBloc.add(CanvasDeviceCastListArtworkEvent(
_canvasDeviceBloc.add(CanvasDeviceChangeControllDeviceEvent(
device,
[playArtwork],
));
Expand Down Expand Up @@ -474,7 +474,7 @@ class CollectionHomePageState extends State<CollectionHomePage>
PlayArtworkV2(token: CastAssetToken(id: id), duration: duration);
final device = _canvasDeviceBloc.state.controllingDevice;
if (device != null) {
_canvasDeviceBloc.add(CanvasDeviceCastListArtworkEvent(
_canvasDeviceBloc.add(CanvasDeviceChangeControllDeviceEvent(
device,
[playArtwork],
));
Expand Down
2 changes: 1 addition & 1 deletion lib/screen/playlists/view_playlist/view_playlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ class _ViewPlaylistScreenState extends State<ViewPlaylistScreen> {
.map((e) => PlayArtworkV2(
token: CastAssetToken(id: e), duration: duration))
.toList();
_canvasDeviceBloc.add(CanvasDeviceCastListArtworkEvent(
_canvasDeviceBloc.add(CanvasDeviceChangeControllDeviceEvent(
device, listPlayArtwork));
},
),
Expand Down

0 comments on commit f5cfcd3

Please sign in to comment.