Skip to content

Commit

Permalink
feat: Added counter for displayed books
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-bak committed Nov 26, 2023
1 parent c2722fc commit 17ea0f9
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 91 deletions.
6 changes: 6 additions & 0 deletions lib/ui/books_screen/books_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ class _BooksScreenState extends State<BooksScreen>
listNumber: 2,
selectedBookIds: selectedBookIds,
onBookSelected: _onItemSelected,
allBooksCount: snapshot.data!.length,
);
} else {
return BooksList(
Expand All @@ -872,6 +873,7 @@ class _BooksScreenState extends State<BooksScreen>
listNumber: 2,
selectedBookIds: selectedBookIds,
onBookSelected: _onItemSelected,
allBooksCount: snapshot.data!.length,
);
}
},
Expand Down Expand Up @@ -927,6 +929,7 @@ class _BooksScreenState extends State<BooksScreen>
listNumber: 1,
selectedBookIds: selectedBookIds,
onBookSelected: _onItemSelected,
allBooksCount: snapshot.data!.length,
);
} else {
return BooksList(
Expand All @@ -937,6 +940,7 @@ class _BooksScreenState extends State<BooksScreen>
listNumber: 1,
selectedBookIds: selectedBookIds,
onBookSelected: _onItemSelected,
allBooksCount: snapshot.data!.length,
);
}
},
Expand Down Expand Up @@ -992,6 +996,7 @@ class _BooksScreenState extends State<BooksScreen>
listNumber: 0,
selectedBookIds: selectedBookIds,
onBookSelected: _onItemSelected,
allBooksCount: snapshot.data!.length,
);
} else {
return BooksList(
Expand All @@ -1002,6 +1007,7 @@ class _BooksScreenState extends State<BooksScreen>
listNumber: 0,
selectedBookIds: selectedBookIds,
onBookSelected: _onItemSelected,
allBooksCount: snapshot.data!.length,
);
}
},
Expand Down
144 changes: 93 additions & 51 deletions lib/ui/books_screen/widgets/books_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,122 @@ 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<Book> books;
final int listNumber;
final Set<int>? selectedBookIds;
final Function(int id)? onBookSelected;
final int allBooksCount;

@override
State<BooksGrid> createState() => _BooksGridState();
}

class _BooksGridState extends State<BooksGrid>
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<CurrentBookCubit>().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<CurrentBookCubit>().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),
));
},
),
),
],
);
}

Expand Down
116 changes: 76 additions & 40 deletions lib/ui/books_screen/widgets/books_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,105 @@ 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<Book> books;
final int listNumber;
final Set<int>? selectedBookIds;
final Function(int id)? onBookSelected;
final int? allBooksCount;

@override
State<BooksList> createState() => _BooksListState();
}

class _BooksListState extends State<BooksList>
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<CurrentBookCubit>().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<CurrentBookCubit>().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;
}
},
);
},
),
],
);
}

Expand Down
1 change: 1 addition & 0 deletions lib/ui/trash_screen/trash_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class TrashScreen extends StatelessWidget {
return BooksList(
books: snapshot.data!,
listNumber: 5,
allBooksCount: null,
);
} else if (snapshot.hasError) {
return Text(
Expand Down
1 change: 1 addition & 0 deletions lib/ui/unfinished_screen/unfinished_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class UnfinishedScreen extends StatelessWidget {
return BooksList(
books: snapshot.data!,
listNumber: 6,
allBooksCount: null,
);
} else if (snapshot.hasError) {
return Text(
Expand Down

0 comments on commit 17ea0f9

Please sign in to comment.