Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 85 additions & 68 deletions lib/data/repositories/multi_server_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class MultiServerRepository {
static const _defaultSortOrder = 'Ascending';
static const _genreArtworkConcurrency = 6;

static String? _sortByParameter(LibrarySortBy? sortBy) =>
sortBy == null ? _defaultSortBy : sortBy.sortByParameter;

List<ServerUserSession>? _cachedSessions;
DateTime _cacheExpiry = DateTime(0);
final Map<String, int> _rowOffsets = {};
Expand Down Expand Up @@ -277,8 +280,8 @@ class MultiServerRepository {

Future<HomeRow> getAggregatedPlaylists({
int limit = _defaultLimit,
String sortBy = _defaultSortBy,
String sortOrder = _defaultSortOrder,
String? sortBy = _defaultSortBy,
String? sortOrder = _defaultSortOrder,
String? mediaType,
}) async {
final sessions = await getLoggedInServers();
Expand Down Expand Up @@ -314,14 +317,20 @@ class MultiServerRepository {
);

final all = results.expand((e) => e).toList();
if (sortBy == 'SortName') {
if (sortBy == null) {
// Keep the server-provided order.
} else if (sortBy == 'SortName') {
if (sortOrder == 'Ascending') {
all.sort((a, b) => a.name.compareTo(b.name));
} else {
all.sort((a, b) => b.name.compareTo(a.name));
}
} else {
_sortAggregatedItems(all, sortBy: sortBy, sortOrder: sortOrder);
_sortAggregatedItems(
all,
sortBy: sortBy,
sortOrder: sortOrder ?? _defaultSortOrder,
);
}

final takenItems = all.take(limit).toList();
Expand All @@ -341,8 +350,8 @@ class MultiServerRepository {

Future<HomeRow> getAggregatedAudioArtists({
int limit = _defaultLimit,
String sortBy = _defaultSortBy,
String sortOrder = _defaultSortOrder,
String? sortBy = _defaultSortBy,
String? sortOrder = _defaultSortOrder,
}) async {
final row = await _getAggregatedSortedItemsRow(
id: 'audioArtists',
Expand All @@ -359,8 +368,8 @@ class MultiServerRepository {

Future<HomeRow> getAggregatedAudioAlbums({
int limit = _defaultLimit,
String sortBy = _defaultSortBy,
String sortOrder = _defaultSortOrder,
String? sortBy = _defaultSortBy,
String? sortOrder = _defaultSortOrder,
}) async {
final row = await _getAggregatedSortedItemsRow(
id: 'audioAlbums',
Expand All @@ -377,8 +386,8 @@ class MultiServerRepository {

Future<HomeRow> getAggregatedAudioPlaylists({
int limit = _defaultLimit,
String sortBy = _defaultSortBy,
String sortOrder = _defaultSortOrder,
String? sortBy = _defaultSortBy,
String? sortOrder = _defaultSortOrder,
}) async {
return getAggregatedPlaylists(
limit: limit,
Expand All @@ -394,8 +403,8 @@ class MultiServerRepository {
required String title,
List<String>? includeItemTypes,
int limit = _defaultLimit,
String sortBy = _defaultSortBy,
String sortOrder = _defaultSortOrder,
String? sortBy = _defaultSortBy,
String? sortOrder = _defaultSortOrder,
}) async {
return _getAggregatedSortedItemsRow(
id: rowId,
Expand All @@ -412,8 +421,8 @@ class MultiServerRepository {

Future<HomeRow> getAggregatedCollections({
int limit = _defaultLimit,
String sortBy = _defaultSortBy,
String sortOrder = _defaultSortOrder,
String? sortBy = _defaultSortBy,
String? sortOrder = _defaultSortOrder,
}) async {
return _getAggregatedSortedItemsRow(
id: 'collections',
Expand All @@ -429,8 +438,8 @@ class MultiServerRepository {

Future<HomeRow> getAggregatedGenres({
int limit = _defaultLimit,
String sortBy = _defaultSortBy,
String sortOrder = _defaultSortOrder,
String? sortBy = _defaultSortBy,
String? sortOrder = _defaultSortOrder,
List<String>? includeItemTypes,
}) async {
const cacheKeyPrefix = 'genres';
Expand Down Expand Up @@ -463,11 +472,14 @@ class MultiServerRepository {
),
);

final all = _sortAggregatedItems(
results.expand((e) => e).toList(growable: false),
sortBy: sortBy,
sortOrder: sortOrder,
);
final combined = results.expand((e) => e).toList(growable: false);
final all = sortBy == null
? combined
: _sortAggregatedItems(
combined,
sortBy: sortBy,
sortOrder: sortOrder ?? _defaultSortOrder,
);

final takenItems = all.take(limit).toList();
final totalCount = sessions.fold<int>(0, (sum, session) {
Expand Down Expand Up @@ -517,9 +529,9 @@ class MultiServerRepository {
final pageCount = (startIndex / _defaultLimit).ceil();
final targetStartIndex = pageCount * _defaultLimit;
_rowOffsets[cacheKey] = targetStartIndex + _defaultLimit;
final sortBy =
prefs?.get(UserPreferences.playlistsRowSortBy).apiValue ??
_defaultSortBy;
final sortBy = _sortByParameter(
prefs?.get(UserPreferences.playlistsRowSortBy),
);

final response = await session.client.itemsApi.getItems(
includeItemTypes: const ['Playlist'],
Expand All @@ -543,9 +555,9 @@ class MultiServerRepository {
final pageCount = (startIndex / _defaultLimit).ceil();
final targetStartIndex = pageCount * _defaultLimit;
_rowOffsets[cacheKey] = targetStartIndex + _defaultLimit;
final sortBy =
prefs?.get(UserPreferences.audioRowsSortBy).apiValue ??
_defaultSortBy;
final sortBy = _sortByParameter(
prefs?.get(UserPreferences.audioRowsSortBy),
);

final response = await session.client.itemsApi.getItems(
includeItemTypes: const ['Playlist'],
Expand All @@ -568,9 +580,9 @@ class MultiServerRepository {
return items;
case HomeRowType.audioArtists:
_rowOffsets[cacheKey] = startIndex + _defaultLimit;
final sortBy =
prefs?.get(UserPreferences.audioRowsSortBy).apiValue ??
_defaultSortBy;
final sortBy = _sortByParameter(
prefs?.get(UserPreferences.audioRowsSortBy),
);

final response = await session.client.itemsApi.getItems(
includeItemTypes: const ['MusicArtist'],
Expand All @@ -589,9 +601,9 @@ class MultiServerRepository {
return items;
case HomeRowType.audioAlbums:
_rowOffsets[cacheKey] = startIndex + _defaultLimit;
final sortBy =
prefs?.get(UserPreferences.audioRowsSortBy).apiValue ??
_defaultSortBy;
final sortBy = _sortByParameter(
prefs?.get(UserPreferences.audioRowsSortBy),
);

final response = await session.client.itemsApi.getItems(
includeItemTypes: const ['MusicAlbum'],
Expand All @@ -610,9 +622,9 @@ class MultiServerRepository {
return items;
case HomeRowType.favorites:
final favoriteFilter = FavoriteTypeFilter.fromRowId(row.id);
final sortBy =
prefs?.get(UserPreferences.favoritesRowSortBy).apiValue ??
_defaultSortBy;
final sortBy = _sortByParameter(
prefs?.get(UserPreferences.favoritesRowSortBy),
);
_rowOffsets[cacheKey] = startIndex + _defaultLimit;

final response = await session.client.itemsApi.getItems(
Expand All @@ -632,15 +644,16 @@ class MultiServerRepository {
response['TotalRecordCount'] as int? ?? items.length;
return items;
case HomeRowType.collections:
final sortBy =
prefs?.get(UserPreferences.collectionsRowSortBy).apiValue ??
_defaultSortBy;
final sortBy = _sortByParameter(
prefs?.get(UserPreferences.collectionsRowSortBy),
);
final sortOrder = sortBy != null ? 'Ascending' : null;
_rowOffsets[cacheKey] = startIndex + _defaultLimit;

final response = await session.client.itemsApi.getItems(
includeItemTypes: const ['BoxSet'],
sortBy: sortBy,
sortOrder: 'Ascending',
sortOrder: sortOrder,
recursive: true,
startIndex: startIndex,
limit: _defaultLimit,
Expand All @@ -653,9 +666,9 @@ class MultiServerRepository {
response['TotalRecordCount'] as int? ?? items.length;
return items;
case HomeRowType.genres:
final sortBy =
prefs?.get(UserPreferences.genresRowSortBy).apiValue ??
_defaultSortBy;
final sortBy = _sortByParameter(
prefs?.get(UserPreferences.genresRowSortBy),
);
final includeItemTypes = prefs
?.get(UserPreferences.genresRowItemFilter)
.includeItemTypes;
Expand Down Expand Up @@ -762,10 +775,14 @@ class MultiServerRepository {
row.rowType == HomeRowType.latestMedia) {
if (row.rowType == HomeRowType.playlists ||
row.rowType == HomeRowType.audioPlaylists) {
final sortBy = row.rowType == HomeRowType.audioPlaylists
? (prefs?.get(UserPreferences.audioRowsSortBy).apiValue ?? _defaultSortBy)
: (prefs?.get(UserPreferences.playlistsRowSortBy).apiValue ?? _defaultSortBy);
if (sortBy == 'SortName') {
final sortBy = _sortByParameter(
row.rowType == HomeRowType.audioPlaylists
? prefs?.get(UserPreferences.audioRowsSortBy)
: prefs?.get(UserPreferences.playlistsRowSortBy),
);
if (sortBy == null) {
// Keep the server-provided order.
} else if (sortBy == 'SortName') {
uniqueCombined.sort((a, b) => a.name.compareTo(b.name));
} else {
_sortAggregatedItems(
Expand All @@ -779,26 +796,24 @@ class MultiServerRepository {
} else {
final sortBy = switch (row.rowType) {
HomeRowType.favorites =>
prefs?.get(UserPreferences.favoritesRowSortBy).apiValue ??
_defaultSortBy,
_sortByParameter(prefs?.get(UserPreferences.favoritesRowSortBy)),
HomeRowType.collections =>
prefs?.get(UserPreferences.collectionsRowSortBy).apiValue ??
_defaultSortBy,
_sortByParameter(prefs?.get(UserPreferences.collectionsRowSortBy)),
HomeRowType.genres =>
prefs?.get(UserPreferences.genresRowSortBy).apiValue ??
_defaultSortBy,
_sortByParameter(prefs?.get(UserPreferences.genresRowSortBy)),
HomeRowType.audioArtists ||
HomeRowType.audioAlbums =>
prefs?.get(UserPreferences.audioRowsSortBy).apiValue ??
_defaultSortBy,
_sortByParameter(prefs?.get(UserPreferences.audioRowsSortBy)),
_ => _defaultSortBy,
};

sortedCombined = _sortAggregatedItems(
uniqueCombined,
sortBy: sortBy,
sortOrder: 'Ascending',
);
sortedCombined = sortBy == null
? uniqueCombined
: _sortAggregatedItems(
uniqueCombined,
sortBy: sortBy,
sortOrder: 'Ascending',
);
}

final totalCount = sessions.fold<int>(0, (sum, session) {
Expand All @@ -816,8 +831,8 @@ class MultiServerRepository {
List<String>? includeItemTypes,
bool? isFavorite,
int limit = _defaultLimit,
String sortBy = _defaultSortBy,
String sortOrder = _defaultSortOrder,
String? sortBy = _defaultSortBy,
String? sortOrder = _defaultSortOrder,
}) async {
final sessions = await getLoggedInServers();
final perServer = (limit * 3).clamp(1, 100);
Expand All @@ -844,12 +859,14 @@ class MultiServerRepository {
),
);

final all = _sortAggregatedItems(
results.expand((e) => e).toList(growable: false),
sortBy: sortBy,
sortOrder: sortOrder,
);

final combined = results.expand((e) => e).toList(growable: false);
final all = sortBy == null
? combined
: _sortAggregatedItems(
combined,
sortBy: sortBy,
sortOrder: sortOrder ?? _defaultSortOrder,
);
final takenItems = all.take(limit).toList();
final totalCount = sessions.fold<int>(0, (sum, session) {
return sum + (_rowTotals['${id}_${session.server.id}'] ?? 0);
Expand Down
Loading
Loading