1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 02:39:23 +02:00

list of library items has been moved to a platform-independent code

This commit is contained in:
Nikolay Pultsin 2011-07-14 18:26:26 +01:00
parent be09a18e71
commit f78280c780
8 changed files with 74 additions and 62 deletions

View file

@ -58,7 +58,7 @@ abstract class LibraryBaseActivity extends BaseActivity implements MenuItem.OnMe
return false; return false;
} }
BookSearchPatternOption.setValue(pattern); BookSearchPatternOption.setValue(pattern);
return LibraryInstance.searchBooks(pattern).hasChildren(); return LibraryInstance.searchBooks(pattern) != null;
} }
protected void showNotFoundToast() { protected void showNotFoundToast() {

View file

@ -19,7 +19,7 @@
package org.geometerplus.android.fbreader.library; package org.geometerplus.android.fbreader.library;
import java.util.Collections; import java.util.List;
import android.app.SearchManager; import android.app.SearchManager;
import android.content.Intent; import android.content.Intent;
@ -34,20 +34,11 @@ import org.geometerplus.fbreader.library.FirstLevelTree;
import org.geometerplus.android.util.UIUtil; import org.geometerplus.android.util.UIUtil;
public class LibraryTopLevelActivity extends LibraryBaseActivity { public class LibraryTopLevelActivity extends LibraryBaseActivity {
private FirstLevelTree mySearchResultsItem;
@Override @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
final ListAdapter adapter = new ListAdapter(this, Collections.<FBTree>emptyList()); new ListAdapter(this, LibraryInstance.getRootTree().subTrees());
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));
onNewIntent(getIntent()); onNewIntent(getIntent());
} }
@ -58,25 +49,22 @@ public class LibraryTopLevelActivity extends LibraryBaseActivity {
super.onDestroy(); super.onDestroy();
} }
private void setSearchResults(Intent intent) { private void setSearchResults() {
final ListAdapter adapter = getListAdapter(); final List<FBTree> trees = LibraryInstance.getRootTree().subTrees();
adapter.remove(mySearchResultsItem); getListAdapter().replaceAll(trees);
mySearchResultsItem = LibraryInstance.getRootTree(Library.ROOT_SEARCH_RESULTS);
adapter.add(0, mySearchResultsItem);
getListView().invalidateViews(); getListView().invalidateViews();
adapter.notifyDataSetChanged(); new OpenTreeRunnable(trees.get(0)).run();
new OpenTreeRunnable(mySearchResultsItem).run();
} }
public void onNewIntent(Intent intent) { public void onNewIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
if (runSearch(intent)) { if (runSearch(intent)) {
setSearchResults(intent); setSearchResults();
} else { } else {
showNotFoundToast(); showNotFoundToast();
} }
} else if (ACTION_FOUND.equals(intent.getAction())) { } else if (ACTION_FOUND.equals(intent.getAction())) {
setSearchResults(intent); setSearchResults();
} }
} }
} }

View file

