From 4458c514a50a882df90d0df0f474a340df65b26b Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 14 Mar 2018 17:37:02 +0200 Subject: [PATCH] added title with button and refactored some code --- .../bottom_sheet_item_title_with_button.xml | 43 +++++ .../res/layout/favourite_title_list_item.xml | 57 ------- .../simpleitems/TitleWithButtonItem.java | 107 ++++++++++++ .../plus/helpers/WaypointDialogHelper.java | 2 +- .../other/FavouritesAdapter.java | 53 +++--- .../FavouritesBottomSheetMenuFragment.java | 155 +++++++++--------- .../other/MapRouteInfoMenu.java | 17 +- 7 files changed, 267 insertions(+), 167 deletions(-) create mode 100644 OsmAnd/res/layout/bottom_sheet_item_title_with_button.xml delete mode 100644 OsmAnd/res/layout/favourite_title_list_item.xml create mode 100644 OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/simpleitems/TitleWithButtonItem.java diff --git a/OsmAnd/res/layout/bottom_sheet_item_title_with_button.xml b/OsmAnd/res/layout/bottom_sheet_item_title_with_button.xml new file mode 100644 index 0000000000..0cf544d140 --- /dev/null +++ b/OsmAnd/res/layout/bottom_sheet_item_title_with_button.xml @@ -0,0 +1,43 @@ + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/favourite_title_list_item.xml b/OsmAnd/res/layout/favourite_title_list_item.xml deleted file mode 100644 index 4f2dc281cf..0000000000 --- a/OsmAnd/res/layout/favourite_title_list_item.xml +++ /dev/null @@ -1,57 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/simpleitems/TitleWithButtonItem.java b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/simpleitems/TitleWithButtonItem.java new file mode 100644 index 0000000000..a674279d60 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/simpleitems/TitleWithButtonItem.java @@ -0,0 +1,107 @@ +package net.osmand.plus.base.bottomsheetmenu.simpleitems; + +import android.graphics.drawable.Drawable; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; +import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; + +public class TitleWithButtonItem extends SimpleBottomSheetItem { + + public static final String textOnLeft = "LEFT"; + public static final String textOnTop = "TOP"; + public static final String textOnRight = "RIGHT"; + public static final String textOnBottom = "BOTTOM"; + + private View.OnClickListener onClickListener; + private String iconPosition; + private Drawable textIcon; + private String textButton; + + public TitleWithButtonItem(View.OnClickListener onClickListener, + Drawable textIcon, + String title, + String textButton, + String iconPosition) { + this.title = title; + this.layoutId = R.layout.bottom_sheet_item_title_with_button; + this.textIcon = textIcon; + this.textButton = textButton; + this.iconPosition = iconPosition; + this.onClickListener = onClickListener; + } + + public static class Builder extends BaseBottomSheetItem.Builder { + + private Drawable textIcon; + protected String title; + private String textOnRight; + protected View.OnClickListener onClickListener; + + private String iconPosition; + + public TitleWithButtonItem.Builder setIcon(Drawable textIcon) { + this.textIcon = textIcon; + return this; + } + + public TitleWithButtonItem.Builder setIconPosition(String iconPosition) { + this.iconPosition = iconPosition; + return this; + } + + public TitleWithButtonItem.Builder setOnClickListener(View.OnClickListener onClickListener) { + this.onClickListener = onClickListener; + return this; + } + + public TitleWithButtonItem.Builder setTextOnRight(String textOnRight) { + this.textOnRight = textOnRight; + return this; + } + + public TitleWithButtonItem.Builder setTitle(String title) { + this.title = title; + return this; + } + + public TitleWithButtonItem create() { + return new TitleWithButtonItem(onClickListener, + textIcon, + title, + textOnRight, + iconPosition); + } + } + + @Override + public void inflate(OsmandApplication app, ViewGroup container, boolean nightMode) { + super.inflate(app, container, nightMode); + + if (textButton != null) { + TextView descriptionTv = (TextView) view.findViewById(R.id.text_button); + descriptionTv.setText(textButton); + descriptionTv.setOnClickListener(onClickListener); + if (textIcon != null) { + switch (iconPosition) { + case textOnLeft: + descriptionTv.setCompoundDrawablesWithIntrinsicBounds(textIcon, null, null, null); + break; + case textOnTop: + descriptionTv.setCompoundDrawablesWithIntrinsicBounds(null, textIcon, null, null); + break; + case textOnRight: + descriptionTv.setCompoundDrawablesWithIntrinsicBounds(null, null, textIcon, null); + break; + case textOnBottom: + descriptionTv.setCompoundDrawablesWithIntrinsicBounds(null, null, null, textIcon); + break; + } + } + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/helpers/WaypointDialogHelper.java b/OsmAnd/src/net/osmand/plus/helpers/WaypointDialogHelper.java index 66ef194e0d..a5a81132dd 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/WaypointDialogHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/WaypointDialogHelper.java @@ -1086,7 +1086,7 @@ public class WaypointDialogHelper { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { if (id == MapRouteInfoMenu.SPINNER_FAV_ID) { - routeMenu.selectFavorite(false, true); + routeMenu.selectFavorite(null,false, true); } else if (id == MapRouteInfoMenu.SPINNER_MAP_ID) { routeMenu.selectOnScreen(false, true); } else if (id == MapRouteInfoMenu.SPINNER_ADDRESS_ID) { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesAdapter.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesAdapter.java index 532adf6824..590f0e6f91 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesAdapter.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesAdapter.java @@ -1,5 +1,6 @@ package net.osmand.plus.mapcontextmenu.other; +import android.app.Activity; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; @@ -15,7 +16,6 @@ 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; import net.osmand.plus.dashboard.DashLocationFragment; import net.osmand.util.MapUtils; @@ -24,32 +24,15 @@ import java.util.List; public class FavouritesAdapter extends RecyclerView.Adapter { - private View.OnClickListener listener; private final Context context; private final List favouritePoints; + private View.OnClickListener listener; private LatLon location; private Float heading; private boolean useCenter; private int screenOrientation; - static class FavouritesViewHolder extends RecyclerView.ViewHolder { - - final TextView title; - final TextView description; - final TextView distance; - final ImageView arrowImage; - final ImageView favouriteImage; - - public FavouritesViewHolder(View itemView) { - super(itemView); - favouriteImage = (ImageView) itemView.findViewById(R.id.favourite_icon); - title = (TextView) itemView.findViewById(R.id.favourite_title); - description = (TextView) itemView.findViewById(R.id.favourite_description); - distance = (TextView) itemView.findViewById(R.id.favourite_distance); - arrowImage = (ImageView) itemView.findViewById(R.id.favourite_direction_icon); - } - } public FavouritesAdapter(Context context, List FavouritePoints) { this.favouritePoints = FavouritePoints; @@ -59,26 +42,18 @@ public class FavouritesAdapter extends RecyclerView.Adapter favouritePoints; + private FavouritesAdapter adapter; + private RecyclerView recyclerView; private boolean sortByDist = false; - private boolean locationUpdateStarted; private boolean compassUpdateAllowed = true; - private List list; - private FavouritesAdapter adapter; @Override public void createMenuItems(Bundle savedInstanceState) { + Bundle args = getArguments(); + target = args.getBoolean(TARGET); + intermediate = args.getBoolean(INTERMEDIATE); final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme; FavouritesDbHelper favouritesDbHelper = getMyApplication().getFavorites(); mapActivity = (MapActivity) getActivity(); - list = favouritesDbHelper.getVisibleFavouritePoints(); + favouritePoints = favouritesDbHelper.getVisibleFavouritePoints(); View titleView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), - 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); + R.layout.bottom_sheet_item_title_with_button, null); + ((TextView) titleView.findViewById(R.id.title)).setText(R.string.favourites); + final TextView textButton = (TextView) titleView.findViewById(R.id.text_button); + recyclerView = new RecyclerView(getContext()); - TextView title = (TextView) titleView.findViewById(R.id.title); - final ImageView sortIconView = (ImageView) titleView.findViewById(R.id.sort_icon); - final TextView sortText = (TextView) titleView.findViewById(R.id.sort_text); - LinearLayout sort = (LinearLayout) titleView.findViewById(R.id.sort_by); - final RecyclerView recyclerView = (RecyclerView) mainView.findViewById(R.id.groups_recycler_view); - - title.setText(R.string.favourites); - sortIconView.setImageDrawable(getIcon(R.drawable.ic_action_sort_by_name, nightMode ? R.color.route_info_go_btn_inking_dark : R.color.dash_search_icon_light)); 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 TitleItem.Builder() - .setCustomView(titleView) - .create()); + textButton.setText(sortByDist ? R.string.sort_by_distance : R.string.sort_by_name); - adapter = new FavouritesAdapter(mapActivity, list); - adapter.setAdapterListener(new View.OnClickListener() { + final TitleWithButtonItem title = new TitleWithButtonItem.Builder() + .setIcon(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)) + .setIconPosition(TitleWithButtonItem.textOnRight) + .setTitle(getString(R.string.favourites)) + .setTextOnRight(getString(sortByDist ? R.string.sort_by_distance : R.string.sort_by_name)) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (location == null) { + return; + } + sortFavourites(); + adapter.notifyDataSetChanged(); + } + }) + .create(); + items.add(title); + + adapter = new FavouritesAdapter(mapActivity, favouritePoints); + adapter.setItemClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = recyclerView.getChildAdapterPosition(v); if (position == RecyclerView.NO_POSITION) { return; } - selectFavorite(list.get(position)); + selectFavorite(favouritePoints.get(position)); } }); - recyclerView.setLayoutManager(new LinearLayoutManager(mapActivity, LinearLayoutManager.VERTICAL, false)); recyclerView.setAdapter(adapter); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override @@ -106,53 +109,42 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag } }); items.add(new BaseBottomSheetItem.Builder(). - setCustomView(mainView). + setCustomView(recyclerView). create()); } - private void sortFavourites(boolean sortByDist, List favourites) { - if (sortByDist) { - Collections.sort(favourites, new Comparator() { - @Override - public int compare(FavouritePoint lhs, FavouritePoint rhs) { - LatLon latLon = new LatLon(location.getLatitude(), location.getLongitude()); - double d1 = MapUtils.getDistance(latLon, lhs.getLatitude(), lhs.getLongitude()); - double d2 = MapUtils.getDistance(latLon, rhs.getLatitude(), rhs.getLongitude()); - if (d1 == d2) { - return 0; - } else if (d1 > d2) { - return 1; - } - return -1; + private void sortFavourites() { + final Collator inst = Collator.getInstance(); + Collections.sort(favouritePoints, new Comparator() { + @Override + public int compare(FavouritePoint lhs, FavouritePoint rhs) { + LatLon latLon = new LatLon(location.getLatitude(), location.getLongitude()); + if (sortByDist) { + double ld = MapUtils.getDistance(latLon, lhs.getLatitude(), + lhs.getLongitude()); + double rd = MapUtils.getDistance(latLon, rhs.getLatitude(), + rhs.getLongitude()); + return Double.compare(ld, rd); } - }); - this.sortByDist = false; - } else { - Collections.sort(favourites, new Comparator() { - @Override - public int compare(FavouritePoint lhs, FavouritePoint rhs) { - return lhs.getName().compareTo(rhs.getName()); - } - }); - this.sortByDist = true; - } + return inst.compare(lhs.getName(), rhs.getName()); + } + }); + sortByDist = !sortByDist; } - private void selectFavorite(FavouritePoint point) { - Bundle args = getArguments(); - boolean target = args.getBoolean(TARGET); - boolean intermediate = args.getBoolean(INTERMEDIATE); + void selectFavorite(FavouritePoint point) { final MapRouteInfoMenu routeMenu = mapActivity.getMapLayers().getMapControlsLayer().getMapRouteInfoMenu(); + TargetPointsHelper targetPointsHelper = getMyApplication().getTargetPointsHelper(); LatLon ll = new LatLon(point.getLatitude(), point.getLongitude()); if (intermediate) { - routeMenu.getTargets().navigateToPoint(ll, true, routeMenu.getTargets().getIntermediatePoints().size(), point.getPointDescription()); + targetPointsHelper.navigateToPoint(ll, true, targetPointsHelper.getIntermediatePoints().size(), point.getPointDescription()); } else if (target) { - routeMenu.getTargets().navigateToPoint(ll, true, -1, point.getPointDescription()); + targetPointsHelper.navigateToPoint(ll, true, -1, point.getPointDescription()); } else { - routeMenu.getTargets().setStartPoint(ll, true, point.getPointDescription()); + targetPointsHelper.setStartPoint(ll, true, point.getPointDescription()); } - if (!intermediate) { + if (!intermediate && getActivity() instanceof MapActivity) { routeMenu.updateFromIcon(); } dismiss(); @@ -240,4 +232,19 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag app.getLocationProvider().addCompassListener(app.getLocationProvider().getNavigationInfo()); } } + + @Override + public void onViewStateRestored(@Nullable Bundle savedInstanceState) { + super.onViewStateRestored(savedInstanceState); + if (savedInstanceState != null) { + Parcelable savedRecyclerLayoutState = savedInstanceState.getParcelable(BUNDLE_RECYCLER_LAYOUT); + recyclerView.getLayoutManager().onRestoreInstanceState(savedRecyclerLayoutState); + } + } + + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putParcelable(BUNDLE_RECYCLER_LAYOUT, recyclerView.getLayoutManager().onSaveInstanceState()); + } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/MapRouteInfoMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/MapRouteInfoMenu.java index ed19b63d1c..f6974b17ec 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/MapRouteInfoMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/MapRouteInfoMenu.java @@ -81,6 +81,7 @@ public class MapRouteInfoMenu implements IRouteInformationListener { private OnDismissListener onDismissListener; private OnMarkerSelectListener onMarkerSelectListener; + View.OnClickListener dismissListener; private static final long SPINNER_MY_LOCATION_ID = 1; public static final long SPINNER_FAV_ID = 2; @@ -242,7 +243,7 @@ public class MapRouteInfoMenu implements IRouteInformationListener { ViewGroup vg = (ViewGroup) parentView.findViewById(R.id.app_modes); vg.removeAllViews(); AppModeDialog.prepareAppModeView(mapActivity, selected, false, - vg, true, false,true, new View.OnClickListener() { + vg, true, false, true, new View.OnClickListener() { @Override public void onClick(View v) { if (selected.size() > 0) { @@ -338,7 +339,7 @@ public class MapRouteInfoMenu implements IRouteInformationListener { @Override public void run() { if (id == SPINNER_FAV_ID) { - selectFavorite(true, false); + selectFavorite(parentView, true, false); } else if (id == SPINNER_MAP_ID) { selectOnScreen(true, false); } else if (id == SPINNER_ADDRESS_ID) { @@ -408,7 +409,7 @@ public class MapRouteInfoMenu implements IRouteInformationListener { } updateFromIcon(parentView); } else if (id == SPINNER_FAV_ID) { - selectFavorite(false, false); + selectFavorite(parentView, false, false); } else if (id == SPINNER_MAP_ID) { selectOnScreen(false, false); } else if (id == SPINNER_ADDRESS_ID) { @@ -470,7 +471,7 @@ public class MapRouteInfoMenu implements IRouteInformationListener { updateMenu(); } - public void selectFavorite(final boolean target, final boolean intermediate) { + public void selectFavorite(@Nullable final View parentView, final boolean target, final boolean intermediate) { FragmentManager fragmentManager = mapActivity.getSupportFragmentManager(); FavouritesBottomSheetMenuFragment fragment = new FavouritesBottomSheetMenuFragment(); Bundle args = new Bundle(); @@ -478,6 +479,12 @@ public class MapRouteInfoMenu implements IRouteInformationListener { args.putBoolean(FavouritesBottomSheetMenuFragment.INTERMEDIATE, intermediate); fragment.setArguments(args); fragment.show(fragmentManager, FavouritesBottomSheetMenuFragment.TAG); + + if (target) { + setupToSpinner(parentView); + } else { + setupFromSpinner(parentView); + } } public void selectMapMarker(final int index, final boolean target, final boolean intermediate) { @@ -846,7 +853,7 @@ public class MapRouteInfoMenu implements IRouteInformationListener { } } - TargetPointsHelper getTargets() { + private TargetPointsHelper getTargets() { return mapActivity.getMyApplication().getTargetPointsHelper(); }