Merge pull request #5222 from osmandapp/WikivoyageImprovements

Wikivoyage contents
This commit is contained in:
Alexander Sytnyk 2018-04-10 16:40:58 +03:00 committed by GitHub
commit 73347369fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 473 additions and 12 deletions

View file

@ -1,12 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:osmand="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/wikivoyage_bg_color"
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:osmand="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/wikivoyage_bg_color"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
@ -55,4 +58,84 @@
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_sheet_cancel_button_height"
android:layout_gravity="bottom|center_horizontal"
android:orientation="vertical">
<View
android:id="@+id/bottom_row_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?attr/wikivoyage_bottom_bar_divider_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/wikivoyage_bottom_bar_color"
android:orientation="horizontal">
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/contents_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="0.5"
android:background="?attr/selectableItemBackground"
android:drawablePadding="@dimen/bottom_sheet_content_padding_small"
android:ellipsize="end"
android:gravity="center_vertical"
android:letterSpacing="@dimen/text_button_letter_spacing"
android:maxLines="1"
android:paddingBottom="@dimen/context_menu_padding_margin_tiny"
android:paddingLeft="@dimen/bottom_sheet_content_padding_small"
android:paddingRight="@dimen/bottom_sheet_content_padding_small"
android:paddingTop="@dimen/context_menu_padding_margin_tiny"
android:text="@string/shared_string_contents"
android:textColor="?attr/wikivoyage_active_color"
android:textSize="@dimen/default_desc_text_size"
osmand:typeface="@string/font_roboto_medium"
tools:drawableLeft="@drawable/ic_action_list_header"
tools:drawableTint="?attr/wikivoyage_active_color"
tools:ignore="UnusedAttribute" />
<View
android:id="@+id/bottom_buttons_divider"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="?attr/wikivoyage_bottom_bar_divider_color"
android:visibility="visible" />
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/save_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|end"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:drawablePadding="@dimen/bottom_sheet_content_padding_small"
android:ellipsize="end"
android:gravity="center_vertical|end"
android:letterSpacing="@dimen/text_button_letter_spacing"
android:maxLines="1"
android:paddingBottom="@dimen/context_menu_padding_margin_tiny"
android:paddingLeft="@dimen/bottom_sheet_content_padding_small"
android:paddingRight="@dimen/bottom_sheet_content_padding_small"
android:paddingTop="@dimen/context_menu_padding_margin_tiny"
android:text="@string/shared_string_save"
android:textColor="?attr/wikivoyage_active_color"
android:textSize="@dimen/default_desc_text_size"
osmand:typeface="@string/font_roboto_medium"
tools:drawableRight="@drawable/ic_action_read_later_fill"
tools:drawableTint="?attr/wikivoyage_active_color"
tools:ignore="UnusedAttribute" />
</LinearLayout>
</LinearLayout>
</FrameLayout>

View file

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_sheet_list_item_height"
android:background="?attr/expandable_list_item_background"
android:orientation="vertical">
<TextView
android:id="@+id/item_label"
android:layout_width="wrap_content"
android:layout_height="@dimen/bottom_sheet_list_item_height"
android:layout_gravity="center_vertical"
android:drawablePadding="@dimen/showAllButtonMarginRight"
android:gravity="center"
android:paddingLeft="@dimen/showAllButtonMarginRight"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/default_list_text_size"
tools:drawableStart="@drawable/ic_action_list_sort"
tools:text="@string/shared_string_contents"
tools:textStyle="bold" />
</LinearLayout>

View file

@ -71,6 +71,8 @@
<attr name="wikivoyage_app_bar_color" format="reference"/>
<attr name="wikivoyage_app_bar_text_color" format="reference"/>
<attr name="wikivoyage_active_color" format="reference"/>
<attr name="wikivoyage_bottom_bar_color" format="reference"/>
<attr name="wikivoyage_bottom_bar_divider_color" format="reference"/>
</declare-styleable>
<declare-styleable name="PagerSlidingTabStrip">

