From 273f9d575688f5cd0f181c1cc9a44a2d34df9d4d Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 13 Apr 2018 19:55:24 +0300 Subject: [PATCH] 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; } + } }