Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ppupha committed May 7, 2024
1 parent 61d3563 commit ec9a065
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 61 deletions.
82 changes: 74 additions & 8 deletions lib/screen/detail/preview/canvas_device_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -222,7 +225,7 @@ class CanvasDeviceBloc extends AuBloc<CanvasDeviceEvent, CanvasDeviceState> {
isConnecting: true,
playingSceneId: ''),
duration: Duration(seconds: 10),
isPlaying: true,
isPlaying: false,
),
DeviceState(
device: CanvasDevice(
Expand All @@ -233,7 +236,7 @@ class CanvasDeviceBloc extends AuBloc<CanvasDeviceEvent, CanvasDeviceState> {
isConnecting: false,
playingSceneId: ''),
duration: Duration(seconds: 10),
isPlaying: true,
isPlaying: false,
),
DeviceState(
device: CanvasDevice(
Expand All @@ -244,7 +247,7 @@ class CanvasDeviceBloc extends AuBloc<CanvasDeviceEvent, CanvasDeviceState> {
isConnecting: false,
playingSceneId: ''),
duration: Duration(seconds: 10),
isPlaying: true,
isPlaying: false,
),
DeviceState(
device: CanvasDevice(
Expand All @@ -255,7 +258,7 @@ class CanvasDeviceBloc extends AuBloc<CanvasDeviceEvent, CanvasDeviceState> {
isConnecting: false,
playingSceneId: ''),
duration: Duration(seconds: 10),
isPlaying: true,
isPlaying: false,
),
],
isLoaded: true),
Expand Down Expand Up @@ -366,9 +369,15 @@ class CanvasDeviceBloc extends AuBloc<CanvasDeviceEvent, CanvasDeviceState> {
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(
Expand All @@ -379,18 +388,75 @@ class CanvasDeviceBloc extends AuBloc<CanvasDeviceEvent, CanvasDeviceState> {
on<CanvasDeviceCancelCastingEvent>((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<CanvasDeviceNextArtworkEvent>((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<CanvasDevicePreviousArtworkEvent>((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<CanvasDevicePauseCastingEvent>((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<CanvasDeviceResumeCastingEvent>((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 (_) {}
});
}
Expand Down
5 changes: 2 additions & 3 deletions lib/screen/playlists/view_playlist/view_playlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,10 @@ class _ViewPlaylistScreenState extends State<ViewPlaylistScreen> {
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));
Expand Down
120 changes: 70 additions & 50 deletions lib/view/stream_common_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,26 @@ class _PlaylistControlState extends State<PlaylistControl> {
}

@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<CanvasDeviceBloc, CanvasDeviceState>(
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(
Expand All @@ -98,43 +105,48 @@ class _PlaylistControlState extends State<PlaylistControl> {
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;
Expand Down Expand Up @@ -178,7 +190,16 @@ class _PlaylistControlState extends State<PlaylistControl> {
}
}

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,
Expand Down Expand Up @@ -224,17 +245,16 @@ class _PlaylistControlState extends State<PlaylistControl> {
setState(() {
_currentSliderValue = value;
});
final controllingDeviceIds =
_canvasDeviceBloc.state.controllingDeviceIds;
final controllingDeviceIds = state.controllingDeviceIds;
if (controllingDeviceIds.isEmpty) {
return;
}
_timer?.cancel();
_timer = Timer(
const Duration(seconds: 300),
() {
// final listPlayArtwork = _canvasDeviceBloc.state.;
// _canvasDeviceBloc.add(CanvasDeviceUpdateDurationEvent(device, artwork))
changeSpeed(speedValues[
speedTitles[_currentSliderValue.round()]]!);
},
);
},
Expand Down

0 comments on commit ec9a065

Please sign in to comment.