Merge pull request #5237 from osmandapp/JsonContentsParser
Json contents parser
This commit is contained in:
commit
35d338e2df
3 changed files with 49 additions and 18 deletions
|
@ -29,6 +29,7 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
|||
|
||||
public static final String CONTENTS_JSON_KEY = "contents_json";
|
||||
public static final String CONTENTS_LINK_KEY = "contents_link";
|
||||
public static final String CONTENTS_TITLE_KEY = "title";
|
||||
|
||||
public static final int REQUEST_LINK_CODE = 0;
|
||||
|
||||
|
@ -70,8 +71,10 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
|||
@Override
|
||||
public boolean onChildClick(ExpandableListView parent, View v,
|
||||
int groupPosition, int childPosition, long id) {
|
||||
String link = contentItem.getSubItems().get(groupPosition).getSubItems().get(childPosition).getLink();
|
||||
sendResult(link);
|
||||
WikivoyageContentItem wikivoyageContentItem = contentItem.getSubItems().get(groupPosition);
|
||||
String link = wikivoyageContentItem.getSubItems().get(childPosition).getLink();
|
||||
String name = wikivoyageContentItem.getLink().substring(1);
|
||||
sendResults(link, name);
|
||||
dismiss();
|
||||
return false;
|
||||
}
|
||||
|
@ -79,8 +82,10 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
|||
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
|
||||
@Override
|
||||
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
|
||||
String link = contentItem.getSubItems().get(groupPosition).getLink();
|
||||
sendResult(link);
|
||||
WikivoyageContentItem wikivoyageContentItem = contentItem.getSubItems().get(groupPosition);
|
||||
String link = wikivoyageContentItem.getLink();
|
||||
String name = wikivoyageContentItem.getLink().substring(1);
|
||||
sendResults(link, name);
|
||||
dismiss();
|
||||
return false;
|
||||
}
|
||||
|
@ -91,9 +96,10 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
|||
items.add(new SimpleBottomSheetItem.Builder().setCustomView(container).create());
|
||||
}
|
||||
|
||||
private void sendResult(String link) {
|
||||
private void sendResults(String link, String name) {
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(CONTENTS_LINK_KEY, link);
|
||||
intent.putExtra(CONTENTS_TITLE_KEY, name);
|
||||
Fragment fragment = getTargetFragment();
|
||||
if (fragment != null) {
|
||||
fragment.onActivityResult(getTargetRequestCode(), REQUEST_LINK_CODE, intent);
|
||||
|
|
|
@ -48,9 +48,29 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
|
|||
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n" +
|
||||
"<meta http-equiv=\"cleartype\" content=\"on\" />\n" +
|
||||
"<link href=\"article_style.css\" type=\"text/css\" rel=\"stylesheet\"/>\n" +
|
||||
"</head><body>\n" + "<script>" + "function scrollAnchor(id) {" +
|
||||
"window.location.hash = id;}</script>";
|
||||
private static final String FOOTER_INNER = "</div></body></html>";
|
||||
"</head><body>\n";
|
||||
private static final String FOOTER_INNER = "<script>var coll = document.getElementsByTagName(\"H2\");" +
|
||||
"var i;" +
|
||||
"for (i = 0; i < coll.length; i++) {" +
|
||||
" coll[i].addEventListener(\"click\", function() {" +
|
||||
" this.classList.toggle(\"active\");" +
|
||||
" var content = this.nextElementSibling;" +
|
||||
" if (content.style.display === \"block\") {" +
|
||||
" content.style.display = \"none\";" +
|
||||
" } else {" +
|
||||
" content.style.display = \"block\";" +
|
||||
" }" +
|
||||
" });" +
|
||||
"}" + "function scrollAnchor(id, title) {" +
|
||||
"openContent(title);" +
|
||||
"window.location.hash = id;}\n" +
|
||||
"function openContent(id) {\n" +
|
||||
"var doc = document.getElementById(id).parentElement;\n" +
|
||||
" doc.classList.toggle(\"active\");\n" +
|
||||
" var content = doc.nextElementSibling;\n" +
|
||||
"content.style.display = \"block\";\n" +
|
||||
"}</script>"
|
||||
+ "</body></html>";
|
||||
|
||||
private long cityId = NO_VALUE;
|
||||
private ArrayList<String> langs;
|
||||
|
@ -144,7 +164,8 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
|
|||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == WikivoyageArticleContentsFragment.REQUEST_LINK_CODE) {
|
||||
String link = data.getStringExtra(WikivoyageArticleContentsFragment.CONTENTS_LINK_KEY);
|
||||
moveToAnchor(link);
|
||||
String title = data.getStringExtra(WikivoyageArticleContentsFragment.CONTENTS_TITLE_KEY);
|
||||
moveToAnchor(link, title);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -227,8 +248,8 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
|
|||
contentWebView.loadDataWithBaseURL(getBaseUrl(), createHtmlContent(article), "text/html", "UTF-8", null);
|
||||
}
|
||||
|
||||
private void moveToAnchor(String id) {
|
||||
contentWebView.loadUrl("javascript:scrollAnchor(\"" + id + "\")");
|
||||
private void moveToAnchor(String id, String title) {
|
||||
contentWebView.loadUrl("javascript:scrollAnchor(\"" + id + "\", \"" + title.trim() + "\")");
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package net.osmand.plus.wikivoyage.data;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
@ -11,6 +12,8 @@ import java.util.List;
|
|||
|
||||
public class WikivoyageJsonParser {
|
||||
|
||||
private static final String TAG = WikivoyageJsonParser.class.getSimpleName();
|
||||
|
||||
private static final String HEADERS = "headers";
|
||||
private static final String SUBHEADERS = "subheaders";
|
||||
private static final String LINK = "link";
|
||||
|
@ -25,7 +28,7 @@ public class WikivoyageJsonParser {
|
|||
reader = new JSONObject(contentsJson);
|
||||
jArray = reader.getJSONObject(HEADERS);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
Log.e(TAG, e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -37,22 +40,23 @@ public class WikivoyageJsonParser {
|
|||
WikivoyageContentItem headerItem = new WikivoyageContentItem(jArray.names().getString(i), link, topContentItem);
|
||||
topContentItem.subItems.add(headerItem);
|
||||
|
||||
JSONArray subheaders = header.getJSONArray(SUBHEADERS);
|
||||
JSONObject subheaders = header.getJSONObject(SUBHEADERS);
|
||||
JSONArray subNames = subheaders.names();
|
||||
List<String> subheaderNames = null;
|
||||
for (int j = 0; j < subheaders.length(); j++) {
|
||||
JSONObject subheader = subheaders.getJSONObject(j);
|
||||
JSONObject subheaderLink = subheader.getJSONObject(subheader.keys().next());
|
||||
String title = subNames.get(j).toString();
|
||||
JSONObject subheaderLink = subheaders.getJSONObject(title);
|
||||
if (subheaderNames == null) {
|
||||
subheaderNames = new ArrayList<>();
|
||||
}
|
||||
subheaderNames.add(subheader.keys().next());
|
||||
subheaderNames.add(title);
|
||||
link = subheaderLink.getString(LINK);
|
||||
|
||||
WikivoyageContentItem subheaderItem = new WikivoyageContentItem(subheader.names().getString(0), link, headerItem);
|
||||
WikivoyageContentItem subheaderItem = new WikivoyageContentItem(title, link, headerItem);
|
||||
headerItem.subItems.add(subheaderItem);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
Log.e(TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
return topContentItem;
|
||||
|
|
Loading…
Reference in a new issue