Small refactoring

This commit is contained in:
Victor Shcherb 2018-04-16 00:50:22 +02:00
parent db0eee7290
commit a09a699ad1
15 changed files with 176 additions and 141 deletions

View file

@ -4,12 +4,12 @@ import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.data.FavouritePoint; import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.MapMarkersHelper.MapMarkersGroup;
import net.osmand.plus.api.SQLiteAPI.SQLiteConnection; import net.osmand.plus.api.SQLiteAPI.SQLiteConnection;
import net.osmand.plus.api.SQLiteAPI.SQLiteCursor; import net.osmand.plus.api.SQLiteAPI.SQLiteCursor;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
@ -111,17 +111,23 @@ public class FavouritesDbHelper {
private void runSyncWithMarkers(FavoriteGroup favGroup) { private void runSyncWithMarkers(FavoriteGroup favGroup) {
MapMarkersHelper helper = context.getMapMarkersHelper(); MapMarkersHelper helper = context.getMapMarkersHelper();
helper.runSynchronization(helper.getOrCreateGroup(favGroup)); MapMarkersGroup group = helper.getMarkersGroup(favGroup);
if(group != null) {
helper.runSynchronization(group);
}
} }
private void removeFromMarkers(FavoriteGroup favGroup) { private void removeFromMarkers(FavoriteGroup favGroup) {
MapMarkersHelper helper = context.getMapMarkersHelper(); MapMarkersHelper helper = context.getMapMarkersHelper();
helper.removeMarkersGroup(helper.getOrCreateGroup(favGroup)); MapMarkersGroup group = helper.getMarkersGroup(favGroup);
if(group != null) {
helper.removeMarkersGroup(group);
}
} }
private void addToMarkers(FavoriteGroup favGroup) { private void addToMarkers(FavoriteGroup favGroup) {
MapMarkersHelper helper = context.getMapMarkersHelper(); MapMarkersHelper helper = context.getMapMarkersHelper();
helper.addOrEnableGroup(helper.getOrCreateGroup(favGroup)); helper.addOrEnableGroup(favGroup);
} }
private File getInternalFile() { private File getInternalFile() {

View file

@ -5,7 +5,6 @@ import android.graphics.Matrix;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import net.osmand.IProgress; import net.osmand.IProgress;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.plus.GPXDatabase.GpxDataItem; import net.osmand.plus.GPXDatabase.GpxDataItem;
@ -15,6 +14,7 @@ import net.osmand.plus.GPXUtilities.Route;
import net.osmand.plus.GPXUtilities.Track; import net.osmand.plus.GPXUtilities.Track;
import net.osmand.plus.GPXUtilities.TrkSegment; import net.osmand.plus.GPXUtilities.TrkSegment;
import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.MapMarkersHelper.MapMarkersGroup;
import net.osmand.plus.OsmandSettings.MetricsConstants; import net.osmand.plus.OsmandSettings.MetricsConstants;
import net.osmand.plus.activities.SavingTrackHelper; import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.helpers.GpxUiHelper;
@ -499,7 +499,7 @@ public class GpxSelectionHelper {
} }
} }
if (syncGroup) { if (syncGroup) {
syncGpx(gpx); syncGpxWithMarkers(gpx);
} }
return sf; return sf;
} }
@ -516,30 +516,31 @@ public class GpxSelectionHelper {
public void clearPoints(GPXFile gpxFile) { public void clearPoints(GPXFile gpxFile) {
gpxFile.clearPoints(); gpxFile.clearPoints();
syncGpx(gpxFile); syncGpxWithMarkers(gpxFile);
} }
public void addPoint(WptPt point, GPXFile gpxFile) { public void addPoint(WptPt point, GPXFile gpxFile) {
gpxFile.addPoint(point); gpxFile.addPoint(point);
syncGpx(gpxFile); syncGpxWithMarkers(gpxFile);
} }
public void addPoints(Collection<? extends WptPt> collection, GPXFile gpxFile) { public void addPoints(Collection<? extends WptPt> collection, GPXFile gpxFile) {
gpxFile.addPoints(collection); gpxFile.addPoints(collection);
syncGpx(gpxFile); syncGpxWithMarkers(gpxFile);
} }
public boolean removePoint(WptPt point, GPXFile gpxFile) { public boolean removePoint(WptPt point, GPXFile gpxFile) {
boolean res = gpxFile.deleteWptPt(point); boolean res = gpxFile.deleteWptPt(point);
syncGpx(gpxFile); syncGpxWithMarkers(gpxFile);
return res; return res;
} }
private void syncGpx(GPXFile gpxFile) { private void syncGpxWithMarkers(GPXFile gpxFile) {
File gpx = new File(gpxFile.path); File gpx = new File(gpxFile.path);
if (gpx.exists()) {
MapMarkersHelper mapMarkersHelper = app.getMapMarkersHelper(); MapMarkersHelper mapMarkersHelper = app.getMapMarkersHelper();
mapMarkersHelper.runSynchronization(mapMarkersHelper.getOrCreateGroup(gpx)); MapMarkersGroup group = mapMarkersHelper.getMarkersGroup(gpxFile);
if (group != null) {
mapMarkersHelper.runSynchronization(group);
} }
} }

View file

@ -285,42 +285,77 @@ public class MapMarkersHelper {
}); });
} }
public boolean isGroupSynced(String id) {
return getMapMarkerGroupById(id) != null;
}
public void runSynchronization(@NonNull MapMarkersGroup group) { public void runSynchronization(final @NonNull MapMarkersGroup group) {
runSynchronization(group, false);
}
public void runSynchronization(@NonNull final MapMarkersGroup group, final boolean loadGpx) {
ctx.runInUIThread(new Runnable() { ctx.runInUIThread(new Runnable() {
@Override @Override
public void run() { public void run() {
new SyncGroupTask(group, loadGpx).executeOnExecutor(executorService); new SyncGroupTask(group).executeOnExecutor(executorService);
} }
}); });
} }
public MapMarkersGroup getMarkersGroup(SelectedGpxFile gpxFile) {
if(gpxFile.getGpxFile() == null) { public MapMarkersGroup getMarkersGroup(GPXFile gpx) {
if(gpx == null || gpx.path == null) {
return null; return null;
} }
return getMapMarkerGroupById(getMarkerGroupId(new File(gpxFile.getGpxFile().path))); return getMapMarkerGroupById(getMarkerGroupId(new File(gpx.path)), MapMarkersGroup.GPX_TYPE);
}
public MapMarkersGroup getMarkersGroup(FavoriteGroup favGroup) {
return getMapMarkerGroupById(getMarkerGroupId(favGroup), MapMarkersGroup.FAVORITES_TYPE);
} }
public void addOrEnableGroup(@NonNull MapMarkersGroup group) { public MapMarkersGroup addOrEnableGpxGroup(@NonNull File file) {
if (!isGroupSynced(group.getId())) { MapMarkersGroup gr = getMapMarkerGroupById(getMarkerGroupId(file), MapMarkersGroup.GPX_TYPE);
markersDbHelper.addGroup(group); if (gr == null) {
addHistoryMarkersToGroup(group); gr = createGPXMarkerGroup(file);
addToGroupsList(group); addGroupInternally(gr);
} else if (group.isDisabled()) {
updateGroupDisabled(group, false);
} }
runSynchronization(group, true); enableGroup(gr);
return gr;
} }
public MapMarkersGroup addOrEnableGroup(@NonNull GPXFile file) {
MapMarkersGroup gr = getMarkersGroup(file);
if (gr == null) {
gr = createGPXMarkerGroup(new File(file.path));
addGroupInternally(gr);
}
enableGroup(gr);
return gr;
}
public MapMarkersGroup addOrEnableGroup(@NonNull FavoriteGroup group) {
MapMarkersGroup gr = getMarkersGroup(group);
if (gr == null) {
gr = createFavMarkerGroup(group);
addGroupInternally(gr);
}
enableGroup(gr);
return gr;
}
public void enableGroup(@NonNull MapMarkersGroup gr) {
if (gr.isDisabled()) {
updateGroupDisabled(gr, false);
}
runSynchronization(gr);
}
private void addGroupInternally(MapMarkersGroup gr) {
markersDbHelper.addGroup(gr);
addHistoryMarkersToGroup(gr);
addToGroupsList(gr);
}
private void addHistoryMarkersToGroup(@NonNull MapMarkersGroup group) { private void addHistoryMarkersToGroup(@NonNull MapMarkersGroup group) {
List<MapMarker> historyMarkers = new ArrayList<>(mapMarkersHistory); List<MapMarker> historyMarkers = new ArrayList<>(mapMarkersHistory);
for (MapMarker m : historyMarkers) { for (MapMarker m : historyMarkers) {
@ -350,7 +385,7 @@ public class MapMarkersHelper {
String id = group.getId(); String id = group.getId();
if (id != null) { if (id != null) {
group.wptCategories = wptCategories; group.wptCategories = wptCategories;
if (wptCategories != null && isGroupSynced(id)) { if (wptCategories != null) {
markersDbHelper.updateGroupCategories(id, group.getWptCategoriesString()); markersDbHelper.updateGroupCategories(id, group.getWptCategoriesString());
} }
} }
@ -401,7 +436,7 @@ public class MapMarkersHelper {
private void addMarkerToGroup(MapMarker marker) { private void addMarkerToGroup(MapMarker marker) {
if (marker != null) { if (marker != null) {
MapMarkersGroup mapMarkersGroup = getMapMarkerGroupById(marker.groupKey); MapMarkersGroup mapMarkersGroup = getMapMarkerGroupById(marker.groupKey, marker.getType());
if (mapMarkersGroup != null) { if (mapMarkersGroup != null) {
mapMarkersGroup.getMarkers().add(marker); mapMarkersGroup.getMarkers().add(marker);
updateGroup(mapMarkersGroup); updateGroup(mapMarkersGroup);
@ -434,7 +469,7 @@ public class MapMarkersHelper {
private void removeMarkerFromGroup(MapMarker marker) { private void removeMarkerFromGroup(MapMarker marker) {
if (marker != null) { if (marker != null) {
MapMarkersGroup mapMarkersGroup = getMapMarkerGroupById(marker.groupKey); MapMarkersGroup mapMarkersGroup = getMapMarkerGroupById(marker.groupKey, marker.getType());
if (mapMarkersGroup != null) { if (mapMarkersGroup != null) {
mapMarkersGroup.getMarkers().remove(marker); mapMarkersGroup.getMarkers().remove(marker);
updateGroup(mapMarkersGroup); updateGroup(mapMarkersGroup);
@ -456,45 +491,44 @@ public class MapMarkersHelper {
} }
@Nullable @Nullable
public MapMarkersGroup getMapMarkerGroupById(String id) { public MapMarkersGroup getMapMarkerGroupById(String id, int type) {
for (MapMarkersGroup group : mapMarkersGroups) { for (MapMarkersGroup group : mapMarkersGroups) {
if ((id == null && group.getId() == null) if ((id == null && group.getId() == null)
|| (group.getId() != null && group.getId().equals(id))) { || (group.getId() != null && group.getId().equals(id))) {
if(type == MapMarkersGroup.ANY_TYPE || type == group.type) {
return group; return group;
} }
} }
}
return null; return null;
} }
public MapMarkersGroup getOrCreateGroup(@NonNull FavoriteGroup favGroup) { private MapMarkersGroup createGPXMarkerGroup(File fl) {
MapMarkersGroup group = getMapMarkerGroupById(favGroup.name); return new MapMarkersGroup(getMarkerGroupId(fl),
if (group == null) { AndroidUtils.trimExtension(fl.getName()),
group = new MapMarkersGroup(favGroup.name, favGroup.name, MapMarkersGroup.FAVORITES_TYPE);
}
return group;
}
public MapMarkersGroup getOrCreateGroup(@NonNull File gpx) {
MapMarkersGroup group = getMapMarkerGroupById(getMarkerGroupId(gpx));
if (group == null) {
group = new MapMarkersGroup(getMarkerGroupId(gpx),
AndroidUtils.trimExtension(gpx.getName()),
MapMarkersGroup.GPX_TYPE); MapMarkersGroup.GPX_TYPE);
} }
return group;
private MapMarkersGroup createFavMarkerGroup(FavoriteGroup favGroup) {
return new MapMarkersGroup(favGroup.name, favGroup.name, MapMarkersGroup.FAVORITES_TYPE);
} }
private String getMarkerGroupId(File gpx) { private String getMarkerGroupId(File gpx) {
return gpx.getAbsolutePath(); return gpx.getAbsolutePath();
} }
private String getMarkerGroupId(FavoriteGroup group) {
return group.name;
}
@NonNull @NonNull
public List<MapMarkersGroup> getGroupsForDisplayedGpx() { public List<MapMarkersGroup> getGroupsForDisplayedGpx() {
List<MapMarkersGroup> res = new ArrayList<>(); List<MapMarkersGroup> res = new ArrayList<>();
List<SelectedGpxFile> selectedGpxFiles = ctx.getSelectedGpxHelper().getSelectedGPXFiles(); List<SelectedGpxFile> selectedGpxFiles = ctx.getSelectedGpxHelper().getSelectedGPXFiles();
for (SelectedGpxFile selected : selectedGpxFiles) { for (SelectedGpxFile selected : selectedGpxFiles) {
MapMarkersGroup group = getOrCreateGroup(new File(selected.getGpxFile().path)); MapMarkersGroup search = getMarkersGroup(selected.getGpxFile());
if (!isGroupSynced(group.getId())) { if (search == null && selected.getGpxFile() != null && selected.getGpxFile().path != null) {
MapMarkersGroup group = createGPXMarkerGroup(new File(selected.getGpxFile().path));
group.disabled = true; group.disabled = true;
createHeaderInGroup(group); createHeaderInGroup(group);
res.add(group); res.add(group);
@ -512,9 +546,10 @@ public class MapMarkersHelper {
for (TravelArticle art : savedArticles) { for (TravelArticle art : savedArticles) {
String gpxName = travelDbHelper.getGPXName(art); String gpxName = travelDbHelper.getGPXName(art);
File path = ctx.getAppPath(IndexConstants.GPX_TRAVEL_DIR + gpxName); File path = ctx.getAppPath(IndexConstants.GPX_TRAVEL_DIR + gpxName);
LOG.debug("Article group " + getMarkerGroupId(path) + " " + path.exists()) ; LOG.debug("Article group " + path.getAbsolutePath() + " " + path.exists()) ;
MapMarkersGroup group = getOrCreateGroup(path); MapMarkersGroup search = getMapMarkerGroupById(getMarkerGroupId(path), MapMarkersGroup.GPX_TYPE);
if (!isGroupSynced(group.getId())) { if (search == null) {
MapMarkersGroup group = createGPXMarkerGroup(path);
group.disabled = true; group.disabled = true;
createHeaderInGroup(group); createHeaderInGroup(group);
res.add(group); res.add(group);
@ -971,7 +1006,7 @@ public class MapMarkersHelper {
file.addPoint(wpt); file.addPoint(wpt);
} }
GPXUtilities.writeGpxFile(fout, file, ctx); GPXUtilities.writeGpxFile(fout, file, ctx);
return getMarkerGroupId(fout); return fout.getAbsolutePath();
} }
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
@ -1070,11 +1105,9 @@ public class MapMarkersHelper {
private class SyncGroupTask extends AsyncTask<Void, Void, Void> { private class SyncGroupTask extends AsyncTask<Void, Void, Void> {
private MapMarkersGroup group; private MapMarkersGroup group;
private boolean loadGpx;
SyncGroupTask(MapMarkersGroup group, boolean loadGpx) { SyncGroupTask(MapMarkersGroup group) {
this.group = group; this.group = group;
this.loadGpx = loadGpx;
} }
@Override @Override
@ -1099,12 +1132,7 @@ public class MapMarkersHelper {
// TODO extract method from Asynctask to Helper directly // TODO extract method from Asynctask to Helper directly
private void runGroupSynchronization() { private void runGroupSynchronization() {
if (!isGroupSynced(group.getId())) {
return;
}
List<MapMarker> groupMarkers = new ArrayList<>(group.getMarkers()); List<MapMarker> groupMarkers = new ArrayList<>(group.getMarkers());
if (group.getType() == MapMarkersGroup.FAVORITES_TYPE) { if (group.getType() == MapMarkersGroup.FAVORITES_TYPE) {
FavoriteGroup favGroup = ctx.getFavorites().getGroup(group.getName()); FavoriteGroup favGroup = ctx.getFavorites().getGroup(group.getName());
if (favGroup == null) { if (favGroup == null) {
@ -1131,10 +1159,6 @@ public class MapMarkersHelper {
String gpxPath = group.getId(); String gpxPath = group.getId();
SelectedGpxFile selectedGpxFile = gpxHelper.getSelectedFileByPath(gpxPath); SelectedGpxFile selectedGpxFile = gpxHelper.getSelectedFileByPath(gpxPath);
GPXFile gpx = selectedGpxFile == null ? null : selectedGpxFile.getGpxFile(); GPXFile gpx = selectedGpxFile == null ? null : selectedGpxFile.getGpxFile();
if (gpx == null && loadGpx) {
gpx = GPXUtilities.loadGPXFile(ctx, new File(gpxPath));
gpxHelper.selectGpxFile(gpx, true, false, false);
}
group.visible = gpx != null || group.visibleUntilRestart; group.visible = gpx != null || group.visibleUntilRestart;
if (gpx == null || group.isDisabled()) { if (gpx == null || group.isDisabled()) {
removeGroupActiveMarkers(group, true); removeGroupActiveMarkers(group, true);
@ -1171,6 +1195,7 @@ public class MapMarkersHelper {
public static class MapMarkersGroup { public static class MapMarkersGroup {
public static final int ANY_TYPE = -1;
public static final int FAVORITES_TYPE = 0; public static final int FAVORITES_TYPE = 0;
public static final int GPX_TYPE = 1; public static final int GPX_TYPE = 1;
@ -1203,6 +1228,10 @@ public class MapMarkersHelper {
return id; return id;
} }
public String getGpxPath() {
return id;
}
public String getName() { public String getName() {
return name; return name;
} }
@ -1318,6 +1347,12 @@ public class MapMarkersHelper {
this.index = index; this.index = index;
} }
public int getType() {
return favouritePoint == null ?
(wptPt == null ? MapMarkersGroup.ANY_TYPE : MapMarkersGroup.GPX_TYPE) :
MapMarkersGroup.FAVORITES_TYPE;
}
public PointDescription getPointDescription(Context ctx) { public PointDescription getPointDescription(Context ctx) {
return new PointDescription(POINT_TYPE_MAP_MARKER, ctx.getString(R.string.map_marker), getOnlyName()); return new PointDescription(POINT_TYPE_MAP_MARKER, ctx.getString(R.string.map_marker), getOnlyName());
} }
@ -1405,4 +1440,7 @@ public class MapMarkersHelper {
return (colorIndex >= 0 && colorIndex < colorsIds.length) ? colorsIds[colorIndex] : colorsIds[0]; return (colorIndex >= 0 && colorIndex < colorsIds.length) ? colorsIds[colorIndex] : colorsIds[0];
} }
} }
} }

View file

@ -158,8 +158,9 @@ public class EditFavoriteGroupDialogFragment extends MenuBottomSheetDialogFragme
items.add(new DividerHalfItem(getContext())); items.add(new DividerHalfItem(getContext()));
final MapMarkersHelper markersHelper = app.getMapMarkersHelper(); final MapMarkersHelper markersHelper = app.getMapMarkersHelper();
final MapMarkersGroup markersGr = markersHelper.getOrCreateGroup(this.group); final FavoriteGroup favGroup = this.group;
final boolean synced = markersHelper.isGroupSynced(markersGr.getId()); final MapMarkersGroup markersGr = markersHelper.getMarkersGroup(this.group);
final boolean synced = markersGr != null;
BaseBottomSheetItem markersGroupItem = new SimpleBottomSheetItem.Builder() BaseBottomSheetItem markersGroupItem = new SimpleBottomSheetItem.Builder()
.setIcon(getContentIcon(synced ? R.drawable.ic_action_delete_dark : R.drawable.ic_action_flag_dark)) .setIcon(getContentIcon(synced ? R.drawable.ic_action_delete_dark : R.drawable.ic_action_flag_dark))
@ -171,7 +172,7 @@ public class EditFavoriteGroupDialogFragment extends MenuBottomSheetDialogFragme
if (synced) { if (synced) {
markersHelper.removeMarkersGroup(markersGr); markersHelper.removeMarkersGroup(markersGr);
} else { } else {
markersHelper.addOrEnableGroup(markersGr); markersHelper.addOrEnableGroup(favGroup);
} }
dismiss(); dismiss();
MapActivity.launchMapActivityMoveToTop(getActivity()); MapActivity.launchMapActivityMoveToTop(getActivity());

View file

@ -421,15 +421,14 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment {
List<PointDescription> names = new ArrayList<>(); List<PointDescription> names = new ArrayList<>();
for (Map.Entry<String, Set<FavouritePoint>> entry : favoritesSelected.entrySet()) { for (Map.Entry<String, Set<FavouritePoint>> entry : favoritesSelected.entrySet()) {
FavoriteGroup favGr = helper.getGroup(entry.getKey()); FavoriteGroup favGr = helper.getGroup(entry.getKey());
MapMarkersGroup markersGr = markersHelper.getOrCreateGroup(favGr);
if (entry.getValue().size() == favGr.points.size()) { if (entry.getValue().size() == favGr.points.size()) {
markersHelper.addOrEnableGroup(markersGr); markersHelper.addOrEnableGroup(favGr);
} else { } else {
for (FavouritePoint fp : entry.getValue()) { for (FavouritePoint fp : entry.getValue()) {
points.add(new LatLon(fp.getLatitude(), fp.getLongitude())); points.add(new LatLon(fp.getLatitude(), fp.getLongitude()));
names.add(new PointDescription(PointDescription.POINT_TYPE_MAP_MARKER, fp.getName())); names.add(new PointDescription(PointDescription.POINT_TYPE_MAP_MARKER, fp.getName()));
} }
markersHelper.addMapMarkers(points, names, markersGr); markersHelper.addMapMarkers(points, names, null);
points.clear(); points.clear();
names.clear(); names.clear();
} }

View file

@ -9,7 +9,6 @@ import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.view.View; import android.view.View;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.plus.FavouritesDbHelper.FavoriteGroup; import net.osmand.plus.FavouritesDbHelper.FavoriteGroup;
import net.osmand.plus.GPXUtilities; import net.osmand.plus.GPXUtilities;
@ -17,6 +16,7 @@ import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.GpxSelectionHelper; import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarkersGroup;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
@ -177,10 +177,10 @@ public class WptPtEditorFragment extends PointEditorFragment {
} }
private void syncGpx(GPXFile gpxFile) { private void syncGpx(GPXFile gpxFile) {
File gpx = new File(gpxFile.path);
if (gpx.exists()) {
MapMarkersHelper helper = getMyApplication().getMapMarkersHelper(); MapMarkersHelper helper = getMyApplication().getMapMarkersHelper();
helper.runSynchronization(helper.getOrCreateGroup(gpx)); MapMarkersGroup group = helper.getMarkersGroup(gpxFile);
if (group != null) {
helper.runSynchronization(group);
} }
} }

View file

@ -28,6 +28,7 @@ public class AddFavouritesGroupBottomSheetDialogFragment extends AddGroupBottomS
if (!group.visible) { if (!group.visible) {
favouritesDbHelper.editFavouriteGroup(group, group.name, group.color, true); favouritesDbHelper.editFavouriteGroup(group, group.name, group.color, true);
} }
addAndSyncGroup(getMyApplication().getMapMarkersHelper().getOrCreateGroup(group)); getMyApplication().getMapMarkersHelper().addOrEnableGroup(group);
dismiss();
} }
} }

View file

@ -57,10 +57,6 @@ public abstract class AddGroupBottomSheetDialogFragment extends MenuBottomSheetD
return false; return false;
} }
protected void addAndSyncGroup(MapMarkersGroup group) {
getMyApplication().getMapMarkersHelper().addOrEnableGroup(group);
dismiss();
}
protected abstract GroupsAdapter createAdapter(); protected abstract GroupsAdapter createAdapter();

View file

@ -69,21 +69,13 @@ public class AddTracksGroupBottomSheetDialogFragment extends AddGroupBottomSheet
fragment.setUsedOnMap(false); fragment.setUsedOnMap(false);
fragment.show(getParentFragment().getChildFragmentManager(), SelectWptCategoriesBottomSheetDialogFragment.TAG); fragment.show(getParentFragment().getChildFragmentManager(), SelectWptCategoriesBottomSheetDialogFragment.TAG);
dismiss(); dismiss();
} else { } else if(dataItem.getFile() != null) {
addAndSyncGroup(createMapMarkersSyncGroup(getMyApplication(), dataItem)); getMyApplication().getMapMarkersHelper().addOrEnableGpxGroup(dataItem.getFile());
dismiss();
} }
} }
private MapMarkersGroup createMapMarkersSyncGroup(OsmandApplication app, GpxDataItem gpxDataItem) {
GpxSelectionHelper gpxSelectionHelper = app.getSelectedGpxHelper();
File gpx = gpxDataItem.getFile();
SelectedGpxFile selectedGpxFile = gpxSelectionHelper.getSelectedFileByPath(gpx.getAbsolutePath());
if (selectedGpxFile == null) {
GPXFile res = GPXUtilities.loadGPXFile(app, gpx);
gpxSelectionHelper.selectGpxFile(res, true, false);
}
return getMyApplication().getMapMarkersHelper().getOrCreateGroup(gpx);
}
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
public class ProcessGpxTask extends AsyncTask<Void, GpxDataItem, Void> { public class ProcessGpxTask extends AsyncTask<Void, GpxDataItem, Void> {

View file

@ -121,11 +121,11 @@ public class SelectWptCategoriesBottomSheetDialogFragment extends MenuBottomShee
if (selectedGpxFile == null) { if (selectedGpxFile == null) {
gpxSelectionHelper.selectGpxFile(gpxFile, true, false); gpxSelectionHelper.selectGpxFile(gpxFile, true, false);
} }
MapMarkersGroup group = mapMarkersHelper.getMarkersGroup(gpxFile);
MapMarkersGroup markersGr = mapMarkersHelper.getOrCreateGroup(new File(gpxFile.path)); if(group == null) {
mapMarkersHelper.updateGroupWptCategories(markersGr, selectedCategories); group = mapMarkersHelper.addOrEnableGroup(gpxFile);
}
mapMarkersHelper.addOrEnableGroup(markersGr); mapMarkersHelper.updateGroupWptCategories(group, selectedCategories);
dismiss(); dismiss();
} }

View file

@ -9,12 +9,12 @@ import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.plus.IconsCache; import net.osmand.plus.IconsCache;
import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarker; import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.MapMarkersHelper.MapMarkersGroup;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.dashboard.DashLocationFragment; import net.osmand.plus.dashboard.DashLocationFragment;
@ -239,7 +239,8 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
final int pos = holder.getAdapterPosition(); final int pos = holder.getAdapterPosition();
final MapMarker marker = getItem(pos); final MapMarker marker = getItem(pos);
mapActivity.getMyApplication().getMapMarkersHelper().moveMapMarkerToHistory(marker); mapActivity.getMyApplication().getMapMarkersHelper().moveMapMarkerToHistory(marker);
MapMarkersHelper.MapMarkersGroup group = mapActivity.getMyApplication().getMapMarkersHelper().getMapMarkerGroupById(marker.groupKey); MapMarkersHelper.MapMarkersGroup group = mapActivity.getMyApplication().getMapMarkersHelper().getMapMarkerGroupById(marker.groupKey,
MapMarkersGroup.ANY_TYPE);
if (group != null) { if (group != null) {
mapActivity.getMyApplication().getMapMarkersHelper().updateGroup(group); mapActivity.getMyApplication().getMapMarkersHelper().updateGroup(group);
} }

View file

@ -9,12 +9,12 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.ImageView; import android.widget.ImageView;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GpxSelectionHelper; import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.GPXUtilities;
import net.osmand.plus.IconsCache; import net.osmand.plus.IconsCache;
import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.GroupHeader; import net.osmand.plus.MapMarkersHelper.GroupHeader;
@ -26,6 +26,7 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.dashboard.DashLocationFragment; import net.osmand.plus.dashboard.DashLocationFragment;
import java.io.File;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
@ -174,7 +175,7 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
public int getGroupHeaderPosition(String groupId) { public int getGroupHeaderPosition(String groupId) {
int pos = -1; int pos = -1;
MapMarkersGroup group = app.getMapMarkersHelper().getMapMarkerGroupById(groupId); MapMarkersGroup group = app.getMapMarkersHelper().getMapMarkerGroupById(groupId, MapMarkersGroup.ANY_TYPE);
if (group != null) { if (group != null) {
pos = items.indexOf(group.getGroupHeader()); pos = items.indexOf(group.getGroupHeader());
} }
@ -398,30 +399,27 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
final MapMarkersHelper mapMarkersHelper = app.getMapMarkersHelper(); final MapMarkersHelper mapMarkersHelper = app.getMapMarkersHelper();
final GPXFile[] gpxFile = new GPXFile[1]; final GPXFile[] gpxFile = new GPXFile[1];
boolean disabled = !enabled; boolean disabled = !enabled;
boolean synced = false;
mapMarkersHelper.updateGroupDisabled(group, disabled); mapMarkersHelper.updateGroupDisabled(group, disabled);
if (group.getType() == MapMarkersGroup.GPX_TYPE) { if (group.getType() == MapMarkersGroup.GPX_TYPE) {
group.setVisibleUntilRestart(disabled); group.setVisibleUntilRestart(disabled);
String gpxPath = group.getId(); String gpxPath = group.getGpxPath();
if (disabled) {
SelectedGpxFile selectedGpxFile = app.getSelectedGpxHelper().getSelectedFileByPath(gpxPath); SelectedGpxFile selectedGpxFile = app.getSelectedGpxHelper().getSelectedFileByPath(gpxPath);
if (selectedGpxFile != null) {
gpxFile[0] = selectedGpxFile.getGpxFile(); gpxFile[0] = selectedGpxFile.getGpxFile();
if (disabled) {
if (selectedGpxFile != null) {
switchGpxVisibility(gpxFile[0], false); switchGpxVisibility(gpxFile[0], false);
} }
} else if (mapMarkersHelper.isGroupSynced(group.getId())) {
mapMarkersHelper.runSynchronization(group, true);
synced = true;
}
}
if (!synced) {
if (mapMarkersHelper.isGroupSynced(group.getId())) {
mapMarkersHelper.runSynchronization(group);
} else { } else {
mapMarkersHelper.addOrEnableGroup(group); if (selectedGpxFile == null) {
// TODO IO load in another thread ?
gpxFile[0] = GPXUtilities.loadGPXFile(app, new File(gpxPath));
}
switchGpxVisibility(gpxFile[0], true);
} }
} }
mapMarkersHelper.runSynchronization(group);
if (disabled) { if (disabled) {
snackbar = Snackbar.make(holder.itemView, app.getString(R.string.group_will_be_removed_after_restart), Snackbar.LENGTH_LONG) snackbar = Snackbar.make(holder.itemView, app.getString(R.string.group_will_be_removed_after_restart), Snackbar.LENGTH_LONG)
@ -431,7 +429,7 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
if (group.getType() == MapMarkersGroup.GPX_TYPE && gpxFile[0] != null) { if (group.getType() == MapMarkersGroup.GPX_TYPE && gpxFile[0] != null) {
switchGpxVisibility(gpxFile[0], true); switchGpxVisibility(gpxFile[0], true);
} }
mapMarkersHelper.addOrEnableGroup(group); mapMarkersHelper.enableGroup(group);
} }
}); });
AndroidUtils.setSnackbarTextColor(snackbar, R.color.color_dialog_buttons_dark); AndroidUtils.setSnackbarTextColor(snackbar, R.color.color_dialog_buttons_dark);

View file

@ -455,8 +455,7 @@ public class TrackPointFragment extends OsmandExpandableListFragment {
R.drawable.ic_action_gshare_dark, MenuItemCompat.SHOW_AS_ACTION_NEVER); R.drawable.ic_action_gshare_dark, MenuItemCompat.SHOW_AS_ACTION_NEVER);
if (getGpx().path != null) { if (getGpx().path != null) {
final MapMarkersHelper markersHelper = app.getMapMarkersHelper(); final MapMarkersHelper markersHelper = app.getMapMarkersHelper();
final MapMarkersGroup markersGr = markersHelper.getOrCreateGroup(new File(getGpx().path)); final boolean synced = markersHelper.getMarkersGroup(getGpx()) != null;
final boolean synced = markersHelper.isGroupSynced(markersGr.getId());
createMenuItem(menu, SELECT_MAP_MARKERS_ID, synced ? R.string.remove_from_map_markers createMenuItem(menu, SELECT_MAP_MARKERS_ID, synced ? R.string.remove_from_map_markers
: R.string.shared_string_add_to_map_markers, R.drawable.ic_action_flag_dark, : R.string.shared_string_add_to_map_markers, R.drawable.ic_action_flag_dark,
R.drawable.ic_action_flag_dark, MenuItemCompat.SHOW_AS_ACTION_NEVER); R.drawable.ic_action_flag_dark, MenuItemCompat.SHOW_AS_ACTION_NEVER);
@ -594,22 +593,25 @@ public class TrackPointFragment extends OsmandExpandableListFragment {
} }
private void syncGpx(GPXFile gpxFile) { private void syncGpx(GPXFile gpxFile) {
File gpx = new File(gpxFile.path);
if (gpx.exists()) {
MapMarkersHelper helper = app.getMapMarkersHelper(); MapMarkersHelper helper = app.getMapMarkersHelper();
helper.runSynchronization(helper.getOrCreateGroup(gpx)); MapMarkersGroup group = helper.getMarkersGroup(gpxFile);
if (group != null) {
helper.runSynchronization(group);
} }
} }
private void addOrRemoveMapMarkersSyncGroup() { private void addOrRemoveMapMarkersSyncGroup() {
final MapMarkersHelper markersHelper = app.getMapMarkersHelper(); final MapMarkersHelper markersHelper = app.getMapMarkersHelper();
final MapMarkersGroup markersGr = markersHelper.getOrCreateGroup(getGpxDataItem().getFile());
final boolean synced = markersHelper.isGroupSynced(markersGr.getId()); MapMarkersGroup markersSearch = markersHelper.getMarkersGroup(getGpx());
if (synced) { final MapMarkersGroup markersGr;
if (markersSearch != null) {
markersGr = markersSearch;
markersHelper.removeMarkersGroup(markersGr); markersHelper.removeMarkersGroup(markersGr);
} else { } else {
markersHelper.addOrEnableGroup(markersGr); markersGr = markersHelper.addOrEnableGroup(getGpx());
} }
final boolean synced = markersGr != null;
getActionBarActivity().invalidateOptionsMenu(); getActionBarActivity().invalidateOptionsMenu();
GPXFile gpxFile = getTrackActivity().getGpx(); GPXFile gpxFile = getTrackActivity().getGpx();
if (gpxFile != null) { if (gpxFile != null) {
@ -624,7 +626,7 @@ public class TrackPointFragment extends OsmandExpandableListFragment {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (synced) { if (synced) {
markersHelper.addOrEnableGroup(markersGr); markersHelper.removeMarkersGroup(markersGr);
getActionBarActivity().invalidateOptionsMenu(); getActionBarActivity().invalidateOptionsMenu();
} else { } else {
Bundle args = new Bundle(); Bundle args = new Bundle();

View file

@ -143,7 +143,7 @@ public class FavouritesLayer extends OsmandMapLayer implements ContextMenuLayer.
} }
for (FavouritePoint o : fullObjects) { for (FavouritePoint o : fullObjects) {
if (o != contextMenuLayer.getMoveableObject()) { if (o != contextMenuLayer.getMoveableObject()) {
MapMarker mapMarker = mapMarkersHelper.getMapMarker(objectInMotion); MapMarker mapMarker = mapMarkersHelper.getMapMarker(o);
drawPoint(canvas, tileBox, latLonBounds, o, mapMarker); drawPoint(canvas, tileBox, latLonBounds, o, mapMarker);
} }
} }

View file

@ -19,7 +19,6 @@ import android.support.annotation.ColorInt;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.PointDescription; import net.osmand.data.PointDescription;
import net.osmand.data.QuadRect; import net.osmand.data.QuadRect;
@ -35,6 +34,7 @@ import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarkersGroup;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings.CommonPreference; import net.osmand.plus.OsmandSettings.CommonPreference;
import net.osmand.plus.R; import net.osmand.plus.R;
@ -532,7 +532,7 @@ public class GPXLayer extends OsmandMapLayer implements ContextMenuLayer.IContex
} }
private boolean isSyncedWithMarkers(SelectedGpxFile g) { private boolean isSyncedWithMarkers(SelectedGpxFile g) {
return mapMarkersHelper.getMarkersGroup(g) != null; return mapMarkersHelper.getMarkersGroup(g.getGpxFile()) != null;
} }
private boolean calculateBelongs(int ex, int ey, int objx, int objy, int radius) { private boolean calculateBelongs(int ex, int ey, int objx, int objy, int radius) {
@ -662,9 +662,9 @@ public class GPXLayer extends OsmandMapLayer implements ContextMenuLayer.IContex
} }
private void syncGpx(GPXFile gpxFile) { private void syncGpx(GPXFile gpxFile) {
File gpx = new File(gpxFile.path); MapMarkersGroup group = view.getApplication().getMapMarkersHelper().getMarkersGroup(gpxFile);
if (gpx.exists()) { if (group != null) {
mapMarkersHelper.runSynchronization(view.getApplication().getMapMarkersHelper().getOrCreateGroup(gpx)); mapMarkersHelper.runSynchronization(group);
} }
} }