mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 18:29:23 +02:00
favorites code: simplified
This commit is contained in:
parent
82cc9fd8d2
commit
8207b5db87
11 changed files with 101 additions and 46 deletions
|
@ -206,7 +206,7 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem
|
||||||
if (book.File.getPhysicalFile() != null) {
|
if (book.File.getPhysicalFile() != null) {
|
||||||
menu.add(0, SHARE_BOOK_ITEM_ID, 0, resource.getResource("shareBook").getValue());
|
menu.add(0, SHARE_BOOK_ITEM_ID, 0, resource.getResource("shareBook").getValue());
|
||||||
}
|
}
|
||||||
if (myLibrary.isBookInFavorites(book)) {
|
if (myLibrary.Collection.isFavorite(book)) {
|
||||||
menu.add(0, REMOVE_FROM_FAVORITES_ITEM_ID, 0, resource.getResource("removeFromFavorites").getValue());
|
menu.add(0, REMOVE_FROM_FAVORITES_ITEM_ID, 0, resource.getResource("removeFromFavorites").getValue());
|
||||||
} else {
|
} else {
|
||||||
menu.add(0, ADD_TO_FAVORITES_ITEM_ID, 0, resource.getResource("addToFavorites").getValue());
|
menu.add(0, ADD_TO_FAVORITES_ITEM_ID, 0, resource.getResource("addToFavorites").getValue());
|
||||||
|
@ -238,11 +238,14 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem
|
||||||
FBUtil.shareBook(this, book);
|
FBUtil.shareBook(this, book);
|
||||||
return true;
|
return true;
|
||||||
case ADD_TO_FAVORITES_ITEM_ID:
|
case ADD_TO_FAVORITES_ITEM_ID:
|
||||||
myLibrary.addBookToFavorites(book);
|
myLibrary.Collection.setBookFavorite(book, true);
|
||||||
return true;
|
return true;
|
||||||
case REMOVE_FROM_FAVORITES_ITEM_ID:
|
case REMOVE_FROM_FAVORITES_ITEM_ID:
|
||||||
myLibrary.removeBookFromFavorites(book);
|
myLibrary.Collection.setBookFavorite(book, false);
|
||||||
|
if (((LibraryTree)getCurrentTree()).onBookChanged(book)) {
|
||||||
|
getListAdapter().replaceAll(getCurrentTree().subTrees());
|
||||||
getListView().invalidateViews();
|
getListView().invalidateViews();
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
case DELETE_BOOK_ITEM_ID:
|
case DELETE_BOOK_ITEM_ID:
|
||||||
tryToDeleteBook(book);
|
tryToDeleteBook(book);
|
||||||
|
|
|
@ -204,6 +204,26 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized boolean hasFavorites() {
|
||||||
|
if (myInterface != null) {
|
||||||
|
try {
|
||||||
|
return myInterface.hasFavorites();
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean isFavorite(Book book) {
|
||||||
|
if (myInterface != null) {
|
||||||
|
try {
|
||||||
|
return myInterface.isFavorite(SerializerUtil.serialize(book));
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public synchronized void setBookFavorite(Book book, boolean favorite) {
|
public synchronized void setBookFavorite(Book book, boolean favorite) {
|
||||||
if (myInterface != null) {
|
if (myInterface != null) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -20,6 +20,9 @@ interface LibraryInterface {
|
||||||
boolean saveBook(in String book, in boolean force);
|
boolean saveBook(in String book, in boolean force);
|
||||||
void removeBook(in String book, in boolean deleteFromDisk);
|
void removeBook(in String book, in boolean deleteFromDisk);
|
||||||
void addBookToRecentList(in String book);
|
void addBookToRecentList(in String book);
|
||||||
|
|
||||||
|
boolean hasFavorites();
|
||||||
|
boolean isFavorite(in String book);
|
||||||
void setBookFavorite(in String book, in boolean favorite);
|
void setBookFavorite(in String book, in boolean favorite);
|
||||||
|
|
||||||
TextPosition getStoredPosition(in long bookId);
|
TextPosition getStoredPosition(in long bookId);
|
||||||
|
|
|
@ -161,6 +161,14 @@ public class LibraryService extends Service {
|
||||||
myCollection.addBookToRecentList(SerializerUtil.deserializeBook(book));
|
myCollection.addBookToRecentList(SerializerUtil.deserializeBook(book));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasFavorites() {
|
||||||
|
return myCollection.hasFavorites();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isFavorite(String book) {
|
||||||
|
return myCollection.isFavorite(SerializerUtil.deserializeBook(book));
|
||||||
|
}
|
||||||
|
|
||||||
public void setBookFavorite(String book, boolean favorite) {
|
public void setBookFavorite(String book, boolean favorite) {
|
||||||
myCollection.setBookFavorite(SerializerUtil.deserializeBook(book), favorite);
|
myCollection.setBookFavorite(SerializerUtil.deserializeBook(book), favorite);
|
||||||
}
|
}
|
||||||
|
|
|
@ -691,6 +691,25 @@ final class SQLiteBooksDatabase extends BooksDatabase {
|
||||||
return ids;
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean hasFavorites() {
|
||||||
|
final Cursor cursor = myDatabase.rawQuery(
|
||||||
|
"SELECT book_id FROM Favorites LIMIT 1", null
|
||||||
|
);
|
||||||
|
boolean result = cursor.moveToNext();
|
||||||
|
cursor.close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean isFavorite(long bookId) {
|
||||||
|
final Cursor cursor = myDatabase.rawQuery(
|
||||||
|
"SELECT book_id FROM Favorites WHERE book_id = ? LIMIT 1",
|
||||||
|
new String[] { String.valueOf(bookId) }
|
||||||
|
);
|
||||||
|
boolean result = cursor.moveToNext();
|
||||||
|
cursor.close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private SQLiteStatement myAddToFavoritesStatement;
|
private SQLiteStatement myAddToFavoritesStatement;
|
||||||
protected void addToFavorites(long bookId) {
|
protected void addToFavorites(long bookId) {
|
||||||
if (myAddToFavoritesStatement == null) {
|
if (myAddToFavoritesStatement == null) {
|
||||||
|
|
|
@ -243,6 +243,17 @@ public class BookCollection extends AbstractBookCollection {
|
||||||
myDatabase.saveRecentBookIds(ids);
|
myDatabase.saveRecentBookIds(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasFavorites() {
|
||||||
|
return myDatabase.hasFavorites();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isFavorite(Book book) {
|
||||||
|
if (book == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return myDatabase.isFavorite(book.getId());
|
||||||
|
}
|
||||||
|
|
||||||
public void setBookFavorite(Book book, boolean favorite) {
|
public void setBookFavorite(Book book, boolean favorite) {
|
||||||
if (favorite) {
|
if (favorite) {
|
||||||
myDatabase.addToFavorites(book.getId());
|
myDatabase.addToFavorites(book.getId());
|
||||||
|
|
|
@ -77,6 +77,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> loadFavoriteIds();
|
protected abstract List<Long> loadFavoriteIds();
|
||||||
|
protected abstract boolean hasFavorites();
|
||||||
|
protected abstract boolean isFavorite(long bookId);
|
||||||
protected abstract void addToFavorites(long bookId);
|
protected abstract void addToFavorites(long bookId);
|
||||||
protected abstract void removeFromFavorites(long bookId);
|
protected abstract void removeFromFavorites(long bookId);
|
||||||
|
|
||||||
|
|
|
@ -59,7 +59,11 @@ public interface IBookCollection {
|
||||||
|
|
||||||
boolean saveBook(Book book, boolean force);
|
boolean saveBook(Book book, boolean force);
|
||||||
void removeBook(Book book, boolean deleteFromDisk);
|
void removeBook(Book book, boolean deleteFromDisk);
|
||||||
|
|
||||||
void addBookToRecentList(Book book);
|
void addBookToRecentList(Book book);
|
||||||
|
|
||||||
|
boolean hasFavorites();
|
||||||
|
boolean isFavorite(Book book);
|
||||||
void setBookFavorite(Book book, boolean favorite);
|
void setBookFavorite(Book book, boolean favorite);
|
||||||
|
|
||||||
ZLTextPosition getStoredPosition(long bookId);
|
ZLTextPosition getStoredPosition(long bookId);
|
||||||
|
|
|
@ -19,18 +19,23 @@
|
||||||
|
|
||||||
package org.geometerplus.fbreader.library;
|
package org.geometerplus.fbreader.library;
|
||||||
|
|
||||||
|
import org.geometerplus.fbreader.book.Book;
|
||||||
|
import org.geometerplus.fbreader.book.IBookCollection;
|
||||||
|
|
||||||
public class FavoritesTree extends FirstLevelTree {
|
public class FavoritesTree extends FirstLevelTree {
|
||||||
FavoritesTree(RootTree root, String id) {
|
private final IBookCollection myCollection;
|
||||||
|
|
||||||
|
FavoritesTree(IBookCollection collection, RootTree root, String id) {
|
||||||
super(root, id);
|
super(root, id);
|
||||||
|
myCollection = collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Status getOpeningStatus() {
|
public Status getOpeningStatus() {
|
||||||
final Status status = super.getOpeningStatus();
|
if (!myCollection.hasFavorites()) {
|
||||||
if (status == Status.READY_TO_OPEN && !hasChildren()) {
|
|
||||||
return Status.CANNOT_OPEN;
|
return Status.CANNOT_OPEN;
|
||||||
}
|
}
|
||||||
return status;
|
return Status.ALWAYS_RELOAD_BEFORE_OPENING;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -38,4 +43,16 @@ public class FavoritesTree extends FirstLevelTree {
|
||||||
return getOpeningStatus() == Status.CANNOT_OPEN
|
return getOpeningStatus() == Status.CANNOT_OPEN
|
||||||
? "noFavorites" : super.getOpeningStatusMessage();
|
? "noFavorites" : super.getOpeningStatusMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void waitForOpening() {
|
||||||
|
clear();
|
||||||
|
for (Book book : myCollection.favorites()) {
|
||||||
|
new BookTree(this, book, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean onBookChanged(Book book) {
|
||||||
|
return !myCollection.isFavorite(book) && removeBook(book, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,7 +99,7 @@ public final class Library {
|
||||||
public Library(IBookCollection collection) {
|
public Library(IBookCollection collection) {
|
||||||
Collection = collection;
|
Collection = collection;
|
||||||
|
|
||||||
new FavoritesTree(myRootTree, ROOT_FAVORITES);
|
new FavoritesTree(collection, myRootTree, ROOT_FAVORITES);
|
||||||
new FirstLevelTree(myRootTree, ROOT_RECENT);
|
new FirstLevelTree(myRootTree, ROOT_RECENT);
|
||||||
new FirstLevelTree(myRootTree, ROOT_BY_AUTHOR);
|
new FirstLevelTree(myRootTree, ROOT_BY_AUTHOR);
|
||||||
new FirstLevelTree(myRootTree, ROOT_BY_TITLE);
|
new FirstLevelTree(myRootTree, ROOT_BY_TITLE);
|
||||||
|
@ -143,10 +143,6 @@ public final class Library {
|
||||||
for (Book book : Collection.recentBooks()) {
|
for (Book book : Collection.recentBooks()) {
|
||||||
new BookTree(getFirstLevelTree(ROOT_RECENT), book, true);
|
new BookTree(getFirstLevelTree(ROOT_RECENT), book, true);
|
||||||
}
|
}
|
||||||
getFirstLevelTree(ROOT_FAVORITES).clear();
|
|
||||||
for (Book book : Collection.favorites()) {
|
|
||||||
new BookTree(getFirstLevelTree(ROOT_FAVORITES), book, true);
|
|
||||||
}
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (Book book : Collection.books()) {
|
for (Book book : Collection.books()) {
|
||||||
addBookToLibrary(book);
|
addBookToLibrary(book);
|
||||||
|
@ -340,39 +336,6 @@ public final class Library {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addBookToRecentList(Book book) {
|
|
||||||
Collection.addBookToRecentList(book);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isBookInFavorites(Book book) {
|
|
||||||
if (book == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
final LibraryTree rootFavorites = getFirstLevelTree(ROOT_FAVORITES);
|
|
||||||
for (FBTree tree : rootFavorites.subTrees()) {
|
|
||||||
if (tree instanceof BookTree && book.equals(((BookTree)tree).Book)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addBookToFavorites(Book book) {
|
|
||||||
if (isBookInFavorites(book)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final LibraryTree rootFavorites = getFirstLevelTree(ROOT_FAVORITES);
|
|
||||||
rootFavorites.getBookSubTree(book, true);
|
|
||||||
Collection.setBookFavorite(book, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeBookFromFavorites(Book book) {
|
|
||||||
if (getFirstLevelTree(ROOT_FAVORITES).removeBook(book, false)) {
|
|
||||||
Collection.setBookFavorite(book, false);
|
|
||||||
fireModelChangedEvent(ChangeListener.Code.BookRemoved);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean canRemoveBookFile(Book book) {
|
public boolean canRemoveBookFile(Book book) {
|
||||||
ZLFile file = book.File;
|
ZLFile file = book.File;
|
||||||
if (file.getPhysicalFile() == null) {
|
if (file.getPhysicalFile() == null) {
|
||||||
|
|
|
@ -118,6 +118,11 @@ public abstract class LibraryTree extends FBTree {
|
||||||
return !toRemove.isEmpty();
|
return !toRemove.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: change to abstract (?)
|
||||||
|
public boolean onBookChanged(Book book) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(FBTree tree) {
|
public int compareTo(FBTree tree) {
|
||||||
final int cmp = super.compareTo(tree);
|
final int cmp = super.compareTo(tree);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue