AdInfoControls: refactor for stream
This commit is contained in:
parent
137652ddd3
commit
778081927f
3 changed files with 55 additions and 49 deletions
|
|
@ -149,7 +149,8 @@ I used this project to test the capabilities of the following technologies:
|
||||||
## Roadmap
|
## Roadmap
|
||||||
|
|
||||||
* CC in Expanded Controls (iOS)
|
* CC in Expanded Controls (iOS)
|
||||||
* Handle Ad Break (ad skip, ad UI, ad countdown)
|
* Handle Ad Break (ad title)
|
||||||
* Handle progress seek
|
|
||||||
* Handle queue
|
* Handle queue
|
||||||
* Handle mini-player
|
* Handle mini-player
|
||||||
|
* Handle progress seek
|
||||||
|
* Understand if it is better to refactor using streams instead of listeners
|
||||||
|
|
@ -5,7 +5,7 @@ import 'ExpandedControlsAdSkipBox.dart';
|
||||||
import 'ExpandedControlsHighlightedText.dart';
|
import 'ExpandedControlsHighlightedText.dart';
|
||||||
import 'ExpandedControlsInfoTextBox.dart';
|
import 'ExpandedControlsInfoTextBox.dart';
|
||||||
|
|
||||||
class ExpandedControlsAdInfoControls extends StatefulWidget {
|
class ExpandedControlsAdInfoControls extends StatelessWidget {
|
||||||
final FlutterCastFramework castFramework;
|
final FlutterCastFramework castFramework;
|
||||||
|
|
||||||
final ExpandedControlsAdSkipBoxController adSkipBoxController;
|
final ExpandedControlsAdSkipBoxController adSkipBoxController;
|
||||||
|
|
@ -22,51 +22,20 @@ class ExpandedControlsAdInfoControls extends StatefulWidget {
|
||||||
this.skipAdTimerText,
|
this.skipAdTimerText,
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
|
|
||||||
@override
|
|
||||||
_ExpandedControlsAdInfoBoxState createState() =>
|
|
||||||
_ExpandedControlsAdInfoBoxState();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _ExpandedControlsAdInfoBoxState
|
|
||||||
extends State<ExpandedControlsAdInfoControls> {
|
|
||||||
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() {
|
void _onSkipAd() {
|
||||||
final sessionManager = widget.castFramework.castContext.sessionManager;
|
final sessionManager = castFramework.castContext.sessionManager;
|
||||||
sessionManager.remoteMediaClient.skipAd();
|
sessionManager.remoteMediaClient.skipAd();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
Widget _getAdControls(MediaStatus? mediaStatus) {
|
||||||
Widget build(BuildContext context) {
|
final adBreakStatus = mediaStatus?.adBreakStatus;
|
||||||
if (!isPlayingAd) {
|
final adBreakId = adBreakStatus?.adBreakId;
|
||||||
|
final adBreakClipId = adBreakStatus?.adBreakClipId;
|
||||||
|
|
||||||
|
final hasAdBreakInfo =
|
||||||
|
adBreakId?.isEmpty == false || adBreakClipId?.isEmpty == false;
|
||||||
|
|
||||||
|
if (mediaStatus?.isPlayingAd != true && !hasAdBreakInfo) {
|
||||||
return Spacer();
|
return Spacer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -81,18 +50,54 @@ class _ExpandedControlsAdInfoBoxState
|
||||||
Expanded(
|
Expanded(
|
||||||
flex: 8,
|
flex: 8,
|
||||||
child: ExpandedControlsInfoTextBox(
|
child: ExpandedControlsInfoTextBox(
|
||||||
text: widget.adInfoBoxText,
|
text: adInfoBoxText,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const Spacer(flex: 1),
|
const Spacer(flex: 1),
|
||||||
ExpandedControlsAdSkipBox(
|
ExpandedControlsAdSkipBox(
|
||||||
controller: widget.adSkipBoxController,
|
controller: adSkipBoxController,
|
||||||
skipAdButtonText: widget.skipAdButtonText,
|
skipAdButtonText: skipAdButtonText,
|
||||||
skipAdTimerText: widget.skipAdTimerText,
|
skipAdTimerText: skipAdTimerText,
|
||||||
onSkipPressed: _onSkipAd,
|
onSkipPressed: _onSkipAd,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final remoteMediaClient =
|
||||||
|
this.castFramework.castContext.sessionManager.remoteMediaClient;
|
||||||
|
|
||||||
|
return StreamBuilder<MediaStatus>(
|
||||||
|
stream:
|
||||||
|
remoteMediaClient.mediaStatusStream.distinct(didChangeAdBreakStatus),
|
||||||
|
builder: (BuildContext context, AsyncSnapshot<MediaStatus> snapshot) {
|
||||||
|
if (snapshot.hasData && snapshot.data?.mediaInfo != null) {
|
||||||
|
var mediaStatus = snapshot.data;
|
||||||
|
return _getAdControls(mediaStatus);
|
||||||
|
} else if (snapshot.hasError) {
|
||||||
|
return Spacer();
|
||||||
|
} else {
|
||||||
|
return Spacer();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool didChangeAdBreakStatus(MediaStatus oldStatus, MediaStatus newStatus) {
|
||||||
|
if (oldStatus.isPlayingAd != newStatus.isPlayingAd) return false;
|
||||||
|
|
||||||
|
final oldAdBreakStatus = oldStatus.adBreakStatus;
|
||||||
|
final newAdBreakStatus = newStatus.adBreakStatus;
|
||||||
|
|
||||||
|
if (oldAdBreakStatus?.adBreakClipId != newAdBreakStatus?.adBreakClipId ||
|
||||||
|
oldAdBreakStatus?.adBreakId != newAdBreakStatus?.adBreakId ||
|
||||||
|
oldAdBreakStatus?.whenSkippableMs != newAdBreakStatus?.whenSkippableMs) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ class _ExpandedControlsAdSkipBoxState extends State<ExpandedControlsAdSkipBox> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final canSkip = progress > whenSkippable;
|
final canSkip = progress >= whenSkippable;
|
||||||
if (canSkip) {
|
if (canSkip) {
|
||||||
return ExpandedControlsBasicButton(
|
return ExpandedControlsBasicButton(
|
||||||
text: widget.skipAdButtonText ?? widget._defaultSkipAdButtonText,
|
text: widget.skipAdButtonText ?? widget._defaultSkipAdButtonText,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue