mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 18:29:23 +02:00
list of library items has been moved to a platform-independent code
This commit is contained in:
parent
be09a18e71
commit
f78280c780
8 changed files with 74 additions and 62 deletions
|
@ -58,7 +58,7 @@ abstract class LibraryBaseActivity extends BaseActivity implements MenuItem.OnMe
|
|||
return false;
|
||||
}
|
||||
BookSearchPatternOption.setValue(pattern);
|
||||
return LibraryInstance.searchBooks(pattern).hasChildren();
|
||||
return LibraryInstance.searchBooks(pattern) != null;
|
||||
}
|
||||
|
||||
protected void showNotFoundToast() {
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
package org.geometerplus.android.fbreader.library;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import android.app.SearchManager;
|
||||
import android.content.Intent;
|
||||
|
@ -34,20 +34,11 @@ import org.geometerplus.fbreader.library.FirstLevelTree;
|
|||
import org.geometerplus.android.util.UIUtil;
|
||||
|
||||
public class LibraryTopLevelActivity extends LibraryBaseActivity {
|
||||
private FirstLevelTree mySearchResultsItem;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
final ListAdapter adapter = new ListAdapter(this, Collections.<FBTree>emptyList());
|
||||
|
||||
adapter.add(LibraryInstance.getRootTree(Library.ROOT_FAVORITES));
|
||||
adapter.add(LibraryInstance.getRootTree(Library.ROOT_RECENT));
|
||||
adapter.add(LibraryInstance.getRootTree(Library.ROOT_BY_AUTHOR));
|
||||
adapter.add(LibraryInstance.getRootTree(Library.ROOT_BY_TITLE));
|
||||
adapter.add(LibraryInstance.getRootTree(Library.ROOT_BY_TAG));
|
||||
adapter.add(LibraryInstance.getRootTree(Library.ROOT_FILE_TREE));
|
||||
new ListAdapter(this, LibraryInstance.getRootTree().subTrees());
|
||||
|
||||
onNewIntent(getIntent());
|
||||
}
|
||||
|
@ -58,25 +49,22 @@ public class LibraryTopLevelActivity extends LibraryBaseActivity {
|
|||
super.onDestroy();
|
||||
}
|
||||
|
||||
private void setSearchResults(Intent intent) {
|
||||
final ListAdapter adapter = getListAdapter();
|
||||
adapter.remove(mySearchResultsItem);
|
||||
mySearchResultsItem = LibraryInstance.getRootTree(Library.ROOT_SEARCH_RESULTS);
|
||||
adapter.add(0, mySearchResultsItem);
|
||||
private void setSearchResults() {
|
||||
final List<FBTree> trees = LibraryInstance.getRootTree().subTrees();
|
||||
getListAdapter().replaceAll(trees);
|
||||
getListView().invalidateViews();
|
||||
adapter.notifyDataSetChanged();
|
||||
new OpenTreeRunnable(mySearchResultsItem).run();
|
||||
new OpenTreeRunnable(trees.get(0)).run();
|
||||
}
|
||||
|
||||
public void onNewIntent(Intent intent) {
|
||||
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
|
||||
if (runSearch(intent)) {
|
||||
setSearchResults(intent);
|
||||
setSearchResults();
|
||||
} else {
|
||||
showNotFoundToast();
|
||||
}
|
||||
} else if (ACTION_FOUND.equals(intent.getAction())) {
|
||||
setSearchResults(intent);
|
||||
setSearchResults();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,8 +22,8 @@ package org.geometerplus.fbreader.library;
|
|||
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
||||
|
||||
public class FavoritesTree extends FirstLevelTree {
|
||||
FavoritesTree(Library library, String id) {
|
||||
super(library, id);
|
||||
FavoritesTree(RootTree root, String id) {
|
||||
super(root, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -25,8 +25,8 @@ import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
|||
import org.geometerplus.fbreader.Paths;
|
||||
|
||||
public class FileFirstLevelTree extends FirstLevelTree {
|
||||
FileFirstLevelTree(Library library, String id) {
|
||||
super(library, id);
|
||||
FileFirstLevelTree(RootTree root, String id) {
|
||||
super(root, id);
|
||||
addChild(Paths.BooksDirectoryOption().getValue(), "fileTreeLibrary");
|
||||
addChild("/", "fileTreeRoot");
|
||||
addChild(Paths.cardDirectory(), "fileTreeCard");
|
||||
|
|
|
@ -26,8 +26,16 @@ public class FirstLevelTree extends LibraryTree {
|
|||
private final String myId;
|
||||
private final ZLResource myResource;
|
||||
|
||||
FirstLevelTree(Library library, String id) {
|
||||
myLibrary = library;
|
||||
FirstLevelTree(RootTree root, int position, String id) {
|
||||
super(root, position);
|
||||
myLibrary = root.getLibrary();
|
||||
myId = id;
|
||||
myResource = Library.resource().getResource(myId);
|
||||
}
|
||||
|
||||
FirstLevelTree(RootTree root, String id) {
|
||||
super(root);
|
||||
myLibrary = root.getLibrary();
|
||||
myId = id;
|
||||
myResource = Library.resource().getResource(myId);
|
||||
}
|
||||
|
|
|
@ -51,23 +51,31 @@ public final class Library {
|
|||
|
||||
private final List<Book> myBooks = new LinkedList<Book>();
|
||||
private final Set<Book> myExternalBooks = new HashSet<Book>();
|
||||
private final Map<String,FirstLevelTree> myRootTrees = new HashMap<String,FirstLevelTree>();
|
||||
private final RootTree myRootTree = new RootTree(this);
|
||||
|
||||
private volatile int myState = STATE_NOT_INITIALIZED;
|
||||
private volatile boolean myInterrupted = false;
|
||||
|
||||
public Library() {
|
||||
myRootTrees.put(ROOT_FAVORITES, new FavoritesTree(this, ROOT_FAVORITES));
|
||||
myRootTrees.put(ROOT_FILE_TREE, new FileFirstLevelTree(this, ROOT_FILE_TREE));
|
||||
new FavoritesTree(myRootTree, ROOT_FAVORITES);
|
||||
new FirstLevelTree(myRootTree, ROOT_RECENT);
|
||||
new FirstLevelTree(myRootTree, ROOT_BY_AUTHOR);
|
||||
new FirstLevelTree(myRootTree, ROOT_BY_TITLE);
|
||||
new FirstLevelTree(myRootTree, ROOT_BY_TAG);
|
||||
new FileFirstLevelTree(myRootTree, ROOT_FILE_TREE);
|
||||
}
|
||||
|
||||
public FirstLevelTree getRootTree(String id) {
|
||||
FirstLevelTree root = myRootTrees.get(id);
|
||||
if (root == null) {
|
||||
root = new FirstLevelTree(this, id);
|
||||
myRootTrees.put(id, root);
|
||||
public LibraryTree getRootTree() {
|
||||
return myRootTree;
|
||||
}
|
||||
return root;
|
||||
|
||||
private FirstLevelTree getFirstLevelTree(String key) {
|
||||
for (FBTree tree : myRootTree.subTrees()) {
|
||||
if (key.equals(tree.getUniqueKey().Id)) {
|
||||
return (FirstLevelTree)tree;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public LibraryTree getLibraryTree(LibraryTree.Key key) {
|
||||
|
@ -75,7 +83,7 @@ public final class Library {
|
|||
return null;
|
||||
}
|
||||
if (key.Parent == null) {
|
||||
return getRootTree(key.Id);
|
||||
return key.Id.equals(myRootTree.getUniqueKey().Id) ? myRootTree : null;
|
||||
}
|
||||
final LibraryTree parentTree = getLibraryTree(key.Parent);
|
||||
return parentTree != null ? (LibraryTree)parentTree.getSubTree(key.Id) : null;
|
||||
|
@ -259,7 +267,7 @@ public final class Library {
|
|||
if (tagTree == null) {
|
||||
LibraryTree parent =
|
||||
((tag != null) && (tag.Parent != null)) ?
|
||||
getTagTree(tag.Parent, tagTreeMap) : getRootTree(ROOT_BY_TAG);
|
||||
getTagTree(tag.Parent, tagTreeMap) : getFirstLevelTree(ROOT_BY_TAG);
|
||||
tagTree = parent.createTagSubTree(tag);
|
||||
tagTreeMap.put(tag, tagTree);
|
||||
}
|
||||
|
@ -284,7 +292,7 @@ public final class Library {
|
|||
for (Author a : authors) {
|
||||
AuthorTree authorTree = authorTreeMap.get(a);
|
||||
if (authorTree == null) {
|
||||
authorTree = getRootTree(ROOT_BY_AUTHOR).createAuthorSubTree(a);
|
||||
authorTree = getFirstLevelTree(ROOT_BY_AUTHOR).createAuthorSubTree(a);
|
||||
authorTreeMap.put(a, authorTree);
|
||||
}
|
||||
if (seriesInfo == null) {
|
||||
|
@ -338,14 +346,14 @@ public final class Library {
|
|||
Character c = title.charAt(0);
|
||||
TitleTree tree = letterTrees.get(c);
|
||||
if (tree == null) {
|
||||
tree = getRootTree(ROOT_BY_TITLE).createTitleSubTree(c.toString());
|
||||
tree = getFirstLevelTree(ROOT_BY_TITLE).createTitleSubTree(c.toString());
|
||||
letterTrees.put(c, tree);
|
||||
}
|
||||
tree.createBookSubTree(book, true);
|
||||
}
|
||||
} else {
|
||||
for (Book book : myBooks) {
|
||||
getRootTree(ROOT_BY_TITLE).createBookSubTree(book, true);
|
||||
getFirstLevelTree(ROOT_BY_TITLE).createBookSubTree(book, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -353,21 +361,21 @@ public final class Library {
|
|||
for (long id : db.loadRecentBookIds()) {
|
||||
Book book = bookById.get(id);
|
||||
if (book != null) {
|
||||
getRootTree(ROOT_RECENT).createBookSubTree(book, true);
|
||||
getFirstLevelTree(ROOT_RECENT).createBookSubTree(book, true);
|
||||
}
|
||||
}
|
||||
|
||||
for (long id : db.loadFavoritesIds()) {
|
||||
Book book = bookById.get(id);
|
||||
if (book != null) {
|
||||
getRootTree(ROOT_FAVORITES).createBookSubTree(book, true);
|
||||
getFirstLevelTree(ROOT_FAVORITES).createBookSubTree(book, true);
|
||||
}
|
||||
}
|
||||
|
||||
getRootTree(ROOT_FAVORITES).sortAllChildren();
|
||||
getRootTree(ROOT_BY_AUTHOR).sortAllChildren();
|
||||
getRootTree(ROOT_BY_TITLE).sortAllChildren();
|
||||
getRootTree(ROOT_BY_TAG).sortAllChildren();
|
||||
getFirstLevelTree(ROOT_FAVORITES).sortAllChildren();
|
||||
getFirstLevelTree(ROOT_BY_AUTHOR).sortAllChildren();
|
||||
getFirstLevelTree(ROOT_BY_TITLE).sortAllChildren();
|
||||
getFirstLevelTree(ROOT_BY_TAG).sortAllChildren();
|
||||
|
||||
db.executeAsATransaction(new Runnable() {
|
||||
public void run() {
|
||||
|
@ -402,23 +410,29 @@ public final class Library {
|
|||
return (recentIds.size() > 1) ? Book.getById(recentIds.get(1)) : null;
|
||||
}
|
||||
|
||||
public LibraryTree searchResults() {
|
||||
return getRootTree(ROOT_SEARCH_RESULTS);
|
||||
private FirstLevelTree createNewSearchResults(String pattern) {
|
||||
final FirstLevelTree old = getFirstLevelTree(ROOT_SEARCH_RESULTS);
|
||||
if (old != null) {
|
||||
old.removeSelf();
|
||||
}
|
||||
return new SearchResultsTree(myRootTree, ROOT_SEARCH_RESULTS, pattern);
|
||||
}
|
||||
|
||||
public LibraryTree searchBooks(String pattern) {
|
||||
waitForState(STATE_FULLY_INITIALIZED);
|
||||
final FirstLevelTree newSearchResults = new SearchResultsTree(this, ROOT_SEARCH_RESULTS, pattern);
|
||||
FirstLevelTree newSearchResults = null;
|
||||
if (pattern != null) {
|
||||
pattern = pattern.toLowerCase();
|
||||
for (Book book : myBooks) {
|
||||
if (book.matches(pattern)) {
|
||||
if (newSearchResults == null) {
|
||||
newSearchResults = createNewSearchResults(pattern);
|
||||
}
|
||||
newSearchResults.createBookSubTree(book, true);
|
||||
}
|
||||
}
|
||||
if (newSearchResults != null) {
|
||||
newSearchResults.sortAllChildren();
|
||||
if (newSearchResults.hasChildren()) {
|
||||
myRootTrees.put(ROOT_SEARCH_RESULTS, newSearchResults);
|
||||
}
|
||||
}
|
||||
return newSearchResults;
|
||||
|
@ -441,7 +455,7 @@ public final class Library {
|
|||
return false;
|
||||
}
|
||||
waitForState(STATE_FULLY_INITIALIZED);
|
||||
final LibraryTree rootFavorites = getRootTree(ROOT_FAVORITES);
|
||||
final LibraryTree rootFavorites = getFirstLevelTree(ROOT_FAVORITES);
|
||||
for (FBTree tree : rootFavorites.subTrees()) {
|
||||
if (tree instanceof BookTree && book.equals(((BookTree)tree).Book)) {
|
||||
return true;
|
||||
|
@ -455,7 +469,7 @@ public final class Library {
|
|||
if (isBookInFavorites(book)) {
|
||||
return;
|
||||
}
|
||||
final LibraryTree rootFavorites = getRootTree(ROOT_FAVORITES);
|
||||
final LibraryTree rootFavorites = getFirstLevelTree(ROOT_FAVORITES);
|
||||
rootFavorites.createBookSubTree(book, true);
|
||||
rootFavorites.sortAllChildren();
|
||||
BooksDatabase.Instance().addToFavorites(book.getId());
|
||||
|
@ -463,7 +477,7 @@ public final class Library {
|
|||
|
||||
public void removeBookFromFavorites(Book book) {
|
||||
waitForState(STATE_FULLY_INITIALIZED);
|
||||
if (getRootTree(ROOT_FAVORITES).removeBook(book)) {
|
||||
if (getFirstLevelTree(ROOT_FAVORITES).removeBook(book)) {
|
||||
BooksDatabase.Instance().removeFromFavorites(book.getId());
|
||||
}
|
||||
}
|
||||
|
@ -499,15 +513,13 @@ public final class Library {
|
|||
}
|
||||
waitForState(STATE_FULLY_INITIALIZED);
|
||||
myBooks.remove(book);
|
||||
if (getRootTree(ROOT_RECENT).removeBook(book)) {
|
||||
if (getFirstLevelTree(ROOT_RECENT).removeBook(book)) {
|
||||
final BooksDatabase db = BooksDatabase.Instance();
|
||||
final List<Long> ids = db.loadRecentBookIds();
|
||||
ids.remove(book.getId());
|
||||
db.saveRecentBookIds(ids);
|
||||
}
|
||||
for (LibraryTree root : myRootTrees.values()) {
|
||||
root.removeBook(book);
|
||||
}
|
||||
myRootTree.removeBook(book);
|
||||
|
||||
BooksDatabase.Instance().deleteFromBookList(book.getId());
|
||||
if ((removeMode & REMOVE_FROM_DISK) != 0) {
|
||||
|
|
|
@ -32,6 +32,10 @@ public abstract class LibraryTree extends FBTree {
|
|||
super(parent);
|
||||
}
|
||||
|
||||
protected LibraryTree(LibraryTree parent, int position) {
|
||||
super(parent, position);
|
||||
}
|
||||
|
||||
public String getTreeTitle() {
|
||||
return getName();
|
||||
}
|
||||
|
|
|
@ -22,8 +22,8 @@ package org.geometerplus.fbreader.library;
|
|||
class SearchResultsTree extends FirstLevelTree {
|
||||
private final String myPattern;
|
||||
|
||||
SearchResultsTree(Library library, String id, String pattern) {
|
||||
super(library, id);
|
||||
SearchResultsTree(RootTree root, String id, String pattern) {
|
||||
super(root, 0, id);
|
||||
myPattern = pattern != null ? pattern : "";
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue