Refactoring gpxTaskListener

This commit is contained in:
Dima-1 2020-02-21 14:23:56 +02:00
parent 1d9d09e377
commit 8e872492c7
2 changed files with 23 additions and 14 deletions

View file

@ -56,8 +56,12 @@ public class GpxSelectionHelper {
private Map<GPXFile, Long> selectedGpxFilesBackUp = new java.util.HashMap<>(); private Map<GPXFile, Long> selectedGpxFilesBackUp = new java.util.HashMap<>();
private SavingTrackHelper savingTrackHelper; private SavingTrackHelper savingTrackHelper;
private final static Log LOG = PlatformUtil.getLog(GpxSelectionHelper.class); private final static Log LOG = PlatformUtil.getLog(GpxSelectionHelper.class);
SelectGpxTask selectGpxTask; private SelectGpxTask selectGpxTask;
private SelectGpxTaskListener gpxTaskListener;
public void setGpxTaskListener(SelectGpxTaskListener gpxTaskListener) {
this.gpxTaskListener = gpxTaskListener;
}
public GpxSelectionHelper(OsmandApplication osmandApplication, SavingTrackHelper trackHelper) { public GpxSelectionHelper(OsmandApplication osmandApplication, SavingTrackHelper trackHelper) {
this.app = osmandApplication; this.app = osmandApplication;
@ -625,7 +629,7 @@ public class GpxSelectionHelper {
return sf; return sf;
} }
public void addRemoveSelected(boolean show, SelectedGpxFile sf) { private void addRemoveSelected(boolean show, SelectedGpxFile sf) {
List<SelectedGpxFile> newSelectedGPXFiles = new ArrayList<>(selectedGPXFiles); List<SelectedGpxFile> newSelectedGPXFiles = new ArrayList<>(selectedGPXFiles);
if (show) { if (show) {
if (!newSelectedGPXFiles.contains(sf)) { if (!newSelectedGPXFiles.contains(sf)) {
@ -963,12 +967,12 @@ public class GpxSelectionHelper {
} }
} }
public void runSelection(Map<String, Boolean> selectedItems, SelectGpxTaskListener gpxTaskListener) { public void runSelection(Map<String, Boolean> selectedItems) {
if (selectGpxTask != null && (selectGpxTask.getStatus() == AsyncTask.Status.RUNNING if (selectGpxTask != null && (selectGpxTask.getStatus() == AsyncTask.Status.RUNNING
|| selectGpxTask.getStatus() == AsyncTask.Status.PENDING)) { || selectGpxTask.getStatus() == AsyncTask.Status.PENDING)) {
selectGpxTask.cancel(false); selectGpxTask.cancel(false);
} }
selectGpxTask = new SelectGpxTask(selectedItems, gpxTaskListener); selectGpxTask = new SelectGpxTask(selectedItems);
selectGpxTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); selectGpxTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
@ -984,12 +988,10 @@ public class GpxSelectionHelper {
public class SelectGpxTask extends AsyncTask<Void, Void, String> { public class SelectGpxTask extends AsyncTask<Void, Void, String> {
final Set<GPXFile> originalSelectedItems = new HashSet<>(); private Set<GPXFile> originalSelectedItems = new HashSet<>();
private SelectGpxTaskListener gpxTaskListener; private Map<String, Boolean> selectedItems;
Map<String, Boolean> selectedItems;
SelectGpxTask(Map<String, Boolean> selectedItems, SelectGpxTaskListener gpxTaskListener) { SelectGpxTask(Map<String, Boolean> selectedItems) {
this.gpxTaskListener = gpxTaskListener;
this.selectedItems = selectedItems; this.selectedItems = selectedItems;
} }
@ -1010,13 +1012,17 @@ public class GpxSelectionHelper {
@Override @Override
protected void onProgressUpdate(Void... values) { protected void onProgressUpdate(Void... values) {
gpxTaskListener.gpxSelectionInProgress(); if (gpxTaskListener != null) {
gpxTaskListener.gpxSelectionInProgress();
}
} }
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
collectSelectedItems(); collectSelectedItems();
gpxTaskListener.gpxSelectionStarted(); if (gpxTaskListener != null) {
gpxTaskListener.gpxSelectionStarted();
}
} }
private void collectSelectedItems() { private void collectSelectedItems() {
@ -1048,7 +1054,9 @@ public class GpxSelectionHelper {
@Override @Override
protected void onPostExecute(String result) { protected void onPostExecute(String result) {
gpxTaskListener.gpxSelectionFinished(); if (gpxTaskListener != null) {
gpxTaskListener.gpxSelectionFinished();
}
} }
} }
} }

View file

@ -205,7 +205,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
selectedGpxHelper.setGpxTaskListener(gpxTaskListener);
if (!importing) { if (!importing) {
if (asyncLoader == null || asyncLoader.getResult() == null) { if (asyncLoader == null || asyncLoader.getResult() == null) {
asyncLoader = new LoadGpxTask(); asyncLoader = new LoadGpxTask();
@ -225,6 +225,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
selectedGpxHelper.setGpxTaskListener(null);
updateEnable = false; updateEnable = false;
if (operationTask != null) { if (operationTask != null) {
operationTask.cancel(true); operationTask.cancel(true);
@ -656,7 +657,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
String path = gpxInfo.currentlyRecordingTrack ? CURRENT_TRACK : gpxInfo.file.getAbsolutePath(); String path = gpxInfo.currentlyRecordingTrack ? CURRENT_TRACK : gpxInfo.file.getAbsolutePath();
selectedItemsFileNames.put(path, selectedItems.contains(gpxInfo)); selectedItemsFileNames.put(path, selectedItems.contains(gpxInfo));
} }
selectedGpxHelper.runSelection(selectedItemsFileNames, gpxTaskListener); selectedGpxHelper.runSelection(selectedItemsFileNames);
} }
public void openSelectionMode(final int actionResId, int darkIcon, int lightIcon, public void openSelectionMode(final int actionResId, int darkIcon, int lightIcon,