1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 10:19:33 +02:00

updating book info

This commit is contained in:
Nikolay Pultsin 2013-02-01 09:42:00 +00:00
parent 7b498f8949
commit 78669b0694
17 changed files with 120 additions and 93 deletions

View file

@ -62,11 +62,11 @@ public final class FBReader extends Activity {
public static final int REQUEST_PREFERENCES = 1;
public static final int REQUEST_BOOK_INFO = 2;
public static final int REQUEST_CANCEL_MENU = 3;
public static final int REQUEST_LIBRARY = 3;
public static final int REQUEST_CANCEL_MENU = 4;
public static final int RESULT_DO_NOTHING = RESULT_FIRST_USER;
public static final int RESULT_REPAINT = RESULT_FIRST_USER + 1;
public static final int RESULT_RELOAD_BOOK = RESULT_FIRST_USER + 2;
public static void openBookActivity(Context context, Book book, Bookmark bookmark) {
context.startActivity(
@ -308,7 +308,7 @@ public final class FBReader extends Activity {
protected void onStart() {
super.onStart();
((BookCollectionShadow)myFBReaderApp.Collection).bindToService(this, new Runnable() {
getCollection().bindToService(this, new Runnable() {
public void run() {
new Thread() {
public void run() {
@ -428,7 +428,7 @@ public final class FBReader extends Activity {
protected void onStop() {
ApiServerImplementation.sendEvent(this, ApiListener.EVENT_READ_MODE_CLOSED);
PopupPanel.removeAllWindows(myFBReaderApp, this);
((BookCollectionShadow)myFBReaderApp.Collection).unbind();
getCollection().unbind();
super.onStop();
}
@ -469,30 +469,24 @@ public final class FBReader extends Activity {
}
}
private void onPreferencesUpdate(int resultCode, Book book) {
if (book != null) {
myFBReaderApp.Collection.saveBook(book, true);
}
switch (resultCode) {
case RESULT_DO_NOTHING:
break;
case RESULT_REPAINT:
{
AndroidFontUtil.clearFontCache();
private void onPreferencesUpdate(Book book) {
final BookModel model = myFBReaderApp.Model;
if (model != null && book != null) {
if (model.Book != null) {
model.Book = book;
if (book == null || model == null || model.Book == null) {
return;
}
final String newEncoding = book.getEncodingNoDetection();
final String oldEncoding = model.Book.getEncodingNoDetection();
model.Book.updateFrom(book);
if (newEncoding != null && !newEncoding.equals(oldEncoding)) {
myFBReaderApp.reloadBook();
} else {
AndroidFontUtil.clearFontCache();
ZLTextHyphenator.Instance().load(model.Book.getLanguage());
}
}
myFBReaderApp.clearTextCaches();
myFBReaderApp.getViewWidget().repaint();
break;
}
case RESULT_RELOAD_BOOK:
myFBReaderApp.reloadBook(book);
break;
}
}
@ -501,8 +495,26 @@ public final class FBReader extends Activity {
switch (requestCode) {
case REQUEST_PREFERENCES:
case REQUEST_BOOK_INFO:
onPreferencesUpdate(resultCode, BookInfoActivity.bookByIntent(data));
if (resultCode != RESULT_DO_NOTHING) {
final Book book = BookInfoActivity.bookByIntent(data);
if (book != null) {
myFBReaderApp.Collection.saveBook(book, true);
onPreferencesUpdate(BookInfoActivity.bookByIntent(data));
}
}
break;
case REQUEST_LIBRARY:
{
getCollection().bindToService(this, new Runnable() {
public void run() {
final BookModel model = myFBReaderApp.Model;
if (model != null && model.Book != null) {
onPreferencesUpdate(myFBReaderApp.Collection.getBookById(model.Book.getId()));
}
}
});
break;
}
case REQUEST_CANCEL_MENU:
myFBReaderApp.runCancelAction(resultCode - 1);
break;
@ -695,4 +707,8 @@ public final class FBReader extends Activity {
final int level = (int)(100 * getWindow().getAttributes().screenBrightness);
return (level >= 0) ? level : 50;
}
private BookCollectionShadow getCollection() {
return (BookCollectionShadow)myFBReaderApp.Collection;
}
}

View file

@ -37,6 +37,6 @@ class ShowLibraryAction extends FBAndroidAction {
if (Reader.Model != null) {
intent.putExtra(FBReader.BOOK_KEY, SerializerUtil.serialize(Reader.Model.Book));
}
OrientationUtil.startActivity(BaseActivity, intent);
OrientationUtil.startActivityForResult(BaseActivity, intent, FBReader.REQUEST_LIBRARY);
}
}

View file

@ -119,7 +119,7 @@ public class BookInfoActivity extends Activity {
myBook.reloadInfoFromFile();
setupBookInfo(myBook);
myDontReloadBook = false;
myResult = Math.max(myResult, FBReader.RESULT_RELOAD_BOOK);
myResult = FBReader.RESULT_REPAINT;
setResult(myResult, intentByBook(myBook));
}
}
@ -153,7 +153,7 @@ public class BookInfoActivity extends Activity {
myDontReloadBook = false;
}
myResult = Math.max(myResult, resultCode);
myResult = FBReader.RESULT_REPAINT;
setResult(myResult, data);
}

View file

@ -153,8 +153,14 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem
protected void onActivityResult(int requestCode, int returnCode, Intent intent) {
if (requestCode == BOOK_INFO_REQUEST) {
final Book book = BookInfoActivity.bookByIntent(intent);
((BookCollectionShadow)myLibrary.Collection).bindToService(this, new Runnable() {
public void run() {
myLibrary.refreshBookInfo(book);
if (((LibraryTree)getCurrentTree()).onBookChanged(book)) {
getListView().invalidateViews();
}
}
});
} else {
super.onActivityResult(requestCode, returnCode, intent);
}

View file

@ -60,15 +60,28 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
}
};
private static Runnable combined(final Runnable action0, final Runnable action1) {
if (action0 == null) {
return action1;
}
if (action1 == null) {
return action0;
}
return new Runnable() {
public void run() {
action0.run();
action1.run();
}
};
}
public synchronized void bindToService(Context context, Runnable onBindAction) {
if (myInterface != null && myContext == context) {
if (onBindAction != null) {
onBindAction.run();
}
} else {
if (onBindAction != null) {
myOnBindAction = onBindAction;
}
myOnBindAction = combined(myOnBindAction, onBindAction);
context.bindService(
new Intent(context, LibraryService.class),
this,

View file

@ -145,17 +145,6 @@ final class SQLiteBooksDatabase extends BooksDatabase {
return book;
}
@Override
protected void reloadBook(Book book) {
final Cursor cursor = myDatabase.rawQuery("SELECT title,encoding,language FROM Books WHERE book_id = " + book.getId(), null);
if (cursor.moveToNext()) {
book.setTitle(cursor.getString(0));
book.setEncoding(cursor.getString(1));
book.setLanguage(cursor.getString(2));
}
cursor.close();
}
protected Book loadBookByFile(long fileId, ZLFile file) {
if (fileId == -1) {
return null;

View file

@ -52,7 +52,7 @@ class BookTitlePreference extends ZLStringPreference {
protected void setValue(String value) {
super.setValue(value);
myBook.setTitle(value);
((EditBookInfoActivity)getContext()).setBookStatus(FBReader.RESULT_REPAINT);
((EditBookInfoActivity)getContext()).updateResult();
}
}
@ -88,7 +88,7 @@ class LanguagePreference extends ZLStringListPreference {
if (result) {
final String value = getValue();
myBook.setLanguage(value.length() > 0 ? value : null);
((EditBookInfoActivity)getContext()).setBookStatus(FBReader.RESULT_REPAINT);
((EditBookInfoActivity)getContext()).updateResult();
}
}
}
@ -142,29 +142,26 @@ class EncodingPreference extends ZLStringListPreference {
final String value = getValue();
if (!value.equalsIgnoreCase(myBook.getEncoding())) {
myBook.setEncoding(value);
((EditBookInfoActivity)getContext()).setBookStatus(FBReader.RESULT_RELOAD_BOOK);
((EditBookInfoActivity)getContext()).updateResult();
}
}
}
}
public class EditBookInfoActivity extends ZLPreferenceActivity {
private int myStatus = FBReader.RESULT_REPAINT;
private Book myBook;
public EditBookInfoActivity() {
super("BookInfo");
}
void setBookStatus(int code) {
myStatus = Math.max(myStatus, code);
setResult(myStatus, BookInfoActivity.intentByBook(myBook));
void updateResult() {
setResult(FBReader.RESULT_REPAINT, BookInfoActivity.intentByBook(myBook));
}
@Override
protected void init(Intent intent) {
myBook = BookInfoActivity.bookByIntent(intent);
myStatus = FBReader.RESULT_REPAINT;
if (myBook == null) {
finish();

View file

@ -72,6 +72,18 @@ public class Book {
myIsSaved = false;
}
public void updateFrom(Book book) {
if (myId != book.myId) {
return;
}
myTitle = book.myTitle;
myEncoding = book.myEncoding;
myLanguage = book.myLanguage;
myAuthors = book.myAuthors != null ? new ArrayList<Author>(book.myAuthors) : null;
myTags = book.myTags != null ? new ArrayList<Tag>(book.myTags) : null;
mySeriesInfo = book.mySeriesInfo;
}
public void reloadInfoFromFile() {
try {
readMetaInfo();

View file

@ -149,16 +149,16 @@ public class BookCollection extends AbstractBookCollection {
}
synchronized (myBooksByFile) {
Listener.BookEvent event = Listener.BookEvent.Added;
if (myBooksByFile.containsKey(book.File)) {
if (!force) {
return;
}
event = Listener.BookEvent.Updated;
}
final Book existing = myBooksByFile.get(book.File);
if (existing == null) {
myBooksByFile.put(book.File, book);
myBooksById.put(book.getId(), book);
fireBookEvent(event, book);
fireBookEvent(Listener.BookEvent.Added, book);
} else if (force) {
existing.updateFrom(book);
fireBookEvent(Listener.BookEvent.Updated, existing);
getBookById(book.getId());
}
}
}

View file

@ -49,7 +49,6 @@ public abstract class BooksDatabase {
protected abstract Map<Long,Book> loadBooks(FileInfoSet infos, boolean existing);
protected abstract void setExistingFlag(Collection<Book> books, boolean flag);
protected abstract Book loadBook(long bookId);
protected abstract void reloadBook(Book book);
protected abstract Book loadBookByFile(long fileId, ZLFile file);
protected abstract List<Author> loadAuthors(long bookId);

View file

@ -40,7 +40,7 @@ public final class SeriesInfo {
public final String Title;
public final BigDecimal Index;
public SeriesInfo(String title, BigDecimal index) {
SeriesInfo(String title, BigDecimal index) {
Title = title;
Index = index;
}

View file

@ -48,7 +48,7 @@ public abstract class BookModel {
return model;
}
public Book Book;
public final Book Book;
public final TOCTree TOCTree = new TOCTree();
public static final class Label {

View file

@ -162,11 +162,8 @@ public final class FBReaderApp extends ZLApplication {
}
}
public void reloadBook(Book book) {
if (Model != null) {
if (book != null) {
Model.Book = book;
}
public void reloadBook() {
if (Model != null && Model.Book != null) {
runWithMessage("loadingBook", new Runnable() {
public void run() {
openBookInternal(Model.Book, null, true);

View file

@ -53,6 +53,10 @@ public class FavoritesTree extends FirstLevelTree {
}
public boolean onBookChanged(Book book) {
return !myCollection.isFavorite(book) && removeBook(book, false);
if (myCollection.isFavorite(book)) {
return super.onBookChanged(book);
} else {
return removeBook(book, false);
}
}
}

View file

@ -185,7 +185,9 @@ public final class Library {
private synchronized void addBookToLibrary(Book book) {
synchronized (myBooks) {
if (myBooks.containsKey(book.getId())) {
final Book existing = myBooks.get(book.getId());
if (existing != null) {
existing.updateFrom(book);
return;
}
myBooks.put(book.getId(), book);
@ -251,17 +253,6 @@ public final class Library {
}
}
private void refreshInTree(String rootId, Book book) {
final FirstLevelTree tree = getFirstLevelTree(rootId);
if (tree != null) {
int index = tree.indexOf(new BookTree(book, true));
if (index >= 0) {
tree.removeBook(book, false);
new BookTree(tree, book, true, index);
}
}
}
public synchronized void refreshBookInfo(Book book) {
if (book == null) {
return;
@ -269,8 +260,6 @@ public final class Library {
Collection.saveBook(book, true);
myBooks.remove(book.getId());
refreshInTree(ROOT_FAVORITES, book);
refreshInTree(ROOT_RECENT, book);
removeFromTree(ROOT_FOUND, book);
removeFromTree(ROOT_BY_TITLE, book);
removeFromTree(ROOT_BY_SERIES, book);

View file

@ -118,9 +118,18 @@ public abstract class LibraryTree extends FBTree {
return !toRemove.isEmpty();
}
// TODO: change to abstract (?)
public boolean onBookChanged(Book book) {
return false;
boolean updated = false;
for (FBTree tree : this) {
if (tree instanceof BookTree) {
final Book b = ((BookTree)tree).Book;
if (b.equals(book)) {
b.updateFrom(book);
updated = true;
}
}
}
return updated;
}
@Override

View file

@ -42,8 +42,4 @@ public class RecentBooksTree extends FirstLevelTree {
new BookTree(this, book, true);
}
}
public boolean onBookChanged(Book book) {
return false;
}
}