Change wikipedia dialog

This commit is contained in:
PavelRatushny 2017-12-26 15:39:56 +02:00
parent a14b55d054
commit 9476bfe3ba
4 changed files with 265 additions and 77 deletions

View file

@ -1,78 +1,101 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.design.widget.CoordinatorLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/actionModeBackground">
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
osmand:layout_scrollFlags="scroll|enterAlways"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/dashboard_map_toolbar"
osmand:contentInsetLeft="54dp"
osmand:contentInsetStart="54dp">
android:layout_height="wrap_content"
osmand:contentInsetLeft="0dp"
osmand:contentInsetStart="0dp"
osmand:contentInsetRight="0dp"
osmand:contentInsetEnd="0dp">
<LinearLayout
android:gravity="end|center_vertical"
android:layout_marginLeft="@dimen/context_menu_padding_margin_small"
android:layout_marginRight="@dimen/context_menu_padding_margin_small"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content">
<net.osmand.plus.widgets.TextViewEx
osmand:typeface="@string/font_roboto_medium"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_marginTop="@dimen/content_padding_small"
android:layout_marginBottom="@dimen/content_padding_small"
android:layout_height="match_parent"
android:background="?attr/ctx_menu_controller_bg"
android:gravity="center_vertical"
android:padding="@dimen/context_menu_padding_margin_tiny"
tools:text="RU"
android:textColor="?attr/ctx_menu_controller_text_color"
android:textSize="@dimen/default_desc_text_size"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dashboard_map_toolbar"
android:gravity="center_vertical">
<ImageButton
android:id="@+id/back_button"
style="@style/Widget.AppCompat.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_arrow_back"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/select_language_text_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/content_padding_small"
android:layout_marginTop="@dimen/content_padding_small"
android:layout_marginLeft="@dimen/context_menu_padding_margin_small"
android:layout_marginRight="@dimen/context_menu_padding_margin_small"
android:background="?attr/ctx_menu_controller_bg"
android:gravity="center_vertical"
android:padding="@dimen/context_menu_padding_margin_tiny"
android:textAllCaps="true"
android:textColor="?attr/ctx_menu_controller_text_color"
android:textSize="@dimen/default_desc_text_size"
osmand:typeface="@string/font_roboto_medium"
tools:text="RU"/>
</LinearLayout>
<TextView
android:id="@+id/title_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/content_padding"
tools:text="Title"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<TextView
android:id="@+id/title_text_view"
android:padding="@dimen/content_padding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Title"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
<android.support.v4.widget.NestedScrollView
osmand:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:padding="@dimen/content_padding"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
<TextView
android:layout_margin="@dimen/content_padding"
android:id="@+id/read_full_article"
android:layout_gravity="bottom|center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/content_padding"/>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<TextView
android:layout_margin="@dimen/content_padding"
android:id="@+id/read_full_article"
android:layout_gravity="bottom|center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:background="@drawable/bt_round_long_night"
android:text="@string/read_full_article"
android:gravity="center_vertical"
android:textAllCaps="true"
android:textColor="?attr/ctx_menu_controller_text_color"/>
</android.support.design.widget.CoordinatorLayout>

View file

