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

@ -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;
}
private FirstLevelTree getFirstLevelTree(String key) {
for (FBTree tree : myRootTree.subTrees()) {
if (key.equals(tree.getUniqueKey().Id)) {
return (FirstLevelTree)tree;
}
}
return root;
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);
}
}
newSearchResults.sortAllChildren();
if (newSearchResults.hasChildren()) {
myRootTrees.put(ROOT_SEARCH_RESULTS, newSearchResults);
if (newSearchResults != null) {
newSearchResults.sortAllChildren();
}
}
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) {