View file

@ -146,6 +146,8 @@
<color name="status_bar_wikivoyage_dark">#101112</color>
<color name="status_bar_wikivoyage_article_light">#f2f2f2</color>
<color name="status_bar_wikivoyage_article_dark">#222526</color>
<color name="bottom_bar_wikivoyage_bg_light">#f5f5f5</color>
<color name="bottom_bar_wikivoyage_bg_dark">#222526</color>
<color name="color_transparent">#0000</color>
@ -400,5 +402,9 @@
<color name="wikivoyage_app_bar_dark">#222526</color>
<color name="wikivoyage_app_bar_text_light">#454545</color>
<color name="wikivoyage_app_bar_text_dark">#ababab</color>
<color name="wikivoyage_contents_icon_dark">#d28521</color>
<color name="wikivoyage_contents_icon_light">#536dfe</color>
<color name="wikivoyage_bottom_bar_divider_light">#e3e3e3</color>
<color name="wikivoyage_bottom_bar_divider_dark">#2d3133</color>
</resources>

View file

@ -13,6 +13,7 @@
<string name="shared_string_read">Read</string>
<string name="saved_articles">Saved articles</string>
<string name="shared_string_explore">Explore</string>
<string name="shared_string_contents">Contents</string>
<string name="shared_string_result">Result</string>
<string name="use_two_digits_longitude">Use two digits longitude</string>
<string name="shared_string_travel">Travel</string>

View file

@ -203,6 +203,8 @@
<item name="wikivoyage_app_bar_color">@color/wikivoyage_app_bar_light</item>
<item name="wikivoyage_app_bar_text_color">@color/wikivoyage_app_bar_text_light</item>
<item name="wikivoyage_active_color">@color/wikivoyage_active_light</item>
<item name="wikivoyage_bottom_bar_color">@color/bottom_bar_wikivoyage_bg_light</item>
<item name="wikivoyage_bottom_bar_divider_color">@color/wikivoyage_bottom_bar_divider_light</item>
</style>
<style name="OverflowMenuButton" parent="@style/Widget.AppCompat.ActionButton.Overflow">
@ -394,6 +396,8 @@
<item name="wikivoyage_app_bar_color">@color/wikivoyage_app_bar_dark</item>
<item name="wikivoyage_app_bar_text_color">@color/wikivoyage_app_bar_text_dark</item>
<item name="wikivoyage_active_color">@color/wikivoyage_active_dark</item>
<item name="wikivoyage_bottom_bar_color">@color/bottom_bar_wikivoyage_bg_dark</item>
<item name="wikivoyage_bottom_bar_divider_color">@color/wikivoyage_bottom_bar_divider_dark</item>
</style>
<style name="FreeVersionBanner" parent="OsmandDarkTheme">

View file

