Skip to content

Commit

Permalink
feat: Option to duplicate existing book
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-bak committed Apr 2, 2024
1 parent 4f44188 commit d9ef33b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
4 changes: 3 additions & 1 deletion assets/translations/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
2 changes: 2 additions & 0 deletions lib/generated/locale_keys.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
10 changes: 9 additions & 1 deletion lib/ui/add_book_screen/add_book_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ class AddBookScreen extends StatefulWidget {
this.fromOpenLibrary = false,
this.fromOpenLibraryEdition = false,
this.editingExistingBook = false,
this.duplicatingBook = false,
this.coverOpenLibraryID,
this.work,
});

final bool fromOpenLibrary;
final bool fromOpenLibraryEdition;
final bool editingExistingBook;
final bool duplicatingBook;
final int? coverOpenLibraryID;
final String? work;

Expand Down Expand Up @@ -85,7 +87,9 @@ class _AddBookScreenState extends State<AddBookScreen> {
_notesCtrl.text = book.notes ?? '';

if (!widget.fromOpenLibrary && !widget.fromOpenLibraryEdition) {
context.read<EditBookCoverCubit>().setCover(book.getCoverBytes());
if (!widget.duplicatingBook) {
context.read<EditBookCoverCubit>().setCover(book.getCoverBytes());
}
}
}

Expand Down Expand Up @@ -143,6 +147,10 @@ class _AddBookScreenState extends State<AddBookScreen> {
if (!mounted) return;
Navigator.pop(context);

if (widget.duplicatingBook) {
Navigator.pop(context);
}

if (widget.fromOpenLibrary) {
Navigator.pop(context);
if (!widget.fromOpenLibraryEdition) return;
Expand Down
25 changes: 23 additions & 2 deletions lib/ui/book_screen/widgets/book_screen_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -116,7 +117,7 @@ class BookScreenAppBar extends StatelessWidget implements PreferredSizeWidget {
actions: [
BlocBuilder<CurrentBookCubit, Book>(
builder: (context, state) {
if (moreButtonOptions.length == 1) {
if (moreButtonOptions.length == 2) {
if (state.deleted == true) {
moreButtonOptions.add(LocaleKeys.restore_book.tr());
moreButtonOptions.add(
Expand Down Expand Up @@ -151,14 +152,34 @@ class BookScreenAppBar extends StatelessWidget implements PreferredSizeWidget {
),
);
} else if (choice == moreButtonOptions[1]) {
final cover = state.getCoverBytes();

context.read<EditBookCoverCubit>().setCover(cover);
final newBook = state.copyWith(
title:
'${state.title} ${LocaleKeys.copyBook.tr()}',
);
newBook.id = null;

context.read<EditBookCubit>().setBook(newBook);
context.read<EditBookCubit>().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);
} else {
_showDeleteRestoreDialog(
context, false, null, state);
}
} else if (choice == moreButtonOptions[2]) {
} else if (choice == moreButtonOptions[3]) {
_showDeleteRestoreDialog(
context,
true,
Expand Down

0 comments on commit d9ef33b

Please sign in to comment.