From 066324bd1abedb3ebab8a3086eb627acce2e2064 Mon Sep 17 00:00:00 2001 From: gianlucaparadise Date: Tue, 4 Jan 2022 19:02:01 +0100 Subject: [PATCH] ExpandedControlsAdInfoControls handling --- lib/src/cast/RemoteMediaClient.dart | 3 +- .../expanded_controls/ExpandedControls.dart | 41 ++------ .../ExpandedControlsAdInfoControls.dart | 98 +++++++++++++++++++ 3 files changed, 109 insertions(+), 33 deletions(-) create mode 100644 lib/src/cast/widgets/expanded_controls/ExpandedControlsAdInfoControls.dart diff --git a/lib/src/cast/RemoteMediaClient.dart b/lib/src/cast/RemoteMediaClient.dart index 580ce08..aef2113 100644 --- a/lib/src/cast/RemoteMediaClient.dart +++ b/lib/src/cast/RemoteMediaClient.dart @@ -12,6 +12,7 @@ typedef AdBreakClipProgressListener = void Function( int durationMs, int whenSkippableMs, ); +typedef MediaStatusListener = void Function(MediaStatus mediaStatus); /// Class for controlling a media player application running on a receiver. class RemoteMediaClient { @@ -40,7 +41,7 @@ class RemoteMediaClient { VoidCallback? onSendingRemoteMediaRequest; /// Called when updated ad break status information is received. - VoidCallback? onAdBreakStatusUpdated; + MediaStatusListener? onAdBreakStatusUpdated; /// Called when receiving media error message. VoidCallback? onMediaError; diff --git a/lib/src/cast/widgets/expanded_controls/ExpandedControls.dart b/lib/src/cast/widgets/expanded_controls/ExpandedControls.dart index 1800dcc..2cec65a 100644 --- a/lib/src/cast/widgets/expanded_controls/ExpandedControls.dart +++ b/lib/src/cast/widgets/expanded_controls/ExpandedControls.dart @@ -1,10 +1,9 @@ import 'package:flutter/material.dart'; -import 'package:flutter_cast_framework/src/cast/widgets/expanded_controls/ExpandedControlsAdSkipBox.dart'; import '../../../../cast.dart'; import 'ExpandedControlsConnectedDeviceLabel.dart'; -import 'ExpandedControlsHighlightedText.dart'; -import 'ExpandedControlsInfoTextBox.dart'; +import 'ExpandedControlsAdInfoControls.dart'; +import 'ExpandedControlsAdSkipBox.dart'; import 'ExpandedControlsPlayer.dart'; import 'ExpandedControlsProgress.dart'; import 'ExpandedControlsToolbar.dart'; @@ -137,11 +136,6 @@ class _ExpandedControlsState extends State { .updateProgress(progressMs, durationMs, whenSkippableMs); } - void _onSkipAd() { - final sessionManager = widget.castFramework.castContext.sessionManager; - sessionManager.remoteMediaClient.skipAd(); - } - Widget _getDecoratedToolbar(MediaInfo? mediaInfo) { // Title and subtitle can't be retrieved at the moment // final title = mediaInfo?.mediaMetadata?.strings[MediaMetadataKey.title] @@ -208,29 +202,6 @@ class _ExpandedControlsState extends State { ); } - List _getAdInfoBox() { - return [ - const Spacer(flex: 2), - const ExpandedControlsHighlightedText( - text: "Ad Title", // TODO: retrieve ad title from API - ), - const SizedBox(height: 8), - Expanded( - flex: 8, - child: ExpandedControlsInfoTextBox( - text: widget.adInfoBoxText, - ), - ), - const Spacer(flex: 1), - ExpandedControlsAdSkipBox( - controller: widget.adSkipBoxController, - skipAdButtonText: widget.skipAdButtonText, - skipAdTimerText: widget.skipAdTimerText, - onSkipPressed: _onSkipAd, - ), - ]; - } - Widget _getFullControls(BuildContext context, MediaInfo? mediaInfo) { return Container( decoration: BoxDecoration( @@ -240,7 +211,13 @@ class _ExpandedControlsState extends State { child: Column( children: [ _getDecoratedToolbar(mediaInfo), - ..._getAdInfoBox(), // TODO: check if is playing ad, otherwise -> Spacer(), + ExpandedControlsAdInfoControls( + adSkipBoxController: widget.adSkipBoxController, + castFramework: widget.castFramework, + adInfoBoxText: widget.adInfoBoxText, + skipAdButtonText: widget.skipAdButtonText, + skipAdTimerText: widget.skipAdTimerText, + ), _getDecoratedControls(context, mediaInfo), ], ), diff --git a/lib/src/cast/widgets/expanded_controls/ExpandedControlsAdInfoControls.dart b/lib/src/cast/widgets/expanded_controls/ExpandedControlsAdInfoControls.dart new file mode 100644 index 0000000..e137700 --- /dev/null +++ b/lib/src/cast/widgets/expanded_controls/ExpandedControlsAdInfoControls.dart @@ -0,0 +1,98 @@ +import 'package:flutter/widgets.dart'; + +import '../../../../cast.dart'; +import 'ExpandedControlsAdSkipBox.dart'; +import 'ExpandedControlsHighlightedText.dart'; +import 'ExpandedControlsInfoTextBox.dart'; + +class ExpandedControlsAdInfoControls extends StatefulWidget { + final FlutterCastFramework castFramework; + + final ExpandedControlsAdSkipBoxController adSkipBoxController; + final String adInfoBoxText; + final String? skipAdButtonText; + final String? skipAdTimerText; + + const ExpandedControlsAdInfoControls({ + Key? key, + required this.castFramework, + required this.adSkipBoxController, + this.adInfoBoxText = "", + this.skipAdButtonText, + this.skipAdTimerText, + }) : super(key: key); + + @override + _ExpandedControlsAdInfoBoxState createState() => + _ExpandedControlsAdInfoBoxState(); +} + +class _ExpandedControlsAdInfoBoxState + extends State { + bool isPlayingAd = false; + + @override + void initState() { + final sessionManager = widget.castFramework.castContext.sessionManager; + sessionManager.remoteMediaClient.onAdBreakStatusUpdated = + _onAdBreakStatusUpdated; + + super.initState(); + } + + @override + void dispose() { + final sessionManager = widget.castFramework.castContext.sessionManager; + sessionManager.remoteMediaClient.onAdBreakStatusUpdated = null; + + super.dispose(); + } + + void _onAdBreakStatusUpdated(MediaStatus mediaStatus) { + if (!mounted || mediaStatus.isPlayingAd == null) return; + + if (mediaStatus.isPlayingAd != this.isPlayingAd) { + setState(() { + if (!mounted) return; + this.isPlayingAd = mediaStatus.isPlayingAd ?? false; + }); + } + } + + void _onSkipAd() { + final sessionManager = widget.castFramework.castContext.sessionManager; + sessionManager.remoteMediaClient.skipAd(); + } + + @override + Widget build(BuildContext context) { + if (!isPlayingAd) { + return Spacer(); + } + + return Expanded( + child: Column( + children: [ + const Spacer(flex: 2), + const ExpandedControlsHighlightedText( + text: "Ad Title", // TODO: retrieve ad title from API + ), + const SizedBox(height: 8), + Expanded( + flex: 8, + child: ExpandedControlsInfoTextBox( + text: widget.adInfoBoxText, + ), + ), + const Spacer(flex: 1), + ExpandedControlsAdSkipBox( + controller: widget.adSkipBoxController, + skipAdButtonText: widget.skipAdButtonText, + skipAdTimerText: widget.skipAdTimerText, + onSkipPressed: _onSkipAd, + ), + ], + ), + ); + } +}