From d9ef33b4fa0ad954caa302cb18b1152727dedba2 Mon Sep 17 00:00:00 2001 From: mateusz-bak <32651935+mateusz-bak@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:07:05 +1100 Subject: [PATCH] feat: Option to duplicate existing book --- assets/translations/en-US.json | 4 ++- lib/generated/locale_keys.g.dart | 2 ++ lib/ui/add_book_screen/add_book_screen.dart | 10 +++++++- .../widgets/book_screen_app_bar.dart | 25 +++++++++++++++++-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/assets/translations/en-US.json b/assets/translations/en-US.json index d503cdaa..f2c0b174 100644 --- a/assets/translations/en-US.json +++ b/assets/translations/en-US.json @@ -288,5 +288,7 @@ "wait_for_downloading_to_finish": "Wait for downloading to finish", "save_without_cover": "Save without the cover", "search_online_for_cover": "Search online for the cover", - "book_cover": "book cover" + "book_cover": "book cover", + "duplicate_book": "Duplicate book", + "copy_book": "copy" } \ No newline at end of file diff --git a/lib/generated/locale_keys.g.dart b/lib/generated/locale_keys.g.dart index f939c6e8..3cd4495e 100644 --- a/lib/generated/locale_keys.g.dart +++ b/lib/generated/locale_keys.g.dart @@ -288,4 +288,6 @@ abstract class LocaleKeys { static const saveWithoutCover = 'save_without_cover'; static const searchOnlineForCover = 'search_online_for_cover'; static const bookCover = 'book_cover'; + static const duplicateBook = 'duplicate_book'; + static const copyBook = 'copy_book'; } diff --git a/lib/ui/add_book_screen/add_book_screen.dart b/lib/ui/add_book_screen/add_book_screen.dart index 7e3577a6..828b128e 100644 --- a/lib/ui/add_book_screen/add_book_screen.dart +++ b/lib/ui/add_book_screen/add_book_screen.dart @@ -30,6 +30,7 @@ class AddBookScreen extends StatefulWidget { this.fromOpenLibrary = false, this.fromOpenLibraryEdition = false, this.editingExistingBook = false, + this.duplicatingBook = false, this.coverOpenLibraryID, this.work, }); @@ -37,6 +38,7 @@ class AddBookScreen extends StatefulWidget { final bool fromOpenLibrary; final bool fromOpenLibraryEdition; final bool editingExistingBook; + final bool duplicatingBook; final int? coverOpenLibraryID; final String? work; @@ -85,7 +87,9 @@ class _AddBookScreenState extends State { _notesCtrl.text = book.notes ?? ''; if (!widget.fromOpenLibrary && !widget.fromOpenLibraryEdition) { - context.read().setCover(book.getCoverBytes()); + if (!widget.duplicatingBook) { + context.read().setCover(book.getCoverBytes()); + } } } @@ -143,6 +147,10 @@ class _AddBookScreenState extends State { if (!mounted) return; Navigator.pop(context); + if (widget.duplicatingBook) { + Navigator.pop(context); + } + if (widget.fromOpenLibrary) { Navigator.pop(context); if (!widget.fromOpenLibraryEdition) return; diff --git a/lib/ui/book_screen/widgets/book_screen_app_bar.dart b/lib/ui/book_screen/widgets/book_screen_app_bar.dart index fd1cfa38..2be8d653 100644 --- a/lib/ui/book_screen/widgets/book_screen_app_bar.dart +++ b/lib/ui/book_screen/widgets/book_screen_app_bar.dart @@ -92,6 +92,7 @@ class BookScreenAppBar extends StatelessWidget implements PreferredSizeWidget { Widget build(BuildContext context) { final moreButtonOptions = [ LocaleKeys.edit_book.tr(), + LocaleKeys.duplicateBook.tr(), ]; // Needed to add BlocBuilder because the status bar was changing @@ -116,7 +117,7 @@ class BookScreenAppBar extends StatelessWidget implements PreferredSizeWidget { actions: [ BlocBuilder( builder: (context, state) { - if (moreButtonOptions.length == 1) { + if (moreButtonOptions.length == 2) { if (state.deleted == true) { moreButtonOptions.add(LocaleKeys.restore_book.tr()); moreButtonOptions.add( @@ -151,6 +152,26 @@ class BookScreenAppBar extends StatelessWidget implements PreferredSizeWidget { ), ); } else if (choice == moreButtonOptions[1]) { + final cover = state.getCoverBytes(); + + context.read().setCover(cover); + final newBook = state.copyWith( + title: + '${state.title} ${LocaleKeys.copyBook.tr()}', + ); + newBook.id = null; + + context.read().setBook(newBook); + context.read().setHasCover(true); + + Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => const AddBookScreen( + duplicatingBook: true, + ), + ), + ); + } else if (choice == moreButtonOptions[2]) { if (state.deleted == false) { _showDeleteRestoreDialog( context, true, null, state); @@ -158,7 +179,7 @@ class BookScreenAppBar extends StatelessWidget implements PreferredSizeWidget { _showDeleteRestoreDialog( context, false, null, state); } - } else if (choice == moreButtonOptions[2]) { + } else if (choice == moreButtonOptions[3]) { _showDeleteRestoreDialog( context, true,