Skip to content

Commit

Permalink
Moved to batched queries in queue item lookup.
Browse files Browse the repository at this point in the history
Improved song count display on queue restore screen.
  • Loading branch information
Komodo5197 committed Dec 9, 2023
1 parent 69589c0 commit 1536516
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 23 deletions.
6 changes: 2 additions & 4 deletions lib/components/QueueRestoreScreen/queue_restore_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class QueueRestoreTile extends StatelessWidget {
Widget build(BuildContext context) {
final queuesBox = Hive.box<FinampStorableQueueInfo>("Queues");
final queueService = GetIt.instance<QueueService>();
int itemCount = info.queue.length +
info.nextUp.length +
((info.currentTrack == null) ? 0 : 1);
int remainingSongs = info.songCount - info.previousTracks.length;
Future<BaseItemDto?> track = (info.currentTrack == null)
? Future.value(null)
: queueService.getTrackFromId(info.currentTrack!);
Expand Down Expand Up @@ -50,7 +48,7 @@ class QueueRestoreTile extends StatelessWidget {
]) +
[
Text(AppLocalizations.of(context)!
.queueRestoreSubtitle2(itemCount))
.queueRestoreSubtitle2(info.songCount,remainingSongs))
])),
trailing: IconButton(
icon: const Icon(Icons.arrow_circle_right_outlined),
Expand Down
7 changes: 5 additions & 2 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@
}
}
},
"queueRestoreSubtitle1": "{song, select, finamp_null{Could not load track.} other{Playing: {song}}}",
"queueRestoreSubtitle1": "Playing: {song}",
"@queueRestoreSubtitle1": {
"description": "Description of playing song in a saved queue",
"placeholders": {
Expand All @@ -630,12 +630,15 @@
}
}
},
"queueRestoreSubtitle2": "{count,plural,=1{1 Song} other{{count} Songs}}",
"queueRestoreSubtitle2": "{count,plural,=1{1 Song} other{{count} Songs}}, {remaining} Unplayed",
"@queueRestoreSubtitle2": {
"description": "Description of length of a saved queue",
"placeholders": {
"count": {
"type": "int"
},
"remaining": {
"type": "int"
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions lib/services/jellyfin_api.chopper.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/services/jellyfin_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ abstract class JellyfinApi extends ChopperService {
/// containing the specified album id.
@Query("AlbumIds") String? albumIds,

@Query("ids") String? ids,

/// When searching within folders, this determines whether or not the search
/// will be recursive. true/false.
@Query("Recursive") bool? recursive,
Expand Down
4 changes: 3 additions & 1 deletion lib/services/jellyfin_api_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class JellyfinApiHelper {
String? sortBy,
String? sortOrder,
String? searchTerm,
required bool isGenres,
List<String>? itemIds,
bool? isGenres, // Unused?
String? filters,

/// The record index to start at. All items with a lower index will be
Expand Down Expand Up @@ -124,6 +125,7 @@ class JellyfinApiHelper {
filters: filters,
startIndex: startIndex,
limit: limit,
ids: itemIds?.join(","),
);
}

Expand Down
29 changes: 13 additions & 16 deletions lib/services/queue_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class QueueService {
SavedQueueState _savedQueueState = SavedQueueState.preInit;
FinampStorableQueueInfo? _failedSavedQueue = null;
static const int _maxSavedQueues = 60;
static const int _savedQueueWorkers = 10;

QueueService() {
// _queueServiceLogger.level = Level.OFF;
Expand Down Expand Up @@ -315,26 +314,24 @@ class QueueService {
refreshQueueStream();

List<String> allIds = info.previousTracks + ( (info.currentTrack == null) ? []:[info.currentTrack!] ) + info.nextUp + info.queue;
Iterator<String> idIterator = allIds.iterator;
List<Future<void>> idFutures = [];
List<String> uniqueIds = allIds.toSet().toList();
Map<String,jellyfin_models.BaseItemDto> idMap = {};

Future<void> worker() async {
while(idIterator.moveNext() && _savedQueueState == SavedQueueState.loading){
String id = idIterator.current;
if ( ! idMap.containsKey(id)){
jellyfin_models.BaseItemDto? item = await getTrackFromId(id);
if(item != null){
idMap[id]=item;
}
if (FinampSettingsHelper.finampSettings.isOffline) {
for (var id in uniqueIds) {
jellyfin_models.BaseItemDto? item = _downloadsHelper.getDownloadedSong(id)?.song;
if(item!=null){
idMap[id]=item;
}
}
} else {
for (int i=0; i<uniqueIds.length; i+=200) {
List<jellyfin_models.BaseItemDto> itemList = await _jellyfinApiHelper.getItems(itemIds: uniqueIds.sublist(i,min(i+200,allIds.length))) ?? [];
for (var d2 in itemList){
idMap[d2.id] = d2;
}
}
}
// Limit parallel item lookups to 10 to reduce interface lag and server load
for ( int i=0; i<_savedQueueWorkers; i++ ){
idFutures.add(worker());
}
await Future.wait(idFutures);

Map<String, List<jellyfin_models.BaseItemDto>> items = {
"previous": [for (var i in info.previousTracks) if ( idMap.containsKey(i) ) idMap[i]!],
Expand Down

0 comments on commit 1536516

Please sign in to comment.