From e05a63357244234c6d86c0cfe1ee96d094ef9ae4 Mon Sep 17 00:00:00 2001 From: Chumva Date: Thu, 15 Mar 2018 18:36:05 +0200 Subject: [PATCH] added asyncTask inside sortFavourites --- ...ottom_sheet_item_with_title_and_button.xml | 13 +- .../FavouritesBottomSheetMenuFragment.java | 126 +++++++----------- 2 files changed, 57 insertions(+), 82 deletions(-) diff --git a/OsmAnd/res/layout/bottom_sheet_item_with_title_and_button.xml b/OsmAnd/res/layout/bottom_sheet_item_with_title_and_button.xml index d022a4f04a..d4d2353655 100644 --- a/OsmAnd/res/layout/bottom_sheet_item_with_title_and_button.xml +++ b/OsmAnd/res/layout/bottom_sheet_item_with_title_and_button.xml @@ -1,11 +1,12 @@ @@ -19,12 +20,13 @@ android:ellipsize="end" android:maxLines="1" android:textAppearance="@style/TextAppearance.ListItemTitle" - osmand:typeface="@string/font_roboto_medium" /> + osmand:typeface="@string/font_roboto_medium" + tools:text="Some Title" /> + osmand:typeface="@string/font_roboto_medium" + tools:drawableRight="@drawable/ic_action_sort_by_name" + tools:text="Some button" /> \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesBottomSheetMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesBottomSheetMenuFragment.java index 971ef67545..5a3a6ebbd1 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesBottomSheetMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesBottomSheetMenuFragment.java @@ -1,10 +1,9 @@ package net.osmand.plus.mapcontextmenu.other; +import android.annotation.SuppressLint; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; -import android.os.Parcelable; -import android.support.annotation.Nullable; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; @@ -34,11 +33,9 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag public static final String TARGET = "target"; public static final String INTERMEDIATE = "intermediate"; public static final String TAG = "FavouritesBottomSheetMenuFragment"; - private static final String BUNDLE_RECYCLER_LAYOUT = "FavouritesBottomSheetMenuFragment.recycler"; private static final String IS_SORTED = "sorted"; private static final String SORTED_BY_TYPE = "sortedByType"; - private ProcessFavouritesTask asyncProcessor; private Location location; private Float heading; private List favouritePoints; @@ -48,7 +45,7 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag private boolean isSorted = false; private boolean locationUpdateStarted; private boolean compassUpdateAllowed = true; - private BaseBottomSheetItem title; + private BottomSheetItemWithTitleAndButton title; @Override public void createMenuItems(Bundle savedInstanceState) { @@ -56,7 +53,7 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag recyclerView = new RecyclerView(getContext()); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); - title = new BottomSheetItemWithTitleAndButton.Builder() + title = (BottomSheetItemWithTitleAndButton) new BottomSheetItemWithTitleAndButton.Builder() .setButtonIcons(null, 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)) .setButtonTitle(getString(sortByDist ? R.string.sort_by_distance : R.string.sort_by_name)) @@ -66,11 +63,10 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag if (location == null) { return; } - asyncProcessor = new ProcessFavouritesTask(); - asyncProcessor.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - ((BottomSheetItemWithTitleAndButton) title).setButtonIcons(null, getIcon(sortByDist ? R.drawable.ic_action_list_sort : R.drawable.ic_action_sort_by_name, + sortFavourites(); + title.setButtonIcons(null, 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)); - ((BottomSheetItemWithTitleAndButton) title).setButtonText(getString(sortByDist ? R.string.sort_by_distance : R.string.sort_by_name)); + title.setButtonText(getString(sortByDist ? R.string.sort_by_distance : R.string.sort_by_name)); } }) .setTitle(getString(R.string.favourites)) @@ -104,8 +100,7 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag if (savedInstanceState != null && savedInstanceState.getBoolean(IS_SORTED)) { location = getMyApplication().getLocationProvider().getLastKnownLocation(); sortByDist = savedInstanceState.getBoolean(SORTED_BY_TYPE); - asyncProcessor = new ProcessFavouritesTask(); - asyncProcessor.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + sortFavourites(); } } @@ -113,13 +108,6 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag Bundle args = getArguments(); boolean target = args.getBoolean(TARGET); boolean intermediate = args.getBoolean(INTERMEDIATE); - Activity activity = getActivity(); - MapRouteInfoMenu routeMenu; - if (activity instanceof MapActivity) { - routeMenu = ((MapActivity) activity).getMapLayers().getMapControlsLayer().getMapRouteInfoMenu(); - } else { - return; - } TargetPointsHelper targetPointsHelper = getMyApplication().getTargetPointsHelper(); LatLon ll = new LatLon(point.getLatitude(), point.getLongitude()); if (intermediate) { @@ -129,10 +117,18 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag } else { targetPointsHelper.setStartPoint(ll, true, point.getPointDescription()); } - if (!intermediate && getActivity() instanceof MapActivity) { - routeMenu.updateFromIcon(); + + Activity activity = getActivity(); + if (activity instanceof MapActivity) { + MapActivity map = ((MapActivity) activity); + MapRouteInfoMenu routeMenu = map.getMapLayers().getMapControlsLayer().getMapRouteInfoMenu(); + routeMenu.setupSpinners(target, intermediate); + if (!intermediate) { + routeMenu.updateFromIcon(); + } + } else { + return; } - routeMenu.setupSpinners(target, intermediate); dismiss(); } @@ -219,19 +215,9 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag } } - @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()); outState.putBoolean(IS_SORTED, isSorted); outState.putBoolean(SORTED_BY_TYPE, !sortByDist); } @@ -241,52 +227,38 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag return false; } - public class ProcessFavouritesTask extends AsyncTask { - - @Override - protected void onPreExecute() { - } - - @Override - protected Void doInBackground(Void... params) { - sortFavourites(); - return null; - } - - 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); + @SuppressLint("StaticFieldLeak") + private void sortFavourites() { + new AsyncTask() { + @Override + protected Void doInBackground(Void... voids) { + 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); + } + return inst.compare(lhs.getName(), rhs.getName()); } - return inst.compare(lhs.getName(), rhs.getName()); + }); + sortByDist = !sortByDist; + isSorted = true; + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (adapter != null) { + adapter.notifyDataSetChanged(); } - }); - sortByDist = !sortByDist; - isSorted = true; - } - - @Override - protected void onPostExecute(Void aVoid) { - asyncProcessor = null; - adapter.notifyDataSetChanged(); - - } - } - - @Override - public void onDestroyView() { - super.onDestroyView(); - if (asyncProcessor != null) { - asyncProcessor.cancel(false); - asyncProcessor = null; - } + } + }.execute(); } }