added asyncTask for sorting items

This commit is contained in:
Chumva 2018-03-15 15:51:07 +02:00
parent da27d8400e
commit 6605590688

View file

@ -1,5 +1,7 @@
package net.osmand.plus.mapcontextmenu.other; package net.osmand.plus.mapcontextmenu.other;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@ -36,6 +38,7 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
private static final String IS_SORTED = "sorted"; private static final String IS_SORTED = "sorted";
private static final String SORTED_BY_TYPE = "sortedByType"; private static final String SORTED_BY_TYPE = "sortedByType";
private ProcessFavouritesTask asyncProcessor;
private Location location; private Location location;
private Float heading; private Float heading;
private List<FavouritePoint> favouritePoints; private List<FavouritePoint> favouritePoints;
@ -45,19 +48,11 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
private boolean isSorted = false; private boolean isSorted = false;
private boolean locationUpdateStarted; private boolean locationUpdateStarted;
private boolean compassUpdateAllowed = true; private boolean compassUpdateAllowed = true;
private boolean target;
private boolean intermediate;
private BaseBottomSheetItem title; private BaseBottomSheetItem title;
private MapRouteInfoMenu routeMenu;
@Override @Override
public void createMenuItems(Bundle savedInstanceState) { public void createMenuItems(Bundle savedInstanceState) {
Bundle args = getArguments();
target = args.getBoolean(TARGET);
intermediate = args.getBoolean(INTERMEDIATE);
favouritePoints = getMyApplication().getFavorites().getVisibleFavouritePoints(); favouritePoints = getMyApplication().getFavorites().getVisibleFavouritePoints();
routeMenu = ((MapActivity) getActivity()).getMapLayers().getMapControlsLayer().getMapRouteInfoMenu();
recyclerView = new RecyclerView(getContext()); recyclerView = new RecyclerView(getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
@ -72,7 +67,8 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
if (location == null) { if (location == null) {
return; return;
} }
sortFavourites(); asyncProcessor = new ProcessFavouritesTask();
asyncProcessor.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
((BottomSheetItemWithTitleAndButton) title).setButtonIcon(null, getIcon(sortByDist ? R.drawable.ic_action_list_sort : R.drawable.ic_action_sort_by_name, ((BottomSheetItemWithTitleAndButton) title).setButtonIcon(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)); 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)); ((BottomSheetItemWithTitleAndButton) title).setButtonText(getString(sortByDist ? R.string.sort_by_distance : R.string.sort_by_name));
@ -104,38 +100,26 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
items.add(new BaseBottomSheetItem.Builder() items.add(new BaseBottomSheetItem.Builder()
.setCustomView(recyclerView) .setCustomView(recyclerView)
.create()); .create());
if (savedInstanceState != null && savedInstanceState.getBoolean(IS_SORTED)) { if (savedInstanceState != null && savedInstanceState.getBoolean(IS_SORTED)) {
location = getMyApplication().getLocationProvider().getLastKnownLocation(); location = getMyApplication().getLocationProvider().getLastKnownLocation();
sortByDist = savedInstanceState.getBoolean(SORTED_BY_TYPE); sortByDist = savedInstanceState.getBoolean(SORTED_BY_TYPE);
sortFavourites(); asyncProcessor = new ProcessFavouritesTask();
asyncProcessor.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
} }
private void sortFavourites() { private void selectFavorite(FavouritePoint point) {
final Collator inst = Collator.getInstance(); Bundle args = getArguments();
Collections.sort(favouritePoints, new Comparator<FavouritePoint>() { boolean target = args.getBoolean(TARGET);
@Override boolean intermediate = args.getBoolean(INTERMEDIATE);
public int compare(FavouritePoint lhs, FavouritePoint rhs) { Activity activity = getActivity();
LatLon latLon = new LatLon(location.getLatitude(), location.getLongitude()); MapRouteInfoMenu routeMenu;
if (sortByDist) { if (activity instanceof MapActivity) {
double ld = MapUtils.getDistance(latLon, lhs.getLatitude(), routeMenu = ((MapActivity) activity).getMapLayers().getMapControlsLayer().getMapRouteInfoMenu();
lhs.getLongitude()); } else {
double rd = MapUtils.getDistance(latLon, rhs.getLatitude(), return;
rhs.getLongitude()); }
return Double.compare(ld, rd);
}
return inst.compare(lhs.getName(), rhs.getName());
}
});
sortByDist = !sortByDist;
isSorted = true;
adapter.notifyDataSetChanged();
}
void selectFavorite(FavouritePoint point) {
TargetPointsHelper targetPointsHelper = getMyApplication().getTargetPointsHelper(); TargetPointsHelper targetPointsHelper = getMyApplication().getTargetPointsHelper();
LatLon ll = new LatLon(point.getLatitude(), point.getLongitude()); LatLon ll = new LatLon(point.getLatitude(), point.getLongitude());
if (intermediate) { if (intermediate) {
targetPointsHelper.navigateToPoint(ll, true, targetPointsHelper.getIntermediatePoints().size(), point.getPointDescription()); targetPointsHelper.navigateToPoint(ll, true, targetPointsHelper.getIntermediatePoints().size(), point.getPointDescription());
@ -250,4 +234,58 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
outState.putBoolean(IS_SORTED, isSorted); outState.putBoolean(IS_SORTED, isSorted);
outState.putBoolean(SORTED_BY_TYPE, !sortByDist); outState.putBoolean(SORTED_BY_TYPE, !sortByDist);
} }
@Override
protected boolean useScrollableItemsContainer() {
return false;
}
public class ProcessFavouritesTask extends AsyncTask<Void, Void, Void> {
@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<FavouritePoint>() {
@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());
}
});
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;
}
}
} }