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

BookElementsHolder (in text view) => BookElementManager (in fbreader.fbreader)

This commit is contained in:
Nikolay Pultsin 2014-12-30 01:51:51 +00:00
parent 6d2ab76f50
commit 0bf543eb6c
5 changed files with 93 additions and 14 deletions

View file

@ -17,26 +17,39 @@
* 02110-1301, USA. * 02110-1301, USA.
*/ */
package org.geometerplus.zlibrary.text.view; package org.geometerplus.fbreader.fbreader;
import java.io.InputStream;
import java.io.IOException;
import java.util.*; import java.util.*;
class BookElementsHolder { import org.geometerplus.zlibrary.core.network.*;
import org.geometerplus.zlibrary.text.view.BookElement;
import org.geometerplus.zlibrary.text.view.ExtensionElementManager;
import org.geometerplus.fbreader.network.opds.*;
class BookElementManager extends ExtensionElementManager {
private final Runnable myScreenRefresher; private final Runnable myScreenRefresher;
private final Map<Map<String,String>,List<BookElement>> myCache = private final Map<Map<String,String>,List<BookElement>> myCache =
new HashMap<Map<String,String>,List<BookElement>>(); new HashMap<Map<String,String>,List<BookElement>>();
private Timer myTimer; private Timer myTimer;
BookElementsHolder(final ZLTextView view) { BookElementManager(final FBReaderApp reader) {
myScreenRefresher = new Runnable() { myScreenRefresher = new Runnable() {
public void run() { public void run() {
view.Application.getViewWidget().reset(); reader.getViewWidget().reset();
view.Application.getViewWidget().repaint(); reader.getViewWidget().repaint();
} }
}; };
} }
synchronized List<BookElement> getElements(Map<String,String> data) { @Override
protected synchronized List<BookElement> getElements(String type, Map<String,String> data) {
if (!"opds".equals(type)) {
return Collections.emptyList();
}
List<BookElement> elements = myCache.get(data); List<BookElement> elements = myCache.get(data);
if (elements == null) { if (elements == null) {
try { try {
@ -57,6 +70,37 @@ class BookElementsHolder {
private void startLoading(final String url, final List<BookElement> elements) { private void startLoading(final String url, final List<BookElement> elements) {
new Thread() { new Thread() {
public void run() { public void run() {
final SimpleOPDSFeedHandler handler = new SimpleOPDSFeedHandler(url);
try {
new QuietNetworkContext().perform(new ZLNetworkRequest.Get(url, true) {
@Override
public void handleStream(InputStream inputStream, int length) throws IOException, ZLNetworkException {
new OPDSXMLReader(handler, false).read(inputStream);
}
});
if (handler.books().isEmpty()) {
throw new RuntimeException();
}
myTimer = null;
final List<OPDSBookItem> items = handler.books();
int index = 0;
for (BookElement book : elements) {
//book.setData(items.get(index));
index = (index + 1) % items.size();
myScreenRefresher.run();
}
} catch (Exception e) {
if (myTimer == null) {
myTimer = new Timer();
}
myTimer.schedule(new TimerTask() {
@Override
public void run() {
startLoading(url, elements);
}
}, 10000);
e.printStackTrace();
}
} }
}.start(); }.start();
} }

View file

@ -40,11 +40,13 @@ import org.geometerplus.fbreader.fbreader.options.*;
public final class FBView extends ZLTextView { public final class FBView extends ZLTextView {
private final FBReaderApp myReader; private final FBReaderApp myReader;
private final ViewOptions myViewOptions; private final ViewOptions myViewOptions;
private final BookElementManager myBookElementManager;
FBView(FBReaderApp reader) { FBView(FBReaderApp reader) {
super(reader); super(reader);
myReader = reader; myReader = reader;
myViewOptions = reader.ViewOptions; myViewOptions = reader.ViewOptions;
myBookElementManager = new BookElementManager(reader);
} }
public void setModel(ZLTextModel model) { public void setModel(ZLTextModel model) {
@ -783,4 +785,9 @@ public final class FBView extends ZLTextView {
super.onScrollingFinished(pageIndex); super.onScrollingFinished(pageIndex);
myReader.storePosition(); myReader.storePosition();
} }
@Override
protected ExtensionElementManager getExtensionManager() {
return myBookElementManager;
}
} }

View file

@ -0,0 +1,33 @@
/*
* Copyright (C) 2007-2014 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.zlibrary.text.view;
import java.util.List;
import java.util.Map;
import org.geometerplus.zlibrary.text.model.ExtensionEntry;
public abstract class ExtensionElementManager {
final List<BookElement> getElements(ExtensionEntry entry) {
return getElements(entry.Type, entry.Data);
}
protected abstract List<BookElement> getElements(String type, Map<String,String> data);
}

View file

@ -109,13 +109,8 @@ public final class ZLTextParagraphCursor {
elements.add(new ZLTextVideoElement(it.getVideoEntry().sources())); elements.add(new ZLTextVideoElement(it.getVideoEntry().sources()));
break; break;
case ZLTextParagraph.Entry.EXTENSION: case ZLTextParagraph.Entry.EXTENSION:
{ elements.addAll(myView.getExtensionManager().getElements(it.getExtensionEntry()));
final ExtensionEntry entry = it.getExtensionEntry();
if ("opds".equals(entry.Type)) {
elements.addAll(myView.Holder.getElements(entry.Data));
}
break; break;
}
case ZLTextParagraph.Entry.STYLE_CSS: case ZLTextParagraph.Entry.STYLE_CSS:
case ZLTextParagraph.Entry.STYLE_OTHER: case ZLTextParagraph.Entry.STYLE_OTHER:
elements.add(new ZLTextStyleElement(it.getStyleEntry())); elements.add(new ZLTextStyleElement(it.getStyleEntry()));

View file

@ -68,8 +68,6 @@ public abstract class ZLTextView extends ZLTextViewBase {
private final ZLTextParagraphCursorCache myCursorCache = new ZLTextParagraphCursorCache(); private final ZLTextParagraphCursorCache myCursorCache = new ZLTextParagraphCursorCache();
final BookElementsHolder Holder = new BookElementsHolder(this);
public ZLTextView(ZLApplication application) { public ZLTextView(ZLApplication application) {
super(application); super(application);
} }
@ -1887,4 +1885,6 @@ public abstract class ZLTextView extends ZLTextViewBase {
} }
return result; return result;
} }
protected abstract ExtensionElementManager getExtensionManager();
} }