@ -311,6 +311,12 @@ public class AndroidUtils {
return dm.heightPixels;
}
public static int getScreenWidth(Activity activity) {
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
public static boolean isValidEmail(CharSequence target) {
return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}

View file

@ -1,5 +1,6 @@
package net.osmand.plus.wikivoyage;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Build;
@ -21,7 +22,9 @@ import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.IndexConstants;
import net.osmand.plus.R;
import net.osmand.plus.widgets.TextViewEx;
import net.osmand.plus.wikivoyage.data.WikivoyageArticle;
import net.osmand.plus.wikivoyage.data.WikivoyageArticleContentsFragment;
import net.osmand.plus.wikivoyage.data.WikivoyageLocalDataHelper;
import net.osmand.util.Algorithms;
@ -42,12 +45,14 @@ 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";
"</head><body>\n" + "<script>" + "function scrollAnchor(id) {" +
"window.location.hash = id;}</script>";
private static final String FOOTER_INNER = "</div></body></html>";
private long cityId = NO_VALUE;
private ArrayList<String> langs;
private String selectedLang;
private String contentsJson;
private TextView selectedLangTv;
private WebView contentWebView;
@ -63,7 +68,6 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
selectedLang = args.getString(SELECTED_LANG_KEY);
}
}
final View mainView = inflate(R.layout.fragment_wikivoyage_article_dialog, container);
setupToolbar((Toolbar) mainView.findViewById(R.id.toolbar));
@ -86,6 +90,29 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
}
});
TextViewEx contentsButton = (TextViewEx) mainView.findViewById(R.id.contents_button);
TextViewEx saveButton = (TextViewEx) mainView.findViewById(R.id.save_button);
saveButton.setCompoundDrawablesWithIntrinsicBounds(
null, null, getActiveIcon(R.drawable.ic_action_read_later), null);
contentsButton.setCompoundDrawablesWithIntrinsicBounds(
getActiveIcon(R.drawable.ic_action_list_header), null, null, null);
contentsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getActivity().getSupportFragmentManager();
Bundle args = new Bundle();
args.putString(WikivoyageArticleContentsFragment.CONTENTS_JSON_KEY, contentsJson);
WikivoyageArticleContentsFragment fragment = new WikivoyageArticleContentsFragment();
fragment.setUsedOnMap(false);
fragment.setArguments(args);
fragment.setTargetFragment(WikivoyageArticleDialogFragment.this, 0);
fragment.show(fm, WikivoyageArticleContentsFragment.TAG);
}
});
contentWebView = (WebView) mainView.findViewById(R.id.content_web_view);
return mainView;
@ -102,6 +129,15 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
outState.putString(SELECTED_LANG_KEY, selectedLang);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == WikivoyageArticleContentsFragment.REQUEST_LINK_CODE) {
String link = data.getStringExtra(WikivoyageArticleContentsFragment.CONTENTS_LINK_KEY);
moveToAnchor(link);
}
}
@Override
protected int getStatusBarColor() {
return nightMode ? R.color.status_bar_wikivoyage_article_dark : R.color.status_bar_wikivoyage_article_light;
@ -152,12 +188,17 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
if (article == null) {
return;
}
contentsJson = article.getContentsJson();
WikivoyageLocalDataHelper.getInstance(getMyApplication()).addToHistory(article);
contentWebView.getSettings().setJavaScriptEnabled(true);
contentWebView.loadDataWithBaseURL(getBaseUrl(), createHtmlContent(article), "text/html", "UTF-8", null);
}
public void moveToAnchor(String id) {
contentWebView.loadUrl("javascript:scrollAnchor(\"" + id + "\")");
}
@NonNull
private String createHtmlContent(@NonNull WikivoyageArticle article) {
StringBuilder sb = new StringBuilder(HEADER_INNER);

View file

@ -0,0 +1,71 @@
package net.osmand.plus.wikivoyage.data;
import android.content.Context;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
class ContentsJsonParser {
static ContentsContainer parseJsonContents(String contentsJson, Context context) {
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
ArrayList<String> listDataHeader = new ArrayList<String>();
LinkedHashMap<String, List<String>> listDataChild = new LinkedHashMap<String, List<String>>();
JSONObject reader = null;
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<String>();
}
if (level == 3) {
if (secondLevel == null) {
secondLevel = new ArrayList<String>();
}
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);
}
static class ContentsContainer {
public LinkedHashMap<String, String> map;
public ArrayList<String> listDataHeader;
public LinkedHashMap<String, List<String>> listDataChild;
public ContentsContainer(LinkedHashMap<String, String> map, ArrayList<String> listDataHeader,
LinkedHashMap<String, List<String>> listChildData) {
this.map = map;
this.listDataHeader = listDataHeader;
this.listDataChild = listChildData;
}
}
}

View file

