Add a delete button to TrackSegmentFragment

This commit is contained in:
Vitaliy 2021-01-11 05:36:06 +02:00
parent c1dcd503c8
commit 6188343341
3 changed files with 43 additions and 18 deletions

View file

@ -84,8 +84,8 @@ import net.osmand.plus.monitoring.OsmandMonitoringPlugin;
import net.osmand.plus.osmedit.OsmEditingPlugin; import net.osmand.plus.osmedit.OsmEditingPlugin;
import net.osmand.plus.osmedit.oauth.OsmOAuthHelper.OsmAuthorizationListener; import net.osmand.plus.osmedit.oauth.OsmOAuthHelper.OsmAuthorizationListener;
import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.widgets.popup.PopUpMenuItem;
import net.osmand.plus.widgets.popup.PopUpMenuHelper; import net.osmand.plus.widgets.popup.PopUpMenuHelper;
import net.osmand.plus.widgets.popup.PopUpMenuItem;
import java.io.File; import java.io.File;
import java.text.DateFormat; import java.text.DateFormat;
@ -105,6 +105,7 @@ import java.util.regex.Pattern;
import static net.osmand.plus.GpxSelectionHelper.CURRENT_TRACK; import static net.osmand.plus.GpxSelectionHelper.CURRENT_TRACK;
import static net.osmand.plus.myplaces.FavoritesActivity.GPX_TAB; import static net.osmand.plus.myplaces.FavoritesActivity.GPX_TAB;
import static net.osmand.plus.myplaces.FavoritesActivity.OPEN_GPX_REQUEST;
import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID;
import static net.osmand.util.Algorithms.capitalizeFirstLetter; import static net.osmand.util.Algorithms.capitalizeFirstLetter;
import static net.osmand.util.Algorithms.collectDirs; import static net.osmand.util.Algorithms.collectDirs;
@ -426,7 +427,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
newIntent.putExtra(TrackActivity.TRACK_FILE_NAME, f.getAbsolutePath()); newIntent.putExtra(TrackActivity.TRACK_FILE_NAME, f.getAbsolutePath());
} }
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
a.startActivity(newIntent); a.startActivityForResult(newIntent, OPEN_GPX_REQUEST);
} }
public void reloadTracks() { public void reloadTracks() {
@ -434,6 +435,10 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
asyncLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getActivity()); asyncLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getActivity());
} }
public void resetTracksLoader() {
asyncLoader = null;
}
@Override @Override
public void onCreateOptionsMenu(Menu menu, @NonNull MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, @NonNull MenuInflater inflater) {
menu.clear(); menu.clear();
@ -1711,20 +1716,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
GpxInfo item = allGpxAdapter.getChild(groupPosition, childPosition); GpxInfo item = allGpxAdapter.getChild(groupPosition, childPosition);
if (!selectionMode) { if (!selectionMode) {
Intent newIntent = new Intent(getActivity(), getMyApplication().getAppCustomization().getTrackActivity()); openTrack(getActivity(), item.file);
// causes wrong position caching: newIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
if (item.currentlyRecordingTrack) {
newIntent.putExtra(TrackActivity.CURRENT_RECORDING, true);
} else {
newIntent.putExtra(TrackActivity.TRACK_FILE_NAME, item.file.getAbsolutePath());
}
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(newIntent);
// item.setExpanded(!item.isExpanded());
// if (item.isExpanded()) {
// descriptionLoader = new LoadLocalIndexDescriptionTask();
// descriptionLoader.execute(item);
// }
} else { } else {
if (!selectedItems.contains(item)) { if (!selectedItems.contains(item)) {
selectedItems.add(item); selectedItems.add(item);

View file

@ -42,6 +42,8 @@ import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.osmand.plus.myplaces.TrackSegmentFragment.TRACK_DELETED_KEY;
/** /**
* *
*/ */
@ -50,6 +52,7 @@ public class FavoritesActivity extends TabActivity {
private static final int OPEN_GPX_DOCUMENT_REQUEST = 1006; private static final int OPEN_GPX_DOCUMENT_REQUEST = 1006;
private static final int IMPORT_FAVOURITES_REQUEST = 1007; private static final int IMPORT_FAVOURITES_REQUEST = 1007;
protected static final int OPEN_GPX_REQUEST = 1008;
public static final String TAB_ID = "selected_tab_id"; public static final String TAB_ID = "selected_tab_id";
@ -152,6 +155,13 @@ public class FavoritesActivity extends TabActivity {
if (data != null && data.getData() != null) { if (data != null && data.getData() != null) {
importHelper.handleGpxOrFavouritesImport(data.getData()); importHelper.handleGpxOrFavouritesImport(data.getData());
} }
} else if (requestCode == OPEN_GPX_REQUEST && resultCode == Activity.RESULT_OK) {
if (data != null && data.getBooleanExtra(TRACK_DELETED_KEY, false)) {
AvailableGPXFragment gpxFragment = getGpxFragment();
if (gpxFragment != null) {
gpxFragment.resetTracksLoader();
}
}
} else { } else {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
} }

View file

@ -1,6 +1,8 @@
package net.osmand.plus.myplaces; package net.osmand.plus.myplaces;
import android.app.Activity;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.AsyncTask; import android.os.AsyncTask;
@ -23,6 +25,7 @@ import androidx.fragment.app.FragmentManager;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.FileUtils;
import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.Track; import net.osmand.GPXUtilities.Track;
import net.osmand.GPXUtilities.TrkSegment; import net.osmand.GPXUtilities.TrkSegment;
@ -55,6 +58,8 @@ import java.util.List;
public class TrackSegmentFragment extends OsmAndListFragment implements TrackBitmapDrawerListener, SegmentActionsListener { public class TrackSegmentFragment extends OsmAndListFragment implements TrackBitmapDrawerListener, SegmentActionsListener {
public static final String TRACK_DELETED_KEY = "track_deleted_key";
private OsmandApplication app; private OsmandApplication app;
private TrackDisplayHelper displayHelper; private TrackDisplayHelper displayHelper;
private TrackActivityFragmentAdapter fragmentAdapter; private TrackActivityFragmentAdapter fragmentAdapter;
@ -134,8 +139,26 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
} }
}); });
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
} MenuItem deleteItem = menu.add(R.string.shared_string_delete)
if (gpxFile.showCurrentTrack) { .setIcon(app.getUIUtilities().getIcon((R.drawable.ic_action_delete_dark)))
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
GPXFile gpx = displayHelper.getGpx();
FragmentActivity activity = getActivity();
if (activity != null && gpx != null) {
if (FileUtils.removeGpxFile(app, new File((gpx.path)))) {
Intent intent = new Intent();
intent.putExtra(TRACK_DELETED_KEY, true);
activity.setResult(Activity.RESULT_OK, intent);
activity.onBackPressed();
}
}
return true;
}
});
deleteItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
} else if (gpxFile.showCurrentTrack) {
MenuItem item = menu.add(R.string.shared_string_refresh).setIcon(R.drawable.ic_action_refresh_dark) MenuItem item = menu.add(R.string.shared_string_refresh).setIcon(R.drawable.ic_action_refresh_dark)
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override @Override