@ -22,8 +22,8 @@ package org.geometerplus.fbreader.library;
import org.geometerplus.zlibrary.core.resources.ZLResource; import org.geometerplus.zlibrary.core.resources.ZLResource;
public class FavoritesTree extends FirstLevelTree { public class FavoritesTree extends FirstLevelTree {
FavoritesTree(Library library, String id) { FavoritesTree(RootTree root, String id) {
super(library, id); super(root, id);
} }
@Override @Override

View file

@ -25,8 +25,8 @@ import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.fbreader.Paths; import org.geometerplus.fbreader.Paths;
public class FileFirstLevelTree extends FirstLevelTree { public class FileFirstLevelTree extends FirstLevelTree {
FileFirstLevelTree(Library library, String id) { FileFirstLevelTree(RootTree root, String id) {
super(library, id); super(root, id);
addChild(Paths.BooksDirectoryOption().getValue(), "fileTreeLibrary"); addChild(Paths.BooksDirectoryOption().getValue(), "fileTreeLibrary");
addChild("/", "fileTreeRoot"); addChild("/", "fileTreeRoot");
addChild(Paths.cardDirectory(), "fileTreeCard"); addChild(Paths.cardDirectory(), "fileTreeCard");

View file

@ -26,8 +26,16 @@ public class FirstLevelTree extends LibraryTree {
private final String myId; private final String myId;
private final ZLResource myResource; private final ZLResource myResource;
FirstLevelTree(Library library, String id) { FirstLevelTree(RootTree root, int position, String id) {
myLibrary = library; 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; myId = id;
myResource = Library.resource().getResource(myId); myResource = Library.resource().getResource(myId);
} }

View file

@ -51,23 +51,31 @@ public final class Library {
private final List<Book> myBooks = new LinkedList<Book>(); private final List<Book> myBooks = new LinkedList<Book>();
private final Set<Book> myExternalBooks = new HashSet<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 int myState = STATE_NOT_INITIALIZED;
private volatile boolean myInterrupted = false; private volatile boolean myInterrupted = false;
public Library() { public Library() {
myRootTrees.put(ROOT_FAVORITES, new FavoritesTree(this, ROOT_FAVORITES)); new FavoritesTree(myRootTree, ROOT_FAVORITES);
myRootTrees.put(ROOT_FILE_TREE, new FileFirstLevelTree(this, ROOT_FILE_TREE)); 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) { public LibraryTree getRootTree() {
FirstLevelTree root = myRootTrees.get(id); return myRootTree;
if (root == null) {
root = new FirstLevelTree(this, id);
myRootTrees.put(id, root);
} }
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) { public LibraryTree getLibraryTree(LibraryTree.Key key) {
@ -75,7 +83,7 @@ public final class Library {
return null; return null;
} }
if (key.Parent == null) { if (key.Parent == null) {
return getRootTree(key.Id); return key.Id.equals(myRootTree.getUniqueKey().Id) ? myRootTree : null;
} }
final LibraryTree parentTree = getLibraryTree(key.Parent); final LibraryTree parentTree = getLibraryTree(key.Parent);
return parentTree != null ? (LibraryTree)parentTree.getSubTree(key.Id) : null; return parentTree != null ? (LibraryTree)parentTree.getSubTree(key.Id) : null;
@ -259,7 +267,7 @@ public final class Library {
if (tagTree == null) { if (tagTree == null) {
LibraryTree parent = LibraryTree parent =
((tag != null) && (tag.Parent != null)) ? ((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); tagTree = parent.createTagSubTree(tag);
tagTreeMap.put(tag, tagTree); tagTreeMap.put(tag, tagTree);
} }
@ -284,7 +292,7 @@ public final class Library {
for (Author a : authors) { for (Author a : authors) {
AuthorTree authorTree = authorTreeMap.get(a); AuthorTree authorTree = authorTreeMap.get(a);
if (authorTree == null) { if (authorTree == null) {
authorTree = getRootTree(ROOT_BY_AUTHOR).createAuthorSubTree(a); authorTree = getFirstLevelTree(ROOT_BY_AUTHOR).createAuthorSubTree(a);
authorTreeMap.put(a, authorTree); authorTreeMap.put(a, authorTree);
} }
if (seriesInfo == null) { if (seriesInfo == null) {
@ -338,14 +346,14 @@ public final class Library {
Character c = title.charAt(0); Character c = title.charAt(0);
TitleTree tree = letterTrees.get(c); TitleTree tree = letterTrees.get(c);
if (tree == null) { if (tree == null) {
tree = getRootTree(ROOT_BY_TITLE).createTitleSubTree(c.toString()); tree = getFirstLevelTree(ROOT_BY_TITLE).createTitleSubTree(c.toString());
letterTrees.put(c, tree); letterTrees.put(c, tree);
} }
tree.createBookSubTree(book, true); tree.createBookSubTree(book, true);
} }
} else { } else {
for (Book book : myBooks) { 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()) { for (long id : db.loadRecentBookIds()) {
Book book = bookById.get(id); Book book = bookById.get(id);
if (book != null) { if (book != null) {
getRootTree(ROOT_RECENT).createBookSubTree(book, true); getFirstLevelTree(ROOT_RECENT).createBookSubTree(book, true);
} }
} }
for (long id : db.loadFavoritesIds()) { for (long id : db.loadFavoritesIds()) {
Book book = bookById.get(id); Book book = bookById.get(id);
if (book != null) { if (book != null) {
getRootTree(ROOT_FAVORITES).createBookSubTree(book, true); getFirstLevelTree(ROOT_FAVORITES).createBookSubTree(book, true);
} }
} }
getRootTree(ROOT_FAVORITES).sortAllChildren(); getFirstLevelTree(ROOT_FAVORITES).sortAllChildren();
getRootTree(ROOT_BY_AUTHOR).sortAllChildren(); getFirstLevelTree(ROOT_BY_AUTHOR).sortAllChildren();
getRootTree(ROOT_BY_TITLE).sortAllChildren(); getFirstLevelTree(ROOT_BY_TITLE).sortAllChildren();
getRootTree(ROOT_BY_TAG).sortAllChildren(); getFirstLevelTree(ROOT_BY_TAG).sortAllChildren();
db.executeAsATransaction(new Runnable() { db.executeAsATransaction(new Runnable() {
public void run() { public void run() {
@ -402,23 +410,29 @@ public final class Library {
return (recentIds.size() > 1) ? Book.getById(recentIds.get(1)) : null; return (recentIds.size() > 1) ? Book.getById(recentIds.get(1)) : null;
} }
public LibraryTree searchResults() { private FirstLevelTree createNewSearchResults(String pattern) {
return getRootTree(ROOT_SEARCH_RESULTS); 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) { public LibraryTree searchBooks(String pattern) {
waitForState(STATE_FULLY_INITIALIZED); waitForState(STATE_FULLY_INITIALIZED);
final FirstLevelTree newSearchResults = new SearchResultsTree(this, ROOT_SEARCH_RESULTS, pattern); FirstLevelTree newSearchResults = null;
if (pattern != null) { if (pattern != null) {
pattern = pattern.toLowerCase(); pattern = pattern.toLowerCase();
for (Book book : myBooks) { for (Book book : myBooks) {
if (book.matches(pattern)) { if (book.matches(pattern)) {
if (newSearchResults == null) {
newSearchResults = createNewSearchResults(pattern);
}
newSearchResults.createBookSubTree(book, true); newSearchResults.createBookSubTree(book, true);
} }
} }
if (newSearchResults != null) {
newSearchResults.sortAllChildren(); newSearchResults.sortAllChildren();
if (newSearchResults.hasChildren()) {
myRootTrees.put(ROOT_SEARCH_RESULTS, newSearchResults);
} }
} }
return newSearchResults; return newSearchResults;
@ -441,7 +455,7 @@ public final class Library {
return false; return false;
} }
waitForState(STATE_FULLY_INITIALIZED); waitForState(STATE_FULLY_INITIALIZED);
final LibraryTree rootFavorites = getRootTree(ROOT_FAVORITES); final LibraryTree rootFavorites = getFirstLevelTree(ROOT_FAVORITES);
for (FBTree tree : rootFavorites.subTrees()) { for (FBTree tree : rootFavorites.subTrees()) {
if (tree instanceof BookTree && book.equals(((BookTree)tree).Book)) { if (tree instanceof BookTree && book.equals(((BookTree)tree).Book)) {
return true; return true;
@ -455,7 +469,7 @@ public final class Library {
if (isBookInFavorites(book)) { if (isBookInFavorites(book)) {
return; return;
} }
final LibraryTree rootFavorites = getRootTree(ROOT_FAVORITES); final LibraryTree rootFavorites = getFirstLevelTree(ROOT_FAVORITES);
rootFavorites.createBookSubTree(book, true); rootFavorites.createBookSubTree(book, true);
rootFavorites.sortAllChildren(); rootFavorites.sortAllChildren();
BooksDatabase.Instance().addToFavorites(book.getId()); BooksDatabase.Instance().addToFavorites(book.getId());
@ -463,7 +477,7 @@ public final class Library {
public void removeBookFromFavorites(Book book) { public void removeBookFromFavorites(Book book) {
waitForState(STATE_FULLY_INITIALIZED); waitForState(STATE_FULLY_INITIALIZED);
if (getRootTree(ROOT_FAVORITES).removeBook(book)) { if (getFirstLevelTree(ROOT_FAVORITES).removeBook(book)) {
BooksDatabase.Instance().removeFromFavorites(book.getId()); BooksDatabase.Instance().removeFromFavorites(book.getId());
} }
} }
@ -499,15 +513,13 @@ public final class Library {
} }
waitForState(STATE_FULLY_INITIALIZED); waitForState(STATE_FULLY_INITIALIZED);
myBooks.remove(book); myBooks.remove(book);
if (getRootTree(ROOT_RECENT).removeBook(book)) { if (getFirstLevelTree(ROOT_RECENT).removeBook(book)) {
final BooksDatabase db = BooksDatabase.Instance(); final BooksDatabase db = BooksDatabase.Instance();
final List<Long> ids = db.loadRecentBookIds(); final List<Long> ids = db.loadRecentBookIds();
ids.remove(book.getId()); ids.remove(book.getId());
db.saveRecentBookIds(ids); db.saveRecentBookIds(ids);
} }
for (LibraryTree root : myRootTrees.values()) { myRootTree.removeBook(book);
root.removeBook(book);
}
BooksDatabase.Instance().deleteFromBookList(book.getId()); BooksDatabase.Instance().deleteFromBookList(book.getId());
if ((removeMode & REMOVE_FROM_DISK) != 0) { if ((removeMode & REMOVE_FROM_DISK) != 0) {

View file

@ -32,6 +32,10 @@ public abstract class LibraryTree extends FBTree {
super(parent); super(parent);
} }
protected LibraryTree(LibraryTree parent, int position) {
super(parent, position);
}
public String getTreeTitle() { public String getTreeTitle() {
return getName(); return getName();
} }

View file

@ -22,8 +22,8 @@ package org.geometerplus.fbreader.library;
class SearchResultsTree extends FirstLevelTree { class SearchResultsTree extends FirstLevelTree {
private final String myPattern; private final String myPattern;
SearchResultsTree(Library library, String id, String pattern) { SearchResultsTree(RootTree root, String id, String pattern) {
super(library, id); super(root, 0, id);
myPattern = pattern != null ? pattern : ""; myPattern = pattern != null ? pattern : "";
} }