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

library -> by authors tree new implementation (events are not fully supported yet)

This commit is contained in:
Nikolay Pultsin 2013-02-06 00:29:03 +00:00
parent c4308ade41
commit 6c3b9028a1
11 changed files with 177 additions and 42 deletions

View file

@ -44,7 +44,7 @@ import org.geometerplus.android.fbreader.*;
import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
import org.geometerplus.android.fbreader.tree.TreeActivity;
public class LibraryActivity extends TreeActivity<LibraryTree> implements MenuItem.OnMenuItemClickListener, View.OnCreateContextMenuListener, Library.ChangeListener {
public class LibraryActivity extends TreeActivity<LibraryTree> implements MenuItem.OnMenuItemClickListener, View.OnCreateContextMenuListener, Library.ChangeListener, IBookCollection.Listener {
static final String START_SEARCH_ACTION = "action.fbreader.library.start-search";
private Library myLibrary;
@ -58,6 +58,7 @@ public class LibraryActivity extends TreeActivity<LibraryTree> implements MenuIt
if (myLibrary == null) {
myLibrary = new Library(new BookCollectionShadow());
myLibrary.addChangeListener(this);
myLibrary.Collection.addListener(this);
}
mySelectedBook =
@ -113,6 +114,7 @@ public class LibraryActivity extends TreeActivity<LibraryTree> implements MenuIt
@Override
protected void onDestroy() {
myLibrary.Collection.removeListener(this);
myLibrary.removeChangeListener(this);
myLibrary = null;
super.onDestroy();
@ -349,4 +351,11 @@ public class LibraryActivity extends TreeActivity<LibraryTree> implements MenuIt
}
});
}
public void onBookEvent(BookEvent event, Book book) {
getCurrentTree().onBookEvent(event, book);
}
public void onBuildEvent(BuildEvent event) {
}
}

View file

