From 17ea0f9747e5e363804514b6024bc017ac8faf06 Mon Sep 17 00:00:00 2001 From: mateusz-bak <32651935+mateusz-bak@users.noreply.github.com> Date: Sun, 26 Nov 2023 20:57:54 +0000 Subject: [PATCH] feat: Added counter for displayed books --- lib/ui/books_screen/books_screen.dart | 6 + lib/ui/books_screen/widgets/books_grid.dart | 144 +++++++++++------- lib/ui/books_screen/widgets/books_list.dart | 116 +++++++++----- lib/ui/trash_screen/trash_screen.dart | 1 + .../unfinished_screen/unfinished_screen.dart | 1 + 5 files changed, 177 insertions(+), 91 deletions(-) diff --git a/lib/ui/books_screen/books_screen.dart b/lib/ui/books_screen/books_screen.dart index 5c24ebfb..3aff8fbe 100644 --- a/lib/ui/books_screen/books_screen.dart +++ b/lib/ui/books_screen/books_screen.dart @@ -862,6 +862,7 @@ class _BooksScreenState extends State listNumber: 2, selectedBookIds: selectedBookIds, onBookSelected: _onItemSelected, + allBooksCount: snapshot.data!.length, ); } else { return BooksList( @@ -872,6 +873,7 @@ class _BooksScreenState extends State listNumber: 2, selectedBookIds: selectedBookIds, onBookSelected: _onItemSelected, + allBooksCount: snapshot.data!.length, ); } }, @@ -927,6 +929,7 @@ class _BooksScreenState extends State listNumber: 1, selectedBookIds: selectedBookIds, onBookSelected: _onItemSelected, + allBooksCount: snapshot.data!.length, ); } else { return BooksList( @@ -937,6 +940,7 @@ class _BooksScreenState extends State listNumber: 1, selectedBookIds: selectedBookIds, onBookSelected: _onItemSelected, + allBooksCount: snapshot.data!.length, ); } }, @@ -992,6 +996,7 @@ class _BooksScreenState extends State listNumber: 0, selectedBookIds: selectedBookIds, onBookSelected: _onItemSelected, + allBooksCount: snapshot.data!.length, ); } else { return BooksList( @@ -1002,6 +1007,7 @@ class _BooksScreenState extends State listNumber: 0, selectedBookIds: selectedBookIds, onBookSelected: _onItemSelected, + allBooksCount: snapshot.data!.length, ); } }, diff --git a/lib/ui/books_screen/widgets/books_grid.dart b/lib/ui/books_screen/widgets/books_grid.dart index 8f76bd7b..7fb2753a 100644 --- a/lib/ui/books_screen/widgets/books_grid.dart +++ b/lib/ui/books_screen/widgets/books_grid.dart @@ -7,17 +7,19 @@ import 'package:openreads/ui/books_screen/widgets/widgets.dart'; class BooksGrid extends StatefulWidget { const BooksGrid({ - Key? key, + super.key, required this.books, required this.listNumber, this.selectedBookIds, this.onBookSelected, - }) : super(key: key); + required this.allBooksCount, + }); final List books; final int listNumber; final Set? selectedBookIds; final Function(int id)? onBookSelected; + final int allBooksCount; @override State createState() => _BooksGridState(); @@ -25,62 +27,102 @@ class BooksGrid extends StatefulWidget { class _BooksGridState extends State with AutomaticKeepAliveClientMixin { + _onPressed(int index, bool multiSelectMode, String heroTag) { + if (widget.books[index].id == null) return; + if (multiSelectMode && widget.onBookSelected != null) { + widget.onBookSelected!(widget.books[index].id!); + return; + } + + context.read().setBook(widget.books[index]); + + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => BookScreen( + id: widget.books[index].id!, + heroTag: heroTag, + ), + ), + ); + } + + onLongPressed(int index) { + if (widget.books[index].id == null) return; + if (widget.onBookSelected != null) { + widget.onBookSelected!(widget.books[index].id!); + return; + } + } + @override Widget build(BuildContext context) { super.build(context); var multiSelectMode = widget.selectedBookIds?.isNotEmpty ?? false; - return GridView.builder( - gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( - crossAxisCount: 3, - childAspectRatio: 1 / 1.5, - mainAxisSpacing: 4, - crossAxisSpacing: 4, - ), - padding: const EdgeInsets.fromLTRB(8, 8, 8, 90), - itemCount: widget.books.length, - itemBuilder: (context, index) { - final heroTag = 'tag_${widget.listNumber}_${widget.books[index].id}'; - Color color = multiSelectMode && - widget.selectedBookIds!.contains(widget.books[index].id) - ? Theme.of(context).colorScheme.primaryContainer - : Colors.transparent; - return Container( - decoration: multiSelectMode - ? BoxDecoration(border: Border.all(color: color, width: 4)) - : null, - child: BookGridCard( - book: widget.books[index], - heroTag: heroTag, - addBottomPadding: (widget.books.length == index + 1), - onPressed: () { - if (widget.books[index].id == null) return; - if (multiSelectMode && widget.onBookSelected != null) { - widget.onBookSelected!(widget.books[index].id!); - return; - } - - context.read().setBook(widget.books[index]); + return CustomScrollView( + slivers: [ + SliverToBoxAdapter( + child: Padding( + padding: const EdgeInsets.fromLTRB(10, 0, 10, 10), + child: Row( + mainAxisAlignment: MainAxisAlignment.end, + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + Text('${widget.books.length} '), + Text( + '(${widget.allBooksCount})', + style: TextStyle( + color: Theme.of(context) + .colorScheme + .onBackground + .withOpacity(0.7), + fontSize: 13, + ), + ), + ], + ), + ), + ), + SliverPadding( + padding: const EdgeInsets.fromLTRB(8, 8, 8, 90), + sliver: SliverGrid.builder( + gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: 3, + childAspectRatio: 1 / 1.5, + mainAxisSpacing: 4, + crossAxisSpacing: 4, + ), + itemCount: widget.books.length, + itemBuilder: (context, index) { + final heroTag = + 'tag_${widget.listNumber}_${widget.books[index].id}'; + Color color = multiSelectMode && + widget.selectedBookIds!.contains(widget.books[index].id) + ? Theme.of(context).colorScheme.primaryContainer + : Colors.transparent; - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => BookScreen( - id: widget.books[index].id!, - heroTag: heroTag, + return Container( + decoration: multiSelectMode + ? BoxDecoration( + border: Border.all(color: color, width: 4), + ) + : null, + child: BookGridCard( + book: widget.books[index], + heroTag: heroTag, + addBottomPadding: (widget.books.length == index + 1), + onPressed: () => _onPressed( + index, + multiSelectMode, + heroTag, ), - ), - ); - }, - onLongPressed: () { - if (widget.books[index].id == null) return; - if (widget.onBookSelected != null) { - widget.onBookSelected!(widget.books[index].id!); - return; - } - }, - )); - }, + onLongPressed: () => onLongPressed(index), + )); + }, + ), + ), + ], ); } diff --git a/lib/ui/books_screen/widgets/books_list.dart b/lib/ui/books_screen/widgets/books_list.dart index fed3b334..3dd6f40b 100644 --- a/lib/ui/books_screen/widgets/books_list.dart +++ b/lib/ui/books_screen/widgets/books_list.dart @@ -7,17 +7,19 @@ import 'package:openreads/ui/books_screen/widgets/widgets.dart'; class BooksList extends StatefulWidget { const BooksList({ - Key? key, + super.key, required this.books, required this.listNumber, this.selectedBookIds, this.onBookSelected, - }) : super(key: key); + required this.allBooksCount, + }); final List books; final int listNumber; final Set? selectedBookIds; final Function(int id)? onBookSelected; + final int? allBooksCount; @override State createState() => _BooksListState(); @@ -25,51 +27,85 @@ class BooksList extends StatefulWidget { class _BooksListState extends State with AutomaticKeepAliveClientMixin { + onPressed(int index, bool multiSelectMode, String heroTag) { + if (widget.books[index].id == null) return; + if (multiSelectMode && widget.onBookSelected != null) { + widget.onBookSelected!(widget.books[index].id!); + return; + } + + context.read().setBook(widget.books[index]); + + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => BookScreen( + id: widget.books[index].id!, + heroTag: heroTag, + ), + ), + ); + } + + onLongPressed(int index) { + if (widget.books[index].id == null) return; + if (widget.onBookSelected != null) { + widget.onBookSelected!(widget.books[index].id!); + return; + } + } + @override Widget build(BuildContext context) { super.build(context); var multiSelectMode = widget.selectedBookIds?.isNotEmpty ?? false; - return ListView.builder( - itemCount: widget.books.length, - itemBuilder: (context, index) { - final heroTag = 'tag_${widget.listNumber}_${widget.books[index].id}'; - Color? color = multiSelectMode && - widget.selectedBookIds!.contains(widget.books[index].id) - ? Theme.of(context).colorScheme.secondaryContainer - : null; - return BookCard( - book: widget.books[index], - heroTag: heroTag, - cardColor: color, - addBottomPadding: (widget.books.length == index + 1), - onPressed: () { - if (widget.books[index].id == null) return; - if (multiSelectMode && widget.onBookSelected != null) { - widget.onBookSelected!(widget.books[index].id!); - return; - } - context.read().setBook(widget.books[index]); - - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => BookScreen( - id: widget.books[index].id!, - heroTag: heroTag, - ), - ), + return CustomScrollView( + slivers: [ + SliverToBoxAdapter( + child: widget.allBooksCount != null + ? Padding( + padding: const EdgeInsets.fromLTRB(10, 0, 10, 10), + child: Row( + mainAxisAlignment: MainAxisAlignment.end, + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + Text('${widget.books.length} '), + Text( + '(${widget.allBooksCount})', + style: TextStyle( + color: Theme.of(context) + .colorScheme + .onBackground + .withOpacity(0.7), + fontSize: 13, + ), + ), + ], + ), + ) + : const SizedBox(), + ), + SliverList.builder( + itemCount: widget.books.length, + itemBuilder: (context, index) { + final heroTag = + 'tag_${widget.listNumber}_${widget.books[index].id}'; + Color? color = multiSelectMode && + widget.selectedBookIds!.contains(widget.books[index].id) + ? Theme.of(context).colorScheme.secondaryContainer + : null; + return BookCard( + book: widget.books[index], + heroTag: heroTag, + cardColor: color, + addBottomPadding: (widget.books.length == index + 1), + onPressed: () => onPressed(index, multiSelectMode, heroTag), + onLongPressed: () => onLongPressed(index), ); }, - onLongPressed: () { - if (widget.books[index].id == null) return; - if (widget.onBookSelected != null) { - widget.onBookSelected!(widget.books[index].id!); - return; - } - }, - ); - }, + ), + ], ); } diff --git a/lib/ui/trash_screen/trash_screen.dart b/lib/ui/trash_screen/trash_screen.dart index c1ddb6fe..413d6ee8 100644 --- a/lib/ui/trash_screen/trash_screen.dart +++ b/lib/ui/trash_screen/trash_screen.dart @@ -41,6 +41,7 @@ class TrashScreen extends StatelessWidget { return BooksList( books: snapshot.data!, listNumber: 5, + allBooksCount: null, ); } else if (snapshot.hasError) { return Text( diff --git a/lib/ui/unfinished_screen/unfinished_screen.dart b/lib/ui/unfinished_screen/unfinished_screen.dart index f514e12f..9ed587c4 100644 --- a/lib/ui/unfinished_screen/unfinished_screen.dart +++ b/lib/ui/unfinished_screen/unfinished_screen.dart @@ -41,6 +41,7 @@ class UnfinishedScreen extends StatelessWidget { return BooksList( books: snapshot.data!, listNumber: 6, + allBooksCount: null, ); } else if (snapshot.hasError) { return Text(