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() {
|
protected List<Long> loadRecentBookIds() {
|
||||||
final Cursor cursor = myDatabase.rawQuery(
|
final Cursor cursor = myDatabase.rawQuery(
|
||||||
"SELECT book_id FROM RecentBooks ORDER BY book_index", null
|
"SELECT book_id FROM RecentBooks ORDER BY book_index", null
|
||||||
|
@ -683,59 +684,73 @@ final class SQLiteBooksDatabase extends BooksDatabase {
|
||||||
return ids;
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean hasFavorites() {
|
@Override
|
||||||
|
protected List<String> labels() {
|
||||||
final Cursor cursor = myDatabase.rawQuery(
|
final Cursor cursor = myDatabase.rawQuery(
|
||||||
"SELECT BookLabel.book_id FROM BookLabel" +
|
"SELECT Labels.name FROM Labels" +
|
||||||
" INNER JOIN Labels ON BookLabel.label_id=Labels.label_id" +
|
" INNER JOIN BookLabel ON BookLabel.label_id=Labels.label_id" +
|
||||||
" WHERE Labels.name='favorite' LIMIT 1", null
|
" 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();
|
cursor.close();
|
||||||
return result;
|
return names;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean isFavorite(long bookId) {
|
@Override
|
||||||
|
protected List<String> labels(long bookId) {
|
||||||
final Cursor cursor = myDatabase.rawQuery(
|
final Cursor cursor = myDatabase.rawQuery(
|
||||||
"SELECT BookLabel.book_id FROM BookLabel" +
|
"SELECT Labels.name FROM Labels" +
|
||||||
" INNER JOIN Labels ON BookLabel.label_id=Labels.label_id" +
|
" INNER JOIN BookLabel ON BookLabel.label_id=Labels.label_id" +
|
||||||
" WHERE BookLabel.book_id=? AND Labels.name='favorite' LIMIT 1",
|
" WHERE BookLabel.book_id=?",
|
||||||
new String[] { String.valueOf(bookId) }
|
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();
|
cursor.close();
|
||||||
return result;
|
return names;
|
||||||
}
|
}
|
||||||
|
|
||||||
private SQLiteStatement myAddToFavoritesStatement;
|
private SQLiteStatement mySetLabelStatement;
|
||||||
protected void addToFavorites(long bookId) {
|
@Override
|
||||||
myDatabase.execSQL("INSERT OR IGNORE INTO Labels (name) VALUES ('favorite')");
|
protected void setLabel(long bookId, String label) {
|
||||||
if (myAddToFavoritesStatement == null) {
|
myDatabase.execSQL("INSERT OR IGNORE INTO Labels (name) VALUES (?)", new Object[] { label });
|
||||||
myAddToFavoritesStatement = myDatabase.compileStatement(
|
if (mySetLabelStatement == null) {
|
||||||
|
mySetLabelStatement = myDatabase.compileStatement(
|
||||||
"INSERT OR IGNORE INTO BookLabel(label_id,book_id)" +
|
"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);
|
mySetLabelStatement.bindLong(1, bookId);
|
||||||
myAddToFavoritesStatement.execute();
|
mySetLabelStatement.bindString(2, label);
|
||||||
|
mySetLabelStatement.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
private SQLiteStatement myRemoveFromFavoritesStatement;
|
private SQLiteStatement myRemoveLabelStatement;
|
||||||
protected void removeFromFavorites(long bookId) {
|
@Override
|
||||||
if (myRemoveFromFavoritesStatement == null) {
|
protected void removeLabel(long bookId, String label) {
|
||||||
myRemoveFromFavoritesStatement = myDatabase.compileStatement(
|
if (myRemoveLabelStatement == null) {
|
||||||
|
myRemoveLabelStatement = myDatabase.compileStatement(
|
||||||
"DELETE FROM BookLabel WHERE book_id=? AND label_id IN" +
|
"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);
|
myRemoveLabelStatement.bindLong(1, bookId);
|
||||||
myRemoveFromFavoritesStatement.execute();
|
myRemoveLabelStatement.bindString(2, label);
|
||||||
|
myRemoveLabelStatement.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<Long> loadFavoriteIds() {
|
@Override
|
||||||
|
protected List<Long> loadBooksForLabelIds(String label) {
|
||||||
final Cursor cursor = myDatabase.rawQuery(
|
final Cursor cursor = myDatabase.rawQuery(
|
||||||
"SELECT BookLabel.book_id FROM BookLabel" +
|
"SELECT BookLabel.book_id FROM BookLabel" +
|
||||||
" INNER JOIN Labels ON BookLabel.label_id=Labels.label_id" +
|
" 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>();
|
final LinkedList<Long> ids = new LinkedList<Long>();
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
|
|
|
@ -293,7 +293,7 @@ public class BookCollection extends AbstractBookCollection {
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Book> booksForLabel(String label) {
|
public List<Book> booksForLabel(String label) {
|
||||||
return books(myDatabase.loadFavoriteIds());
|
return books(myDatabase.loadBooksForLabelIds(label));
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Book> books(List<Long> ids) {
|
private List<Book> books(List<Long> ids) {
|
||||||
|
@ -489,27 +489,23 @@ public class BookCollection extends AbstractBookCollection {
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> labels() {
|
public List<String> labels() {
|
||||||
return myDatabase.hasFavorites()
|
return myDatabase.labels();
|
||||||
? Collections.singletonList(Book.FAVORITE_LABEL)
|
|
||||||
: Collections.<String>emptyList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> labels(Book book) {
|
public List<String> labels(Book book) {
|
||||||
if (book == null) {
|
if (book == null) {
|
||||||
return Collections.<String>emptyList();
|
return Collections.<String>emptyList();
|
||||||
}
|
}
|
||||||
return myDatabase.isFavorite(book.getId())
|
return myDatabase.labels(book.getId());
|
||||||
? Collections.singletonList(Book.FAVORITE_LABEL)
|
|
||||||
: Collections.<String>emptyList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLabel(Book book, String label) {
|
public void setLabel(Book book, String label) {
|
||||||
myDatabase.addToFavorites(book.getId());
|
myDatabase.setLabel(book.getId(), label);
|
||||||
fireBookEvent(BookEvent.Updated, book);
|
fireBookEvent(BookEvent.Updated, book);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeLabel(Book book, String label) {
|
public void removeLabel(Book book, String label) {
|
||||||
myDatabase.removeFromFavorites(book.getId());
|
myDatabase.removeLabel(book.getId(), label);
|
||||||
fireBookEvent(BookEvent.Updated, book);
|
fireBookEvent(BookEvent.Updated, book);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -76,11 +76,11 @@ public abstract class BooksDatabase {
|
||||||
protected abstract List<Long> loadRecentBookIds();
|
protected abstract List<Long> loadRecentBookIds();
|
||||||
protected abstract void saveRecentBookIds(final List<Long> ids);
|
protected abstract void saveRecentBookIds(final List<Long> ids);
|
||||||
|
|
||||||
protected abstract List<Long> loadFavoriteIds();
|
protected abstract List<Long> loadBooksForLabelIds(String label);
|
||||||
protected abstract boolean hasFavorites();
|
protected abstract List<String> labels();
|
||||||
protected abstract boolean isFavorite(long bookId);
|
protected abstract List<String> labels(long bookId);
|
||||||
protected abstract void addToFavorites(long bookId);
|
protected abstract void setLabel(long bookId, String label);
|
||||||
protected abstract void removeFromFavorites(long bookId);
|
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) {
|
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);
|
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