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_JSON_KEY = "contents_json";
|
||||||
public static final String CONTENTS_LINK_KEY = "contents_link";
|
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;
|
public static final int REQUEST_LINK_CODE = 0;
|
||||||
|
|
||||||
|
@ -70,8 +71,10 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
||||||
@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) {
|
||||||
String link = contentItem.getSubItems().get(groupPosition).getSubItems().get(childPosition).getLink();
|
WikivoyageContentItem wikivoyageContentItem = contentItem.getSubItems().get(groupPosition);
|
||||||
sendResult(link);
|
String link = wikivoyageContentItem.getSubItems().get(childPosition).getLink();
|
||||||
|
String name = wikivoyageContentItem.getLink().substring(1);
|
||||||
|
sendResults(link, name);
|
||||||
dismiss();
|
dismiss();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -79,8 +82,10 @@ 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) {
|
||||||
String link = contentItem.getSubItems().get(groupPosition).getLink();
|
WikivoyageContentItem wikivoyageContentItem = contentItem.getSubItems().get(groupPosition);
|
||||||
sendResult(link);
|
String link = wikivoyageContentItem.getLink();
|
||||||
|
String name = wikivoyageContentItem.getLink().substring(1);
|
||||||
|
sendResults(link, name);
|
||||||
dismiss();
|
dismiss();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -91,9 +96,10 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
|
||||||
items.add(new SimpleBottomSheetItem.Builder().setCustomView(container).create());
|
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 intent = new Intent();
|
||||||
intent.putExtra(CONTENTS_LINK_KEY, link);
|
intent.putExtra(CONTENTS_LINK_KEY, link);
|
||||||
|
intent.putExtra(CONTENTS_TITLE_KEY, name);
|
||||||
Fragment fragment = getTargetFragment();
|
Fragment fragment = getTargetFragment();
|
||||||
if (fragment != null) {
|
if (fragment != null) {
|
||||||
fragment.onActivityResult(getTargetRequestCode(), REQUEST_LINK_CODE, intent);
|
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 name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n" +
|
||||||
"<meta http-equiv=\"cleartype\" content=\"on\" />\n" +
|
"<meta http-equiv=\"cleartype\" content=\"on\" />\n" +
|
||||||
"<link href=\"article_style.css\" type=\"text/css\" rel=\"stylesheet\"/>\n" +
|
"<link href=\"article_style.css\" type=\"text/css\" rel=\"stylesheet\"/>\n" +
|
||||||
"</head><body>\n" + "<script>" + "function scrollAnchor(id) {" +
|
"</head><body>\n";
|
||||||
"window.location.hash = id;}</script>";
|
private static final String FOOTER_INNER = "<script>var coll = document.getElementsByTagName(\"H2\");" +
|
||||||
private static final String FOOTER_INNER = "</div></body></html>";
|
"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 long cityId = NO_VALUE;
|
||||||
private ArrayList<String> langs;
|
private ArrayList<String> langs;
|
||||||
|
@ -144,7 +164,8 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
if (requestCode == WikivoyageArticleContentsFragment.REQUEST_LINK_CODE) {
|
if (requestCode == WikivoyageArticleContentsFragment.REQUEST_LINK_CODE) {
|
||||||
String link = data.getStringExtra(WikivoyageArticleContentsFragment.CONTENTS_LINK_KEY);
|
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);
|
contentWebView.loadDataWithBaseURL(getBaseUrl(), createHtmlContent(article), "text/html", "UTF-8", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void moveToAnchor(String id) {
|
private void moveToAnchor(String id, String title) {
|
||||||
contentWebView.loadUrl("javascript:scrollAnchor(\"" + id + "\")");
|
contentWebView.loadUrl("javascript:scrollAnchor(\"" + id + "\", \"" + title.trim() + "\")");
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package net.osmand.plus.wikivoyage.data;
|
package net.osmand.plus.wikivoyage.data;
|
||||||
|
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
|
@ -11,6 +12,8 @@ import java.util.List;
|
||||||
|
|
||||||
public class WikivoyageJsonParser {
|
public class WikivoyageJsonParser {
|
||||||
|
|
||||||
|
private static final String TAG = WikivoyageJsonParser.class.getSimpleName();
|
||||||
|
|
||||||
private static final String HEADERS = "headers";
|
private static final String HEADERS = "headers";
|
||||||
private static final String SUBHEADERS = "subheaders";
|
private static final String SUBHEADERS = "subheaders";
|
||||||
private static final String LINK = "link";
|
private static final String LINK = "link";
|
||||||
|
@ -25,7 +28,7 @@ public class WikivoyageJsonParser {
|
||||||
reader = new JSONObject(contentsJson);
|
reader = new JSONObject(contentsJson);
|
||||||
jArray = reader.getJSONObject(HEADERS);
|
jArray = reader.getJSONObject(HEADERS);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
Log.e(TAG, e.getMessage(), e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,22 +40,23 @@ public class WikivoyageJsonParser {
|
||||||
WikivoyageContentItem headerItem = new WikivoyageContentItem(jArray.names().getString(i), link, topContentItem);
|
WikivoyageContentItem headerItem = new WikivoyageContentItem(jArray.names().getString(i), link, topContentItem);
|
||||||
topContentItem.subItems.add(headerItem);
|
topContentItem.subItems.add(headerItem);
|
||||||
|
|
||||||
JSONArray subheaders = header.getJSONArray(SUBHEADERS);
|
JSONObject subheaders = header.getJSONObject(SUBHEADERS);
|
||||||
|
JSONArray subNames = subheaders.names();
|
||||||
List<String> subheaderNames = null;
|
List<String> subheaderNames = null;
|
||||||
for (int j = 0; j < subheaders.length(); j++) {
|
for (int j = 0; j < subheaders.length(); j++) {
|
||||||
JSONObject subheader = subheaders.getJSONObject(j);
|
String title = subNames.get(j).toString();
|
||||||
JSONObject subheaderLink = subheader.getJSONObject(subheader.keys().next());
|
JSONObject subheaderLink = subheaders.getJSONObject(title);
|
||||||
if (subheaderNames == null) {
|
if (subheaderNames == null) {
|
||||||
subheaderNames = new ArrayList<>();
|
subheaderNames = new ArrayList<>();
|
||||||
}
|
}
|
||||||
subheaderNames.add(subheader.keys().next());
|
subheaderNames.add(title);
|
||||||
link = subheaderLink.getString(LINK);
|
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);
|
headerItem.subItems.add(subheaderItem);
|
||||||
}
|
}
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
Log.e(TAG, e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return topContentItem;
|
return topContentItem;
|
||||||
|
|
Loading…
Reference in a new issue