Merge pull request #5234 from osmandapp/JsonContentsParser
Wikivoyage contents parser
This commit is contained in:
commit
0e94f534b0
3 changed files with 116 additions and 105 deletions
|
@ -19,12 +19,8 @@ import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
|
||||||
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
|
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
|
||||||
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
|
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
|
||||||
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
|
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
|
||||||
import net.osmand.plus.wikivoyage.data.ContentsJsonParser;
|
import net.osmand.plus.wikivoyage.data.WikivoyageJsonParser;
|
||||||
import net.osmand.plus.wikivoyage.data.ContentsJsonParser.ContentsContainer;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFragment {
|
public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFragment {
|
||||||
|
|
||||||
|
@ -37,9 +33,6 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
||||||
|
|
||||||
private ExpandableListView expListView;
|
private ExpandableListView expListView;
|
||||||
|
|
||||||
private LinkedHashMap<String, String> map;
|
|
||||||
private String link;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createMenuItems(Bundle savedInstanceState) {
|
public void createMenuItems(Bundle savedInstanceState) {
|
||||||
Bundle args = getArguments();
|
Bundle args = getArguments();
|
||||||
|
@ -52,20 +45,15 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ContentsContainer contentsContainer = ContentsJsonParser.parseJsonContents(contentsJson);
|
final WikivoyageJsonParser.WikivoyageContentItem wikivoyageContentItem = WikivoyageJsonParser.parseJsonContents(contentsJson);
|
||||||
if (contentsContainer == null) {
|
if (wikivoyageContentItem == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final ArrayList<String> listDataHeader = contentsContainer.listDataHeader;
|
|
||||||
final LinkedHashMap<String, List<String>> listDataChild = contentsContainer.listDataChild;
|
|
||||||
|
|
||||||
map = contentsContainer.map;
|
|
||||||
|
|
||||||
items.add(new TitleItem(getString(R.string.shared_string_contents)));
|
items.add(new TitleItem(getString(R.string.shared_string_contents)));
|
||||||
|
|
||||||
expListView = new ExpandableListView(getContext());
|
expListView = new ExpandableListView(getContext());
|
||||||
ExpandableListAdapter listAdapter = new ExpandableListAdapter(getContext(), listDataHeader, listDataChild);
|
ExpandableListAdapter listAdapter = new ExpandableListAdapter(getContext(), wikivoyageContentItem);
|
||||||
|
|
||||||
expListView.setAdapter(listAdapter);
|
expListView.setAdapter(listAdapter);
|
||||||
Drawable transparent = ContextCompat.getDrawable(getContext(), R.color.color_transparent);
|
Drawable transparent = ContextCompat.getDrawable(getContext(), R.color.color_transparent);
|
||||||
|
@ -80,9 +68,9 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
||||||
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
|
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onChildClick(ExpandableListView parent, View v,
|
public boolean onChildClick(ExpandableListView parent, View v,
|
||||||
int groupPosition, int childPosition, long id) {
|
int groupPosition, int childPosition, long id) {
|
||||||
link = map.get(listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition));
|
String link = wikivoyageContentItem.getSubItems().get(groupPosition).getSubItems().get(childPosition).getLink();
|
||||||
sendResult();
|
sendResult(link);
|
||||||
dismiss();
|
dismiss();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -90,8 +78,8 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
||||||
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
|
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
|
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
|
||||||
link = map.get(listDataHeader.get(groupPosition));
|
String link = wikivoyageContentItem.getSubItems().get(groupPosition).getLink();
|
||||||
sendResult();
|
sendResult(link);
|
||||||
dismiss();
|
dismiss();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -102,7 +90,7 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
||||||
items.add(new SimpleBottomSheetItem.Builder().setCustomView(container).create());
|
items.add(new SimpleBottomSheetItem.Builder().setCustomView(container).create());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendResult() {
|
private void sendResult(String link) {
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
intent.putExtra(CONTENTS_LINK_KEY, link);
|
intent.putExtra(CONTENTS_LINK_KEY, link);
|
||||||
Fragment fragment = getTargetFragment();
|
Fragment fragment = getTargetFragment();
|
||||||
|
@ -130,17 +118,14 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
|
|
||||||
private List<String> listDataHeader;
|
private WikivoyageJsonParser.WikivoyageContentItem wikivoyageContentItem;
|
||||||
private LinkedHashMap<String, List<String>> listDataChild;
|
|
||||||
|
|
||||||
private Drawable itemGroupIcon;
|
private Drawable itemGroupIcon;
|
||||||
private Drawable itemChildIcon;
|
private Drawable itemChildIcon;
|
||||||
|
|
||||||
ExpandableListAdapter(Context context, List<String> listDataHeader,
|
ExpandableListAdapter(Context context, WikivoyageJsonParser.WikivoyageContentItem wikivoyageContentItem) {
|
||||||
LinkedHashMap<String, List<String>> listChildData) {
|
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.listDataHeader = listDataHeader;
|
this.wikivoyageContentItem = wikivoyageContentItem;
|
||||||
this.listDataChild = listChildData;
|
|
||||||
|
|
||||||
itemGroupIcon = getIcon(R.drawable.ic_action_list_header, nightMode
|
itemGroupIcon = getIcon(R.drawable.ic_action_list_header, nightMode
|
||||||
? R.color.wikivoyage_contents_parent_icon_dark : R.color.wikivoyage_contents_parent_icon_light);
|
? R.color.wikivoyage_contents_parent_icon_dark : R.color.wikivoyage_contents_parent_icon_light);
|
||||||
|
@ -151,7 +136,7 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getChild(int groupPosition, int childPosititon) {
|
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
|
@Override
|
||||||
|
@ -176,7 +161,7 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
||||||
|
|
||||||
convertView.findViewById(R.id.upper_row_divider).setVisibility(View.GONE);
|
convertView.findViewById(R.id.upper_row_divider).setVisibility(View.GONE);
|
||||||
txtListChild.setTypeface(null);
|
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);
|
convertView.findViewById(R.id.bottom_row_divider).setVisibility(View.VISIBLE);
|
||||||
} else {
|
} else {
|
||||||
convertView.findViewById(R.id.bottom_row_divider).setVisibility(View.GONE);
|
convertView.findViewById(R.id.bottom_row_divider).setVisibility(View.GONE);
|
||||||
|
@ -187,18 +172,18 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getChildrenCount(int groupPosition) {
|
public int getChildrenCount(int groupPosition) {
|
||||||
List<String> list = listDataChild.get(listDataHeader.get(groupPosition));
|
return wikivoyageContentItem.getSubItems().get(groupPosition).getSubItems().size();
|
||||||
return list == null ? 0 : list.size();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getGroup(int groupPosition) {
|
public Object getGroup(int groupPosition) {
|
||||||
return this.listDataHeader.get(groupPosition);
|
return wikivoyageContentItem.getSubItems().get(groupPosition).getName();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getGroupCount() {
|
public int getGroupCount() {
|
||||||
return this.listDataHeader.size();
|
return wikivoyageContentItem.getSubItems().size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,71 +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<String, String> map = new LinkedHashMap<>();
|
|
||||||
ArrayList<String> listDataHeader = new ArrayList<>();
|
|
||||||
LinkedHashMap<String, List<String>> listDataChild = new LinkedHashMap<>();
|
|
||||||
|
|
||||||
JSONObject reader;
|
|
||||||
try {
|
|
||||||
reader = new JSONObject(contentsJson);
|
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
List<String> secondLevel = null;
|
|
||||||
JSONArray jArray = reader.names();
|
|
||||||
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);
|
|
||||||
|
|
||||||
int level = contacts.getInt(0);
|
|
||||||
|
|
||||||
if (level == 2) {
|
|
||||||
listDataHeader.add(reader.names().getString(i));
|
|
||||||
secondLevel = new ArrayList<>();
|
|
||||||
}
|
|
||||||
if (level == 3) {
|
|
||||||
if (secondLevel == null) {
|
|
||||||
secondLevel = new ArrayList<>();
|
|
||||||
}
|
|
||||||
secondLevel.add(reader.names().getString(i));
|
|
||||||
listDataChild.put(listDataHeader.get(listDataHeader.size() - 1), secondLevel);
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new ContentsContainer(map, listDataHeader, listDataChild);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class ContentsContainer {
|
|
||||||
|
|
||||||
public LinkedHashMap<String, String> map;
|
|
||||||
public ArrayList<String> listDataHeader;
|
|
||||||
public LinkedHashMap<String, List<String>> listDataChild;
|
|
||||||
|
|
||||||
ContentsContainer(LinkedHashMap<String, String> map,
|
|
||||||
ArrayList<String> listDataHeader,
|
|
||||||
LinkedHashMap<String, List<String>> listChildData) {
|
|
||||||
this.map = map;
|
|
||||||
this.listDataHeader = listDataHeader;
|
|
||||||
this.listDataChild = listChildData;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,97 @@
|
||||||
|
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 WikivoyageContentItem parseJsonContents(String contentsJson) {
|
||||||
|
|
||||||
|
JSONObject jArray;
|
||||||
|
JSONObject reader;
|
||||||
|
|
||||||
|
try {
|
||||||
|
reader = new JSONObject(contentsJson);
|
||||||
|
jArray = reader.getJSONObject(HEADERS);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return 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);
|
||||||
|
WikivoyageContentItem contentHeaderItem = new WikivoyageContentItem(jArray.names().getString(i), link, topWikivoyageContentItem);
|
||||||
|
topWikivoyageContentItem.subItems.add(contentHeaderItem);
|
||||||
|
|
||||||
|
JSONArray jsonSubheaders = jsonHeader.getJSONArray(SUBHEADERS);
|
||||||
|
List<String> subheaderNames = null;
|
||||||
|
for (int j = 0; j < jsonSubheaders.length(); j++) {
|
||||||
|
JSONObject jsonSubheader = jsonSubheaders.getJSONObject(j);
|
||||||
|
JSONObject jsonSubheaderLink = jsonSubheader.getJSONObject(jsonSubheader.keys().next());
|
||||||
|
if (subheaderNames == null) {
|
||||||
|
subheaderNames = new ArrayList<>();
|
||||||
|
}
|
||||||
|
subheaderNames.add(jsonSubheader.keys().next());
|
||||||
|
link = jsonSubheaderLink.getString(LINK);
|
||||||
|
|
||||||
|
WikivoyageContentItem contentsSubHeaderContainer = new WikivoyageContentItem(jsonSubheader.names().getString(0), link, contentHeaderItem);
|
||||||
|
contentHeaderItem.subItems.add(contentsSubHeaderContainer);
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return topWikivoyageContentItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class WikivoyageContentItem {
|
||||||
|
|
||||||
|
private String link;
|
||||||
|
private String name;
|
||||||
|
private ArrayList<WikivoyageContentItem> subItems = new ArrayList<>();
|
||||||
|
private WikivoyageContentItem parent;
|
||||||
|
|
||||||
|
private WikivoyageContentItem(String name, String link) {
|
||||||
|
this.name = name;
|
||||||
|
this.link = link;
|
||||||
|
}
|
||||||
|
|
||||||
|
private WikivoyageContentItem(String name, String link, WikivoyageContentItem parent) {
|
||||||
|
this.parent = parent;
|
||||||
|
this.name = name;
|
||||||
|
this.link = link;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLink() {
|
||||||
|
return link;
|
||||||
|
}
|
||||||
|
|
||||||
|
public WikivoyageContentItem getParent() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<WikivoyageContentItem> getSubItems() {
|
||||||
|
return subItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue