From 583b3659ee756828f0ee173818b2ad3d77198a5c Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 13 Mar 2018 18:28:25 +0200 Subject: [PATCH] refactored some code and deleted onClickListener --- ..._favourite.xml => favourite_list_item.xml} | 1 + ...itle.xml => favourite_title_list_item.xml} | 0 .../other/FavouritesAdapter.java | 58 ++++++++----------- .../FavouritesBottomSheetMenuFragment.java | 42 ++++++-------- 4 files changed, 45 insertions(+), 56 deletions(-) rename OsmAnd/res/layout/{bottom_sheet_item_favourite.xml => favourite_list_item.xml} (99%) rename OsmAnd/res/layout/{bottom_sheet_item_favourite_title.xml => favourite_title_list_item.xml} (100%) diff --git a/OsmAnd/res/layout/bottom_sheet_item_favourite.xml b/OsmAnd/res/layout/favourite_list_item.xml similarity index 99% rename from OsmAnd/res/layout/bottom_sheet_item_favourite.xml rename to OsmAnd/res/layout/favourite_list_item.xml index 3867fd5021..a2cc26af62 100644 --- a/OsmAnd/res/layout/bottom_sheet_item_favourite.xml +++ b/OsmAnd/res/layout/favourite_list_item.xml @@ -89,6 +89,7 @@ android:textColor="?android:textColorSecondary" android:textSize="@dimen/default_desc_text_size" tools:text="Amsterdam Weekend" /> + diff --git a/OsmAnd/res/layout/bottom_sheet_item_favourite_title.xml b/OsmAnd/res/layout/favourite_title_list_item.xml similarity index 100% rename from OsmAnd/res/layout/bottom_sheet_item_favourite_title.xml rename to OsmAnd/res/layout/favourite_title_list_item.xml diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesAdapter.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesAdapter.java index 791995bdcc..532adf6824 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesAdapter.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesAdapter.java @@ -13,6 +13,7 @@ import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.plus.IconsCache; import net.osmand.plus.OsmAndFormatter; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.FavoriteImageDrawable; @@ -23,9 +24,9 @@ import java.util.List; public class FavouritesAdapter extends RecyclerView.Adapter { - private FavouritesAdapterListener listener; + private View.OnClickListener listener; private final Context context; - private final List FavouritePoints; + private final List favouritePoints; private LatLon location; private Float heading; @@ -38,89 +39,80 @@ public class FavouritesAdapter extends RecyclerView.Adapter FavouritePoints) { - this.FavouritePoints = FavouritePoints; + this.favouritePoints = FavouritePoints; this.context = context; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { - View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.bottom_sheet_item_favourite, viewGroup, false); + View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.favourite_list_item, viewGroup, false); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (listener != null) { - listener.onItemClick(view); + listener.onClick(view); } } }); return new FavouritesViewHolder(view); } - @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof FavouritesViewHolder) { - MapActivity mapActivity; - if (context instanceof MapActivity) { - mapActivity = (MapActivity) context; - } else { - return; - } - IconsCache iconsCache = mapActivity.getMyApplication().getIconsCache(); + OsmandApplication app = ((MapActivity) context).getMyApplication(); + IconsCache iconsCache = app.getIconsCache(); FavouritesViewHolder favouritesViewHolder = (FavouritesViewHolder) holder; FavouritePoint favouritePoint = getItem(position); favouritesViewHolder.title.setText(favouritePoint.getName()); - if (!favouritePoint.getCategory().equals("")) { - favouritesViewHolder.description.setText(favouritePoint.getCategory()); - } else { + + if (favouritePoint.getCategory().equals("")) { favouritesViewHolder.description.setText(R.string.shared_string_favorites); + } else { + favouritesViewHolder.description.setText(favouritePoint.getCategory()); } - Location myloc = mapActivity.getMyApplication().getLocationProvider().getLastKnownLocation(); + Location myloc = app.getLocationProvider().getLastKnownLocation(); if (myloc == null) { return; } float dist = (float) MapUtils.getDistance(favouritePoint.getLatitude(), favouritePoint.getLongitude(), myloc.getLatitude(), myloc.getLongitude()); - favouritesViewHolder.distance.setText(OsmAndFormatter.getFormattedDistance(dist, mapActivity.getMyApplication())); - favouritesViewHolder.FavouriteImage.setImageDrawable(FavoriteImageDrawable.getOrCreate(mapActivity, favouritePoint.getColor(), false)); + favouritesViewHolder.distance.setText(OsmAndFormatter.getFormattedDistance(dist, app)); + favouritesViewHolder.favouriteImage.setImageDrawable(FavoriteImageDrawable.getOrCreate(context, favouritePoint.getColor(), false)); favouritesViewHolder.arrowImage.setImageDrawable(iconsCache.getIcon(R.drawable.ic_direction_arrow)); DashLocationFragment.updateLocationView(useCenter, location, heading, favouritesViewHolder.arrowImage, favouritesViewHolder.distance, favouritePoint.getLatitude(), favouritePoint.getLongitude(), - screenOrientation, mapActivity.getMyApplication(), mapActivity); + screenOrientation, app, context); } } @Override public int getItemCount() { - return FavouritePoints.size(); + return favouritePoints.size(); } private FavouritePoint getItem(int position) { - return FavouritePoints.get(position); + return favouritePoints.get(position); } - public void setAdapterListener(FavouritesAdapterListener listener) { + public void setAdapterListener(View.OnClickListener listener) { this.listener = listener; } - public interface FavouritesAdapterListener { - void onItemClick(View view); - } - public void setScreenOrientation(int screenOrientation) { this.screenOrientation = screenOrientation; } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesBottomSheetMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesBottomSheetMenuFragment.java index ffd307ab33..68893dd99a 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesBottomSheetMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesBottomSheetMenuFragment.java @@ -19,6 +19,7 @@ import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; import net.osmand.plus.dashboard.DashLocationFragment; import net.osmand.util.MapUtils; @@ -53,7 +54,7 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag list = favouritesDbHelper.getVisibleFavouritePoints(); View titleView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), - R.layout.bottom_sheet_item_favourite_title, null); + R.layout.favourite_title_list_item, null); View mainView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_marker_add_group_bottom_sheet_dialog, null); @@ -68,28 +69,27 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); sortText.setText(R.string.sort_by_name); sort.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (location == null) { - return; - } - sortFavourites(sortByDist, list); - sortText.setText(sortByDist ? R.string.sort_by_distance : R.string.sort_by_name); - sortIconView.setImageDrawable(getIcon(sortByDist ? R.drawable.ic_action_list_sort : R.drawable.ic_action_sort_by_name, - nightMode ? R.color.route_info_go_btn_inking_dark : R.color.dash_search_icon_light)); - adapter.notifyDataSetChanged(); - } - } - ); - items.add(new BaseBottomSheetItem.Builder() + @Override + public void onClick(View v) { + if (location == null) { + return; + } + sortFavourites(sortByDist, list); + sortText.setText(sortByDist ? R.string.sort_by_distance : R.string.sort_by_name); + sortIconView.setImageDrawable(getIcon(sortByDist ? R.drawable.ic_action_list_sort : R.drawable.ic_action_sort_by_name, + nightMode ? R.color.route_info_go_btn_inking_dark : R.color.dash_search_icon_light)); + adapter.notifyDataSetChanged(); + } + }); + items.add(new TitleItem.Builder() .setCustomView(titleView) .create()); - createAdapter(); - adapter.setAdapterListener(new FavouritesAdapter.FavouritesAdapterListener() { + adapter = new FavouritesAdapter(mapActivity, list); + adapter.setAdapterListener(new View.OnClickListener() { @Override - public void onItemClick(View view) { - int position = recyclerView.getChildAdapterPosition(view); + public void onClick(View v) { + int position = recyclerView.getChildAdapterPosition(v); if (position == RecyclerView.NO_POSITION) { return; } @@ -158,10 +158,6 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag dismiss(); } - public void createAdapter() { - adapter = new FavouritesAdapter(mapActivity, list); - } - @Override public void onResume() { super.onResume();