1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 10:19:33 +02:00

Favorites item in library (in progress)

This commit is contained in:
Nikolay Pultsin 2010-12-06 17:49:11 +00:00
parent 215dd88f22
commit d1f4f7798a
4 changed files with 67 additions and 1 deletions

View file

@ -60,7 +60,7 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
private void migrate(Context context) { private void migrate(Context context) {
final int version = myDatabase.getVersion(); final int version = myDatabase.getVersion();
final int currentVersion = 10; final int currentVersion = 11;
if (version >= currentVersion) { if (version >= currentVersion) {
return; return;
} }
@ -89,6 +89,8 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
updateTables8(); updateTables8();
case 9: case 9:
updateTables9(); updateTables9();
case 10:
updateTables10();
} }
myDatabase.setTransactionSuccessful(); myDatabase.setTransactionSuccessful();
myDatabase.endTransaction(); myDatabase.endTransaction();
@ -646,6 +648,36 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
return ids; return ids;
} }
private SQLiteStatement mySaveFavoritesStatement;
protected void saveFavoritesIds(final List<Long> ids) {
if (mySaveFavoritesStatement == null) {
mySaveFavoritesStatement = myDatabase.compileStatement(
"INSERT 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();
}
}
});
}
protected List<Long> loadFavoritesIds() {
final Cursor cursor = myDatabase.rawQuery(
"SELECT book_id FROM Favorites", null
);
final LinkedList<Long> ids = new LinkedList<Long>();
while (cursor.moveToNext()) {
ids.add(cursor.getLong(0));
}
cursor.close();
return ids;
}
protected List<Bookmark> loadBookmarks(long bookId) { protected List<Bookmark> loadBookmarks(long bookId) {
LinkedList<Bookmark> list = new LinkedList<Bookmark>(); LinkedList<Bookmark> list = new LinkedList<Bookmark>();
Cursor cursor = myDatabase.rawQuery( Cursor cursor = myDatabase.rawQuery(
@ -1071,4 +1103,10 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
private void updateTables9() { private void updateTables9() {
myDatabase.execSQL("CREATE INDEX BookList_BookIndex ON BookList (book_id)"); myDatabase.execSQL("CREATE INDEX BookList_BookIndex ON BookList (book_id)");
} }
private void updateTables10() {
myDatabase.execSQL(
"CREATE TABLE IF NOT EXISTS Favorites(" +
"book_id INTEGER UNIQUE NOT NULL REFERENCES Books(book_id))");
}
} }

View file

@ -78,6 +78,7 @@ public class LibraryTreeActivity extends LibraryBaseActivity {
} else if (PATH_BY_TAG.equals(path[0])) { } else if (PATH_BY_TAG.equals(path[0])) {
tree = Library.byTag(); tree = Library.byTag();
} else if (PATH_FAVORITES.equals(path[0])) { } else if (PATH_FAVORITES.equals(path[0])) {
tree = Library.favorites();
} }
for (int i = 1; i < path.length; ++i) { for (int i = 1; i < path.length; ++i) {

View file

@ -84,6 +84,9 @@ 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> loadFavoritesIds();
protected abstract void saveFavoritesIds(final List<Long> ids);
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);
} }

View file

@ -36,6 +36,7 @@ public final class Library {
private final LibraryTree myLibraryByAuthor = new RootTree(); private final LibraryTree myLibraryByAuthor = new RootTree();
private final LibraryTree myLibraryByTag = new RootTree(); private final LibraryTree myLibraryByTag = new RootTree();
private final LibraryTree myRecentBooks = new RootTree(); private final LibraryTree myRecentBooks = new RootTree();
private final LibraryTree myFavorites = new RootTree();
private final LibraryTree mySearchResult = new RootTree(); private final LibraryTree mySearchResult = new RootTree();
private volatile int myState = STATE_NOT_INITIALIZED; private volatile int myState = STATE_NOT_INITIALIZED;
@ -268,6 +269,14 @@ public final class Library {
} }
} }
for (long id : db.loadFavoritesIds()) {
Book book = bookById.get(id);
if (book != null) {
myFavorites.createBookSubTree(book, true);
}
}
myFavorites.sortAllChildren();
db.executeAsATransaction(new Runnable() { db.executeAsATransaction(new Runnable() {
public void run() { public void run() {
for (Book book : myBooks) { for (Book book : myBooks) {
@ -309,6 +318,11 @@ public final class Library {
return (recentIds.size() > 0) ? Book.getById(recentIds.get(0)) : null; return (recentIds.size() > 0) ? Book.getById(recentIds.get(0)) : null;
} }
public LibraryTree favorites() {
waitForState(STATE_FULLY_INITIALIZED);
return myFavorites;
}
public LibraryTree searchResults() { public LibraryTree searchResults() {
return mySearchResult; return mySearchResult;
} }
@ -340,6 +354,16 @@ public final class Library {
db.saveRecentBookIds(ids); 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 static final int REMOVE_DONT_REMOVE = 0x00; public static final int REMOVE_DONT_REMOVE = 0x00;
public static final int REMOVE_FROM_LIBRARY = 0x01; public static final int REMOVE_FROM_LIBRARY = 0x01;
public static final int REMOVE_FROM_DISK = 0x02; public static final int REMOVE_FROM_DISK = 0x02;