Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added notes field #336

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/translations/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"open_library_ID": "Open Library ID",
"enter_tags": "Tags",
"my_review": "My review",
"notes": "Notes",
"add_new_book": "Add new book",
"select_start_date": "Select reading start date",
"select_finish_date": "Select reading finish date",
Expand Down
78 changes: 65 additions & 13 deletions lib/database/database_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';

// Instruction how to add a new database field:
// 1. Add new parameters to the Book class in book.dart
// 2. Increase version number in the createDatabase method below
// 3. Add new fields to the booksTable in the onCreate argument below
// 4. Add a new case to the onUpgrade argument below
// 5. Add a new list of migration scripts to the migrationScriptsVx
// 6. Add a new method _updateBookDatabaseVytoVx
// 7. Update existing methods with new migration scripts
// 7. Update existing methods names with new version number

class DatabaseProvider {
static final DatabaseProvider dbProvider = DatabaseProvider();

Expand All @@ -18,7 +28,7 @@ class DatabaseProvider {

return await openDatabase(
path,
version: 4,
version: 5,
onCreate: (Database db, int version) async {
await db.execute("CREATE TABLE booksTable ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
Expand All @@ -39,6 +49,7 @@ class DatabaseProvider {
"olid TEXT, "
"tags TEXT, "
"my_review TEXT, "
"notes TEXT, "
"has_cover INTEGER, "
"cover BLOB, "
"blur_hash TEXT "
Expand All @@ -50,13 +61,16 @@ class DatabaseProvider {

switch (oldVersion) {
case 1:
_updateBookDatabaseV1toV4(batch);
_updateBookDatabaseV1toV5(batch);
break;
case 2:
_updateBookDatabaseV2toV4(batch);
_updateBookDatabaseV2toV5(batch);
break;
case 3:
_updateBookDatabaseV3toV4(batch);
_updateBookDatabaseV3toV5(batch);
break;
case 4:
_updateBookDatabaseV4toV5(batch);
break;
}

Expand All @@ -66,18 +80,56 @@ class DatabaseProvider {
);
}

void _updateBookDatabaseV1toV4(Batch batch) {
batch.execute("ALTER TABLE booksTable ADD description TEXT");
batch.execute("ALTER TABLE booksTable ADD book_type TEXT DEFAULT paper");
batch.execute("ALTER TABLE booksTable ADD has_cover INTEGER DEFAULT 0");
void _executeBatch(Batch batch, List<String> scripts) {
for (var script in scripts) {
batch.execute(script);
}
}

final migrationScriptsV2 = [
"ALTER TABLE booksTable ADD description TEXT",
];

final migrationScriptsV3 = [
"ALTER TABLE booksTable ADD book_type TEXT",
];

final migrationScriptsV4 = [
"ALTER TABLE booksTable ADD has_cover INTEGER DEFAULT 0",
];

final migrationScriptsV5 = [
"ALTER TABLE booksTable ADD notes TEXT",
];

void _updateBookDatabaseV1toV5(Batch batch) {
_executeBatch(
batch,
migrationScriptsV2 +
migrationScriptsV3 +
migrationScriptsV4 +
migrationScriptsV5,
);
}

void _updateBookDatabaseV2toV5(Batch batch) {
_executeBatch(
batch,
migrationScriptsV3 + migrationScriptsV4 + migrationScriptsV5,
);
}

void _updateBookDatabaseV2toV4(Batch batch) {
batch.execute("ALTER TABLE booksTable ADD book_type TEXT");
batch.execute("ALTER TABLE booksTable ADD has_cover INTEGER DEFAULT 0");
void _updateBookDatabaseV3toV5(Batch batch) {
_executeBatch(
batch,
migrationScriptsV4 + migrationScriptsV5,
);
}

void _updateBookDatabaseV3toV4(Batch batch) {
batch.execute("ALTER TABLE booksTable ADD has_cover INTEGER DEFAULT 0");
void _updateBookDatabaseV4toV5(Batch batch) {
_executeBatch(
batch,
migrationScriptsV5,
);
}
}
1 change: 1 addition & 0 deletions lib/generated/locale_keys.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ abstract class LocaleKeys {
static const open_library_ID = 'open_library_ID';
static const enter_tags = 'enter_tags';
static const my_review = 'my_review';
static const notes = 'notes';
static const add_new_book = 'add_new_book';
static const select_start_date = 'select_start_date';
static const select_finish_date = 'select_finish_date';
Expand Down
7 changes: 7 additions & 0 deletions lib/logic/cubit/edit_book_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ class EditBookCubit extends Cubit<Book> {
emit(book);
}

void setNotes(String notes) {
final book = state.copyWith();
book.notes = notes.isNotEmpty ? notes : null;

emit(book);
}

void setBlurHash(String? blurHash) {
final book = state.copyWith();
book.blurHash = blurHash;
Expand Down
9 changes: 8 additions & 1 deletion lib/model/book.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Book {
String? olid;
String? tags;
String? myReview;
String? notes;
Uint8List? cover; // Not used since 2.2.0
String? blurHash;
BookType bookType;
Expand All @@ -47,6 +48,7 @@ class Book {
this.olid,
this.tags,
this.myReview,
this.notes,
this.cover,
this.blurHash,
this.bookType = BookType.paper,
Expand Down Expand Up @@ -90,6 +92,7 @@ class Book {
olid: json['olid'],
tags: json['tags'],
myReview: json['my_review'],
notes: json['notes'],
cover: json['cover'] != null
? Uint8List.fromList(json['cover'].cast<int>().toList())
: null,
Expand Down Expand Up @@ -119,6 +122,7 @@ class Book {
String? olid,
String? tags,
String? myReview,
String? notes,
Uint8List? cover,
String? blurHash,
BookType? bookType,
Expand All @@ -142,6 +146,7 @@ class Book {
olid: olid ?? this.olid,
tags: tags ?? this.tags,
myReview: myReview ?? this.myReview,
notes: notes ?? this.notes,
cover: cover ?? this.cover,
blurHash: blurHash ?? this.blurHash,
bookType: bookType ?? this.bookType,
Expand All @@ -168,6 +173,7 @@ class Book {
olid: olid,
tags: tags,
myReview: myReview,
notes: notes,
cover: null,
blurHash: blurHash,
bookType: bookType,
Expand Down Expand Up @@ -211,7 +217,7 @@ class Book {
tags: oldBook.bookTags != null && oldBook.bookTags != 'null'
? jsonDecode(oldBook.bookTags!).join('|||||')
: null,
myReview: oldBook.bookNotes,
notes: oldBook.bookNotes,
cover: oldBook.bookCoverImg,
blurHash: blurHash,
bookType: BookType.paper,
Expand All @@ -238,6 +244,7 @@ class Book {
'olid': olid,
'tags': tags,
'my_review': myReview,
'notes': notes,
'cover': cover,
'blur_hash': blurHash,
'has_cover': hasCover ? 1 : 0,
Expand Down
18 changes: 18 additions & 0 deletions lib/ui/add_book_screen/add_book_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class _AddBookScreenState extends State<AddBookScreen> {
final _olidCtrl = TextEditingController();
final _tagsCtrl = TextEditingController();
final _myReviewCtrl = TextEditingController();
final _notesCtrl = TextEditingController();

final _animDuration = const Duration(milliseconds: 250);

Expand All @@ -75,6 +76,7 @@ class _AddBookScreenState extends State<AddBookScreen> {
_isbnCtrl.text = book.isbn ?? '';
_olidCtrl.text = book.olid ?? '';
_myReviewCtrl.text = book.myReview ?? '';
_notesCtrl.text = book.notes ?? '';

if (!widget.fromOpenLibrary && !widget.fromOpenLibraryEdition) {
context.read<EditBookCoverCubit>().setCover(book.getCoverFile());
Expand Down Expand Up @@ -309,6 +311,10 @@ class _AddBookScreenState extends State<AddBookScreen> {
_myReviewCtrl.addListener(() {
context.read<EditBookCubit>().setMyReview(_myReviewCtrl.text);
});

_notesCtrl.addListener(() {
context.read<EditBookCubit>().setNotes(_notesCtrl.text);
});
}

@override
Expand Down Expand Up @@ -341,6 +347,7 @@ class _AddBookScreenState extends State<AddBookScreen> {
_olidCtrl.dispose();
_tagsCtrl.dispose();
_myReviewCtrl.dispose();
_notesCtrl.dispose();

super.dispose();
}
Expand Down Expand Up @@ -530,6 +537,17 @@ class _AddBookScreenState extends State<AddBookScreen> {
maxLines: 15,
textCapitalization: TextCapitalization.sentences,
),
const SizedBox(height: 10),
BookTextField(
controller: _notesCtrl,
hint: LocaleKeys.notes.tr(),
icon: FontAwesomeIcons.noteSticky,
keyboardType: TextInputType.multiline,
maxLength: 5000,
hideCounter: false,
maxLines: 15,
textCapitalization: TextCapitalization.sentences,
),
const SizedBox(height: 30),
Row(
children: [
Expand Down
11 changes: 11 additions & 0 deletions lib/ui/book_screen/book_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,17 @@ class BookScreen extends StatelessWidget {
text: state.myReview!,
)
: const SizedBox(),
SizedBox(
height: (state.notes != null && state.notes!.isNotEmpty)
? 5
: 0,
),
(state.notes != null && state.notes!.isNotEmpty)
? BookDetail(
title: LocaleKeys.notes.tr(),
text: state.notes!,
)
: const SizedBox(),
const SizedBox(height: 50.0),
],
),
Expand Down
Loading