diff --git a/lib/features/library/page/library_authors_page.dart b/lib/features/library/page/library_authors_page.dart index a375948..adf380b 100644 --- a/lib/features/library/page/library_authors_page.dart +++ b/lib/features/library/page/library_authors_page.dart @@ -31,15 +31,7 @@ class _LibraryAuthorsPageState extends ConsumerState { ) => valueOrNull.entries.isEmpty ? const FullscreenEmptyLibrarySliver() - : SliverList.builder( - itemBuilder: (context, index) { - final author = valueOrNull.keys.elementAt(index); - final tomes = valueOrNull.values.elementAt(index); - - return AuthorTomesSliver(author: author, tomes: tomes); - }, - itemCount: valueOrNull.entries.length, - ), + : authorTomesSliver(authorTomes: valueOrNull.entries), AsyncValue( :final error?, ) => diff --git a/lib/features/library/view/author_tomes_sliver.dart b/lib/features/library/view/author_tomes_sliver.dart index bc5ea69..393fe3e 100644 --- a/lib/features/library/view/author_tomes_sliver.dart +++ b/lib/features/library/view/author_tomes_sliver.dart @@ -4,34 +4,25 @@ import 'package:bsr/features/library/library.dart'; import 'package:bsr/features/library/view/view.dart'; import 'package:flutter/material.dart'; -class AuthorTomesSliver extends StatelessWidget { - const AuthorTomesSliver({ - required this.author, - required this.tomes, - super.key, - }); +SliverList authorTomesSliver({ + required Iterable>> + authorTomes, +}) { + final entries = authorTomes.fold( + [], + (previousValue, element) => [ + ...previousValue, + element.key, + ...element.value.values, + ], + ); - final String author; - final LinkedHashMap tomes; - - @override - Widget build(BuildContext context) { - return SliverList.list( - children: [ - SliverToBoxAdapter( - child: AuthorListTile( - author: author, - ), - ), - SliverList.builder( - itemBuilder: (context, index) => SliverToBoxAdapter( - child: TomeListTile( - tome: tomes.values.elementAt(index), - ), - ), - itemCount: tomes.length, - ), - ], - ); - } + return SliverList.builder( + itemBuilder: (context, index) => switch (entries[index]) { + final String author => AuthorListTile(author: author), + final CachedTome tome => TomeListTile(tome: tome), + _ => throw Exception('Unexpected type'), + }, + itemCount: entries.length, + ); }