mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 18:29:23 +02:00
Favorites (in progress)
This commit is contained in:
parent
d1f4f7798a
commit
69e31c007f
4 changed files with 43 additions and 22 deletions
|
@ -648,22 +648,26 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
|
||||||
return ids;
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
private SQLiteStatement mySaveFavoritesStatement;
|
private SQLiteStatement myAddToFavoritesStatement;
|
||||||
protected void saveFavoritesIds(final List<Long> ids) {
|
protected void addToFavorites(long bookId) {
|
||||||
if (mySaveFavoritesStatement == null) {
|
if (myAddToFavoritesStatement == null) {
|
||||||
mySaveFavoritesStatement = myDatabase.compileStatement(
|
myAddToFavoritesStatement = myDatabase.compileStatement(
|
||||||
"INSERT INTO Favorites (book_id) VALUES (?)"
|
"INSERT OR IGNORE INTO Favorites(book_id) VALUES (?)"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
executeAsATransaction(new Runnable() {
|
myAddToFavoritesStatement.bindLong(1, bookId);
|
||||||
public void run() {
|
myAddToFavoritesStatement.execute();
|
||||||
myDatabase.delete("Favorites", null, null);
|
|
||||||
for (long id : ids) {
|
|
||||||
mySaveFavoritesStatement.bindLong(1, id);
|
|
||||||
mySaveFavoritesStatement.execute();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SQLiteStatement myRemoveFromFavoritesStatement;
|
||||||
|
protected void removeFromFavorites(long bookId) {
|
||||||
|
if (myRemoveFromFavoritesStatement == null) {
|
||||||
|
myRemoveFromFavoritesStatement = myDatabase.compileStatement(
|
||||||
|
"DELETE FROM Favorites WHERE book_id = ?"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
myRemoveFromFavoritesStatement.bindLong(1, bookId);
|
||||||
|
myRemoveFromFavoritesStatement.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<Long> loadFavoritesIds() {
|
protected List<Long> loadFavoritesIds() {
|
||||||
|
|
|
@ -85,7 +85,8 @@ public abstract class BooksDatabase {
|
||||||
protected abstract void saveRecentBookIds(final List<Long> ids);
|
protected abstract void saveRecentBookIds(final List<Long> ids);
|
||||||
|
|
||||||
protected abstract List<Long> loadFavoritesIds();
|
protected abstract List<Long> loadFavoritesIds();
|
||||||
protected abstract void saveFavoritesIds(final List<Long> ids);
|
protected abstract void addToFavorites(long bookId);
|
||||||
|
protected abstract void removeFromFavorites(long bookId);
|
||||||
|
|
||||||
protected Bookmark createBookmark(long id, long bookId, String bookTitle, String text, Date creationDate, Date modificationDate, Date accessDate, int accessCounter, String modelId, int paragraphIndex, int wordIndex, int charIndex) {
|
protected Bookmark createBookmark(long id, long bookId, String bookTitle, String text, Date creationDate, Date modificationDate, Date accessDate, int accessCounter, String modelId, int paragraphIndex, int wordIndex, int charIndex) {
|
||||||
return new Bookmark(id, bookId, bookTitle, text, creationDate, modificationDate, accessDate, accessCounter, modelId, paragraphIndex, wordIndex, charIndex);
|
return new Bookmark(id, bookId, bookTitle, text, creationDate, modificationDate, accessDate, accessCounter, modelId, paragraphIndex, wordIndex, charIndex);
|
||||||
|
|
|
@ -354,13 +354,19 @@ public final class Library {
|
||||||
db.saveRecentBookIds(ids);
|
db.saveRecentBookIds(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addBookToFavorites(Book book) {
|
public void addBookToFavorites(Book book) {
|
||||||
final BooksDatabase db = BooksDatabase.Instance();
|
waitForState(STATE_FULLY_INITIALIZED);
|
||||||
final List<Long> ids = db.loadFavoritesIds();
|
if (!myFavorites.containsBook(book)) {
|
||||||
final Long bookId = book.getId();
|
myFavorites.createBookSubTree(book, true);
|
||||||
if (!ids.contains(bookId)) {
|
myFavorites.sortAllChildren();
|
||||||
ids.add(bookId);
|
BooksDatabase.Instance().addToFavorites(book.getId());
|
||||||
db.saveFavoritesIds(ids);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeBookFromFavorites(Book book) {
|
||||||
|
waitForState(STATE_FULLY_INITIALIZED);
|
||||||
|
if (myFavorites.removeBook(book)) {
|
||||||
|
BooksDatabase.Instance().removeFromFavorites(book.getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -404,6 +410,7 @@ public final class Library {
|
||||||
db.saveRecentBookIds(ids);
|
db.saveRecentBookIds(ids);
|
||||||
}
|
}
|
||||||
mySearchResult.removeBook(book);
|
mySearchResult.removeBook(book);
|
||||||
|
myFavorites.removeBook(book);
|
||||||
|
|
||||||
BooksDatabase.Instance().deleteFromBookList(book.getId());
|
BooksDatabase.Instance().deleteFromBookList(book.getId());
|
||||||
if ((removeMode & REMOVE_FROM_DISK) != 0) {
|
if ((removeMode & REMOVE_FROM_DISK) != 0) {
|
||||||
|
|
|
@ -46,6 +46,15 @@ public abstract class LibraryTree extends FBTree {
|
||||||
return new BookTree(this, book, showAuthors);
|
return new BookTree(this, book, showAuthors);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean containsBook(Book book) {
|
||||||
|
for (FBTree tree : this) {
|
||||||
|
if ((tree instanceof BookTree) && ((BookTree)tree).Book.equals(book)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean removeBook(Book book) {
|
public boolean removeBook(Book book) {
|
||||||
final LinkedList<FBTree> toRemove = new LinkedList<FBTree>();
|
final LinkedList<FBTree> toRemove = new LinkedList<FBTree>();
|
||||||
for (FBTree tree : this) {
|
for (FBTree tree : this) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue