From edc931a04f7787c8fac21a1a31cc0ebc8a85aa96 Mon Sep 17 00:00:00 2001 From: Nikolay Pultsin Date: Wed, 19 Jan 2011 20:35:19 +0000 Subject: [PATCH] book-by-title library branch has been implemented --- .../fbreader/library/LibraryBaseActivity.java | 4 ++ .../fbreader/library/LibraryTreeActivity.java | 2 + .../fbreader/library/Library.java | 46 +++++++++++++++++++ .../fbreader/library/LibraryTree.java | 4 ++ .../fbreader/library/TitleTree.java | 34 ++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 src/org/geometerplus/fbreader/library/TitleTree.java diff --git a/src/org/geometerplus/android/fbreader/library/LibraryBaseActivity.java b/src/org/geometerplus/android/fbreader/library/LibraryBaseActivity.java index 7b9237319..8e8a4c199 100644 --- a/src/org/geometerplus/android/fbreader/library/LibraryBaseActivity.java +++ b/src/org/geometerplus/android/fbreader/library/LibraryBaseActivity.java @@ -203,6 +203,10 @@ abstract class LibraryBaseActivity extends BaseActivity implements MenuItem.OnMe if (tree instanceof AuthorTree) { return mySelectedBook.authors().contains(((AuthorTree)tree).Author); } + if (tree instanceof TitleTree) { + final String title = mySelectedBook.getTitle(); + return tree != null && title.trim().startsWith(((TitleTree)tree).Title); + } if (tree instanceof SeriesTree) { final SeriesInfo info = mySelectedBook.getSeriesInfo(); final String series = ((SeriesTree)tree).Series; diff --git a/src/org/geometerplus/android/fbreader/library/LibraryTreeActivity.java b/src/org/geometerplus/android/fbreader/library/LibraryTreeActivity.java index cd8c432f2..86f439bd0 100644 --- a/src/org/geometerplus/android/fbreader/library/LibraryTreeActivity.java +++ b/src/org/geometerplus/android/fbreader/library/LibraryTreeActivity.java @@ -80,6 +80,8 @@ public class LibraryTreeActivity extends LibraryBaseActivity { tree = LibraryInstance.searchResults(); } else if (PATH_BY_AUTHOR.equals(path[0])) { tree = LibraryInstance.byAuthor(); + } else if (PATH_BY_TITLE.equals(path[0])) { + tree = LibraryInstance.byTitle(); } else if (PATH_BY_TAG.equals(path[0])) { tree = LibraryInstance.byTag(); } else if (PATH_FAVORITES.equals(path[0])) { diff --git a/src/org/geometerplus/fbreader/library/Library.java b/src/org/geometerplus/fbreader/library/Library.java index 526017390..717848e06 100644 --- a/src/org/geometerplus/fbreader/library/Library.java +++ b/src/org/geometerplus/fbreader/library/Library.java @@ -39,6 +39,7 @@ public final class Library { private final LinkedList myBooks = new LinkedList(); private final HashSet myExternalBooks = new HashSet(); private final LibraryTree myLibraryByAuthor = new RootTree(); + private final LibraryTree myLibraryByTitle = new RootTree(); private final LibraryTree myLibraryByTag = new RootTree(); private final LibraryTree myRecentBooks = new RootTree(); private final LibraryTree myFavorites = new RootTree(); @@ -270,6 +271,45 @@ public final class Library { } } + boolean doGroupTitlesByFirstLetter = false; + if (myBooks.size() > 10) { + final HashSet letterSet = new HashSet(); + for (Book book : myBooks) { + String title = book.getTitle(); + if (title != null) { + title = title.trim(); + if (!"".equals(title)) { + letterSet.add(title.charAt(0)); + } + } + } + doGroupTitlesByFirstLetter = letterSet.size() > myBooks.size() + 4; + } + if (doGroupTitlesByFirstLetter) { + final HashMap letterTrees = new HashMap(); + for (Book book : myBooks) { + String title = book.getTitle(); + if (title == null) { + continue; + } + title = title.trim(); + if ("".equals(title)) { + continue; + } + Character c = title.charAt(0); + TitleTree tree = letterTrees.get(c); + if (tree == null) { + tree = myLibraryByTitle.createTitleSubTree(c.toString()); + letterTrees.put(c, tree); + } + tree.createBookSubTree(book, true); + } + } else { + for (Book book : myBooks) { + myLibraryByTitle.createBookSubTree(book, true); + } + } + final BooksDatabase db = BooksDatabase.Instance(); for (long id : db.loadRecentBookIds()) { Book book = bookById.get(id); @@ -287,6 +327,7 @@ public final class Library { myFavorites.sortAllChildren(); myLibraryByAuthor.sortAllChildren(); + myLibraryByTitle.sortAllChildren(); myLibraryByTag.sortAllChildren(); db.executeAsATransaction(new Runnable() { @@ -317,6 +358,11 @@ public final class Library { return myLibraryByAuthor; } + public LibraryTree byTitle() { + waitForState(STATE_FULLY_INITIALIZED); + return myLibraryByTitle; + } + public LibraryTree byTag() { waitForState(STATE_FULLY_INITIALIZED); return myLibraryByTag; diff --git a/src/org/geometerplus/fbreader/library/LibraryTree.java b/src/org/geometerplus/fbreader/library/LibraryTree.java index e14fc32d7..9b7182bb6 100644 --- a/src/org/geometerplus/fbreader/library/LibraryTree.java +++ b/src/org/geometerplus/fbreader/library/LibraryTree.java @@ -38,6 +38,10 @@ public abstract class LibraryTree extends FBTree { return new TagTree(this, tag); } + TitleTree createTitleSubTree(String title) { + return new TitleTree(this, title); + } + AuthorTree createAuthorSubTree(Author author) { return new AuthorTree(this, author); } diff --git a/src/org/geometerplus/fbreader/library/TitleTree.java b/src/org/geometerplus/fbreader/library/TitleTree.java new file mode 100644 index 000000000..17105a3c5 --- /dev/null +++ b/src/org/geometerplus/fbreader/library/TitleTree.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2009-2011 Geometer Plus + * + * 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; + +public final class TitleTree extends LibraryTree { + public final String Title; + + TitleTree(LibraryTree parent, String title) { + super(parent); + Title = title; + } + + @Override + public String getName() { + return Title; + } +}