From 9476bfe3ba4f7cb985ae01554747d72d0411c5fe Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Tue, 26 Dec 2017 15:39:56 +0200 Subject: [PATCH] Change wikipedia dialog --- .../res/layout/wikipedia_dialog_fragment.xml | 123 +++++----- .../WikipediaDialogFragment.java | 211 ++++++++++++++++-- .../builders/AmenityMenuBuilder.java | 5 +- .../controllers/AmenityMenuController.java | 3 +- 4 files changed, 265 insertions(+), 77 deletions(-) diff --git a/OsmAnd/res/layout/wikipedia_dialog_fragment.xml b/OsmAnd/res/layout/wikipedia_dialog_fragment.xml index 53fb1be62a..4813511f44 100644 --- a/OsmAnd/res/layout/wikipedia_dialog_fragment.xml +++ b/OsmAnd/res/layout/wikipedia_dialog_fragment.xml @@ -1,78 +1,101 @@ - + android:layout_height="wrap_content"> + android:layout_height="wrap_content" + osmand:contentInsetLeft="0dp" + osmand:contentInsetStart="0dp" + osmand:contentInsetRight="0dp" + osmand:contentInsetEnd="0dp"> + android:layout_height="wrap_content"> - + + + + + + + + + + + - - - - - - - - - + android:id="@+id/content" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:padding="@dimen/content_padding"/> + - \ No newline at end of file + + + \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java index 33bc455d94..fd523cf1b2 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java @@ -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 namesSet = new TreeSet<>(); + namesSet.addAll(amenity.getNames("content", "en")); + namesSet.addAll(amenity.getNames("description", "en")); + + Map 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 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 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; + } + } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java index fb33160461..ec6cba3305 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java @@ -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) { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java index 3bf9257b97..be7b689bee 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java @@ -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);