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

library auto-sorting

This commit is contained in:
Nikolay Pultsin 2011-07-17 14:49:53 +01:00
parent 31075d08a0
commit f79aaeb132
10 changed files with 97 additions and 60 deletions

View file

@ -63,7 +63,7 @@
</activity>
<activity android:name="org.geometerplus.android.fbreader.CancelActivity" android:theme="@android:style/Theme.Dialog" android:configChanges="orientation|keyboardHidden"/>
<activity android:name="org.geometerplus.android.fbreader.image.ImageViewActivity" android:process=":imageView" android:configChanges="orientation|keyboardHidden"/>
<activity android:name="org.geometerplus.android.fbreader.BookInfoActivity" android:configChanges="orientation|keyboardHidden" android:process=":library"/>
<activity android:name="org.geometerplus.android.fbreader.library.BookInfoActivity" android:configChanges="orientation|keyboardHidden" android:process=":library"/>
<service android:name="org.geometerplus.android.fbreader.library.InitializationService" android:process=":library" />
<receiver android:name="org.geometerplus.android.fbreader.library.KillerCallback" android:process=":library" />
<activity android:name="org.geometerplus.android.fbreader.library.LibraryActivity" android:launchMode="singleTask" android:process=":library" android:configChanges="orientation|keyboardHidden">

View file

@ -63,7 +63,7 @@
</activity>
<activity android:name="org.geometerplus.android.fbreader.CancelActivity" android:theme="@android:style/Theme.Dialog" android:configChanges="orientation|keyboardHidden"/>
<activity android:name="org.geometerplus.android.fbreader.image.ImageViewActivity" android:process=":imageView" android:configChanges="orientation|keyboardHidden"/>
<activity android:name="org.geometerplus.android.fbreader.BookInfoActivity" android:configChanges="orientation|keyboardHidden" android:process=":library"/>
<activity android:name="org.geometerplus.android.fbreader.library.BookInfoActivity" android:configChanges="orientation|keyboardHidden" android:process=":library"/>
<service android:name="org.geometerplus.android.fbreader.library.InitializationService" android:process=":library" />
<receiver android:name="org.geometerplus.android.fbreader.library.KillerCallback" android:process=":library" />
<activity android:name="org.geometerplus.android.fbreader.library.LibraryActivity" android:launchMode="singleTask" android:process=":library" android:configChanges="orientation|keyboardHidden">

View file