@ -0,0 +1,218 @@
package net.osmand.plus.wikivoyage.data;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.plus.R;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFragment {
public final static String TAG = "WikivoyageArticleContentsFragment";
public final static String CONTENTS_JSON_KEY = "contents_json";
public final static String CONTENTS_LINK_KEY = "contents_link";
public final static int REQUEST_LINK_CODE = 0;
private LinkedHashMap<String, String> map;
private String link;
@Override
public void createMenuItems(Bundle savedInstanceState) {
Bundle args = getArguments();
String contentsJson;
if (args != null) {
contentsJson = args.getString(CONTENTS_JSON_KEY);
} else {
return;
}
ContentsJsonParser.ContentsContainer contentsContainer = ContentsJsonParser.parseJsonContents(contentsJson, getContext());
if (contentsContainer == null) {
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)));
ExpandableListView expListView = new ExpandableListView(getContext());
ExpandableListAdapter listAdapter = new ExpandableListAdapter(getContext(), listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
expListView.setChildDivider(ContextCompat.getDrawable(getContext(), R.color.color_transparent));
expListView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
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(0);
dismiss();
return false;
}
});
int width = AndroidUtils.getScreenWidth(getActivity());
if (android.os.Build.VERSION.SDK_INT < 18) {
expListView.setIndicatorBounds(width - (AndroidUtils.dpToPx(getContext(), 50)), width - (AndroidUtils.dpToPx(getContext(), 10)));
} else {
expListView.setIndicatorBoundsRelative(width - (AndroidUtils.dpToPx(getContext(), 50)), width - (AndroidUtils.dpToPx(getContext(), 10)));
}
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
link = map.get(listDataHeader.get(groupPosition));
sendResult(0);
if (listDataChild.get(listDataHeader.get(groupPosition)) == null) {
dismiss();
}
return false;
}
});
LinearLayout container = new LinearLayout(getContext());
container.addView(expListView);
BaseBottomSheetItem favoritesItem = new SimpleBottomSheetItem.Builder()
.setCustomView(container)
.create();
items.add(favoritesItem);
}
private void sendResult(int requestLinkCode) {
Intent intent = new Intent();
intent.putExtra(CONTENTS_LINK_KEY, link);
Fragment fragment = getTargetFragment();
if (fragment != null) {
fragment.onActivityResult(
getTargetRequestCode(), requestLinkCode, intent);
}
}
@Override
protected boolean useScrollableItemsContainer() {
return false;
}
class ExpandableListAdapter extends OsmandBaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private LinkedHashMap<String, List<String>> listDataChild;
private Drawable itemGroupIcon;
private Drawable itemChildIcon;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
LinkedHashMap<String, List<String>> listChildData) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listChildData;
itemGroupIcon = getIcon(R.drawable.ic_action_contents,
isNightMode() ? R.color.wikivoyage_active_dark : R.color.wikivoyage_active_light);
itemChildIcon = getIcon(R.drawable.ic_action_list_bullet,
isNightMode() ? R.color.route_info_unchecked_mode_icon_color : R.color.ctx_menu_nearby_routes_text_color_dark);
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.wikivoyage_contents_list_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.item_label);
txtListChild.setText(childText);
txtListChild.setTextColor(getResolvedColor(isNightMode() ? R.color.wikivoyage_contents_icon_dark : R.color.wikivoyage_contents_icon_light));
txtListChild.setCompoundDrawablesWithIntrinsicBounds(itemChildIcon, null, null, null);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
if (this.listDataChild.get(this.listDataHeader.get(groupPosition)) != null) {
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size();
} else {
return 0;
}
}
@Override
public Object getGroup(int groupPosition) {
return this.listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this.listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.wikivoyage_contents_list_item, null);
}
TextView lblListHeader = (TextView) convertView.findViewById(R.id.item_label);
lblListHeader.setText(headerTitle);
lblListHeader.setTextColor(getResolvedColor(isNightMode() ? R.color.wikivoyage_contents_icon_dark : R.color.wikivoyage_contents_icon_light));
lblListHeader.setCompoundDrawablesWithIntrinsicBounds(itemGroupIcon, null, null, null);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}

View file

@ -164,6 +164,12 @@ public class AndroidUtils {
return dm.heightPixels;
}
public static int getScreenWidth(Activity activity) {
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
public static boolean isValidEmail(CharSequence target) {
return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}