@ -1,38 +1,63 @@
package net.osmand.plus.mapcontextmenu;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.Toolbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.data.Amenity;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.helpers.FileNameTranslationHelper;
import net.osmand.util.Algorithms;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
public class WikipediaDialogFragment extends DialogFragment {
public static final String PREFERRED_LANGUAGE = "preferred_language";
public static final String TAG = "WikipediaDialogFragment";
private View mainView;
private boolean darkTheme;
private Amenity amenity;
private String lang;
public void setAmenity(Amenity amenity) {
this.amenity = amenity;
}
public void setLanguage(String lang) {
this.lang = lang;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
OsmandApplication app = getMyApplication();
darkTheme = app.getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_DARK_THEME;
int themeId = darkTheme ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
@ -52,50 +77,188 @@ public class WikipediaDialogFragment extends DialogFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View mainView = inflater.inflate(R.layout.wikipedia_dialog_fragment, container, false);
mainView = inflater.inflate(R.layout.wikipedia_dialog_fragment, container, false);
Toolbar toolbar = (Toolbar) mainView.findViewById(R.id.toolbar);
toolbar.setNavigationIcon(getMyApplication().getIconsCache().getIcon(R.drawable.ic_arrow_back));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
mainView.setBackgroundColor(ContextCompat.getColor(getContext(), darkTheme ? R.color.ctx_menu_bottom_view_bg_dark : R.color.ctx_menu_bottom_view_bg_light));
AppBarLayout appBarLayout = (AppBarLayout) mainView.findViewById(R.id.app_bar);
appBarLayout.setBackgroundColor(ContextCompat.getColor(getContext(), darkTheme ? R.color.ctx_menu_buttons_bg_dark: R.color.ctx_menu_buttons_bg_light));
int toolbarTextColor = ContextCompat.getColor(getContext(), R.color.dashboard_subheader_text_light);
ImageButton backButton = (ImageButton) mainView.findViewById(R.id.back_button);
backButton.setImageDrawable(getMyApplication().getIconsCache().getPaintedIcon(R.drawable.ic_arrow_back, toolbarTextColor));
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
String preferredLanguage = "";
Bundle args = getArguments();
if (args != null) {
preferredLanguage = args.getString(PREFERRED_LANGUAGE);
}
TextView titleTextView = (TextView) mainView.findViewById(R.id.title_text_view);
titleTextView.setTextColor(toolbarTextColor);
if (TextUtils.isEmpty(preferredLanguage)) {
preferredLanguage = getMyApplication().getLanguage();
}
final TextView readFullArticleButton = (TextView) mainView.findViewById(R.id.read_full_article);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) readFullArticleButton.getLayoutParams();
params.setBehavior(new CoordinatorLayout.Behavior() {
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof AppBarLayout;
}
final String title = TextUtils.isEmpty(preferredLanguage) ? amenity.getName() : amenity.getName(preferredLanguage);
((TextView) mainView.findViewById(R.id.title_text_view)).setText(title);
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
if (dependency instanceof AppBarLayout) {
int readFullArticleButtonHeight = child.getMeasuredHeight();
if (readFullArticleButtonHeight != 0) {
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
int viewBottomMargin = lp.bottomMargin;
int distanceToScroll = child.getHeight() + viewBottomMargin;
float ratio = dependency.getY() / readFullArticleButtonHeight;
child.setTranslationY(-distanceToScroll * ratio);
}
}
return true;
}
});
readFullArticleButton.setLayoutParams(params);
readFullArticleButton.setBackgroundResource(darkTheme ? R.drawable.bt_round_long_night : R.drawable.bt_round_long_day);
int paddingLeft = (int) getResources().getDimension(R.dimen.content_padding_small);
int paddingRight = (int) getResources().getDimension(R.dimen.dialog_content_margin);
readFullArticleButton.setPadding(paddingLeft, 0, paddingRight, 0);
readFullArticleButton.setCompoundDrawablesWithIntrinsicBounds(getIcon(R.drawable.ic_world_globe_dark), null, null, null);
readFullArticleButton.setCompoundDrawablePadding(paddingLeft);
String langSelected = amenity.getContentLanguage("content", preferredLanguage, "en");
if (Algorithms.isEmpty(langSelected)) {
langSelected = "en";
}
String content = amenity.getDescription(langSelected);
final TextView selectLanguageTextView = mainView.findViewById(R.id.select_language_text_view);
selectLanguageTextView.setCompoundDrawablesWithIntrinsicBounds(getIcon(R.drawable.ic_action_map_language), null, null, null);
selectLanguageTextView.setCompoundDrawablePadding((int) getResources().getDimension(R.dimen.context_menu_padding_margin_small));
return mainView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
populateWiki();
}
@Override
public void onDestroyView() {
super.onDestroyView();
Dialog dialog = getDialog();
if (dialog != null) {
dialog.setDismissMessage(null);
}
super.onDestroyView();
}
private void populateWiki() {
if (amenity != null) {
String preferredLanguage = lang;
if (TextUtils.isEmpty(preferredLanguage)) {
preferredLanguage = getMyApplication().getLanguage();
}
final String title = TextUtils.isEmpty(preferredLanguage) ? amenity.getName() : amenity.getName(lang);
((TextView) mainView.findViewById(R.id.title_text_view)).setText(title);
String lng = amenity.getContentLanguage("content", preferredLanguage, "en");
if (Algorithms.isEmpty(lng)) {
lng = "en";
}
final String langSelected = lng;
mainView.findViewById(R.id.read_full_article).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String article = "https://" + langSelected.toLowerCase() + ".wikipedia.org/wiki/" + title.replace(' ', '_');
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(article));
startActivity(i);
}
});
final TextView selectLanguageTextView = mainView.findViewById(R.id.select_language_text_view);
selectLanguageTextView.setText(langSelected);
selectLanguageTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopupLangMenu(selectLanguageTextView, langSelected);
}
});
String content = amenity.getDescription(langSelected);
TextView articleTextView = (TextView) mainView.findViewById(R.id.content);
articleTextView.setText(Html.fromHtml(content));
}
}
private void showPopupLangMenu(View view, final String langSelected) {
final PopupMenu optionsMenu = new PopupMenu(getContext(), view, Gravity.RIGHT);
Set<String> namesSet = new TreeSet<>();
namesSet.addAll(amenity.getNames("content", "en"));
namesSet.addAll(amenity.getNames("description", "en"));
Map<String, String> names = new HashMap<>();
for (String n : namesSet) {
names.put(n, FileNameTranslationHelper.getVoiceName(getContext(), n));
}
String selectedLangName = names.get(langSelected);
if (selectedLangName != null) {
names.remove(langSelected);
}
Map<String, String> sortedNames = AndroidUtils.sortByValue(names);
if (selectedLangName != null) {
MenuItem item = optionsMenu.getMenu().add(selectedLangName);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
setLanguage(langSelected);
populateWiki();
return true;
}
});
}
for (final Map.Entry<String, String> e : sortedNames.entrySet()) {
MenuItem item = optionsMenu.getMenu().add(e.getValue());
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
setLanguage(e.getKey());
populateWiki();
return true;
}
});
}
optionsMenu.show();
}
private Drawable getIcon(int resId) {
int colorId = darkTheme ? R.color.ctx_menu_controller_button_text_color_dark_n : R.color.ctx_menu_controller_button_text_color_light_n;
return getMyApplication().getIconsCache().getIcon(resId, colorId);
}
private OsmandApplication getMyApplication() {
return (OsmandApplication) getActivity().getApplication();
}
public static boolean showInstance(AppCompatActivity activity, Amenity amenity) {
try {
if (!amenity.getType().isWiki()) {
return false;
}
OsmandApplication app = (OsmandApplication) activity.getApplication();
String lang = app.getSettings().MAP_PREFERRED_LOCALE.get();
WikipediaDialogFragment wikipediaDialogFragment = new WikipediaDialogFragment();
wikipediaDialogFragment.setAmenity(amenity);
wikipediaDialogFragment.setLanguage(lang);
wikipediaDialogFragment.setRetainInstance(true);
wikipediaDialogFragment.show(activity.getSupportFragmentManager(), TAG);
return true;
} catch (RuntimeException e) {
return false;
}
}
}

