Skip to content

Commit

Permalink
Fix QueueList out of index, fix error when last queue item is dismissed
Browse files Browse the repository at this point in the history
  • Loading branch information
jmshrv committed Aug 6, 2021
1 parent 5f9f9bf commit c8f7672
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
33 changes: 21 additions & 12 deletions lib/components/PlayerScreen/QueueList.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,27 @@ class QueueList extends StatefulWidget {
}

class _QueueListState extends State<QueueList> {
Stream<_QueueListStreamState> _queueListStream =
Rx.combineLatest2<List<MediaItem>?, dynamic, _QueueListStreamState>(
AudioService.queueStream,
// We turn this future into a stream because using rxdart is
// easier than having nested StreamBuilders/FutureBuilders
AudioService.customAction("getShuffleIndices").asStream(),
(a, b) => _QueueListStreamState(queue: a, shuffleIndicies: b));

List<MediaItem>? _queue;

@override
Widget build(BuildContext context) {
return StreamBuilder<_QueueListStreamState>(
// stream: AudioService.queueStream,
stream:
Rx.combineLatest2<List<MediaItem>?, dynamic, _QueueListStreamState>(
AudioService.queueStream,
// We turn this future into a stream because using rxdart is
// easier than having nested StreamBuilders/FutureBuilders
AudioService.customAction("getShuffleIndices").asStream(),
(a, b) => _QueueListStreamState(queue: a, shuffleIndicies: b)),
stream: _queueListStream,
builder: (context, snapshot) {
connectIfDisconnected();
if (snapshot.hasData) {
if (_queue == null) {
_queue = snapshot.data!.queue;
}
return ListView.builder(
controller: widget.scrollController,
itemCount: snapshot.data!.queue?.length ?? 0,
Expand All @@ -49,15 +56,17 @@ class _QueueListState extends State<QueueList> {
? snapshot.data!.shuffleIndicies![index]
: index;
return Dismissible(
onDismissed: (direction) {
snapshot.data?.queue?.removeAt(actualIndex);
AudioService.customAction("removeQueueItem", actualIndex);
onDismissed: (direction) async {
setState(() {
_queue?.removeAt(actualIndex);
});
await AudioService.customAction(
"removeQueueItem", actualIndex);
},
key: Key(snapshot.data!.queue![actualIndex].id),
child: ListTile(
leading: AlbumImage(
itemId:
snapshot.data?.queue?[actualIndex].extras?["parentId"],
itemId: _queue?[actualIndex].extras?["parentId"],
),
title: Text(
snapshot.data!.queue?[actualIndex].title ??
Expand Down
22 changes: 17 additions & 5 deletions lib/services/MusicPlayerBackgroundTask.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ class MusicPlayerBackgroundTask extends BackgroundAudioTask {

// Tell Jellyfin we're no longer playing audio if we're online
if (!FinampSettingsHelper.finampSettings.isOffline) {
jellyfinApiData.stopPlaybackProgress(_generatePlaybackProgressInfo());
final playbackInfo = _generatePlaybackProgressInfo();
if (playbackInfo != null) {
jellyfinApiData.stopPlaybackProgress(playbackInfo);
}
}

// Stop playing audio.
Expand Down Expand Up @@ -393,8 +396,10 @@ class MusicPlayerBackgroundTask extends BackgroundAudioTask {
DateTime.now().millisecondsSinceEpoch -
_lastUpdateTime!.millisecondsSinceEpoch >=
10000) {
await jellyfinApiData
.updatePlaybackProgress(_generatePlaybackProgressInfo());
final playbackInfo = _generatePlaybackProgressInfo();
if (playbackInfo != null) {
await jellyfinApiData.updatePlaybackProgress(playbackInfo);
}

// if updatePlaybackProgress fails, the last update time won't be set.
_lastUpdateTime = DateTime.now();
Expand Down Expand Up @@ -507,8 +512,15 @@ class MusicPlayerBackgroundTask extends BackgroundAudioTask {
}
}

/// Generates PlaybackProgressInfo from current player info
PlaybackProgressInfo _generatePlaybackProgressInfo() {
/// Generates PlaybackProgressInfo from current player info. Returns null if
/// _queue is empty.
PlaybackProgressInfo? _generatePlaybackProgressInfo() {
if (_queue.length == 0) {
// This function relies on _queue having items, so we return null if it's
// empty to avoid more errors.
return null;
}

try {
return PlaybackProgressInfo(
itemId: _queue[_player.currentIndex ?? 0].extras!["itemId"],
Expand Down

0 comments on commit c8f7672

Please sign in to comment.