From 82f0a9ccd8353265a679d675c53b736fce26ab7b Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 13 Apr 2018 16:01:08 +0300 Subject: [PATCH 1/6] change contents json parser --- .../WikivoyageArticleContentsFragment.java | 5 + .../WikivoyageArticleDialogFragment.java | 15 ++ .../wikivoyage/data/ContentsJsonParser.java | 152 ++++++++++++++++-- 3 files changed, 161 insertions(+), 11 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java index 6d0e9d6c5b..42eb655e68 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java @@ -57,6 +57,11 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag return; } + ContentsJsonParser.Container contentsContainer2 = ContentsJsonParser.parseJsonContents2(contentsJson); + if (contentsContainer2 == null) { + return; + } + final ArrayList listDataHeader = contentsContainer.listDataHeader; final LinkedHashMap> listDataChild = contentsContainer.listDataChild; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java index 6edf4e53fa..92fab5140b 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java @@ -50,6 +50,21 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen "window.location.hash = id;}"; private static final String FOOTER_INNER = ""; + private static final String collapsibleJS = ""; + private long cityId = NO_VALUE; private ArrayList langs; private String selectedLang; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/ContentsJsonParser.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/ContentsJsonParser.java index 76773c7507..5b9346db1f 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/ContentsJsonParser.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/ContentsJsonParser.java @@ -17,6 +17,7 @@ public class ContentsJsonParser { LinkedHashMap map = new LinkedHashMap<>(); ArrayList listDataHeader = new ArrayList<>(); LinkedHashMap> listDataChild = new LinkedHashMap<>(); + Container headers = new Container(); JSONObject reader; try { @@ -26,26 +27,49 @@ public class ContentsJsonParser { return null; } List secondLevel = null; - JSONArray jArray = reader.names(); + JSONObject jArray = null; + try { + jArray = reader.getJSONObject("headers"); + } catch (JSONException e) { + e.printStackTrace(); + } + for (int i = 0; i < jArray.length(); i++) { try { - JSONArray contacts = reader.getJSONArray(reader.names().getString(i)); - String link = contacts.getString(1); - map.put(reader.names().getString(i), link); + String link = ""; + JSONObject header = jArray.getJSONObject(jArray.names().getString(i)); + link = header.getString("link"); + map.put(jArray.names().getString(i), link); - int level = contacts.getInt(0); - if (level == 2) { - listDataHeader.add(reader.names().getString(i)); + Container firstLvl = new Container(jArray.names().getString(i), link); + headers.childs.add(firstLvl); + firstLvl.setParent(headers); + + + listDataHeader.add(jArray.names().getString(i)); + JSONArray contacts = header.getJSONArray("subheaders"); + + if (contacts.length() > 1) { secondLevel = new ArrayList<>(); } - if (level == 3) { + for (int j = 0; j < contacts.length(); j++) { + String names = header.names().toString(); + JSONObject subheader = contacts.getJSONObject(j); + JSONObject subheader2 = subheader.getJSONObject(subheader.names().getString(0)); if (secondLevel == null) { secondLevel = new ArrayList<>(); } - secondLevel.add(reader.names().getString(i)); + secondLevel.add(subheader.names().getString(0)); listDataChild.put(listDataHeader.get(listDataHeader.size() - 1), secondLevel); + link = subheader2.getString("link"); + map.put(subheader.names().getString(0), link); + + Container secondLvl = new Container(subheader.names().getString(0), link); + firstLvl.childs.add(secondLvl); + secondLvl.setParent(headers); + } } catch (JSONException e) { e.printStackTrace(); @@ -54,6 +78,64 @@ public class ContentsJsonParser { return new ContentsContainer(map, listDataHeader, listDataChild); } + @Nullable + public static Container parseJsonContents2(String contentsJson) { + Container headers = new Container(); + + JSONObject reader; + try { + reader = new JSONObject(contentsJson); + } catch (JSONException e) { + e.printStackTrace(); + return null; + } + List secondLevel = null; + JSONObject jArray = null; + try { + jArray = reader.getJSONObject("headers"); + } catch (JSONException e) { + e.printStackTrace(); + } + + for (int i = 0; i < jArray.length(); i++) { + try { + + String link = ""; + JSONObject header = jArray.getJSONObject(jArray.names().getString(i)); + link = header.getString("link"); + + + Container firstLvl = new Container(jArray.names().getString(i), link); + headers.childs.add(firstLvl); + firstLvl.setParent(headers); + + + JSONArray contacts = header.getJSONArray("subheaders"); + + if (contacts.length() > 1) { + secondLevel = new ArrayList<>(); + } + for (int j = 0; j < contacts.length(); j++) { + JSONObject subheader = contacts.getJSONObject(j); + JSONObject subheader2 = subheader.getJSONObject(subheader.names().getString(0)); + if (secondLevel == null) { + secondLevel = new ArrayList<>(); + } + secondLevel.add(subheader.names().getString(0)); + link = subheader2.getString("link"); + + Container secondLvl = new Container(subheader.names().getString(0), link); + firstLvl.childs.add(secondLvl); + secondLvl.setParent(headers); + + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + return headers; + } + public static class ContentsContainer { public LinkedHashMap map; @@ -61,11 +143,59 @@ public class ContentsJsonParser { public LinkedHashMap> listDataChild; ContentsContainer(LinkedHashMap map, - ArrayList listDataHeader, - LinkedHashMap> listChildData) { + ArrayList listDataHeader, + LinkedHashMap> listChildData) { this.map = map; this.listDataHeader = listDataHeader; this.listDataChild = listChildData; } } + + public static class Container { + String name; + String link; + ArrayList childs = new ArrayList<>(); + Container parent; + + public ArrayList getChilds() { + return childs; + } + + public void setChilds(ArrayList childs) { + this.childs = childs; + } + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLink() { + return link; + } + + public void setLink(String link) { + this.link = link; + } + + public Container getParent() { + return parent; + } + + public void setParent(Container parent) { + this.parent = parent; + } + + public Container(String name, String link) { + this.name = name; + this.link = link; + } + + public Container() { + } + } } From 8cea14a7e57e3bdbc11c5965025f9d5084bf68cc Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 13 Apr 2018 18:15:17 +0300 Subject: [PATCH 2/6] get collections from contentsContainer --- .../WikivoyageArticleContentsFragment.java | 32 ++- .../wikivoyage/data/ContentsJsonParser.java | 201 ------------------ .../wikivoyage/data/WikivoyageJsonParser.java | 110 ++++++++++ 3 files changed, 131 insertions(+), 212 deletions(-) delete mode 100644 OsmAnd/src/net/osmand/plus/wikivoyage/data/ContentsJsonParser.java create mode 100644 OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java index 42eb655e68..2789587ec2 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java @@ -19,8 +19,7 @@ import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; -import net.osmand.plus.wikivoyage.data.ContentsJsonParser; -import net.osmand.plus.wikivoyage.data.ContentsJsonParser.ContentsContainer; +import net.osmand.plus.wikivoyage.data.WikivoyageJsonParser; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -52,21 +51,32 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag return; } - ContentsContainer contentsContainer = ContentsJsonParser.parseJsonContents(contentsJson); + WikivoyageJsonParser.ContentsContainer contentsContainer = WikivoyageJsonParser.parseJsonContents(contentsJson); if (contentsContainer == null) { return; } - ContentsJsonParser.Container contentsContainer2 = ContentsJsonParser.parseJsonContents2(contentsJson); - if (contentsContainer2 == null) { - return; + final ArrayList listDataHeader = new ArrayList(); + final LinkedHashMap> listDataChild = new LinkedHashMap>(); + map = new LinkedHashMap<>(); + + for (int i = 0; i < contentsContainer.getChilds().size(); i++) { + WikivoyageJsonParser.ContentsContainer Header = contentsContainer.getChilds().get(i); + listDataHeader.add(Header.getName()); + map.put(Header.getName(), Header.getLink()); + + if (Header.getChilds() != null && !Header.getChilds().isEmpty()) { + ArrayList subHeaders = new ArrayList<>(); + + for (int j = 0; j < Header.getChilds().size(); j++) { + WikivoyageJsonParser.ContentsContainer subHeader = Header.getChilds().get(j); + subHeaders.add(subHeader.getName()); + map.put(subHeader.getName(), subHeader.getLink()); + } + listDataChild.put(Header.getName(), subHeaders); + } } - final ArrayList listDataHeader = contentsContainer.listDataHeader; - final LinkedHashMap> listDataChild = contentsContainer.listDataChild; - - map = contentsContainer.map; - items.add(new TitleItem(getString(R.string.shared_string_contents))); expListView = new ExpandableListView(getContext()); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/ContentsJsonParser.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/ContentsJsonParser.java deleted file mode 100644 index 5b9346db1f..0000000000 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/ContentsJsonParser.java +++ /dev/null @@ -1,201 +0,0 @@ -package net.osmand.plus.wikivoyage.data; - -import android.support.annotation.Nullable; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; - -public class ContentsJsonParser { - - @Nullable - public static ContentsContainer parseJsonContents(String contentsJson) { - LinkedHashMap map = new LinkedHashMap<>(); - ArrayList listDataHeader = new ArrayList<>(); - LinkedHashMap> listDataChild = new LinkedHashMap<>(); - Container headers = new Container(); - - JSONObject reader; - try { - reader = new JSONObject(contentsJson); - } catch (JSONException e) { - e.printStackTrace(); - return null; - } - List secondLevel = null; - JSONObject jArray = null; - try { - jArray = reader.getJSONObject("headers"); - } catch (JSONException e) { - e.printStackTrace(); - } - - for (int i = 0; i < jArray.length(); i++) { - try { - - String link = ""; - JSONObject header = jArray.getJSONObject(jArray.names().getString(i)); - link = header.getString("link"); - map.put(jArray.names().getString(i), link); - - - Container firstLvl = new Container(jArray.names().getString(i), link); - headers.childs.add(firstLvl); - firstLvl.setParent(headers); - - - listDataHeader.add(jArray.names().getString(i)); - JSONArray contacts = header.getJSONArray("subheaders"); - - if (contacts.length() > 1) { - secondLevel = new ArrayList<>(); - } - for (int j = 0; j < contacts.length(); j++) { - String names = header.names().toString(); - JSONObject subheader = contacts.getJSONObject(j); - JSONObject subheader2 = subheader.getJSONObject(subheader.names().getString(0)); - if (secondLevel == null) { - secondLevel = new ArrayList<>(); - } - secondLevel.add(subheader.names().getString(0)); - listDataChild.put(listDataHeader.get(listDataHeader.size() - 1), secondLevel); - link = subheader2.getString("link"); - map.put(subheader.names().getString(0), link); - - Container secondLvl = new Container(subheader.names().getString(0), link); - firstLvl.childs.add(secondLvl); - secondLvl.setParent(headers); - - } - } catch (JSONException e) { - e.printStackTrace(); - } - } - return new ContentsContainer(map, listDataHeader, listDataChild); - } - - @Nullable - public static Container parseJsonContents2(String contentsJson) { - Container headers = new Container(); - - JSONObject reader; - try { - reader = new JSONObject(contentsJson); - } catch (JSONException e) { - e.printStackTrace(); - return null; - } - List secondLevel = null; - JSONObject jArray = null; - try { - jArray = reader.getJSONObject("headers"); - } catch (JSONException e) { - e.printStackTrace(); - } - - for (int i = 0; i < jArray.length(); i++) { - try { - - String link = ""; - JSONObject header = jArray.getJSONObject(jArray.names().getString(i)); - link = header.getString("link"); - - - Container firstLvl = new Container(jArray.names().getString(i), link); - headers.childs.add(firstLvl); - firstLvl.setParent(headers); - - - JSONArray contacts = header.getJSONArray("subheaders"); - - if (contacts.length() > 1) { - secondLevel = new ArrayList<>(); - } - for (int j = 0; j < contacts.length(); j++) { - JSONObject subheader = contacts.getJSONObject(j); - JSONObject subheader2 = subheader.getJSONObject(subheader.names().getString(0)); - if (secondLevel == null) { - secondLevel = new ArrayList<>(); - } - secondLevel.add(subheader.names().getString(0)); - link = subheader2.getString("link"); - - Container secondLvl = new Container(subheader.names().getString(0), link); - firstLvl.childs.add(secondLvl); - secondLvl.setParent(headers); - - } - } catch (JSONException e) { - e.printStackTrace(); - } - } - return headers; - } - - public static class ContentsContainer { - - public LinkedHashMap map; - public ArrayList listDataHeader; - public LinkedHashMap> listDataChild; - - ContentsContainer(LinkedHashMap map, - ArrayList listDataHeader, - LinkedHashMap> listChildData) { - this.map = map; - this.listDataHeader = listDataHeader; - this.listDataChild = listChildData; - } - } - - public static class Container { - String name; - String link; - ArrayList childs = new ArrayList<>(); - Container parent; - - public ArrayList getChilds() { - return childs; - } - - public void setChilds(ArrayList childs) { - this.childs = childs; - } - - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getLink() { - return link; - } - - public void setLink(String link) { - this.link = link; - } - - public Container getParent() { - return parent; - } - - public void setParent(Container parent) { - this.parent = parent; - } - - public Container(String name, String link) { - this.name = name; - this.link = link; - } - - public Container() { - } - } -} diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java new file mode 100644 index 0000000000..a2f8e4eeab --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java @@ -0,0 +1,110 @@ +package net.osmand.plus.wikivoyage.data; + +import android.support.annotation.Nullable; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +public class WikivoyageJsonParser { + + private static final String HEADERS = "headers"; + private static final String SUBHEADERS = "subheaders"; + private static final String LINK = "link"; + + @Nullable + public static ContentsContainer parseJsonContents(String contentsJson) { + + JSONObject jArray; + JSONObject reader; + + try { + reader = new JSONObject(contentsJson); + jArray = reader.getJSONObject(HEADERS); + } catch (JSONException e) { + e.printStackTrace(); + return null; + } + + ContentsContainer topContentsContainer = new ContentsContainer(HEADERS, null); + for (int i = 0; i < jArray.length(); i++) { + try { + String link = ""; + JSONObject jsonHeader = jArray.getJSONObject(jArray.names().getString(i)); + link = jsonHeader.getString(LINK); + ContentsContainer contentsHeaderContainer = new ContentsContainer(jArray.names().getString(i), link); + topContentsContainer.childs.add(contentsHeaderContainer); + contentsHeaderContainer.setParent(topContentsContainer); + + JSONArray jsonSubheaders = jsonHeader.getJSONArray(SUBHEADERS); + List subheaderNames = null; + for (int j = 0; j < jsonSubheaders.length(); j++) { + JSONObject jsonSubheader = jsonSubheaders.getJSONObject(j); + JSONObject jsonSubheaderLink = jsonSubheader.getJSONObject(jsonSubheader.names().getString(0)); + if (subheaderNames == null) { + subheaderNames = new ArrayList<>(); + } + subheaderNames.add(jsonSubheader.names().getString(0)); + link = jsonSubheaderLink.getString(LINK); + + ContentsContainer contentsSubHeaderContainer = new ContentsContainer(jsonSubheader.names().getString(0), link); + contentsHeaderContainer.childs.add(contentsSubHeaderContainer); + contentsSubHeaderContainer.setParent(topContentsContainer); + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + return topContentsContainer; + } + + public static class ContentsContainer { + + ArrayList childs = new ArrayList<>(); + ContentsContainer parent; + + String name; + String link; + + public ArrayList getChilds() { + return childs; + } + + public void setChilds(ArrayList childs) { + this.childs = childs; + } + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLink() { + return link; + } + + public void setLink(String link) { + this.link = link; + } + + public ContentsContainer getParent() { + return parent; + } + + public void setParent(ContentsContainer parent) { + this.parent = parent; + } + + public ContentsContainer(String name, String link) { + this.name = name; + this.link = link; + } + } +} From 6169fe89d02fc9af5a0ad10a8d85b7b089e3849f Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 13 Apr 2018 18:20:18 +0300 Subject: [PATCH 3/6] delete unnecessary js --- .../article/WikivoyageArticleDialogFragment.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java index 92fab5140b..6edf4e53fa 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java @@ -50,21 +50,6 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen "window.location.hash = id;}"; private static final String FOOTER_INNER = ""; - private static final String collapsibleJS = ""; - private long cityId = NO_VALUE; private ArrayList langs; private String selectedLang; From cf77d0b44563782cf4febf00c36b3e3a2fba7101 Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 13 Apr 2018 18:28:25 +0300 Subject: [PATCH 4/6] cleaned some code --- .../WikivoyageArticleContentsFragment.java | 10 ++++---- .../wikivoyage/data/WikivoyageJsonParser.java | 24 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java index 2789587ec2..4bb0248b74 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java @@ -60,16 +60,16 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag final LinkedHashMap> listDataChild = new LinkedHashMap>(); map = new LinkedHashMap<>(); - for (int i = 0; i < contentsContainer.getChilds().size(); i++) { - WikivoyageJsonParser.ContentsContainer Header = contentsContainer.getChilds().get(i); + for (int i = 0; i < contentsContainer.getChildItems().size(); i++) { + WikivoyageJsonParser.ContentsContainer Header = contentsContainer.getChildItems().get(i); listDataHeader.add(Header.getName()); map.put(Header.getName(), Header.getLink()); - if (Header.getChilds() != null && !Header.getChilds().isEmpty()) { + if (Header.getChildItems() != null && !Header.getChildItems().isEmpty()) { ArrayList subHeaders = new ArrayList<>(); - for (int j = 0; j < Header.getChilds().size(); j++) { - WikivoyageJsonParser.ContentsContainer subHeader = Header.getChilds().get(j); + for (int j = 0; j < Header.getChildItems().size(); j++) { + WikivoyageJsonParser.ContentsContainer subHeader = Header.getChildItems().get(j); subHeaders.add(subHeader.getName()); map.put(subHeader.getName(), subHeader.getLink()); } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java index a2f8e4eeab..bb923958cd 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java @@ -36,7 +36,7 @@ public class WikivoyageJsonParser { JSONObject jsonHeader = jArray.getJSONObject(jArray.names().getString(i)); link = jsonHeader.getString(LINK); ContentsContainer contentsHeaderContainer = new ContentsContainer(jArray.names().getString(i), link); - topContentsContainer.childs.add(contentsHeaderContainer); + topContentsContainer.childItems.add(contentsHeaderContainer); contentsHeaderContainer.setParent(topContentsContainer); JSONArray jsonSubheaders = jsonHeader.getJSONArray(SUBHEADERS); @@ -51,7 +51,7 @@ public class WikivoyageJsonParser { link = jsonSubheaderLink.getString(LINK); ContentsContainer contentsSubHeaderContainer = new ContentsContainer(jsonSubheader.names().getString(0), link); - contentsHeaderContainer.childs.add(contentsSubHeaderContainer); + contentsHeaderContainer.childItems.add(contentsSubHeaderContainer); contentsSubHeaderContainer.setParent(topContentsContainer); } } catch (JSONException e) { @@ -63,18 +63,23 @@ public class WikivoyageJsonParser { public static class ContentsContainer { - ArrayList childs = new ArrayList<>(); + ArrayList childItems = new ArrayList<>(); ContentsContainer parent; String name; String link; - public ArrayList getChilds() { - return childs; + public ContentsContainer(String name, String link) { + this.name = name; + this.link = link; } - public void setChilds(ArrayList childs) { - this.childs = childs; + public ArrayList getChildItems() { + return childItems; + } + + public void setChildItems(ArrayList childItems) { + this.childItems = childItems; } @@ -101,10 +106,5 @@ public class WikivoyageJsonParser { public void setParent(ContentsContainer parent) { this.parent = parent; } - - public ContentsContainer(String name, String link) { - this.name = name; - this.link = link; - } } } From 1416120be571c74d324a3aef0dbfd2023fb4fc85 Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 13 Apr 2018 18:31:31 +0300 Subject: [PATCH 5/6] rename ContentsContainer to ContentsItem --- .../WikivoyageArticleContentsFragment.java | 14 +++---- .../wikivoyage/data/WikivoyageJsonParser.java | 38 +++++++++---------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java index 4bb0248b74..506d03cd78 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java @@ -51,8 +51,8 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag return; } - WikivoyageJsonParser.ContentsContainer contentsContainer = WikivoyageJsonParser.parseJsonContents(contentsJson); - if (contentsContainer == null) { + WikivoyageJsonParser.ContentsItem contentsItem = WikivoyageJsonParser.parseJsonContents(contentsJson); + if (contentsItem == null) { return; } @@ -60,8 +60,8 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag final LinkedHashMap> listDataChild = new LinkedHashMap>(); map = new LinkedHashMap<>(); - for (int i = 0; i < contentsContainer.getChildItems().size(); i++) { - WikivoyageJsonParser.ContentsContainer Header = contentsContainer.getChildItems().get(i); + for (int i = 0; i < contentsItem.getChildItems().size(); i++) { + WikivoyageJsonParser.ContentsItem Header = contentsItem.getChildItems().get(i); listDataHeader.add(Header.getName()); map.put(Header.getName(), Header.getLink()); @@ -69,9 +69,9 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag ArrayList subHeaders = new ArrayList<>(); for (int j = 0; j < Header.getChildItems().size(); j++) { - WikivoyageJsonParser.ContentsContainer subHeader = Header.getChildItems().get(j); - subHeaders.add(subHeader.getName()); - map.put(subHeader.getName(), subHeader.getLink()); + WikivoyageJsonParser.ContentsItem subHeaderItem = Header.getChildItems().get(j); + subHeaders.add(subHeaderItem.getName()); + map.put(subHeaderItem.getName(), subHeaderItem.getLink()); } listDataChild.put(Header.getName(), subHeaders); } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java index bb923958cd..036b83d4ed 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java @@ -16,7 +16,7 @@ public class WikivoyageJsonParser { private static final String LINK = "link"; @Nullable - public static ContentsContainer parseJsonContents(String contentsJson) { + public static ContentsItem parseJsonContents(String contentsJson) { JSONObject jArray; JSONObject reader; @@ -29,15 +29,15 @@ public class WikivoyageJsonParser { return null; } - ContentsContainer topContentsContainer = new ContentsContainer(HEADERS, null); + ContentsItem topContentsItem = new ContentsItem(HEADERS, null); for (int i = 0; i < jArray.length(); i++) { try { String link = ""; JSONObject jsonHeader = jArray.getJSONObject(jArray.names().getString(i)); link = jsonHeader.getString(LINK); - ContentsContainer contentsHeaderContainer = new ContentsContainer(jArray.names().getString(i), link); - topContentsContainer.childItems.add(contentsHeaderContainer); - contentsHeaderContainer.setParent(topContentsContainer); + ContentsItem contentsHeaderItem = new ContentsItem(jArray.names().getString(i), link); + topContentsItem.childItems.add(contentsHeaderItem); + contentsHeaderItem.setParent(topContentsItem); JSONArray jsonSubheaders = jsonHeader.getJSONArray(SUBHEADERS); List subheaderNames = null; @@ -50,35 +50,35 @@ public class WikivoyageJsonParser { subheaderNames.add(jsonSubheader.names().getString(0)); link = jsonSubheaderLink.getString(LINK); - ContentsContainer contentsSubHeaderContainer = new ContentsContainer(jsonSubheader.names().getString(0), link); - contentsHeaderContainer.childItems.add(contentsSubHeaderContainer); - contentsSubHeaderContainer.setParent(topContentsContainer); + ContentsItem contentsSubHeaderContainer = new ContentsItem(jsonSubheader.names().getString(0), link); + contentsHeaderItem.childItems.add(contentsSubHeaderContainer); + contentsSubHeaderContainer.setParent(topContentsItem); } } catch (JSONException e) { e.printStackTrace(); } } - return topContentsContainer; + return topContentsItem; } - public static class ContentsContainer { + public static class ContentsItem { - ArrayList childItems = new ArrayList<>(); - ContentsContainer parent; + private ArrayList childItems = new ArrayList<>(); + private ContentsItem parent; - String name; - String link; + private String name; + private String link; - public ContentsContainer(String name, String link) { + public ContentsItem(String name, String link) { this.name = name; this.link = link; } - public ArrayList getChildItems() { + public ArrayList getChildItems() { return childItems; } - public void setChildItems(ArrayList childItems) { + public void setChildItems(ArrayList childItems) { this.childItems = childItems; } @@ -99,11 +99,11 @@ public class WikivoyageJsonParser { this.link = link; } - public ContentsContainer getParent() { + public ContentsItem getParent() { return parent; } - public void setParent(ContentsContainer parent) { + public void setParent(ContentsItem parent) { this.parent = parent; } } From 273f9d575688f5cd0f181c1cc9a44a2d34df9d4d Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 13 Apr 2018 19:55:24 +0300 Subject: [PATCH 6/6] remove setters and collections --- .../WikivoyageArticleContentsFragment.java | 66 +++++-------------- .../wikivoyage/data/WikivoyageJsonParser.java | 57 +++++++--------- 2 files changed, 40 insertions(+), 83 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java index 506d03cd78..2fe0444a6e 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleContentsFragment.java @@ -21,9 +21,6 @@ import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; import net.osmand.plus.wikivoyage.data.WikivoyageJsonParser; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFragment { @@ -36,9 +33,6 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag private ExpandableListView expListView; - private LinkedHashMap map; - private String link; - @Override public void createMenuItems(Bundle savedInstanceState) { Bundle args = getArguments(); @@ -51,36 +45,15 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag return; } - WikivoyageJsonParser.ContentsItem contentsItem = WikivoyageJsonParser.parseJsonContents(contentsJson); - if (contentsItem == null) { + final WikivoyageJsonParser.WikivoyageContentItem wikivoyageContentItem = WikivoyageJsonParser.parseJsonContents(contentsJson); + if (wikivoyageContentItem == null) { return; } - final ArrayList listDataHeader = new ArrayList(); - final LinkedHashMap> listDataChild = new LinkedHashMap>(); - map = new LinkedHashMap<>(); - - for (int i = 0; i < contentsItem.getChildItems().size(); i++) { - WikivoyageJsonParser.ContentsItem Header = contentsItem.getChildItems().get(i); - listDataHeader.add(Header.getName()); - map.put(Header.getName(), Header.getLink()); - - if (Header.getChildItems() != null && !Header.getChildItems().isEmpty()) { - ArrayList subHeaders = new ArrayList<>(); - - for (int j = 0; j < Header.getChildItems().size(); j++) { - WikivoyageJsonParser.ContentsItem subHeaderItem = Header.getChildItems().get(j); - subHeaders.add(subHeaderItem.getName()); - map.put(subHeaderItem.getName(), subHeaderItem.getLink()); - } - listDataChild.put(Header.getName(), subHeaders); - } - } - items.add(new TitleItem(getString(R.string.shared_string_contents))); expListView = new ExpandableListView(getContext()); - ExpandableListAdapter listAdapter = new ExpandableListAdapter(getContext(), listDataHeader, listDataChild); + ExpandableListAdapter listAdapter = new ExpandableListAdapter(getContext(), wikivoyageContentItem); expListView.setAdapter(listAdapter); Drawable transparent = ContextCompat.getDrawable(getContext(), R.color.color_transparent); @@ -95,9 +68,9 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, - int groupPosition, int childPosition, long id) { - link = map.get(listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition)); - sendResult(); + int groupPosition, int childPosition, long id) { + String link = wikivoyageContentItem.getSubItems().get(groupPosition).getSubItems().get(childPosition).getLink(); + sendResult(link); dismiss(); return false; } @@ -105,8 +78,8 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { - link = map.get(listDataHeader.get(groupPosition)); - sendResult(); + String link = wikivoyageContentItem.getSubItems().get(groupPosition).getLink(); + sendResult(link); dismiss(); return false; } @@ -117,7 +90,7 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag items.add(new SimpleBottomSheetItem.Builder().setCustomView(container).create()); } - private void sendResult() { + private void sendResult(String link) { Intent intent = new Intent(); intent.putExtra(CONTENTS_LINK_KEY, link); Fragment fragment = getTargetFragment(); @@ -145,17 +118,14 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag private Context context; - private List listDataHeader; - private LinkedHashMap> listDataChild; + private WikivoyageJsonParser.WikivoyageContentItem wikivoyageContentItem; private Drawable itemGroupIcon; private Drawable itemChildIcon; - ExpandableListAdapter(Context context, List listDataHeader, - LinkedHashMap> listChildData) { + ExpandableListAdapter(Context context, WikivoyageJsonParser.WikivoyageContentItem wikivoyageContentItem) { this.context = context; - this.listDataHeader = listDataHeader; - this.listDataChild = listChildData; + this.wikivoyageContentItem = wikivoyageContentItem; itemGroupIcon = getIcon(R.drawable.ic_action_list_header, nightMode ? R.color.wikivoyage_contents_parent_icon_dark : R.color.wikivoyage_contents_parent_icon_light); @@ -166,7 +136,7 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag @Override public Object getChild(int groupPosition, int childPosititon) { - return listDataChild.get(listDataHeader.get(groupPosition)).get(childPosititon); + return wikivoyageContentItem.getSubItems().get(groupPosition).getSubItems().get(childPosititon).getName(); } @Override @@ -191,7 +161,7 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag convertView.findViewById(R.id.upper_row_divider).setVisibility(View.GONE); txtListChild.setTypeface(null); - if (childPosition == listDataChild.get(listDataHeader.get(groupPosition)).size() - 1) { + if (childPosition == wikivoyageContentItem.getSubItems().get(groupPosition).getSubItems().size() - 1) { convertView.findViewById(R.id.bottom_row_divider).setVisibility(View.VISIBLE); } else { convertView.findViewById(R.id.bottom_row_divider).setVisibility(View.GONE); @@ -202,18 +172,18 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag @Override public int getChildrenCount(int groupPosition) { - List list = listDataChild.get(listDataHeader.get(groupPosition)); - return list == null ? 0 : list.size(); + return wikivoyageContentItem.getSubItems().get(groupPosition).getSubItems().size(); } @Override public Object getGroup(int groupPosition) { - return this.listDataHeader.get(groupPosition); + return wikivoyageContentItem.getSubItems().get(groupPosition).getName(); + } @Override public int getGroupCount() { - return this.listDataHeader.size(); + return wikivoyageContentItem.getSubItems().size(); } @Override diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java index 036b83d4ed..cb791f9913 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageJsonParser.java @@ -16,7 +16,7 @@ public class WikivoyageJsonParser { private static final String LINK = "link"; @Nullable - public static ContentsItem parseJsonContents(String contentsJson) { + public static WikivoyageContentItem parseJsonContents(String contentsJson) { JSONObject jArray; JSONObject reader; @@ -29,82 +29,69 @@ public class WikivoyageJsonParser { return null; } - ContentsItem topContentsItem = new ContentsItem(HEADERS, null); + WikivoyageContentItem topWikivoyageContentItem = new WikivoyageContentItem(HEADERS, null); for (int i = 0; i < jArray.length(); i++) { try { String link = ""; JSONObject jsonHeader = jArray.getJSONObject(jArray.names().getString(i)); link = jsonHeader.getString(LINK); - ContentsItem contentsHeaderItem = new ContentsItem(jArray.names().getString(i), link); - topContentsItem.childItems.add(contentsHeaderItem); - contentsHeaderItem.setParent(topContentsItem); + WikivoyageContentItem contentHeaderItem = new WikivoyageContentItem(jArray.names().getString(i), link, topWikivoyageContentItem); + topWikivoyageContentItem.subItems.add(contentHeaderItem); JSONArray jsonSubheaders = jsonHeader.getJSONArray(SUBHEADERS); List subheaderNames = null; for (int j = 0; j < jsonSubheaders.length(); j++) { JSONObject jsonSubheader = jsonSubheaders.getJSONObject(j); - JSONObject jsonSubheaderLink = jsonSubheader.getJSONObject(jsonSubheader.names().getString(0)); + JSONObject jsonSubheaderLink = jsonSubheader.getJSONObject(jsonSubheader.keys().next()); if (subheaderNames == null) { subheaderNames = new ArrayList<>(); } - subheaderNames.add(jsonSubheader.names().getString(0)); + subheaderNames.add(jsonSubheader.keys().next()); link = jsonSubheaderLink.getString(LINK); - ContentsItem contentsSubHeaderContainer = new ContentsItem(jsonSubheader.names().getString(0), link); - contentsHeaderItem.childItems.add(contentsSubHeaderContainer); - contentsSubHeaderContainer.setParent(topContentsItem); + WikivoyageContentItem contentsSubHeaderContainer = new WikivoyageContentItem(jsonSubheader.names().getString(0), link, contentHeaderItem); + contentHeaderItem.subItems.add(contentsSubHeaderContainer); } } catch (JSONException e) { e.printStackTrace(); } } - return topContentsItem; + return topWikivoyageContentItem; } - public static class ContentsItem { + public static class WikivoyageContentItem { - private ArrayList childItems = new ArrayList<>(); - private ContentsItem parent; - - private String name; private String link; + private String name; + private ArrayList subItems = new ArrayList<>(); + private WikivoyageContentItem parent; - public ContentsItem(String name, String link) { + private WikivoyageContentItem(String name, String link) { this.name = name; this.link = link; } - public ArrayList getChildItems() { - return childItems; + private WikivoyageContentItem(String name, String link, WikivoyageContentItem parent) { + this.parent = parent; + this.name = name; + this.link = link; } - public void setChildItems(ArrayList childItems) { - this.childItems = childItems; - } - - public String getName() { return name; } - public void setName(String name) { - this.name = name; - } - public String getLink() { return link; } - public void setLink(String link) { - this.link = link; - } - - public ContentsItem getParent() { + public WikivoyageContentItem getParent() { return parent; } - public void setParent(ContentsItem parent) { - this.parent = parent; + public ArrayList getSubItems() { + return subItems; } + } }