@ -19,6 +19,8 @@
package org.geometerplus.fbreader.library;
import java.util.Collections;
public class AuthorTree extends LibraryTree {
public final Author Author;
@ -31,8 +33,14 @@ public class AuthorTree extends LibraryTree {
Author = author;
}
SeriesTree createSeriesSubTree(String series) {
return new SeriesTree(this, series);
SeriesTree getSeriesSubTree(String series) {
final SeriesTree temp = new SeriesTree(series);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return (SeriesTree)subTrees().get(position);
} else {
return new SeriesTree(this, series, - position - 1);
}
}
@Override

View file

@ -22,8 +22,12 @@ package org.geometerplus.fbreader.library;
import org.geometerplus.fbreader.tree.FBTree;
public final class BookInSeriesTree extends BookTree {
BookInSeriesTree(LibraryTree parent, Book book) {
super(parent, book, false);
BookInSeriesTree(Book book) {
super(book, false);
}
BookInSeriesTree(LibraryTree parent, Book book, int position) {
super(parent, book, false, position);
}
@Override

View file

@ -25,12 +25,23 @@ public class BookTree extends LibraryTree {
public final Book Book;
private final boolean myShowAuthors;
BookTree(Book book, boolean showAuthors) {
Book = book;
myShowAuthors = showAuthors;
}
BookTree(LibraryTree parent, Book book, boolean showAuthors) {
super(parent);
Book = book;
myShowAuthors = showAuthors;
}
BookTree(LibraryTree parent, Book book, boolean showAuthors, int position) {
super(parent, position);
Book = book;
myShowAuthors = showAuthors;
}
@Override
public String getName() {
return Book.getTitle();

View file

@ -257,21 +257,15 @@ public final class Library {
myNullList.add(null);
}
private TagTree getTagTree(Tag tag, HashMap<Tag,TagTree> tagTreeMap) {
TagTree tagTree = tagTreeMap.get(tag);
if (tagTree == null) {
LibraryTree parent =
((tag != null) && (tag.Parent != null)) ?
getTagTree(tag.Parent, tagTreeMap) : getFirstLevelTree(ROOT_BY_TAG);
tagTree = parent.createTagSubTree(tag);
tagTreeMap.put(tag, tagTree);
private LibraryTree getTagTree(Tag tag) {
if (tag == null || tag.Parent == null) {
return getFirstLevelTree(ROOT_BY_TAG).getTagSubTree(tag);
} else {
return getTagTree(tag.Parent).getTagSubTree(tag);
}
return tagTree;
}
private void build() {
final HashMap<Tag,TagTree> tagTreeMap = new HashMap<Tag,TagTree>();
final HashMap<AuthorSeriesPair,SeriesTree> seriesTreeMap = new HashMap<AuthorSeriesPair,SeriesTree>();
final HashMap<Long,Book> bookById = new HashMap<Long,Book>();
collectBooks();
@ -286,16 +280,12 @@ public final class Library {
for (Author a : authors) {
final AuthorTree authorTree = getFirstLevelTree(ROOT_BY_AUTHOR).getAuthorSubTree(a);
if (seriesInfo == null) {
authorTree.createBookSubTree(book, false);
authorTree.getBookSubTree(book, false);
} else {
final String series = seriesInfo.Name;
final AuthorSeriesPair pair = new AuthorSeriesPair(a, series);
SeriesTree seriesTree = seriesTreeMap.get(pair);
if (seriesTree == null) {
seriesTree = authorTree.createSeriesSubTree(series);
seriesTreeMap.put(pair, seriesTree);
}
seriesTree.createBookInSeriesSubTree(book);
final SeriesTree seriesTree = authorTree.getSeriesSubTree(series);
seriesTree.getBookInSeriesSubTree(book);
}
}
@ -304,7 +294,7 @@ public final class Library {
tags = (List<Tag>)myNullList;
}
for (Tag t : tags) {
getTagTree(t, tagTreeMap).createBookSubTree(book, true);
getTagTree(t).getBookSubTree(book, true);
}
}
@ -323,7 +313,6 @@ public final class Library {
doGroupTitlesByFirstLetter = myBooks.size() > letterSet.size() * 5 / 4;
}
if (doGroupTitlesByFirstLetter) {
final HashMap<Character,TitleTree> letterTrees = new HashMap<Character,TitleTree>();
for (Book book : myBooks) {
String title = book.getTitle();
if (title == null) {
@ -334,16 +323,12 @@ public final class Library {
continue;
}
Character c = title.charAt(0);
TitleTree tree = letterTrees.get(c);
if (tree == null) {
tree = getFirstLevelTree(ROOT_BY_TITLE).createTitleSubTree(c.toString());
letterTrees.put(c, tree);
}
tree.createBookSubTree(book, true);
final TitleTree tree = getFirstLevelTree(ROOT_BY_TITLE).getTitleSubTree(c.toString());
tree.getBookSubTree(book, true);
}
} else {
for (Book book : myBooks) {
getFirstLevelTree(ROOT_BY_TITLE).createBookSubTree(book, true);
getFirstLevelTree(ROOT_BY_TITLE).getBookSubTree(book, true);
}
}
@ -351,22 +336,17 @@ public final class Library {
for (long id : db.loadRecentBookIds()) {
Book book = bookById.get(id);
if (book != null) {
getFirstLevelTree(ROOT_RECENT).createBookSubTree(book, true);
new BookTree(getFirstLevelTree(ROOT_RECENT), book, true);
}
}
for (long id : db.loadFavoritesIds()) {
Book book = bookById.get(id);
if (book != null) {
getFirstLevelTree(ROOT_FAVORITES).createBookSubTree(book, true);
getFirstLevelTree(ROOT_FAVORITES).getBookSubTree(book, true);
}
}
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() {
for (Book book : myBooks) {
@ -418,12 +398,9 @@ public final class Library {
if (newSearchResults == null) {
newSearchResults = createNewSearchResults(pattern);
}
newSearchResults.createBookSubTree(book, true);
newSearchResults.getBookSubTree(book, true);
}
}
if (newSearchResults != null) {
newSearchResults.sortAllChildren();
}
}
return newSearchResults;
}
@ -460,8 +437,7 @@ public final class Library {
return;
}
final LibraryTree rootFavorites = getFirstLevelTree(ROOT_FAVORITES);
rootFavorites.createBookSubTree(book, true);
rootFavorites.sortAllChildren();
rootFavorites.getBookSubTree(book, true);
BooksDatabase.Instance().addToFavorites(book.getId());
}

View file

@ -48,12 +48,24 @@ public abstract class LibraryTree extends FBTree {
return true;
}
TagTree createTagSubTree(Tag tag) {
return new TagTree(this, tag);
TagTree getTagSubTree(Tag tag) {
final TagTree temp = new TagTree(tag);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return (TagTree)subTrees().get(position);
} else {
return new TagTree(this, tag, - position - 1);
}
}
TitleTree createTitleSubTree(String title) {
return new TitleTree(this, title);
TitleTree getTitleSubTree(String title) {
final TitleTree temp = new TitleTree(title);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return (TitleTree)subTrees().get(position);
} else {
return new TitleTree(this, title, - position - 1);
}
}
AuthorTree getAuthorSubTree(Author author) {
@ -66,8 +78,14 @@ public abstract class LibraryTree extends FBTree {
}
}
BookTree createBookSubTree(Book book, boolean showAuthors) {
return new BookTree(this, book, showAuthors);
BookTree getBookSubTree(Book book, boolean showAuthors) {
final BookTree temp = new BookTree(book, showAuthors);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return (BookTree)subTrees().get(position);
} else {
return new BookTree(this, book, showAuthors, - position - 1);
}
}
public boolean removeBook(Book book) {

View file

@ -19,11 +19,17 @@
package org.geometerplus.fbreader.library;
import java.util.Collections;
public final class SeriesTree extends LibraryTree {
public final String Series;
SeriesTree(LibraryTree parent, String series) {
super(parent);
SeriesTree(String series) {
Series = series;
}
SeriesTree(LibraryTree parent, String series, int position) {
super(parent, position);
Series = series;
}
@ -37,8 +43,14 @@ public final class SeriesTree extends LibraryTree {
return getName();
}
BookTree createBookInSeriesSubTree(Book book) {
return new BookInSeriesTree(this, book);
BookTree getBookInSeriesSubTree(Book book) {
final BookInSeriesTree temp = new BookInSeriesTree(book);
int position = Collections.binarySearch(subTrees(), temp);
if (position >= 0) {
return (BookInSeriesTree)subTrees().get(position);
} else {
return new BookInSeriesTree(this, book, - position - 1);
}
}
@Override

View file

@ -22,8 +22,12 @@ package org.geometerplus.fbreader.library;
public final class TagTree extends LibraryTree {
public final Tag Tag;
TagTree(LibraryTree parent, Tag tag) {
super(parent);
TagTree(Tag tag) {
Tag = tag;
}
TagTree(LibraryTree parent, Tag tag, int position) {
super(parent, position);
Tag = tag;
}

View file

@ -22,8 +22,12 @@ package org.geometerplus.fbreader.library;
public final class TitleTree extends LibraryTree {
public final String Title;
TitleTree(LibraryTree parent, String title) {
super(parent);
TitleTree(String title) {
Title = title;
}
TitleTree(LibraryTree parent, String title, int position) {
super(parent, position);
Title = title;
}