mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 02:09:35 +02:00
BookCollection sends label to db
This commit is contained in:
parent
54df61dfb2
commit
981e31c44f
3 changed files with 54 additions and 43 deletions
|
@ -671,6 +671,7 @@ final class SQLiteBooksDatabase extends BooksDatabase {
|
|||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Long> loadRecentBookIds() {
|
||||
final Cursor cursor = myDatabase.rawQuery(
|
||||
"SELECT book_id FROM RecentBooks ORDER BY book_index", null
|
||||
|
@ -683,59 +684,73 @@ final class SQLiteBooksDatabase extends BooksDatabase {
|
|||
return ids;
|
||||
}
|
||||
|
||||
protected boolean hasFavorites() {
|
||||
@Override
|
||||
protected List<String> labels() {
|
||||
final Cursor cursor = myDatabase.rawQuery(
|
||||
"SELECT BookLabel.book_id FROM BookLabel" +
|
||||
" INNER JOIN Labels ON BookLabel.label_id=Labels.label_id" +
|
||||
" WHERE Labels.name='favorite' LIMIT 1", null
|
||||
"SELECT Labels.name FROM Labels" +
|
||||
" INNER JOIN BookLabel ON BookLabel.label_id=Labels.label_id" +
|
||||
" GROUP BY Labels.name",
|
||||
null
|
||||
);
|
||||
boolean result = cursor.moveToNext();
|
||||
final LinkedList<String> names = new LinkedList<String>();
|
||||
while (cursor.moveToNext()) {
|
||||
names.add(cursor.getString(0));
|
||||
}
|
||||
cursor.close();
|
||||
return result;
|
||||
return names;
|
||||
}
|
||||
|
||||
protected boolean isFavorite(long bookId) {
|
||||
@Override
|
||||
protected List<String> labels(long bookId) {
|
||||
final Cursor cursor = myDatabase.rawQuery(
|
||||
"SELECT BookLabel.book_id FROM BookLabel" +
|
||||
" INNER JOIN Labels ON BookLabel.label_id=Labels.label_id" +
|
||||
" WHERE BookLabel.book_id=? AND Labels.name='favorite' LIMIT 1",
|
||||
"SELECT Labels.name FROM Labels" +
|
||||
" INNER JOIN BookLabel ON BookLabel.label_id=Labels.label_id" +
|
||||
" WHERE BookLabel.book_id=?",
|
||||
new String[] { String.valueOf(bookId) }
|
||||
);
|
||||
boolean result = cursor.moveToNext();
|
||||
final LinkedList<String> names = new LinkedList<String>();
|
||||
while (cursor.moveToNext()) {
|
||||
names.add(cursor.getString(0));
|
||||
}
|
||||
cursor.close();
|
||||
return result;
|
||||
return names;
|
||||
}
|
||||
|
||||
private SQLiteStatement myAddToFavoritesStatement;
|
||||
protected void addToFavorites(long bookId) {
|
||||
myDatabase.execSQL("INSERT OR IGNORE INTO Labels (name) VALUES ('favorite')");
|
||||
if (myAddToFavoritesStatement == null) {
|
||||
myAddToFavoritesStatement = myDatabase.compileStatement(
|
||||
private SQLiteStatement mySetLabelStatement;
|
||||
@Override
|
||||
protected void setLabel(long bookId, String label) {
|
||||
myDatabase.execSQL("INSERT OR IGNORE INTO Labels (name) VALUES (?)", new Object[] { label });
|
||||
if (mySetLabelStatement == null) {
|
||||
mySetLabelStatement = myDatabase.compileStatement(
|
||||
"INSERT OR IGNORE INTO BookLabel(label_id,book_id)" +
|
||||
" SELECT label_id,? FROM Labels WHERE name='favorite'"
|
||||
" SELECT label_id,? FROM Labels WHERE name=?"
|
||||
);
|
||||
}
|
||||
myAddToFavoritesStatement.bindLong(1, bookId);
|
||||
myAddToFavoritesStatement.execute();
|
||||
mySetLabelStatement.bindLong(1, bookId);
|
||||
mySetLabelStatement.bindString(2, label);
|
||||
mySetLabelStatement.execute();
|
||||
}
|
||||
|
||||
private SQLiteStatement myRemoveFromFavoritesStatement;
|
||||
protected void removeFromFavorites(long bookId) {
|
||||
if (myRemoveFromFavoritesStatement == null) {
|
||||
myRemoveFromFavoritesStatement = myDatabase.compileStatement(
|
||||
private SQLiteStatement myRemoveLabelStatement;
|
||||
@Override
|
||||
protected void removeLabel(long bookId, String label) {
|
||||
if (myRemoveLabelStatement == null) {
|
||||
myRemoveLabelStatement = myDatabase.compileStatement(
|
||||
"DELETE FROM BookLabel WHERE book_id=? AND label_id IN" +
|
||||
" (SELECT label_id FROM Labels WHERE name='favorite')"
|
||||
" (SELECT label_id FROM Labels WHERE name=?)"
|
||||
);
|
||||
}
|
||||
myRemoveFromFavoritesStatement.bindLong(1, bookId);
|
||||
myRemoveFromFavoritesStatement.execute();
|
||||
myRemoveLabelStatement.bindLong(1, bookId);
|
||||
myRemoveLabelStatement.bindString(2, label);
|
||||
myRemoveLabelStatement.execute();
|
||||
}
|
||||
|
||||
protected List<Long> loadFavoriteIds() {
|
||||
@Override
|
||||
protected List<Long> loadBooksForLabelIds(String label) {
|
||||
final Cursor cursor = myDatabase.rawQuery(
|
||||
"SELECT BookLabel.book_id FROM BookLabel" +
|
||||
" INNER JOIN Labels ON BookLabel.label_id=Labels.label_id" +
|
||||
" WHERE Labels.name='favorite'", null
|
||||
" WHERE Labels.name=?", new String[] { label }
|
||||
);
|
||||
final LinkedList<Long> ids = new LinkedList<Long>();
|
||||
while (cursor.moveToNext()) {
|
||||
|
|
|
@ -293,7 +293,7 @@ public class BookCollection extends AbstractBookCollection {
|
|||
}
|
||||
|
||||
public List<Book> booksForLabel(String label) {
|
||||
return books(myDatabase.loadFavoriteIds());
|
||||
return books(myDatabase.loadBooksForLabelIds(label));
|
||||
}
|
||||
|
||||
private List<Book> books(List<Long> ids) {
|
||||
|
@ -489,27 +489,23 @@ public class BookCollection extends AbstractBookCollection {
|
|||
}
|
||||
|
||||
public List<String> labels() {
|
||||
return myDatabase.hasFavorites()
|
||||
? Collections.singletonList(Book.FAVORITE_LABEL)
|
||||
: Collections.<String>emptyList();
|
||||
return myDatabase.labels();
|
||||
}
|
||||
|
||||
public List<String> labels(Book book) {
|
||||
if (book == null) {
|
||||
return Collections.<String>emptyList();
|
||||
}
|
||||
return myDatabase.isFavorite(book.getId())
|
||||
? Collections.singletonList(Book.FAVORITE_LABEL)
|
||||
: Collections.<String>emptyList();
|
||||
return myDatabase.labels(book.getId());
|
||||
}
|
||||
|
||||
public void setLabel(Book book, String label) {
|
||||
myDatabase.addToFavorites(book.getId());
|
||||
myDatabase.setLabel(book.getId(), label);
|
||||
fireBookEvent(BookEvent.Updated, book);
|
||||
}
|
||||
|
||||
public void removeLabel(Book book, String label) {
|
||||
myDatabase.removeFromFavorites(book.getId());
|
||||
myDatabase.removeLabel(book.getId(), label);
|
||||
fireBookEvent(BookEvent.Updated, book);
|
||||
}
|
||||
|
||||
|
|
|
@ -76,11 +76,11 @@ public abstract class BooksDatabase {
|
|||
protected abstract List<Long> loadRecentBookIds();
|
||||
protected abstract void saveRecentBookIds(final List<Long> ids);
|
||||
|
||||
protected abstract List<Long> loadFavoriteIds();
|
||||
protected abstract boolean hasFavorites();
|
||||
protected abstract boolean isFavorite(long bookId);
|
||||
protected abstract void addToFavorites(long bookId);
|
||||
protected abstract void removeFromFavorites(long bookId);
|
||||
protected abstract List<Long> loadBooksForLabelIds(String label);
|
||||
protected abstract List<String> labels();
|
||||
protected abstract List<String> labels(long bookId);
|
||||
protected abstract void setLabel(long bookId, String label);
|
||||
protected abstract void removeLabel(long bookId, String label);
|
||||
|
||||
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, boolean isVisible) {
|
||||
return new Bookmark(id, bookId, bookTitle, text, creationDate, modificationDate, accessDate, accessCounter, modelId, paragraphIndex, wordIndex, charIndex, isVisible);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue