Skip to content

Commit

Permalink
Enable multi select on long press instead of multi select button
Browse files Browse the repository at this point in the history
  • Loading branch information
vbh committed Sep 16, 2023
1 parent d91edf5 commit 33aeb90
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 145 deletions.
3 changes: 1 addition & 2 deletions assets/translations/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,5 @@
"dark_mode_amoled": "AMOLED dark mode",
"selected": "Selected",
"change_book_type": "Change book type",
"update_successful_message": "Updated successfully!",
"multi_select": "Multi select"
"update_successful_message": "Updated successfully!"
}
1 change: 0 additions & 1 deletion lib/generated/locale_keys.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,4 @@ abstract class LocaleKeys {
static const selected = 'selected';
static const change_book_type = 'change_book_type';
static const update_successful_message = 'update_successful_message';
static const multi_select = 'multi_select';
}
19 changes: 3 additions & 16 deletions lib/ui/books_screen/books_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class BooksScreen extends StatefulWidget {
class _BooksScreenState extends State<BooksScreen>
with AutomaticKeepAliveClientMixin {
late List<String> moreButtonOptions;
bool multiSelect = false;
Set<int> selectedBookIds = {};

_onItemSelected(int id) {
Expand Down Expand Up @@ -437,7 +436,6 @@ class _BooksScreenState extends State<BooksScreen>
LocaleKeys.sort_filter.tr(),
LocaleKeys.statistics.tr(),
LocaleKeys.settings.tr(),
LocaleKeys.multi_select.tr(),
];

return BlocBuilder<ThemeBloc, ThemeState>(
Expand All @@ -447,16 +445,16 @@ class _BooksScreenState extends State<BooksScreen>

return WillPopScope(
child: Scaffold(
appBar: multiSelect
appBar: selectedBookIds.isNotEmpty
? _buildMultiSelectAppBar(context)
: _buildAppBar(context),
floatingActionButton: multiSelect
floatingActionButton: selectedBookIds.isNotEmpty
? _buildMultiSelectFAB(state)
: _buildFAB(context),
body: _buildScaffoldBody(),
),
onWillPop: () {
if (multiSelect) {
if (selectedBookIds.isNotEmpty) {
_resetMultiselectMode();
return Future.value(false);
}
Expand Down Expand Up @@ -490,7 +488,6 @@ class _BooksScreenState extends State<BooksScreen>

void _resetMultiselectMode() {
setState(() {
multiSelect = false;
selectedBookIds = {};
});
}
Expand Down Expand Up @@ -691,10 +688,6 @@ class _BooksScreenState extends State<BooksScreen>
goToStatisticsScreen();
} else if (choice == moreButtonOptions[2]) {
goToSettingsScreen();
} else if (choice == moreButtonOptions[3]) {
setState(() {
multiSelect = !multiSelect;
});
}
},
);
Expand Down Expand Up @@ -738,7 +731,6 @@ class _BooksScreenState extends State<BooksScreen>
list: snapshot.data!,
),
listNumber: 2,
multiSelectMode: multiSelect,
selectedBookIds: selectedBookIds,
onBookSelected: _onItemSelected,
);
Expand All @@ -749,7 +741,6 @@ class _BooksScreenState extends State<BooksScreen>
list: snapshot.data!,
),
listNumber: 2,
multiSelectMode: multiSelect,
selectedBookIds: selectedBookIds,
onBookSelected: _onItemSelected,
);
Expand Down Expand Up @@ -805,7 +796,6 @@ class _BooksScreenState extends State<BooksScreen>
list: snapshot.data!,
),
listNumber: 1,
multiSelectMode: multiSelect,
selectedBookIds: selectedBookIds,
onBookSelected: _onItemSelected,
);
Expand All @@ -816,7 +806,6 @@ class _BooksScreenState extends State<BooksScreen>
list: snapshot.data!,
),
listNumber: 1,
multiSelectMode: multiSelect,
selectedBookIds: selectedBookIds,
onBookSelected: _onItemSelected,
);
Expand Down Expand Up @@ -872,7 +861,6 @@ class _BooksScreenState extends State<BooksScreen>
list: snapshot.data!,
),
listNumber: 0,
multiSelectMode: multiSelect,
selectedBookIds: selectedBookIds,
onBookSelected: _onItemSelected,
);
Expand All @@ -883,7 +871,6 @@ class _BooksScreenState extends State<BooksScreen>
list: snapshot.data!,
),
listNumber: 0,
multiSelectMode: multiSelect,
selectedBookIds: selectedBookIds,
onBookSelected: _onItemSelected,
);
Expand Down
3 changes: 3 additions & 0 deletions lib/ui/books_screen/widgets/book_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ class BookCard extends StatelessWidget {
required this.onPressed,
required this.heroTag,
required this.addBottomPadding,
this.onLongPressed,
this.cardColor,
}) : super(key: key);

final Book book;
final String heroTag;
final bool addBottomPadding;
final Function() onPressed;
final Function()? onLongPressed;
final Color? cardColor;

Widget _buildSortAttribute() {
Expand Down Expand Up @@ -169,6 +171,7 @@ class BookCard extends StatelessWidget {
borderRadius: BorderRadius.circular(cornerRadius),
child: InkWell(
onTap: onPressed,
onLongPress: onLongPressed,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
child: Row(
Expand Down
3 changes: 3 additions & 0 deletions lib/ui/books_screen/widgets/book_grid_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ class BookGridCard extends StatelessWidget {
required this.onPressed,
required this.heroTag,
required this.addBottomPadding,
this.onLongPressed,
}) : super(key: key);

final Book book;
final String heroTag;
final bool addBottomPadding;
final Function() onPressed;
final Function()? onLongPressed;

@override
Widget build(BuildContext context) {
return InkWell(
onTap: onPressed,
onLongPress: onLongPressed,
child: book.cover != null
? ClipRRect(
borderRadius: BorderRadius.circular(3),
Expand Down
17 changes: 11 additions & 6 deletions lib/ui/books_screen/widgets/books_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ class BooksGrid extends StatefulWidget {
Key? key,
required this.books,
required this.listNumber,
this.multiSelectMode = false,
this.selectedBookIds,
this.onBookSelected,
}) : super(key: key);

final List<Book> books;
final int listNumber;
final bool multiSelectMode;
final Set<int>? selectedBookIds;
final Function(int id)? onBookSelected;

Expand All @@ -29,6 +27,7 @@ class _BooksGridState extends State<BooksGrid>
@override
Widget build(BuildContext context) {
super.build(context);
var multiSelectMode = widget.selectedBookIds?.isNotEmpty ?? false;
return GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
Expand All @@ -40,13 +39,13 @@ class _BooksGridState extends State<BooksGrid>
itemCount: widget.books.length,
itemBuilder: (context, index) {
final heroTag = 'tag_${widget.listNumber}_${widget.books[index].id}';
Color color = widget.multiSelectMode &&
Color color = multiSelectMode &&
widget.selectedBookIds!.contains(widget.books[index].id)
? Theme.of(context).colorScheme.primaryContainer
: Colors.transparent;

return Container(
decoration: widget.multiSelectMode
decoration: multiSelectMode
? BoxDecoration(border: Border.all(color: color, width: 4))
: null,
child: BookGridCard(
Expand All @@ -55,8 +54,7 @@ class _BooksGridState extends State<BooksGrid>
addBottomPadding: (widget.books.length == index + 1),
onPressed: () {
if (widget.books[index].id == null) return;

if (widget.multiSelectMode && widget.onBookSelected != null) {
if (multiSelectMode && widget.onBookSelected != null) {
widget.onBookSelected!(widget.books[index].id!);
return;
}
Expand All @@ -73,6 +71,13 @@ class _BooksGridState extends State<BooksGrid>
),
);
},
onLongPressed: (){
if (widget.books[index].id == null) return;
if (widget.onBookSelected != null) {
widget.onBookSelected!(widget.books[index].id!);
return;
}
},
));
},
);
Expand Down
14 changes: 10 additions & 4 deletions lib/ui/books_screen/widgets/books_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ class BooksList extends StatefulWidget {
Key? key,
required this.books,
required this.listNumber,
this.multiSelectMode = false,
this.selectedBookIds,
this.onBookSelected,
}) : super(key: key);

final List<Book> books;
final int listNumber;
final bool multiSelectMode;
final Set<int>? selectedBookIds;
final Function(int id)? onBookSelected;

Expand All @@ -29,11 +27,12 @@ class _BooksListState extends State<BooksList>
@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 = widget.multiSelectMode &&
Color? color = multiSelectMode &&
widget.selectedBookIds!.contains(widget.books[index].id)
? Theme.of(context).colorScheme.secondaryContainer
: null;
Expand All @@ -44,7 +43,7 @@ class _BooksListState extends State<BooksList>
addBottomPadding: (widget.books.length == index + 1),
onPressed: () {
if (widget.books[index].id == null) return;
if (widget.multiSelectMode && widget.onBookSelected != null) {
if (multiSelectMode && widget.onBookSelected != null) {
widget.onBookSelected!(widget.books[index].id!);
return;
}
Expand All @@ -61,6 +60,13 @@ class _BooksListState extends State<BooksList>
),
);
},
onLongPressed: (){
if (widget.books[index].id == null) return;
if (widget.onBookSelected != null) {
widget.onBookSelected!(widget.books[index].id!);
return;
}
},
);
},
);
Expand Down
Loading

0 comments on commit 33aeb90

Please sign in to comment.