1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 18:29:23 +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_PREFERENCES = 1;
public static final int REQUEST_BOOK_INFO = 2; 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_DO_NOTHING = RESULT_FIRST_USER;
public static final int RESULT_REPAINT = RESULT_FIRST_USER + 1; 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) { public static void openBookActivity(Context context, Book book, Bookmark bookmark) {
context.startActivity( context.startActivity(
@ -308,7 +308,7 @@ public final class FBReader extends Activity {
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
((BookCollectionShadow)myFBReaderApp.Collection).bindToService(this, new Runnable() { getCollection().bindToService(this, new Runnable() {
public void run() { public void run() {
new Thread() { new Thread() {
public void run() { public void run() {
@ -428,7 +428,7 @@ public final class FBReader extends Activity {
protected void onStop() { protected void onStop() {
ApiServerImplementation.sendEvent(this, ApiListener.EVENT_READ_MODE_CLOSED); ApiServerImplementation.sendEvent(this, ApiListener.EVENT_READ_MODE_CLOSED);
PopupPanel.removeAllWindows(myFBReaderApp, this); PopupPanel.removeAllWindows(myFBReaderApp, this);
((BookCollectionShadow)myFBReaderApp.Collection).unbind(); getCollection().unbind();
super.onStop(); super.onStop();
} }
@ -469,30 +469,24 @@ public final class FBReader extends Activity {
} }
} }
private void onPreferencesUpdate(int resultCode, Book book) { private void onPreferencesUpdate(Book book) {
if (book != null) {
myFBReaderApp.Collection.saveBook(book, true);
}
switch (resultCode) {
case RESULT_DO_NOTHING:
break;
case RESULT_REPAINT:
{
AndroidFontUtil.clearFontCache();
final BookModel model = myFBReaderApp.Model; final BookModel model = myFBReaderApp.Model;
if (model != null && book != null) { if (book == null || model == null || model.Book == null) {
if (model.Book != null) { return;
model.Book = book; }
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()); ZLTextHyphenator.Instance().load(model.Book.getLanguage());
}
}
myFBReaderApp.clearTextCaches(); myFBReaderApp.clearTextCaches();
myFBReaderApp.getViewWidget().repaint(); myFBReaderApp.getViewWidget().repaint();
break;
}
case RESULT_RELOAD_BOOK:
myFBReaderApp.reloadBook(book);
break;
} }
} }
@ -501,8 +495,26 @@ public final class FBReader extends Activity {
switch (requestCode) { switch (requestCode) {
case REQUEST_PREFERENCES: case REQUEST_PREFERENCES:
case REQUEST_BOOK_INFO: 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; 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: case REQUEST_CANCEL_MENU:
myFBReaderApp.runCancelAction(resultCode - 1); myFBReaderApp.runCancelAction(resultCode - 1);
break; break;
@ -695,4 +707,8 @@ public final class FBReader extends Activity {
final int level = (int)(100 * getWindow().getAttributes().screenBrightness); final int level = (int)(100 * getWindow().getAttributes().screenBrightness);
return (level >= 0) ? level : 50; 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) { if (Reader.Model != null) {
intent.putExtra(FBReader.BOOK_KEY, SerializerUtil.serialize(Reader.Model.Book)); 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(); myBook.reloadInfoFromFile();
setupBookInfo(myBook); setupBookInfo(myBook);
myDontReloadBook = false; myDontReloadBook = false;
myResult = Math.max(myResult, FBReader.RESULT_RELOAD_BOOK); myResult = FBReader.RESULT_REPAINT;
setResult(myResult, intentByBook(myBook)); setResult(myResult, intentByBook(myBook));
} }
} }
@ -153,7 +153,7 @@ public class BookInfoActivity extends Activity {
myDontReloadBook = false; myDontReloadBook = false;
} }
myResult = Math.max(myResult, resultCode); myResult = FBReader.RESULT_REPAINT;
setResult(myResult, data); 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) { protected void onActivityResult(int requestCode, int returnCode, Intent intent) {
if (requestCode == BOOK_INFO_REQUEST) { if (requestCode == BOOK_INFO_REQUEST) {
final Book book = BookInfoActivity.bookByIntent(intent); final Book book = BookInfoActivity.bookByIntent(intent);
((BookCollectionShadow)myLibrary.Collection).bindToService(this, new Runnable() {
public void run() {
myLibrary.refreshBookInfo(book); myLibrary.refreshBookInfo(book);
if (((LibraryTree)getCurrentTree()).onBookChanged(book)) {
getListView().invalidateViews(); getListView().invalidateViews();
}
}
});
} else { } else {
super.onActivityResult(requestCode, returnCode, intent); 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) { public synchronized void bindToService(Context context, Runnable onBindAction) {
if (myInterface != null && myContext == context) { if (myInterface != null && myContext == context) {
if (onBindAction != null) { if (onBindAction != null) {
onBindAction.run(); onBindAction.run();
} }
} else { } else {
if (onBindAction != null) { myOnBindAction = combined(myOnBindAction, onBindAction);
myOnBindAction = onBindAction;
}
context.bindService( context.bindService(
new Intent(context, LibraryService.class), new Intent(context, LibraryService.class),
this, this,

View file

@ -145,17 +145,6 @@ final class SQLiteBooksDatabase extends BooksDatabase {
return book; 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) { protected Book loadBookByFile(long fileId, ZLFile file) {
if (fileId == -1) { if (fileId == -1) {
return null; return null;

View file

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

View file

@ -72,6 +72,18 @@ public class Book {
myIsSaved = false; 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() { public void reloadInfoFromFile() {
try { try {
readMetaInfo(); readMetaInfo();

View file

@ -149,16 +149,16 @@ public class BookCollection extends AbstractBookCollection {
} }
synchronized (myBooksByFile) { synchronized (myBooksByFile) {
Listener.BookEvent event = Listener.BookEvent.Added; final Book existing = myBooksByFile.get(book.File);
if (myBooksByFile.containsKey(book.File)) { if (existing == null) {
if (!force) {
return;
}
event = Listener.BookEvent.Updated;
}
myBooksByFile.put(book.File, book); myBooksByFile.put(book.File, book);
myBooksById.put(book.getId(), 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 Map<Long,Book> loadBooks(FileInfoSet infos, boolean existing);
protected abstract void setExistingFlag(Collection<Book> books, boolean flag); protected abstract void setExistingFlag(Collection<Book> books, boolean flag);
protected abstract Book loadBook(long bookId); protected abstract Book loadBook(long bookId);
protected abstract void reloadBook(Book book);
protected abstract Book loadBookByFile(long fileId, ZLFile file); protected abstract Book loadBookByFile(long fileId, ZLFile file);
protected abstract List<Author> loadAuthors(long bookId); protected abstract List<Author> loadAuthors(long bookId);

View file

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

View file

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

View file

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

View file

@ -53,6 +53,10 @@ public class FavoritesTree extends FirstLevelTree {
} }
public boolean onBookChanged(Book book) { 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) { private synchronized void addBookToLibrary(Book book) {
synchronized (myBooks) { synchronized (myBooks) {
if (myBooks.containsKey(book.getId())) { final Book existing = myBooks.get(book.getId());
if (existing != null) {
existing.updateFrom(book);
return; return;
} }
myBooks.put(book.getId(), book); 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) { public synchronized void refreshBookInfo(Book book) {
if (book == null) { if (book == null) {
return; return;
@ -269,8 +260,6 @@ public final class Library {
Collection.saveBook(book, true); Collection.saveBook(book, true);
myBooks.remove(book.getId()); myBooks.remove(book.getId());
refreshInTree(ROOT_FAVORITES, book);
refreshInTree(ROOT_RECENT, book);
removeFromTree(ROOT_FOUND, book); removeFromTree(ROOT_FOUND, book);
removeFromTree(ROOT_BY_TITLE, book); removeFromTree(ROOT_BY_TITLE, book);
removeFromTree(ROOT_BY_SERIES, book); removeFromTree(ROOT_BY_SERIES, book);

View file

@ -118,9 +118,18 @@ public abstract class LibraryTree extends FBTree {
return !toRemove.isEmpty(); return !toRemove.isEmpty();
} }
// TODO: change to abstract (?)
public boolean onBookChanged(Book book) { 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 @Override

View file

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