added asyncTask inside sortFavourites

This commit is contained in:
Chumva 2018-03-15 18:36:05 +02:00
parent 0e451e0ec1
commit e05a633572
2 changed files with 57 additions and 82 deletions

View file

@ -1,11 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:osmand="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_sheet_title_height"
android:background="?attr/selectableItemBackground"
android:gravity="center_vertical"
android:minHeight="@dimen/bottom_sheet_list_item_height"
android:minHeight="@dimen/bottom_sheet_title_height"
android:paddingLeft="@dimen/content_padding"
android:paddingRight="@dimen/content_padding">
@ -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" />
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/text_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="@dimen/bottom_sheet_title_height"
android:background="?attr/selectableItemBackground"
android:drawablePadding="2dp"
android:gravity="center_vertical"
@ -32,9 +34,10 @@
android:paddingLeft="@dimen/content_padding"
android:paddingRight="@dimen/content_padding"
android:textAllCaps="true"
android:textAppearance="@style/TextAppearance.ListItemTitle"
android:textColor="?attr/color_dialog_buttons"
android:textSize="@dimen/default_desc_text_size"
osmand:typeface="@string/font_roboto_medium" />
osmand:typeface="@string/font_roboto_medium"
tools:drawableRight="@drawable/ic_action_sort_by_name"
tools:text="Some button" />
</LinearLayout>

View file

@ -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<FavouritePoint> 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<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);
@SuppressLint("StaticFieldLeak")
private void sortFavourites() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
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());
}
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();
}
}