Discard split track changes

This commit is contained in:
Vitaliy 2020-07-16 11:52:49 +03:00
parent ed1db1978a
commit 0e69e7cadf
3 changed files with 146 additions and 67 deletions

View file

@ -9,14 +9,14 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.google.android.material.slider.Slider;
import net.osmand.PlatformUtil;
import net.osmand.plus.GPXDatabase.GpxDataItem;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayItemType;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
@ -32,7 +32,6 @@ import net.osmand.plus.myplaces.SplitTrackAsyncTask.SplitTrackListener;
import org.apache.commons.logging.Log;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@ -50,9 +49,9 @@ public class SplitIntervalBottomSheet extends MenuBottomSheetDialogFragment {
public static final String SELECTED_TIME_SPLIT_INTERVAL = "selected_time_split_interval";
public static final String SELECTED_DISTANCE_SPLIT_INTERVAL = "selected_distance_split_interval";
private OsmandApplication app;
private SelectedGpxFile selectedGpxFile;
private TrackDrawInfo trackDrawInfo;
private Map<String, Integer> timeSplitOptions = new LinkedHashMap<>();
private Map<String, Double> distanceSplitOptions = new LinkedHashMap<>();
@ -73,6 +72,10 @@ public class SplitIntervalBottomSheet extends MenuBottomSheetDialogFragment {
super.onCreate(savedInstanceState);
app = requiredMyApplication();
Fragment target = getTargetFragment();
if (target instanceof TrackAppearanceFragment) {
trackDrawInfo = ((TrackAppearanceFragment) target).getTrackDrawInfo();
}
Bundle arguments = getArguments();
if (savedInstanceState != null) {
String gpxFilePath = savedInstanceState.getString(TRACK_FILE_PATH);
@ -101,10 +104,10 @@ public class SplitIntervalBottomSheet extends MenuBottomSheetDialogFragment {
sliderContainer = view.findViewById(R.id.slider_container);
slider = sliderContainer.findViewById(R.id.split_slider);
splitValueMin = (TextView) view.findViewById(R.id.split_value_min);
splitValueMax = (TextView) view.findViewById(R.id.split_value_max);
selectedSplitValue = (TextView) view.findViewById(R.id.split_value_tv);
splitIntervalNoneDescr = (TextView) view.findViewById(R.id.split_interval_none_descr);
splitValueMin = view.findViewById(R.id.split_value_min);
splitValueMax = view.findViewById(R.id.split_value_max);
selectedSplitValue = view.findViewById(R.id.split_value_tv);
splitIntervalNoneDescr = view.findViewById(R.id.split_interval_none_descr);
UiUtilities.setupSlider(slider, nightMode, null);
@ -146,17 +149,16 @@ public class SplitIntervalBottomSheet extends MenuBottomSheetDialogFragment {
}
private void updateSelectedSplitParams() {
GpxDataItem gpxDataItem = app.getGpxDbHelper().getItem(new File(selectedGpxFile.getGpxFile().path));
if (gpxDataItem != null) {
if (gpxDataItem.getSplitType() == GpxSplitType.DISTANCE.getType()) {
if (trackDrawInfo != null) {
if (trackDrawInfo.getSplitType() == GpxSplitType.DISTANCE.getType()) {
selectedSplitType = GpxSplitType.DISTANCE;
List<Double> splitOptions = new ArrayList<>(distanceSplitOptions.values());
int index = splitOptions.indexOf(gpxDataItem.getSplitInterval());
int index = splitOptions.indexOf(trackDrawInfo.getSplitInterval());
selectedDistanceSplitInterval = Math.max(index, 0);
} else if (gpxDataItem.getSplitType() == GpxSplitType.TIME.getType()) {
} else if (trackDrawInfo.getSplitType() == GpxSplitType.TIME.getType()) {
selectedSplitType = GpxSplitType.TIME;
List<Integer> splitOptions = new ArrayList<>(timeSplitOptions.values());
int index = splitOptions.indexOf((int) gpxDataItem.getSplitInterval());
int index = splitOptions.indexOf((int) trackDrawInfo.getSplitInterval());
selectedTimeSplitInterval = Math.max(index, 0);
}
}
@ -288,12 +290,12 @@ public class SplitIntervalBottomSheet extends MenuBottomSheetDialogFragment {
@Override
protected void onRightBottomButtonClick() {
updateSplit();
applySelectedSplit();
updateSplitInDatabase();
dismiss();
}
private void updateSplitInDatabase() {
private void updateSplit() {
double splitInterval = 0;
if (selectedSplitType == GpxSplitType.NO_SPLIT) {
splitInterval = 0;
@ -302,10 +304,8 @@ public class SplitIntervalBottomSheet extends MenuBottomSheetDialogFragment {
} else if (selectedSplitType == GpxSplitType.TIME) {
splitInterval = new ArrayList<>(timeSplitOptions.values()).get(selectedTimeSplitInterval);
}
GpxDataItem gpxDataItem = app.getGpxDbHelper().getItem(new File(selectedGpxFile.getGpxFile().path));
if (gpxDataItem != null) {
app.getGpxDbHelper().updateSplit(gpxDataItem, selectedSplitType, splitInterval);
}
trackDrawInfo.setSplitType(selectedSplitType.getType());
trackDrawInfo.setSplitInterval(splitInterval);
}
private void applySelectedSplit() {
@ -327,26 +327,27 @@ public class SplitIntervalBottomSheet extends MenuBottomSheetDialogFragment {
}
}
};
List<GpxDisplayGroup> groups = selectedGpxFile.getDisplayGroups(app);
GpxDataItem gpxDataItem = app.getGpxDbHelper().getItem(new File(selectedGpxFile.getGpxFile().path));
boolean isJoinSegments = gpxDataItem != null && gpxDataItem.isJoinSegments();
new SplitTrackAsyncTask(app, selectedSplitType, groups, splitTrackListener, isJoinSegments,
List<GpxDisplayGroup> groups = getDisplayGroups();
new SplitTrackAsyncTask(app, selectedSplitType, groups, splitTrackListener, trackDrawInfo.isJoinSegments(),
timeSplit, distanceSplit).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@NonNull
private List<GpxDisplayGroup> getDisplayGroups() {
public List<GpxDisplayGroup> getDisplayGroups() {
List<GpxDisplayGroup> groups = new ArrayList<>();
for (GpxDisplayGroup group : selectedGpxFile.getDisplayGroups(app)) {
if (GpxSelectionHelper.GpxDisplayItemType.TRACK_SEGMENT == group.getType()) {
groups.add(group);
Fragment target = getTargetFragment();
if (target instanceof TrackAppearanceFragment) {
List<GpxDisplayGroup> result = ((TrackAppearanceFragment) target).getGpxDisplayGroups();
for (GpxDisplayGroup group : result) {
if (GpxDisplayItemType.TRACK_SEGMENT == group.getType()) {
groups.add(group);
}
}
}
return groups;
}
public static void showInstance(@NonNull FragmentManager fragmentManager, TrackDrawInfo trackDrawInfo) {
public static void showInstance(@NonNull FragmentManager fragmentManager, TrackDrawInfo trackDrawInfo, Fragment target) {
try {
if (fragmentManager.findFragmentByTag(SplitIntervalBottomSheet.TAG) == null) {
Bundle args = new Bundle();
@ -354,6 +355,7 @@ public class SplitIntervalBottomSheet extends MenuBottomSheetDialogFragment {
SplitIntervalBottomSheet splitIntervalBottomSheet = new SplitIntervalBottomSheet();
splitIntervalBottomSheet.setArguments(args);
splitIntervalBottomSheet.setTargetFragment(target, 0);
splitIntervalBottomSheet.show(fragmentManager, SplitIntervalBottomSheet.TAG);
}
} catch (RuntimeException e) {

View file

@ -4,6 +4,7 @@ import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
@ -13,10 +14,12 @@ import net.osmand.plus.routepreparationmenu.cards.BaseCard;
public class SplitIntervalCard extends BaseCard {
private TrackDrawInfo trackDrawInfo;
private Fragment targetFragment;
public SplitIntervalCard(@NonNull MapActivity mapActivity, TrackDrawInfo trackDrawInfo) {
public SplitIntervalCard(@NonNull MapActivity mapActivity, TrackDrawInfo trackDrawInfo, Fragment targetFragment) {
super(mapActivity);
this.trackDrawInfo = trackDrawInfo;
this.targetFragment = targetFragment;
}
@Override
@ -36,7 +39,7 @@ public class SplitIntervalCard extends BaseCard {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SplitIntervalBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), trackDrawInfo);
SplitIntervalBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), trackDrawInfo, targetFragment);
}
});
}

View file

@ -14,6 +14,7 @@ import androidx.annotation.NonNull;
import net.osmand.AndroidUtils;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.plus.GPXDatabase.GpxDataItem;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
@ -24,9 +25,13 @@ import net.osmand.plus.base.ContextMenuFragment;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.myplaces.DirectionArrowsCard;
import net.osmand.plus.myplaces.SaveGpxAsyncTask;
import net.osmand.plus.myplaces.SplitTrackAsyncTask;
import net.osmand.plus.routepreparationmenu.cards.BaseCard;
import net.osmand.util.Algorithms;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import static net.osmand.plus.track.TrackDrawInfo.TRACK_FILE_PATH;
@ -34,11 +39,40 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
private OsmandApplication app;
private GpxDataItem gpxDataItem;
private TrackDrawInfo trackDrawInfo;
private SelectedGpxFile selectedGpxFile;
private int menuTitleHeight;
@Override
public int getMainLayoutId() {
return R.layout.track_appearance;
}
@Override
public int getHeaderViewHeight() {
return menuTitleHeight;
}
@Override
public boolean isHeaderViewDetached() {
return false;
}
@Override
public int getToolbarHeight() {
return 0;
}
public float getMiddleStateKoef() {
return 0.5f;
}
public TrackDrawInfo getTrackDrawInfo() {
return trackDrawInfo;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -48,12 +82,13 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
if (savedInstanceState != null) {
trackDrawInfo = new TrackDrawInfo();
trackDrawInfo.readBundle(savedInstanceState);
gpxDataItem = app.getGpxDbHelper().getItem(new File(trackDrawInfo.getFilePath()));
selectedGpxFile = app.getSelectedGpxHelper().getSelectedFileByPath(trackDrawInfo.getFilePath());
} else if (arguments != null) {
String gpxFilePath = arguments.getString(TRACK_FILE_PATH);
selectedGpxFile = app.getSelectedGpxHelper().getSelectedFileByPath(gpxFilePath);
File file = new File(selectedGpxFile.getGpxFile().path);
GpxDataItem gpxDataItem = app.getGpxDbHelper().getItem(file);
gpxDataItem = app.getGpxDbHelper().getItem(file);
trackDrawInfo = new TrackDrawInfo(gpxDataItem);
}
}
@ -78,6 +113,14 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
return view;
}
@Override
protected void calculateLayout(View view, boolean initLayout) {
menuTitleHeight = view.findViewById(R.id.route_menu_top_shadow_all).getHeight()
+ view.findViewById(R.id.control_buttons).getHeight()
- view.findViewById(R.id.buttons_shadow).getHeight();
super.calculateLayout(view, initLayout);
}
@Override
public void onResume() {
super.onResume();
@ -136,6 +179,7 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
discardChanges();
dismiss();
}
});
@ -167,13 +211,57 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
app.getSelectedGpxHelper().updateSelectedGpxFile(selectedGpxFile);
GpxDataItem item = new GpxDataItem(new File(gpxFile.path), gpxFile);
app.getGpxDbHelper().add(item);
gpxDataItem = new GpxDataItem(new File(gpxFile.path), gpxFile);
app.getGpxDbHelper().add(gpxDataItem);
saveGpx(gpxFile);
}
private void saveGpx(GPXFile gpxFile) {
new SaveGpxAsyncTask(gpxFile, null).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
private void discardChanges() {
if (gpxDataItem.getSplitType() != trackDrawInfo.getSplitType() || gpxDataItem.getSplitInterval() != trackDrawInfo.getSplitInterval()) {
int timeSplit = (int) gpxDataItem.getSplitInterval();
double distanceSplit = gpxDataItem.getSplitInterval();
GpxSplitType splitType = null;
if (gpxDataItem.getSplitType() == GpxSplitType.DISTANCE.getType()) {
splitType = GpxSplitType.DISTANCE;
} else if (gpxDataItem.getSplitType() == GpxSplitType.TIME.getType()) {
splitType = GpxSplitType.TIME;
}
if (splitType != null) {
SplitTrackAsyncTask.SplitTrackListener splitTrackListener = new SplitTrackAsyncTask.SplitTrackListener() {
@Override
public void trackSplittingStarted() {
}
@Override
public void trackSplittingFinished() {
if (selectedGpxFile != null) {
List<GpxDisplayGroup> groups = getGpxDisplayGroups();
selectedGpxFile.setDisplayGroups(groups, app);
}
}
};
List<GpxDisplayGroup> groups = getGpxDisplayGroups();
new SplitTrackAsyncTask(app, splitType, groups, splitTrackListener, trackDrawInfo.isJoinSegments(),
timeSplit, distanceSplit).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}
private void saveGpx(final GPXFile gpxFile) {
new SaveGpxAsyncTask(gpxFile, new SaveGpxAsyncTask.SaveGpxListener() {
@Override
public void gpxSavingStarted() {
}
@Override
public void gpxSavingFinished() {
app.showShortToastMessage(R.string.shared_string_track_is_saved, Algorithms.getFileWithoutDirs(gpxFile.path));
}
}).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private void updateCards() {
@ -182,7 +270,7 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
ViewGroup cardsContainer = getCardsContainer();
cardsContainer.removeAllViews();
BaseCard splitIntervalCard = new SplitIntervalCard(mapActivity, trackDrawInfo);
BaseCard splitIntervalCard = new SplitIntervalCard(mapActivity, trackDrawInfo, this);
cardsContainer.addView(splitIntervalCard.build(mapActivity));
BaseCard arrowsCard = new DirectionArrowsCard(mapActivity, trackDrawInfo);
@ -196,36 +284,22 @@ public class TrackAppearanceFragment extends ContextMenuFragment {
}
}
@Override
public int getMainLayoutId() {
return R.layout.track_appearance;
}
private long modifiedTime = -1;
private List<GpxDisplayGroup> displayGroups;
@Override
public int getHeaderViewHeight() {
return menuTitleHeight;
}
@Override
protected void calculateLayout(View view, boolean initLayout) {
menuTitleHeight = view.findViewById(R.id.route_menu_top_shadow_all).getHeight()
+ view.findViewById(R.id.control_buttons).getHeight()
- view.findViewById(R.id.buttons_shadow).getHeight();
super.calculateLayout(view, initLayout);
}
@Override
public boolean isHeaderViewDetached() {
return false;
}
@Override
public int getToolbarHeight() {
return 0;
}
public float getMiddleStateKoef() {
return 0.5f;
public List<GpxDisplayGroup> getGpxDisplayGroups() {
GPXFile gpxFile = selectedGpxFile.getGpxFile();
if (gpxFile == null) {
return new ArrayList<>();
}
if (gpxFile.modifiedTime != modifiedTime) {
modifiedTime = gpxFile.modifiedTime;
displayGroups = app.getSelectedGpxHelper().collectDisplayGroups(gpxFile);
if (selectedGpxFile.getDisplayGroups(app) != null) {
displayGroups = selectedGpxFile.getDisplayGroups(app);
}
}
return displayGroups;
}
public static boolean showInstance(@NonNull MapActivity mapActivity, TrackAppearanceFragment fragment) {