Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stop splitting series names that contain a comma into separate series on the author page

Barnabascbd7cd6a8ed6
diff --git a/lib/services/api_service.dart b/lib/services/api_service.dart
index aac9f6cc..56f212b6 100644
--- a/lib/services/api_service.dart
+++ b/lib/services/api_service.dart
@@ -1579,7 +1579,7 @@ class ApiService {
/// Get full author details including description/bio.
Future<Map<String, dynamic>?> getAuthorById(String authorId, {String? libraryId}) async {
try {
- var url = '$_cleanBaseUrl/api/authors/$authorId?include=items';
+ var url = '$_cleanBaseUrl/api/authors/$authorId?include=items,series';
if (libraryId != null) url += '&library=$libraryId';
final response = await _authGet(
Uri.parse(url),
diff --git a/lib/widgets/author_books_sheet.dart b/lib/widgets/author_books_sheet.dart
index a64688f7..f2d3b228 100644
--- a/lib/widgets/author_books_sheet.dart
+++ b/lib/widgets/author_books_sheet.dart
@@ -61,6 +61,7 @@ class AuthorBooksSheet extends StatefulWidget {
class _AuthorBooksSheetState extends State<AuthorBooksSheet> {
List<Map<String, dynamic>> _books = [];
+ List<Map<String, dynamic>> _seriesGroups = [];
bool _isLoading = true;
String? _description;
String? _imageUrl;
@@ -78,6 +79,19 @@ class _AuthorBooksSheetState extends State<AuthorBooksSheet> {
_loadAuthorAndBooks();
}
+ /// Grouped items carry their series as a single map; every other screen
+ /// reads the expanded item's list, so store it in that shape.
+ void _normalizeGroupedSeries() {
+ for (final group in _seriesGroups) {
+ final items = group['items'] as List<dynamic>? ?? [];
+ for (final item in items.whereType<Map<String, dynamic>>()) {
+ final metadata = (item['media'] as Map<String, dynamic>?)?['metadata'] as Map<String, dynamic>?;
+ final series = metadata?['series'];
+ if (series is Map<String, dynamic>) metadata!['series'] = [series];
+ }
+ }
+ }
+
Future<void> _loadViewSettings() async {
final grid = await PlayerSettings.getSheetGridView();
final collapse = await PlayerSettings.getSheetCollapseSeries();
@@ -110,9 +124,14 @@ class _AuthorBooksSheetState extends State<AuthorBooksSheet> {
} else {
_imageUrl = null;
}
- // libraryItems from the author endpoint include full metadata with series info
final rawItems = authorData['libraryItems'] as List<dynamic>? ?? [];
_books = rawItems.whereType<Map<String, dynamic>>().toList();
+ // Books grouped by series, each item carrying its id/name/sequence for
+ // that series. The flat list only has the joined `seriesName` string,
+ // which cannot be split back apart when a name contains a comma.
+ final rawSeries = authorData['series'] as List<dynamic>? ?? [];
+ _seriesGroups = rawSeries.whereType<Map<String, dynamic>>().toList();
+ _normalizeGroupedSeries();
registerBookCovers(context.read<LibraryProvider>(), _books);
}
_isLoading = false;
@@ -165,6 +184,48 @@ class _AuthorBooksSheetState extends State<AuthorBooksSheet> {
}
List<_BookSection> _buildSections(AppLocalizations l) {
+ if (_seriesGroups.isNotEmpty) {
+ final grouped = _buildSectionsFromGroups(l);
+ if (grouped != null) return grouped;
+ }
+ return _buildSectionsFromNames(l);
+ }
+
+ /// Sections built from the server's series grouping. Null when the grouping
+ /// came back in a shape we cannot read, so the caller can fall back.
+ List<_BookSection>? _buildSectionsFromGroups(AppLocalizations l) {
+ final sections = <_BookSection>[];
+ final grouped = <String>{};
+
+ for (final group in _seriesGroups) {
+ final name = (group['name'] as String? ?? '').trim();
+ final items = (group['items'] as List<dynamic>? ?? []).whereType<Map<String, dynamic>>().toList();
+ if (name.isEmpty || items.isEmpty) continue;
+ for (final item in items) {
+ final id = item['id'] as String? ?? '';
+ if (id.isNotEmpty) grouped.add(id);
+ }
+ sections.add(_BookSection(label: name, seriesId: group['id'] as String?, books: items));
+ }
+
+ if (sections.isEmpty) return null;
+
+ final standalones = [
+ for (final book in _books)
+ if (!grouped.contains(book['id'] as String? ?? '')) book,
+ ];
+
+ sections.sort((a, b) => a.label.toLowerCase().compareTo(b.label.toLowerCase()));
+ _sortByTitle(standalones);
+ if (standalones.isNotEmpty) {
+ sections.add(_BookSection(label: l.standalone, books: standalones, isStandalone: true));
+ }
+ return sections;
+ }
+
+ /// Fallback for servers that do not return the grouped series list: rebuild
+ /// the grouping from the joined `seriesName` string.
+ List<_BookSection> _buildSectionsFromNames(AppLocalizations l) {
final seriesMap = <String, _BookSection>{};
final standalones = <Map<String, dynamic>>[];
@@ -222,12 +283,7 @@ class _AuthorBooksSheetState extends State<AuthorBooksSheet> {
final seriesSections = seriesMap.values.toList()
..sort((a, b) => a.label.toLowerCase().compareTo(b.label.toLowerCase()));
- // Sort standalones alphabetically by title
- standalones.sort((a, b) {
- final tA = ((a['media'] as Map<String, dynamic>?)?['metadata'] as Map<String, dynamic>?)?['title'] as String? ?? '';
- final tB = ((b['media'] as Map<String, dynamic>?)?['metadata'] as Map<String, dynamic>?)?['title'] as String? ?? '';
- return tA.toLowerCase().compareTo(tB.toLowerCase());
- });
+ _sortByTitle(standalones);
final allSections = <_BookSection>[...seriesSections];
if (standalones.isNotEmpty) {
@@ -236,9 +292,26 @@ class _AuthorBooksSheetState extends State<AuthorBooksSheet> {
return allSections;
}
+ void _sortByTitle(List<Map<String, dynamic>> books) {
+ books.sort((a, b) {
+ final tA = ((a['media'] as Map<String, dynamic>?)?['metadata'] as Map<String, dynamic>?)?['title'] as String? ?? '';
+ final tB = ((b['media'] as Map<String, dynamic>?)?['metadata'] as Map<String, dynamic>?)?['title'] as String? ?? '';
+ return tA.toLowerCase().compareTo(tB.toLowerCase());
+ });
+ }
+
/// Get just the "#N" for a book within a specific series.
String? _sequenceFor(Map<String, dynamic> book, String seriesName) {
- final sn = ((book['media'] as Map<String, dynamic>?)?['metadata'] as Map<String, dynamic>?)?['seriesName'] as String? ?? '';
+ final metadata = (book['media'] as Map<String, dynamic>?)?['metadata'] as Map<String, dynamic>? ?? {};
+ final structured = metadata['series'];
+ if (structured is List) {
+ for (final entry in structured.whereType<Map<String, dynamic>>()) {
+ if ((entry['name'] as String? ?? '').toLowerCase() != seriesName.toLowerCase()) continue;
+ final seq = entry['sequence'];
+ return seq == null || seq.toString().trim().isEmpty ? null : '#${seq.toString().trim()}';
+ }
+ }
+ final sn = metadata['seriesName'] as String? ?? '';
final pattern = RegExp(r'#\s*([\d.]+)');
for (final entry in sn.split(',').map((e) => e.trim())) {
final name = entry.replaceFirst(RegExp(r'\s*#\s*[\d.]+$'), '').trim();