View file

@ -32,6 +32,7 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.FontCache;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.WikipediaDialogFragment;
import net.osmand.plus.osmedit.OsmEditingPlugin;
import net.osmand.plus.views.POIMapLayer;
import net.osmand.plus.widgets.TextViewEx;
@ -235,7 +236,7 @@ public class AmenityMenuBuilder extends MenuBuilder {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
POIMapLayer.showWikipediaDialog(view.getContext(), app, amenity);
WikipediaDialogFragment.showInstance(mapActivity, amenity);
}
});
button.setAllCaps(true);
@ -285,7 +286,7 @@ public class AmenityMenuBuilder extends MenuBuilder {
ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
POIMapLayer.showWikipediaDialog(view.getContext(), app, amenity);
WikipediaDialogFragment.showInstance(mapActivity, amenity);
}
});
} else if (isText && text.length() > 200) {

View file

@ -15,6 +15,7 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.MenuController;
import net.osmand.plus.mapcontextmenu.OpeningHoursInfo;
import net.osmand.plus.mapcontextmenu.WikipediaDialogFragment;
import net.osmand.plus.mapcontextmenu.builders.AmenityMenuBuilder;
import net.osmand.plus.transport.TransportStopRoute;
import net.osmand.plus.render.RenderingIcons;
@ -69,7 +70,7 @@ public class AmenityMenuController extends MenuController {
leftTitleButtonController = new TitleButtonController() {
@Override
public void buttonPressed() {
POIMapLayer.showWikipediaDialog(mapActivity, mapActivity.getMyApplication(), amenity);
WikipediaDialogFragment.showInstance(mapActivity, amenity);
}
};
leftTitleButtonController.caption = getMapActivity().getString(R.string.context_menu_read_article);