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

Merge branch 'master' into library-service

Conflicts:
	src/org/geometerplus/fbreader/library/Library.java
This commit is contained in:
Nikolay Pultsin 2013-02-09 16:28:40 +04:00
commit 40e98cd33c
12 changed files with 153 additions and 52 deletions

View file

@ -75,23 +75,20 @@ class LibraryTreeAdapter extends TreeAdapter {
private int getCoverResourceId(LibraryTree tree) {
if (tree.getBook() != null) {
return R.drawable.ic_list_library_book;
} else if (tree instanceof FirstLevelTree) {
final String id = tree.getUniqueKey().Id;
if (LibraryTree.ROOT_FAVORITES.equals(id)) {
} else if (tree instanceof FavoritesTree) {
return R.drawable.ic_list_library_favorites;
} else if (LibraryTree.ROOT_RECENT.equals(id)) {
} else if (tree instanceof RecentBooksTree) {
return R.drawable.ic_list_library_recent;
} else if (LibraryTree.ROOT_BY_AUTHOR.equals(id)) {
} else if (tree instanceof AuthorListTree) {
return R.drawable.ic_list_library_authors;
} else if (LibraryTree.ROOT_BY_TITLE.equals(id)) {
} else if (tree instanceof TitleListTree) {
return R.drawable.ic_list_library_books;
} else if (LibraryTree.ROOT_BY_TAG.equals(id)) {
} else if (tree instanceof TagListTree) {
return R.drawable.ic_list_library_tags;
} else if (LibraryTree.ROOT_FILE_TREE.equals(id)) {
} else if (tree instanceof FileFirstLevelTree) {
return R.drawable.ic_list_library_folder;
} else if (LibraryTree.ROOT_FOUND.equals(id)) {
} else if (tree instanceof SearchResultsTree) {
return R.drawable.ic_list_library_search;
}
} else if (tree instanceof FileTree) {
final ZLFile file = ((FileTree)tree).getFile();
if (file.isArchive()) {

View file

@ -288,6 +288,17 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
}
}
public synchronized List<String> titles() {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return myInterface.titles();
} catch (RemoteException e) {
return Collections.emptyList();
}
}
public synchronized boolean saveBook(Book book, boolean force) {
if (myInterface == null) {
return false;

View file

@ -25,6 +25,7 @@ interface LibraryInterface {
boolean hasSeries();
List<String> series();
List<String> tags();
List<String> titles();
boolean saveBook(in String book, in boolean force);
void removeBook(in String book, in boolean deleteFromDisk);

View file

@ -190,6 +190,10 @@ public class LibraryService extends Service {
return strings;
}
public List<String> titles() {
return myCollection.titles();
}
public boolean saveBook(String book, boolean force) {
return myCollection.saveBook(SerializerUtil.deserializeBook(book), force);
}

View file

@ -332,6 +332,16 @@ public class BookCollection extends AbstractBookCollection {
return new ArrayList<String>(series);
}
public List<String> titles() {
synchronized (myBooksByFile) {
final List<String> titles = new ArrayList<String>(myBooksByFile.size());
for (Book book : myBooksByFile.values()) {
titles.add(book.getTitle());
}
return titles;
}
}
public Book getRecentBook(int index) {
List<Long> recentIds = myDatabase.loadRecentBookIds();
return recentIds.size() > index ? getBookById(recentIds.get(index)) : null;

View file

@ -63,9 +63,10 @@ public interface IBookCollection {
Book getBookById(long id);
List<Author> authors();
List<Tag> tags();
boolean hasSeries();
List<String> series();
List<Tag> tags();
List<String> titles();
boolean saveBook(Book book, boolean force);
void removeBook(Book book, boolean deleteFromDisk);

View file

@ -21,7 +21,7 @@ package org.geometerplus.fbreader.library;
import org.geometerplus.zlibrary.core.resources.ZLResource;
public class FirstLevelTree extends LibraryTree {
abstract class FirstLevelTree extends LibraryTree {
private final String myId;
private final ZLResource myResource;

View file

@ -71,7 +71,6 @@ public final class Library {
private final Map<Long,Book> myBooks = Collections.synchronizedMap(new HashMap<Long,Book>());
private final RootTree myRootTree;
private boolean myDoGroupTitlesByFirstLetter;
private final static int STATUS_LOADING = 1;
private final static int STATUS_SEARCHING = 2;
@ -95,7 +94,7 @@ public final class Library {
new FavoritesTree(myRootTree);
new RecentBooksTree(myRootTree);
new AuthorListTree(myRootTree);
new FirstLevelTree(myRootTree, LibraryTree.ROOT_BY_TITLE);
new TitleListTree(myRootTree);
new SeriesListTree(myRootTree);
new TagListTree(myRootTree);
new FileFirstLevelTree(myRootTree);
@ -177,17 +176,6 @@ public final class Library {
myBooks.put(book.getId(), book);
}
if (myDoGroupTitlesByFirstLetter) {
final String letter = TitleTree.firstTitleLetter(book);
if (letter != null) {
final TitleTree tree =
getFirstLevelTree(LibraryTree.ROOT_BY_TITLE).getTitleSubTree(letter);
tree.createBookWithAuthorsSubTree(book);
}
} else {
getFirstLevelTree(LibraryTree.ROOT_BY_TITLE).createBookWithAuthorsSubTree(book);
}
synchronized (this) {
final SearchResultsTree found = (SearchResultsTree)getFirstLevelTree(LibraryTree.ROOT_FOUND);
if (found != null && book.matches(found.getPattern())) {
@ -211,7 +199,6 @@ public final class Library {
Collection.saveBook(book, true);
myBooks.remove(book.getId());
removeFromTree(LibraryTree.ROOT_FOUND, book);
removeFromTree(LibraryTree.ROOT_BY_TITLE, book);
addBookToLibrary(book);
fireModelChangedEvent(ChangeListener.Code.BookAdded);
}

View file

@ -26,13 +26,13 @@ import org.geometerplus.fbreader.tree.FBTree;
public abstract class LibraryTree extends FBTree {
public static final String ROOT_FOUND = "found";
public static final String ROOT_FAVORITES = "favorites";
public static final String ROOT_RECENT = "recent";
public static final String ROOT_BY_AUTHOR = "byAuthor";
public static final String ROOT_BY_TITLE = "byTitle";
public static final String ROOT_BY_SERIES = "bySeries";
public static final String ROOT_BY_TAG = "byTag";
public static final String ROOT_FILE_TREE = "fileTree";
static final String ROOT_FAVORITES = "favorites";
static final String ROOT_RECENT = "recent";
static final String ROOT_BY_AUTHOR = "byAuthor";
static final String ROOT_BY_TITLE = "byTitle";
static final String ROOT_BY_SERIES = "bySeries";
static final String ROOT_BY_TAG = "byTag";
static final String ROOT_FILE_TREE = "fileTree";
public final IBookCollection Collection;
@ -74,16 +74,6 @@ public abstract class LibraryTree extends FBTree {
}
}
TitleTree getTitleSubTree(String title) {
final TitleTree temp = new TitleTree(Collection, title);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return (TitleTree)subTrees().get(position);
} else {
return new TitleTree(this, title, - position - 1);
}
}
boolean createBookWithAuthorsSubTree(Book book) {
final BookWithAuthorsTree temp = new BookWithAuthorsTree(Collection, book);
int position = Collections.binarySearch(subTrees(), temp);

View file

@ -19,7 +19,7 @@
package org.geometerplus.fbreader.library;
class SearchResultsTree extends FirstLevelTree {
public class SearchResultsTree extends FirstLevelTree {
private final String myPattern;
SearchResultsTree(RootTree root, String id, String pattern) {

View file

@ -0,0 +1,97 @@
/*
* 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.*;
import org.geometerplus.fbreader.book.Book;
import org.geometerplus.fbreader.book.BookEvent;
public class TitleListTree extends FirstLevelTree {
private boolean myDoGroupByFirstLetter;
TitleListTree(RootTree root) {
super(root, ROOT_BY_TITLE);
}
@Override
public Status getOpeningStatus() {
return Status.ALWAYS_RELOAD_BEFORE_OPENING;
}
@Override
public void waitForOpening() {
clear();
myDoGroupByFirstLetter = false;
final TreeSet<String> letterSet = new TreeSet<String>();
final List<String> titles = Collection.titles();
if (titles.size() > 10) {
for (String t : titles) {
final String letter = TitleTree.firstTitleLetter(t);
if (letter != null) {
letterSet.add(letter);
}
}
myDoGroupByFirstLetter = titles.size() > letterSet.size() * 5 / 4;
}
if (myDoGroupByFirstLetter) {
for (String letter : letterSet) {
createTitleSubTree(letter);
}
} else {
for (Book b : Collection.books()) {
createBookWithAuthorsSubTree(b);
}
}
}
@Override
public boolean onBookEvent(BookEvent event, Book book) {
switch (event) {
case Added:
if (myDoGroupByFirstLetter) {
final String letter = TitleTree.firstTitleLetter(book);
return letter != null && createTitleSubTree(letter);
} else {
return createBookWithAuthorsSubTree(book);
}
case Removed:
// TODO: implement
return false;
default:
case Updated:
// TODO: implement
return false;
}
}
boolean createTitleSubTree(String title) {
final TitleTree temp = new TitleTree(Collection, title);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return false;
} else {
new TitleTree(this, title, - position - 1);
return true;
}
}
}

View file

@ -27,7 +27,10 @@ public final class TitleTree extends LibraryTree {
if (book == null) {
return null;
}
String title = book.getTitle();
return firstTitleLetter(book.getTitle());
}
static String firstTitleLetter(String title) {
if (title == null) {
return null;
}