From abd756ba8e9e10c6d3462e9aa5a260e22b7a193d Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Tue, 12 Dec 2017 12:32:34 +0200 Subject: [PATCH] Refactor --- .../plus/mapcontextmenu/MenuBuilder.java | 124 ++++++++++-------- .../builders/FavouritePointMenuBuilder.java | 17 ++- 2 files changed, 81 insertions(+), 60 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java index 6c069d5b94..245613a779 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java @@ -681,18 +681,70 @@ public class MenuBuilder { return new CollapsableView(textView, collapsed); } - protected CollapsableView getCollapsableWikiView(Context context, boolean collapsed) { - return getCollapsableItemsView(context, collapsed, nearestWiki, null); + protected CollapsableView getCollapsableFavouritesView(Context context, boolean collapsed, List points, FavouritePoint selectedPoint) { + LinearLayout view = (LinearLayout) buildCollapsableContentView(context, collapsed); + + for (final FavouritePoint point : points) { + boolean selected = selectedPoint != null && selectedPoint.equals(point); + TextViewEx button = buildButtonInCollapsableView(context, selected); + String name = point.getName(); + button.setText(name); + + button.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); + button.setSingleLine(true); + button.setEllipsize(TextUtils.TruncateAt.END); + if (!selected) { + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + LatLon latLon = new LatLon(point.getLatitude(), point.getLongitude()); + PointDescription pointDescription = new PointDescription(PointDescription.POINT_TYPE_FAVORITE, point.getName()); + mapActivity.getContextMenu().show(latLon, pointDescription, point); + } + }); + } + view.addView(button); + } + + return new CollapsableView(view, collapsed); } - protected CollapsableView getCollapsableItemsView(Context context, boolean collapsed, final List items, Object selectedObject) { + protected CollapsableView getCollapsableWikiView(Context context, boolean collapsed) { + LinearLayout view = (LinearLayout) buildCollapsableContentView(context, collapsed); + + for (final Amenity wiki : nearestWiki) { + TextViewEx button = buildButtonInCollapsableView(context, false); + String name = wiki.getName(preferredMapAppLang, transliterateNames); + button.setText(name); + + button.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); + button.setSingleLine(true); + button.setEllipsize(TextUtils.TruncateAt.END); + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + LatLon latLon = new LatLon(wiki.getLocation().getLatitude(), wiki.getLocation().getLongitude()); + PointDescription pointDescription = mapActivity.getMapLayers().getPoiMapLayer().getObjectName(wiki); + mapActivity.getContextMenu().show(latLon, pointDescription, wiki); + } + }); + view.addView(button); + } + + return new CollapsableView(view, collapsed); + } + + protected LinearLayout buildCollapsableContentView(Context context, boolean collapsed) { final LinearLayout view = new LinearLayout(context); view.setOrientation(LinearLayout.VERTICAL); view.setVisibility(collapsed ? View.GONE : View.VISIBLE); LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); llParams.setMargins(dpToPx(64f), 0, dpToPx(12f), 0); view.setLayoutParams(llParams); + return view; + } + protected TextViewEx buildButtonInCollapsableView(Context context, boolean selected) { ColorStateList buttonColorStateList = new ColorStateList( new int[][] { new int[]{android.R.attr.state_pressed}, @@ -703,60 +755,24 @@ public class MenuBuilder { context.getResources().getColor(light ? R.color.ctx_menu_controller_button_text_color_light_n : R.color.ctx_menu_controller_button_text_color_dark_n) } ); - for (final Object item : items) { - TextViewEx button = new TextViewEx(new ContextThemeWrapper(view.getContext(), light ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme)); - LinearLayout.LayoutParams llWikiButtonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) context.getResources().getDimension(R.dimen.context_menu_controller_height)); - llWikiButtonParams.setMargins(0, 0, 0, dpToPx(8f)); - button.setLayoutParams(llWikiButtonParams); - button.setTypeface(FontCache.getRobotoMedium(context)); - boolean selected = selectedObject != null && selectedObject.equals(item); - int bg; - if (selected) { - bg = light ? R.drawable.context_menu_controller_bg_light_selected: R.drawable.context_menu_controller_bg_dark_selected; - } else { - bg = light ? R.drawable.context_menu_controller_bg_light : R.drawable.context_menu_controller_bg_dark; - } - button.setBackgroundResource(bg); - int paddingSides = (int) context.getResources().getDimension(R.dimen.context_menu_button_padding_x); - button.setPadding(paddingSides, 0, paddingSides, 0); - button.setTextColor(buttonColorStateList); - String name = ""; - if (item instanceof Amenity) { - name = ((Amenity) item).getName(preferredMapAppLang, transliterateNames); - } else if (item instanceof FavouritePoint) { - name = ((FavouritePoint) item).getName(); - } - button.setText(name); - - button.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); - button.setSingleLine(true); - button.setEllipsize(TextUtils.TruncateAt.END); - if (!selected) { - button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - LatLon latLon = null; - PointDescription pointDescription = null; - if (item instanceof Amenity) { - Amenity amenity = (Amenity) item; - latLon = new LatLon(amenity.getLocation().getLatitude(), amenity.getLocation().getLongitude()); - pointDescription = mapActivity.getMapLayers().getPoiMapLayer().getObjectName(amenity); - } else if (item instanceof FavouritePoint) { - FavouritePoint fav = (FavouritePoint) item; - latLon = new LatLon(fav.getLatitude(), fav.getLongitude()); - pointDescription = new PointDescription(PointDescription.POINT_TYPE_FAVORITE, fav.getName()); - } - if (latLon != null && pointDescription != null) { - mapActivity.getContextMenu().show(latLon, pointDescription, item); - } - } - }); - } - view.addView(button); + TextViewEx button = new TextViewEx(new ContextThemeWrapper(context, light ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme)); + LinearLayout.LayoutParams llWikiButtonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) context.getResources().getDimension(R.dimen.context_menu_controller_height)); + llWikiButtonParams.setMargins(0, 0, 0, dpToPx(8f)); + button.setLayoutParams(llWikiButtonParams); + button.setTypeface(FontCache.getRobotoMedium(context)); + int bg; + if (selected) { + bg = light ? R.drawable.context_menu_controller_bg_light_selected: R.drawable.context_menu_controller_bg_dark_selected; + } else { + bg = light ? R.drawable.context_menu_controller_bg_light : R.drawable.context_menu_controller_bg_dark; } + button.setBackgroundResource(bg); + int paddingSides = (int) context.getResources().getDimension(R.dimen.context_menu_button_padding_x); + button.setPadding(paddingSides, 0, paddingSides, 0); + button.setTextColor(buttonColorStateList); - return new CollapsableView(view, collapsed); + return button; } protected boolean processNearstWiki() { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java index b08eeb096e..2be55ef241 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java @@ -1,12 +1,17 @@ package net.osmand.plus.mapcontextmenu.builders; import android.graphics.Color; +import android.text.TextUtils; +import android.view.Gravity; import android.view.View; +import android.widget.LinearLayout; import net.osmand.ResultMatcher; import net.osmand.binary.BinaryMapIndexReader; import net.osmand.data.Amenity; import net.osmand.data.FavouritePoint; +import net.osmand.data.LatLon; +import net.osmand.data.PointDescription; import net.osmand.data.QuadRect; import net.osmand.data.TransportStop; import net.osmand.osm.PoiCategory; @@ -14,7 +19,7 @@ import net.osmand.plus.FavouritesDbHelper.FavoriteGroup; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.MenuBuilder; -import net.osmand.util.Algorithms; +import net.osmand.plus.widgets.TextViewEx; import net.osmand.util.MapUtils; import java.util.List; @@ -62,19 +67,19 @@ public class FavouritePointMenuBuilder extends MenuBuilder { builder.setLight(light); builder.buildInternal(view); } - buildGroupFavsView(view); + buildGroupFavouritesView(view); } - private void buildGroupFavsView(View view) { + private void buildGroupFavouritesView(View view) { FavoriteGroup favoriteGroup = app.getFavorites().getGroup(fav); - List groupFavs = favoriteGroup.points; - if (groupFavs.size() > 0) { + List groupFavourites = favoriteGroup.points; + if (groupFavourites.size() > 0) { int color = favoriteGroup.color == 0 || favoriteGroup.color == Color.BLACK ? view.getResources().getColor(R.color.color_favorite) : favoriteGroup.color; int disabledColor = light ? R.color.secondary_text_light : R.color.secondary_text_dark; color = favoriteGroup.visible ? (color | 0xff000000) : view.getResources().getColor(disabledColor); String name = view.getContext().getString(R.string.context_menu_points_of_group); buildRow(view, app.getIconsCache().getPaintedIcon(R.drawable.ic_action_folder, color), name, 0, - true, getCollapsableItemsView(view.getContext(), true, groupFavs, fav), + true, getCollapsableFavouritesView(view.getContext(), true, groupFavourites, fav), false, 0, false, null); } }