@ -121,6 +121,19 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
}
}
public synchronized List<Book> books(Author author) {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return SerializerUtil.deserializeBookList(
myInterface.booksForAuthor(Util.authorToString(author))
);
} catch (RemoteException e) {
return Collections.emptyList();
}
}
public synchronized List<Book> books(String pattern) {
if (myInterface == null) {
return Collections.emptyList();
@ -195,10 +208,7 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
final List<String> strings = myInterface.authors();
final List<Author> authors = new ArrayList<Author>(strings.size());
for (String s : strings) {
final String[] splited = s.split("\000");
if (splited.length == 2) {
authors.add(new Author(splited[0], splited[1]));
}
authors.add(Util.stringToAuthor(s));
}
return authors;
} catch (RemoteException e) {

View file

@ -11,6 +11,7 @@ interface LibraryInterface {
int size();
List<String> books();
List<String> booksForPattern(in String pattern);
List<String> booksForAuthor(in String author);
List<String> recentBooks();
List<String> favorites();
String getBookByFile(in String file);

View file

@ -124,6 +124,10 @@ public class LibraryService extends Service {
return SerializerUtil.serializeBookList(myCollection.books());
}
public List<String> booksForAuthor(String author) {
return SerializerUtil.serializeBookList(myCollection.books(Util.stringToAuthor(author)));
}
public List<String> booksForPattern(String pattern) {
return SerializerUtil.serializeBookList(myCollection.books(pattern));
}
@ -152,12 +156,7 @@ public class LibraryService extends Service {
final List<Author> authors = myCollection.authors();
final List<String> strings = new ArrayList<String>(authors.size());
for (Author a : authors) {
strings.add(
new StringBuilder(a.DisplayName)
.append('\000')
.append(a.SortKey)
.toString()
);
strings.add(Util.authorToString(a));
}
return strings;
}

View file

@ -20,6 +20,8 @@
package org.geometerplus.fbreader.book;
public final class Author implements Comparable<Author> {
public static final Author NULL = new Author("", "");
public final String DisplayName;
public final String SortKey;

View file

@ -194,6 +194,18 @@ public class BookCollection extends AbstractBookCollection {
}
}
public List<Book> books(Author author) {
final boolean isNull = Author.NULL.equals(author);
final LinkedList<Book> filtered = new LinkedList<Book>();
for (Book b : books()) {
final List<Author> bookAuthors = b.authors();
if (isNull && bookAuthors.isEmpty() || bookAuthors.contains(author)) {
filtered.add(b);
}
}
return filtered;
}
public List<Book> books(String pattern) {
if (pattern == null || pattern.length() == 0) {
return Collections.emptyList();
@ -231,7 +243,12 @@ public class BookCollection extends AbstractBookCollection {
final Set<Author> authors = new TreeSet<Author>();
synchronized (myBooksByFile) {
for (Book book : myBooksByFile.values()) {
authors.addAll(book.authors());
final List<Author> bookAuthors = book.authors();
if (bookAuthors.isEmpty()) {
authors.add(Author.NULL);
} else {
authors.addAll(bookAuthors);
}
}
}
return new ArrayList<Author>(authors);

View file

@ -44,6 +44,7 @@ public interface IBookCollection {
int size();
List<Book> books();
List<Book> books(Author author);
List<Book> books(String pattern);
List<Book> recentBooks();
List<Book> favorites();

View file

@ -0,0 +1,82 @@
/*
* Copyright (C) 2009-2013 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.fbreader.library;
import java.util.Collections;
import java.util.List;
import org.geometerplus.fbreader.book.*;
public class AuthorListTree extends FirstLevelTree {
private final IBookCollection myCollection;
AuthorListTree(IBookCollection collection, RootTree root) {
super(root, Library.ROOT_BY_AUTHOR);
myCollection = collection;
}
@Override
public Status getOpeningStatus() {
return Status.ALWAYS_RELOAD_BEFORE_OPENING;
}
@Override
public void waitForOpening() {
clear();
for (Author a : myCollection.authors()) {
createAuthorSubTree(a);
}
}
@Override
public boolean onBookEvent(BookEvent event, Book book) {
switch (event) {
default:
case Added:
{
final List<Author> bookAuthors = book.authors();
boolean changed = false;
if (bookAuthors.isEmpty()) {
changed &= createAuthorSubTree(Author.NULL);
} else for (Author a : myCollection.authors()) {
changed &= createAuthorSubTree(a);
}
return changed;
}
case Removed:
// TODO: implement
return false;
case Updated:
// TODO: implement
return false;
}
}
private boolean createAuthorSubTree(Author author) {
final AuthorTree temp = new AuthorTree(myCollection, author);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return false;
} else {
new AuthorTree(myCollection, this, author, - position - 1);
return true;
}
}
}

View file

@ -19,25 +19,28 @@
package org.geometerplus.fbreader.library;
import org.geometerplus.fbreader.book.Author;
import org.geometerplus.fbreader.book.Book;
import org.geometerplus.fbreader.book.*;
public class AuthorTree extends LibraryTree {
private final IBookCollection myCollection;
public final Author Author;
AuthorTree(Author author) {
AuthorTree(IBookCollection collection, Author author) {
myCollection = collection;
Author = author;
}
AuthorTree(LibraryTree parent, Author author, int position) {
AuthorTree(IBookCollection collection, AuthorListTree parent, Author author, int position) {
super(parent, position);
myCollection = collection;
Author = author;
}
@Override
public String getName() {
return
Author != null ?
Author != null && Author != Author.NULL ?
Author.DisplayName :
Library.resource().getResource("unknownAuthor").getValue();
}
@ -64,4 +67,38 @@ public class AuthorTree extends LibraryTree {
public boolean containsBook(Book book) {
return book != null && book.authors().contains(Author);
}
@Override
public Status getOpeningStatus() {
return Status.ALWAYS_RELOAD_BEFORE_OPENING;
}
@Override
public void waitForOpening() {
clear();
for (Book book : myCollection.books(Author)) {
final SeriesInfo seriesInfo = book.getSeriesInfo();
if (seriesInfo == null) {
getBookSubTree(book, false);
} else {
getSeriesSubTree(seriesInfo.Title).getBookInSeriesSubTree(book);
}
}
}
@Override
public boolean onBookEvent(BookEvent event, Book book) {
switch (event) {
default:
case Added:
// TODO: implement
return false;
case Removed:
// TODO: implement
return false;
case Updated:
// TODO: implement
return false;
}
}
}

View file

@ -101,14 +101,14 @@ public final class Library {
new FavoritesTree(collection, myRootTree);
new RecentBooksTree(collection, myRootTree);
new FirstLevelTree(myRootTree, ROOT_BY_AUTHOR);
new AuthorListTree(collection, myRootTree);
new FirstLevelTree(myRootTree, ROOT_BY_TITLE);
new FirstLevelTree(myRootTree, ROOT_BY_TAG);
new FileFirstLevelTree(collection, myRootTree);
}
public void init() {
Collection.addListener(new BookCollection.Listener() {
Collection.addListener(new IBookCollection.Listener() {
public void onBookEvent(BookEvent event, Book book) {
switch (event) {
case Added:
@ -193,19 +193,7 @@ public final class Library {
myBooks.put(book.getId(), book);
}
List<Author> authors = book.authors();
if (authors.isEmpty()) {
authors = (List<Author>)myNullList;
}
final SeriesInfo seriesInfo = book.getSeriesInfo();
for (Author a : authors) {
final AuthorTree authorTree = getFirstLevelTree(ROOT_BY_AUTHOR).getAuthorSubTree(a);
if (seriesInfo == null) {
authorTree.getBookSubTree(book, false);
} else {
authorTree.getSeriesSubTree(seriesInfo.Title).getBookInSeriesSubTree(book);
}
}
if (seriesInfo != null) {
FirstLevelTree seriesRoot = getFirstLevelTree(ROOT_BY_SERIES);
@ -263,7 +251,6 @@ public final class Library {
removeFromTree(ROOT_FOUND, book);
removeFromTree(ROOT_BY_TITLE, book);
removeFromTree(ROOT_BY_SERIES, book);
removeFromTree(ROOT_BY_AUTHOR, book);
removeFromTree(ROOT_BY_TAG, book);
addBookToLibrary(book);
fireModelChangedEvent(ChangeListener.Code.BookAdded);

View file

@ -69,16 +69,6 @@ public abstract class LibraryTree extends FBTree {
}
}
AuthorTree getAuthorSubTree(Author author) {
final AuthorTree temp = new AuthorTree(author);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return (AuthorTree)subTrees().get(position);
} else {
return new AuthorTree(this, author, - position - 1);
}
}
BookTree getBookSubTree(Book book, boolean showAuthors) {
final BookTree temp = new BookTree(book, showAuthors);
int position = Collections.binarySearch(subTrees(), temp);