From ec9a06569616d26ae8f6b0effdc2fdf627c8b1d5 Mon Sep 17 00:00:00 2001 From: PuPha Date: Tue, 7 May 2024 08:29:35 +0700 Subject: [PATCH] Update --- .../detail/preview/canvas_device_bloc.dart | 82 ++++++++++-- .../view_playlist/view_playlist.dart | 5 +- lib/view/stream_common_widget.dart | 120 ++++++++++-------- 3 files changed, 146 insertions(+), 61 deletions(-) diff --git a/lib/screen/detail/preview/canvas_device_bloc.dart b/lib/screen/detail/preview/canvas_device_bloc.dart index d79af2aa4..6e7c767f2 100644 --- a/lib/screen/detail/preview/canvas_device_bloc.dart +++ b/lib/screen/detail/preview/canvas_device_bloc.dart @@ -154,7 +154,10 @@ class CanvasDeviceState { .firstWhereOrNull((deviceState) => deviceState.device.isConnecting) ?.device; - bool get isCasting => connectingDevice != null; + bool get isCasting { + return devices.any((element) => + element.device == controllingDevice && element.isPlaying == true); + } } class DeviceState { @@ -222,7 +225,7 @@ class CanvasDeviceBloc extends AuBloc { isConnecting: true, playingSceneId: ''), duration: Duration(seconds: 10), - isPlaying: true, + isPlaying: false, ), DeviceState( device: CanvasDevice( @@ -233,7 +236,7 @@ class CanvasDeviceBloc extends AuBloc { isConnecting: false, playingSceneId: ''), duration: Duration(seconds: 10), - isPlaying: true, + isPlaying: false, ), DeviceState( device: CanvasDevice( @@ -244,7 +247,7 @@ class CanvasDeviceBloc extends AuBloc { isConnecting: false, playingSceneId: ''), duration: Duration(seconds: 10), - isPlaying: true, + isPlaying: false, ), DeviceState( device: CanvasDevice( @@ -255,7 +258,7 @@ class CanvasDeviceBloc extends AuBloc { isConnecting: false, playingSceneId: ''), duration: Duration(seconds: 10), - isPlaying: true, + isPlaying: false, ), ], isLoaded: true), @@ -366,9 +369,15 @@ class CanvasDeviceBloc extends AuBloc { if (!ok) { throw Exception('Failed to cast to device'); } + final currentDeviceState = state.devices + .firstWhereOrNull((element) => element.device.id == device.id); + if (currentDeviceState == null) { + throw Exception('Device not found'); + } emit(state .replaceDeviceState( - device: device, deviceState: DeviceState(device: device)) + device: device, + deviceState: currentDeviceState.copyWith(isPlaying: true)) .copyWith(controllingDeviceIds: [device.id])); } catch (_) { emit(state.replaceDeviceState( @@ -379,18 +388,75 @@ class CanvasDeviceBloc extends AuBloc { on((event, emit) async { final device = event.device; try { + final currentDeviceState = state.devices + .firstWhereOrNull((element) => element.device.id == device.id); + if (currentDeviceState == null) { + throw Exception('Device not found'); + } await _canvasClientServiceV2.cancelCasting(device); emit(state.replaceDeviceState( - device: device, deviceState: DeviceState(device: device))); + device: device, + deviceState: currentDeviceState.copyWith(isPlaying: false))); } catch (_) {} }); on((event, emit) async { final device = event.device; try { + final currentDeviceState = state.devices + .firstWhereOrNull((element) => element.device.id == device.id); + if (currentDeviceState == null) { + throw Exception('Device not found'); + } await _canvasClientServiceV2.nextArtwork(device); emit(state.replaceDeviceState( - device: device, deviceState: DeviceState(device: device))); + device: device, + deviceState: currentDeviceState.copyWith(isPlaying: true))); + } catch (_) {} + }); + + on((event, emit) async { + final device = event.device; + try { + final currentDeviceState = state.devices + .firstWhereOrNull((element) => element.device.id == device.id); + if (currentDeviceState == null) { + throw Exception('Device not found'); + } + await _canvasClientServiceV2.previousArtwork(device); + emit(state.replaceDeviceState( + device: device, + deviceState: currentDeviceState.copyWith(isPlaying: true))); + } catch (_) {} + }); + + on((event, emit) async { + final device = event.device; + try { + final currentDeviceState = state.devices + .firstWhereOrNull((element) => element.device.id == device.id); + if (currentDeviceState == null) { + throw Exception('Device not found'); + } + await _canvasClientServiceV2.pauseCasting(device); + emit(state.replaceDeviceState( + device: device, + deviceState: currentDeviceState.copyWith(isPlaying: false))); + } catch (_) {} + }); + + on((event, emit) async { + final device = event.device; + try { + final currentDeviceState = state.devices + .firstWhereOrNull((element) => element.device.id == device.id); + if (currentDeviceState == null) { + throw Exception('Device not found'); + } + await _canvasClientServiceV2.resumeCasting(device); + emit(state.replaceDeviceState( + device: device, + deviceState: currentDeviceState.copyWith(isPlaying: true))); } catch (_) {} }); } diff --git a/lib/screen/playlists/view_playlist/view_playlist.dart b/lib/screen/playlists/view_playlist/view_playlist.dart index ce2420117..6d84951a1 100644 --- a/lib/screen/playlists/view_playlist/view_playlist.dart +++ b/lib/screen/playlists/view_playlist/view_playlist.dart @@ -389,11 +389,10 @@ class _ViewPlaylistScreenState extends State { log.info('Playlist tokenIds is null'); return; } - final durationInSecond = 30; + final duration = Duration(seconds: 10).inMilliseconds; final listPlayArtwork = listTokenIds .map((e) => PlayArtworkV2( - token: CastAssetToken(id: e), - duration: durationInSecond)) + token: CastAssetToken(id: e), duration: duration)) .toList(); _canvasDeviceBloc.add(CanvasDeviceCastListArtworkEvent( device, listPlayArtwork)); diff --git a/lib/view/stream_common_widget.dart b/lib/view/stream_common_widget.dart index 0f26eb786..b9881871d 100644 --- a/lib/view/stream_common_widget.dart +++ b/lib/view/stream_common_widget.dart @@ -73,19 +73,26 @@ class _PlaylistControlState extends State { } @override - Widget build(BuildContext context) => Container( - padding: const EdgeInsets.all(15), - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(10), - color: AppColor.primaryBlack, - ), - child: Column( - children: [ - _buildPlayControls(context), - const SizedBox(height: 15), - _buildSpeedControl(context), - ], - )); + Widget build(BuildContext context) => + BlocConsumer( + bloc: _canvasDeviceBloc, + listener: (context, state) {}, + builder: (context, state) { + return Container( + padding: const EdgeInsets.all(15), + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(10), + color: AppColor.primaryBlack, + ), + child: Column( + children: [ + _buildPlayControls(context, state), + const SizedBox(height: 15), + _buildSpeedControl(context, state), + ], + )); + }, + ); Widget _buildPlayButton({required String icon, required Function() onTap}) => Expanded( @@ -98,43 +105,48 @@ class _PlaylistControlState extends State { color: AppColor.auGreyBackground, ), child: SvgPicture.asset( - 'assets/images/$icon.svg', + icon, ), ), ), ); - Widget _buildPlayControls(BuildContext context) => Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - 'play_collection'.tr(), - style: Theme.of(context).textTheme.ppMori400White12, - ), - const SizedBox(height: 4), - Row( - children: [ - _buildPlayButton( - icon: 'chevron_left_icon', - onTap: () => { - onPrevious(context), - }), - const SizedBox(width: 15), - _buildPlayButton( - icon: 'stream_play_icon', - onTap: () => { - onPauseOrResume(context), - }), - const SizedBox(width: 15), - _buildPlayButton( - icon: 'chevron_right_icon', - onTap: () => { - onNext(context), - }), - ], - ) - ], - ); + Widget _buildPlayControls(BuildContext context, CanvasDeviceState state) { + final isCasting = state.isCasting; + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + 'play_collection'.tr(), + style: Theme.of(context).textTheme.ppMori400White12, + ), + const SizedBox(height: 4), + Row( + children: [ + _buildPlayButton( + icon: 'assets/images/chevron_left_icon.svg', + onTap: () => { + onPrevious(context), + }), + const SizedBox(width: 15), + _buildPlayButton( + icon: isCasting + ? 'assets/images/stream_pause_icon.svg' + : 'assets/images/stream_play_icon.svg', + onTap: () => { + onPauseOrResume(context), + }), + const SizedBox(width: 15), + _buildPlayButton( + icon: 'assets/images/chevron_right_icon.svg', + onTap: () => { + onNext(context), + }), + ], + ) + ], + ); + } void onPrevious(BuildContext context) { final controllingDevice = _canvasDeviceBloc.state.controllingDevice; @@ -178,7 +190,16 @@ class _PlaylistControlState extends State { } } - Widget _buildSpeedControl(BuildContext context) { + void changeSpeed(Duration duration) { + final controllingDevice = _canvasDeviceBloc.state.controllingDevice; + if (controllingDevice == null) { + return; + } + _canvasDeviceBloc + .add(CanvasDeviceUpdateDurationEvent(controllingDevice, [])); + } + + Widget _buildSpeedControl(BuildContext context, CanvasDeviceState state) { final speedTitles = speedValues.keys.toList(); return Column( crossAxisAlignment: CrossAxisAlignment.start, @@ -224,8 +245,7 @@ class _PlaylistControlState extends State { setState(() { _currentSliderValue = value; }); - final controllingDeviceIds = - _canvasDeviceBloc.state.controllingDeviceIds; + final controllingDeviceIds = state.controllingDeviceIds; if (controllingDeviceIds.isEmpty) { return; } @@ -233,8 +253,8 @@ class _PlaylistControlState extends State { _timer = Timer( const Duration(seconds: 300), () { - // final listPlayArtwork = _canvasDeviceBloc.state.; - // _canvasDeviceBloc.add(CanvasDeviceUpdateDurationEvent(device, artwork)) + changeSpeed(speedValues[ + speedTitles[_currentSliderValue.round()]]!); }, ); },