freezer/lib/ui/player_bar.dart

319 lines
10 KiB
Dart
Raw Normal View History

2021-09-02 20:45:14 +00:00
import 'dart:async';
2020-06-23 19:23:12 +00:00
import 'package:audio_service/audio_service.dart';
import 'package:flutter/material.dart';
2021-11-01 16:41:25 +00:00
import 'package:freezer/api/definitions.dart';
import 'package:freezer/page_routes/fade.dart';
2020-06-23 19:23:12 +00:00
import 'package:freezer/settings.dart';
2021-07-02 16:28:59 +00:00
import 'package:freezer/translations.i18n.dart';
2020-06-23 19:23:12 +00:00
import '../api/player.dart';
import 'cached_image.dart';
import 'player_screen.dart';
2021-09-01 12:38:32 +00:00
class PlayerBar extends StatefulWidget {
final bool shouldHandleClicks;
2021-11-01 16:41:25 +00:00
final bool shouldHaveHero;
final Color? backgroundColor;
const PlayerBar({
Key? key,
this.shouldHandleClicks = true,
this.shouldHaveHero = true,
this.backgroundColor,
}) : super(key: key);
2021-09-01 12:38:32 +00:00
@override
_PlayerBarState createState() => _PlayerBarState();
}
class _PlayerBarState extends State<PlayerBar> {
final double iconSize = 28;
2021-09-02 20:45:14 +00:00
late StreamSubscription mediaItemSub;
late bool _isNothingPlaying = audioHandler.mediaItem.value == null;
2021-11-01 16:41:25 +00:00
final focusNode = FocusNode();
2021-09-01 12:38:32 +00:00
double parsePosition(Duration position) {
if (audioHandler.mediaItem.value == null) return 0.0;
if (audioHandler.mediaItem.value!.duration!.inSeconds == 0)
return 0.0; //Division by 0
2021-09-01 12:38:32 +00:00
return position.inSeconds /
audioHandler.mediaItem.value!.duration!.inSeconds;
2020-06-23 19:23:12 +00:00
}
2021-09-02 20:45:14 +00:00
@override
void initState() {
mediaItemSub = audioHandler.mediaItem.listen((playingItem) {
if ((playingItem == null && !_isNothingPlaying) ||
(playingItem != null && _isNothingPlaying))
setState(() => _isNothingPlaying = playingItem == null);
});
super.initState();
}
2021-11-01 16:41:25 +00:00
Color get backgroundColor =>
widget.backgroundColor ?? Theme.of(context).bottomAppBarColor;
2021-09-02 20:45:14 +00:00
@override
void dispose() {
2021-11-01 16:41:25 +00:00
focusNode.dispose();
2021-09-02 20:45:14 +00:00
mediaItemSub.cancel();
super.dispose();
}
bool _gestureRegistered = false;
2020-06-23 19:23:12 +00:00
@override
Widget build(BuildContext context) {
2021-09-02 20:45:14 +00:00
return _isNothingPlaying
? const SizedBox()
: GestureDetector(
onHorizontalDragUpdate: (details) async {
if (_gestureRegistered) return;
final double sensitivity = 12.69;
//Right swipe
_gestureRegistered = true;
if (details.delta.dx > sensitivity) {
await audioHandler.skipToPrevious();
}
//Left
if (details.delta.dx < -sensitivity) {
await audioHandler.skipToNext();
}
_gestureRegistered = false;
return;
},
child: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
StreamBuilder<MediaItem?>(
stream: audioHandler.mediaItem,
2021-11-01 16:41:25 +00:00
initialData: audioHandler.mediaItem.valueOrNull,
2021-09-02 20:45:14 +00:00
builder: (context, snapshot) {
if (!snapshot.hasData) return const SizedBox();
final currentMediaItem = snapshot.data!;
2021-11-01 16:41:25 +00:00
final image = CachedImage(
width: 50,
height: 50,
url: currentMediaItem.extras!['thumb'] ??
audioHandler.mediaItem.value!.artUri as String?,
);
final leadingWidget = widget.shouldHaveHero
? Hero(tag: currentMediaItem.id, child: image)
: image;
return Material(
2021-09-02 20:45:14 +00:00
// For Android TV: indicate focus by grey
2021-11-01 16:41:25 +00:00
color: focusNode.hasFocus
? Color.lerp(backgroundColor, Colors.grey, 0.26)
: backgroundColor,
2021-09-02 20:45:14 +00:00
child: ListTile(
dense: true,
focusNode: focusNode,
contentPadding:
EdgeInsets.symmetric(horizontal: 8.0),
onTap: widget.shouldHandleClicks
2021-11-01 16:41:25 +00:00
? _pushPlayerScreen
2021-09-02 20:45:14 +00:00
: null,
2021-11-01 16:41:25 +00:00
leading: AnimatedSwitcher(
duration: const Duration(milliseconds: 250),
child: leadingWidget),
2021-09-02 20:45:14 +00:00
title: Text(
currentMediaItem.displayTitle!,
overflow: TextOverflow.clip,
maxLines: 1,
),
subtitle: Text(
currentMediaItem.displaySubtitle ?? '',
overflow: TextOverflow.clip,
maxLines: 1,
),
trailing: IconTheme(
data: IconThemeData(
color: settings.isDark
? Colors.white
: Colors.grey[600]),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
PrevNextButton(
iconSize,
prev: true,
),
PlayPauseButton(iconSize),
PrevNextButton(iconSize)
],
),
)));
}),
SizedBox(
height: 3.0,
child: StreamBuilder<Duration>(
stream: AudioService.position,
builder: (context, snapshot) {
return LinearProgressIndicator(
value: parsePosition(snapshot.data ?? Duration.zero),
);
}),
),
]),
);
2020-06-23 19:23:12 +00:00
}
2021-11-01 16:41:25 +00:00
void _pushPlayerScreen() {
final builder = (BuildContext context) => PlayerScreen();
if (settings.blurPlayerBackground) {
Navigator.of(context).push(FadePageRoute(builder: builder));
return;
}
Navigator.of(context).pushRoute(builder: builder);
}
2020-06-23 19:23:12 +00:00
}
class PrevNextButton extends StatelessWidget {
final double size;
final bool prev;
final bool hidePrev;
PrevNextButton(this.size, {this.prev = false, this.hidePrev = false});
@override
Widget build(BuildContext context) {
2021-11-01 16:41:25 +00:00
return StreamBuilder<MediaItem?>(
stream: audioHandler.mediaItem,
2021-09-01 12:38:32 +00:00
builder: (context, snapshot) {
2021-02-09 20:14:14 +00:00
if (!prev) {
return IconButton(
icon: Icon(
Icons.skip_next,
semanticLabel: "Play next".i18n,
),
2021-02-09 20:14:14 +00:00
iconSize: size,
2021-08-29 22:25:18 +00:00
onPressed:
2021-11-01 16:41:25 +00:00
playerHelper.queueIndex == audioHandler.queue.value.length - 1
2021-08-29 22:25:18 +00:00
? null
2021-09-01 12:38:32 +00:00
: () => audioHandler.skipToNext(),
2021-02-09 20:14:14 +00:00
);
}
2021-09-01 12:38:32 +00:00
final canGoPrev = playerHelper.queueIndex > 0;
if (!canGoPrev && hidePrev)
return const SizedBox(width: 0.0, height: 0.0);
2021-08-29 22:25:18 +00:00
return IconButton(
icon: Icon(
Icons.skip_previous,
semanticLabel: "Play previous".i18n,
),
iconSize: size,
2021-09-01 12:38:32 +00:00
onPressed: canGoPrev ? () => audioHandler.skipToPrevious() : null,
2021-08-29 22:25:18 +00:00
);
2021-02-09 20:14:14 +00:00
},
);
2020-06-23 19:23:12 +00:00
}
}
class PlayPauseButton extends StatefulWidget {
final double size;
2021-11-01 16:41:25 +00:00
final bool filled;
final Color? iconColor;
/// The color of the card if [filled] is true
final Color? color;
const PlayPauseButton(
this.size, {
Key? key,
this.filled = false,
this.color,
this.iconColor,
}) : super(key: key);
2020-06-23 19:23:12 +00:00
@override
_PlayPauseButtonState createState() => _PlayPauseButtonState();
}
2020-06-23 19:23:12 +00:00
class _PlayPauseButtonState extends State<PlayPauseButton>
with SingleTickerProviderStateMixin {
2021-11-01 16:41:25 +00:00
late AnimationController _controller =
AnimationController(vsync: this, duration: Duration(milliseconds: 200));
late Animation<double> _animation =
CurvedAnimation(parent: _controller, curve: Curves.easeInOut);
late StreamSubscription _subscription;
late bool _canPlay = audioHandler.playbackState.value.playing ||
audioHandler.playbackState.value.processingState ==
AudioProcessingState.ready;
2020-06-23 19:23:12 +00:00
@override
void initState() {
2021-11-01 16:41:25 +00:00
_subscription = audioHandler.playbackState.listen((playbackState) {
if (playbackState.playing ||
playbackState.processingState == AudioProcessingState.ready) {
if (playbackState.playing)
_controller.forward();
else
_controller.reverse();
if (!_canPlay) setState(() => _canPlay = true);
return;
}
setState(() => _canPlay = false);
});
super.initState();
}
2020-06-23 19:23:12 +00:00
@override
void dispose() {
2021-11-01 16:41:25 +00:00
_subscription.cancel();
_controller.dispose();
super.dispose();
}
2020-06-23 19:23:12 +00:00
2021-11-01 16:41:25 +00:00
void _playPause() {
if (audioHandler.playbackState.value.playing)
audioHandler.pause();
else
audioHandler.play();
}
@override
Widget build(BuildContext context) {
2021-11-01 16:41:25 +00:00
final Widget? child;
if (_canPlay) {
final icon = AnimatedIcon(
icon: AnimatedIcons.play_pause,
progress: _animation,
semanticLabel: audioHandler.playbackState.value.playing
? 'Pause'.i18n
: 'Play'.i18n,
);
if (!widget.filled)
return IconButton(
color: widget.iconColor,
icon: icon,
iconSize: widget.size,
onPressed: _playPause);
child = InkWell(
borderRadius: BorderRadius.circular(50.0),
child: IconTheme.merge(
child: Center(child: icon),
data: IconThemeData(
size: widget.size / 2, color: widget.iconColor)),
onTap: _playPause);
} else
switch (audioHandler.playbackState.value.processingState) {
//Stopped/Error
case AudioProcessingState.error:
case AudioProcessingState.idle:
child = null;
break;
//Loading, connecting, rewinding...
default:
child = const Center(child: CircularProgressIndicator());
break;
}
if (widget.filled)
return SizedBox.square(
dimension: widget.size,
child: Card(
color: widget.color,
elevation: 2.0,
shape: CircleBorder(),
child: child));
else
return SizedBox.square(dimension: widget.size, child: child);
2020-06-23 19:23:12 +00:00
}
}