mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 10:19:33 +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;
|
||||
}
|
||||
|
||||
private SQLiteStatement mySaveFavoritesStatement;
|
||||
protected void saveFavoritesIds(final List<Long> ids) {
|
||||
if (mySaveFavoritesStatement == null) {
|
||||
mySaveFavoritesStatement = myDatabase.compileStatement(
|
||||
"INSERT INTO Favorites (book_id) VALUES (?)"
|
||||
private SQLiteStatement myAddToFavoritesStatement;
|
||||
protected void addToFavorites(long bookId) {
|
||||
if (myAddToFavoritesStatement == null) {
|
||||
myAddToFavoritesStatement = myDatabase.compileStatement(
|
||||
"INSERT OR IGNORE INTO Favorites(book_id) VALUES (?)"
|
||||
);
|
||||
}
|
||||
executeAsATransaction(new Runnable() {
|
||||
public void run() {
|
||||
myDatabase.delete("Favorites", null, null);
|
||||
for (long id : ids) {
|
||||
mySaveFavoritesStatement.bindLong(1, id);
|
||||
mySaveFavoritesStatement.execute();
|
||||
}
|
||||
}
|
||||
});
|
||||
myAddToFavoritesStatement.bindLong(1, bookId);
|
||||
myAddToFavoritesStatement.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() {
|
||||
|
|
|
@ -85,7 +85,8 @@ public abstract class BooksDatabase {
|
|||
protected abstract void saveRecentBookIds(final List<Long> ids);
|
||||
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
||||
public static void addBookToFavorites(Book book) {
|
||||
final BooksDatabase db = BooksDatabase.Instance();
|
||||
final List<Long> ids = db.loadFavoritesIds();
|
||||
final Long bookId = book.getId();
|
||||
if (!ids.contains(bookId)) {
|
||||
ids.add(bookId);
|
||||
db.saveFavoritesIds(ids);
|
||||
public void addBookToFavorites(Book book) {
|
||||
waitForState(STATE_FULLY_INITIALIZED);
|
||||
if (!myFavorites.containsBook(book)) {
|
||||
myFavorites.createBookSubTree(book, true);
|
||||
myFavorites.sortAllChildren();
|
||||
BooksDatabase.Instance().addToFavorites(book.getId());
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
mySearchResult.removeBook(book);
|
||||
myFavorites.removeBook(book);
|
||||
|
||||
BooksDatabase.Instance().deleteFromBookList(book.getId());
|
||||
if ((removeMode & REMOVE_FROM_DISK) != 0) {
|
||||
|
|
|
@ -46,6 +46,15 @@ public abstract class LibraryTree extends FBTree {
|
|||
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) {
|
||||
final LinkedList<FBTree> toRemove = new LinkedList<FBTree>();
|
||||
for (FBTree tree : this) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue