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

code simplification

This commit is contained in:
Nikolay Pultsin 2011-03-02 16:34:28 +00:00
parent 48de79c40f
commit 348c749054
6 changed files with 20 additions and 56 deletions

View file

@ -25,10 +25,9 @@ import android.view.Menu;
import android.view.ContextMenu; import android.view.ContextMenu;
import org.geometerplus.fbreader.network.NetworkTree; import org.geometerplus.fbreader.network.NetworkTree;
import org.geometerplus.fbreader.network.AddCustomCatalogItemTree;
class AddCustomCatalogItemActions extends NetworkTreeActions { class AddCustomCatalogItemActions extends NetworkTreeActions {
public static final int RUN_ITEM_ID = 0; public static final int RUN_ITEM_ID = 0;
@Override @Override

View file

@ -474,7 +474,6 @@ class NetworkCatalogActions extends NetworkTreeActions {
private void removeCustomLink(ICustomNetworkLink link) { private void removeCustomLink(ICustomNetworkLink link) {
final NetworkLibrary library = NetworkLibrary.Instance(); final NetworkLibrary library = NetworkLibrary.Instance();
library.removeCustomLink(link); library.removeCustomLink(link);
library.updateChildren();
library.synchronize(); library.synchronize();
NetworkView.Instance().fireModelChangedAsync(); NetworkView.Instance().fireModelChangedAsync();
} }

View file

@ -91,7 +91,6 @@ public class NetworkLibraryActivity extends NetworkBaseActivity {
public void run() { public void run() {
final NetworkLibrary library = NetworkLibrary.Instance(); final NetworkLibrary library = NetworkLibrary.Instance();
library.addCustomLink(link); library.addCustomLink(link);
library.updateChildren();
library.synchronize(); library.synchronize();
NetworkView.Instance().fireModelChangedAsync(); NetworkView.Instance().fireModelChangedAsync();
getListView().invalidateViews(); getListView().invalidateViews();
@ -227,7 +226,7 @@ public class NetworkLibraryActivity extends NetworkBaseActivity {
if (!NetworkView.Instance().isInitialized()) { if (!NetworkView.Instance().isInitialized()) {
return 0; return 0;
} }
return myTree.subTrees().size() + 2; // subtrees + <search item> + <add custom catalog item> return myTree.subTrees().size() + 1; // subtrees + <search item>
} }
public final NetworkTree getItem(int position) { public final NetworkTree getItem(int position) {
@ -236,8 +235,6 @@ public class NetworkLibraryActivity extends NetworkBaseActivity {
return NetworkView.Instance().getSearchItemTree(); return NetworkView.Instance().getSearchItemTree();
} else if (position > 0 && position <= size) { } else if (position > 0 && position <= size) {
return (NetworkTree)myTree.subTrees().get(position - 1); return (NetworkTree)myTree.subTrees().get(position - 1);
} else if (position == size + 1) {
return NetworkView.Instance().getAddCustomCatalogItemTree();
} }
return null; return null;
} }
@ -319,7 +316,6 @@ public class NetworkLibraryActivity extends NetworkBaseActivity {
} }
} }
library.setActiveLanguageCodes(newActiveCodes); library.setActiveLanguageCodes(newActiveCodes);
library.invalidateChildren();
library.synchronize(); library.synchronize();
NetworkView.Instance().fireModelChanged(); NetworkView.Instance().fireModelChanged();
} }

View file

@ -71,7 +71,6 @@ class NetworkView {
final NetworkTree root = library.getRootTree(); final NetworkTree root = library.getRootTree();
mySearchItem = new SearchItemTree(root); mySearchItem = new SearchItemTree(root);
myAddCustomCatalogItem = new AddCustomCatalogItemTree(root);
myInitialized = true; myInitialized = true;
} }
@ -297,13 +296,8 @@ class NetworkView {
*/ */
private SearchItemTree mySearchItem; private SearchItemTree mySearchItem;
private AddCustomCatalogItemTree myAddCustomCatalogItem;
public SearchItemTree getSearchItemTree() { public SearchItemTree getSearchItemTree() {
return mySearchItem; return mySearchItem;
} }
public AddCustomCatalogItemTree getAddCustomCatalogItemTree() {
return myAddCustomCatalogItem;
}
} }

View file

@ -17,7 +17,7 @@
* 02110-1301, USA. * 02110-1301, USA.
*/ */
package org.geometerplus.android.fbreader.network; package org.geometerplus.fbreader.network;
import org.geometerplus.zlibrary.core.resources.ZLResource; import org.geometerplus.zlibrary.core.resources.ZLResource;

View file

@ -119,6 +119,7 @@ public class NetworkLibrary {
allCodes.removeAll(languageCodes()); allCodes.removeAll(languageCodes());
allCodes.addAll(codes); allCodes.addAll(codes);
activeLanguageCodesOption().setValue(commaSeparatedString(allCodes)); activeLanguageCodesOption().setValue(commaSeparatedString(allCodes));
invalidateChildren();
} }
private String commaSeparatedString(Collection<String> codes) { private String commaSeparatedString(Collection<String> codes) {
@ -149,8 +150,7 @@ public class NetworkLibrary {
private final RootTree myRootTree = new RootTree(); private final RootTree myRootTree = new RootTree();
private boolean myUpdateChildren = true; private boolean myChildrenAreInvalid = true;
private boolean myInvalidateChildren;
private boolean myUpdateVisibility; private boolean myUpdateVisibility;
private NetworkLibrary() { private NetworkLibrary() {
@ -188,24 +188,6 @@ public class NetworkLibrary {
); );
} }
/*testDate(new ATOMUpdated(2010, 1, 1, 1, 0, 0, 0, 2, 0),
new ATOMUpdated(2009, 12, 31, 23, 0, 0, 0, 0, 0));
testDate(new ATOMUpdated(2010, 12, 31, 23, 40, 0, 0, -1, -30),
new ATOMUpdated(2011, 1, 1, 1, 10, 0, 0, 0, 0));
testDate(new ATOMUpdated(2010, 1, 31, 23, 40, 0, 0, -1, -30),
new ATOMUpdated(2010, 2, 1, 1, 10, 0, 0, 0, 0));
testDate(new ATOMUpdated(2010, 2, 28, 23, 40, 0, 0, -1, -30),
new ATOMUpdated(2010, 3, 1, 1, 10, 0, 0, 0, 0));
testDate(new ATOMUpdated(2012, 2, 28, 23, 40, 0, 0, -1, -30),
new ATOMUpdated(2012, 2, 29, 1, 10, 0, 0, 0, 0));
testDate(new ATOMUpdated(2012, 2, 15, 23, 40, 0, 0, -1, -30),
new ATOMUpdated(2012, 2, 16, 1, 10, 0, 0, 0, 0));
testDate(new ATOMUpdated(2012, 2, 15, 23, 40, 1, 0, 3, 30),
new ATOMUpdated(2012, 2, 15, 23, 40, 0, 0, 3, 30));
testDate(new ATOMUpdated(2012, 2, 15, 23, 40, 0, 0, 3, 30),
new ATOMUpdated(2012, 2, 15, 23, 40, 1, 0, 3, 30));
testDate(new ATOMUpdated(2012, 2, 15, 23, 40, 0, 0.001f, 3, 30),
new ATOMUpdated(2012, 2, 15, 23, 40, 0, 0, 3, 30));*/
myIsAlreadyInitialized = true; myIsAlreadyInitialized = true;
} }
@ -272,7 +254,7 @@ public class NetworkLibrary {
synchronized (myLinks) { synchronized (myLinks) {
removeAllLoadedLinks(); removeAllLoadedLinks();
myLinks.addAll(myBackgroundLinks); myLinks.addAll(myBackgroundLinks);
updateChildren(); invalidateChildren();
} }
} }
} }
@ -291,30 +273,22 @@ public class NetworkLibrary {
} }
public void invalidateChildren() { public void invalidateChildren() {
myInvalidateChildren = true; myChildrenAreInvalid = true;
}
public void updateChildren() {
myUpdateChildren = true;
} }
public void invalidateVisibility() { public void invalidateVisibility() {
myUpdateVisibility = true; myUpdateVisibility = true;
} }
private static boolean linkIsInvalid(INetworkLink link, INetworkLink nodeLink) { private static boolean linkIsChanged(INetworkLink link) {
if (link instanceof ICustomNetworkLink) { return
if (link != nodeLink) { link instanceof ICustomNetworkLink &&
throw new RuntimeException("Two equal custom links!!! That's impossible"); ((ICustomNetworkLink)link).hasChanges();
}
return ((ICustomNetworkLink) link).hasChanges();
}
return !link.equals(nodeLink);
} }
private static void makeValid(INetworkLink link) { private static void makeValid(INetworkLink link) {
if (link instanceof ICustomNetworkLink) { if (link instanceof ICustomNetworkLink) {
((ICustomNetworkLink) link).resetChanges(); ((ICustomNetworkLink)link).resetChanges();
} }
} }
@ -340,9 +314,9 @@ public class NetworkLibrary {
++nodeCount; ++nodeCount;
continue; continue;
} }
final INetworkLink nodeLink = ((NetworkCatalogTree) currentNode).Item.Link; final INetworkLink nodeLink = ((NetworkCatalogTree)currentNode).Item.Link;
if (link == nodeLink) { if (link == nodeLink) {
if (linkIsInvalid(link, nodeLink)) { if (linkIsChanged(link)) {
toRemove.add(currentNode); toRemove.add(currentNode);
} else { } else {
processed = true; processed = true;
@ -359,7 +333,7 @@ public class NetworkLibrary {
break; break;
} }
} }
if (newNodeLink == null || linkIsInvalid(newNodeLink, nodeLink)) { if (newNodeLink == null || linkIsChanged(nodeLink)) {
toRemove.add(currentNode); toRemove.add(currentNode);
currentNode = null; currentNode = null;
++nodeCount; ++nodeCount;
@ -390,6 +364,7 @@ public class NetworkLibrary {
for (FBTree tree : toRemove) { for (FBTree tree : toRemove) {
tree.removeSelf(); tree.removeSelf();
} }
new AddCustomCatalogItemTree(myRootTree);
} }
private void updateVisibility() { private void updateVisibility() {
@ -402,9 +377,8 @@ public class NetworkLibrary {
} }
public void synchronize() { public void synchronize() {
if (myUpdateChildren || myInvalidateChildren) { if (myChildrenAreInvalid) {
myUpdateChildren = false; myChildrenAreInvalid = false;
myInvalidateChildren = false;
makeUpToDate(); makeUpToDate();
} }
if (myUpdateVisibility) { if (myUpdateVisibility) {
@ -499,6 +473,7 @@ public class NetworkLibrary {
} }
} }
NetworkDatabase.Instance().saveCustomLink(link); NetworkDatabase.Instance().saveCustomLink(link);
invalidateChildren();
} }
public void removeCustomLink(ICustomNetworkLink link) { public void removeCustomLink(ICustomNetworkLink link) {
@ -506,6 +481,7 @@ public class NetworkLibrary {
myLinks.remove(link); myLinks.remove(link);
} }
NetworkDatabase.Instance().deleteCustomLink(link); NetworkDatabase.Instance().deleteCustomLink(link);
invalidateChildren();
} }
public boolean hasCustomLinkTitle(String title, INetworkLink exceptFor) { public boolean hasCustomLinkTitle(String title, INetworkLink exceptFor) {