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

Library updating changes

git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@1687 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
Vasiliy Bout 2010-09-02 19:41:35 +00:00
parent e688b18c59
commit 31f8e71bed
8 changed files with 133 additions and 39 deletions

View file

@ -21,6 +21,8 @@ package org.geometerplus.fbreader.network;
import java.util.*;
import android.util.Log;
import org.geometerplus.zlibrary.core.util.ZLNetworkUtil;
import org.geometerplus.zlibrary.core.options.ZLStringOption;
import org.geometerplus.zlibrary.core.network.ZLNetworkRequest;
@ -184,24 +186,20 @@ public class NetworkLibrary {
}
private static class LinksComparator implements Comparator<INetworkLink> {
private static String filterLinkTitle(String title) {
for (int index = 0; index < title.length(); ++index) {
final char ch = title.charAt(index);
if (ch < 128 && Character.isLetter(ch)) {
return title.substring(index);
}
}
return title;
}
public int compare(INetworkLink link1, INetworkLink link2) {
String title1 = link1.getTitle();
for (int index = 0; index < title1.length(); ++index) {
final char ch = title1.charAt(index);
if (ch < 128 && Character.isLetter(ch)) {
title1 = title1.substring(index);
break;
}
}
String title2 = link2.getTitle();
for (int index = 0; index < title2.length(); ++index) {
final char ch = title2.charAt(index);
if (ch < 128 && Character.isLetter(ch)) {
title2 = title2.substring(index);
break;
}
}
return title1.compareTo(title2);
final String title1 = filterLinkTitle(link1.getTitle());
final String title2 = filterLinkTitle(link2.getTitle());
return title1.compareToIgnoreCase(title2);
}
}
@ -220,6 +218,7 @@ public class NetworkLibrary {
private final RootTree myRootTree = new RootTree();
private boolean myUpdateChildren = true;
private boolean myInvalidateChildren;
private boolean myUpdateVisibility;
private NetworkLibrary() {
@ -329,7 +328,7 @@ public class NetworkLibrary {
synchronized (myLinks) {
myLoadedLinks.clear();
myLoadedLinks.addAll(myBackgroundLinks);
invalidate();
updateChildren();
}
}
}
@ -347,7 +346,11 @@ public class NetworkLibrary {
return url;
}
public void invalidate() {
public void invalidateChildren() {
myInvalidateChildren = true;
}
public void updateChildren() {
myUpdateChildren = true;
}
@ -355,7 +358,29 @@ public class NetworkLibrary {
myUpdateVisibility = true;
}
private static boolean linksEqual(INetworkLink l1, INetworkLink l2) {
return l1 == l2 || l1.getSiteName().equals(l2.getSiteName());
}
private static boolean linkIsInvalid(INetworkLink link, INetworkLink nodeLink) {
if (link instanceof ICustomNetworkLink) {
if (link != nodeLink) {
throw new RuntimeException("Two equal custom links!!! That's impossible");
}
return ((ICustomNetworkLink) link).hasChanges();
}
return !link.equals(nodeLink);
}
private static void makeValid(INetworkLink link) {
if (link instanceof ICustomNetworkLink) {
((ICustomNetworkLink) link).resetChanges();
}
}
private void makeUpToDate() {
Log.w("FBREADER", "makeUpToDate...");
final LinkedList<FBTree> toRemove = new LinkedList<FBTree>();
ListIterator<FBTree> nodeIterator = myRootTree.subTrees().listIterator();
@ -380,34 +405,49 @@ public class NetworkLibrary {
continue;
}
final INetworkLink nodeLink = ((NetworkCatalogTree) currentNode).Item.Link;
if (nodeLink == link) {
if (link instanceof ICustomNetworkLink) {
if (linksEqual(link, nodeLink)) {
if (linkIsInvalid(link, nodeLink)) {
Log.w("FBREADER", "invalid link: " + link.getSiteName() + " /// " + link.getTitle());
toRemove.add(currentNode);
} else {
Log.w("FBREADER", "link is ok: " + link.getSiteName() + " /// " + link.getTitle());
processed = true;
}
currentNode = null;
++nodeCount;
break;
} else {
boolean found = false;
INetworkLink newNodeLink = null;
ListIterator<INetworkLink> jt = myLinks.listIterator(it);
while (jt.hasNext()) {
if (nodeLink == jt.next()) {
found = true;
final INetworkLink jlnk = jt.next();
if (linksEqual(nodeLink, jlnk)) {
newNodeLink = jlnk;
break;
}
}
if (!found) {
if (newNodeLink == null) {
Log.w("FBREADER", "nodeLink not found: " + nodeLink.getSiteName() + " /// " + nodeLink.getTitle());
toRemove.add(currentNode);
currentNode = null;
++nodeCount;
} else {
break;
if (linkIsInvalid(newNodeLink, nodeLink)) {
Log.w("FBREADER", "move nodeLink: " + nodeLink.getSiteName() + " /// " + nodeLink.getTitle());
Log.w("FBREADER", "new link will be: " + newNodeLink.getSiteName() + " /// " + newNodeLink.getTitle());
toRemove.add(currentNode);
currentNode = null;
++nodeCount;
} else {
Log.w("FBREADER", "need node for link: " + link.getSiteName() + " /// " + link.getTitle());
break;
}
}
}
}
if (!processed) {
Log.w("FBREADER", "create node for link: " + link.getSiteName() + " /// " + link.getTitle());
makeValid(link);
final int nextIndex = nodeIterator.nextIndex();
new NetworkCatalogRootTree(myRootTree, link, nodeCount++).Item.onDisplayItem();
nodeIterator = myRootTree.subTrees().listIterator(nextIndex + 1);
@ -419,7 +459,10 @@ public class NetworkLibrary {
if (currentNode == null) {
currentNode = nodeIterator.next();
}
toRemove.add(currentNode);
if (currentNode instanceof NetworkCatalogTree) {
Log.w("FBREADER", "remove node: " + currentNode.getName());
toRemove.add(currentNode);
}
currentNode = null;
}
@ -438,8 +481,14 @@ public class NetworkLibrary {
}
public void synchronize() {
if (myUpdateChildren) {
if (myUpdateChildren || myInvalidateChildren) {
if (myInvalidateChildren) {
final LinksComparator cmp = new LinksComparator();
//Collections.sort(myLoadedLinks, cmp); // this collection is always sorted
Collections.sort(myCustomLinks, cmp);
}
myUpdateChildren = false;
myInvalidateChildren = false;
makeUpToDate();
}
if (myUpdateVisibility) {