Refactoring of markers syncing in progress

This commit is contained in:
Alexander Sytnyk 2018-03-17 17:36:16 +02:00
parent cdcab890e7
commit 49fb85b725
6 changed files with 83 additions and 119 deletions

View file

@ -10,7 +10,6 @@ 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.MarkersSyncGroup;
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;
@ -110,6 +109,17 @@ public class FavouritesDbHelper {
return changed; return changed;
} }
private void syncWithMarkers(FavoriteGroup favGroup) {
context.getMapMarkersHelper().syncGroupAsync(MapMarkersHelper.createGroup(favGroup));
}
private void removeFromMarkers(FavoriteGroup favGroup) {
context.getMapMarkersHelper().removeMarkersSyncGroup(MapMarkersHelper.createGroup(favGroup).getId());
}
private void addToMarkers(FavoriteGroup favGroup) {
context.getMapMarkersHelper().addMarkersSyncGroup(MapMarkersHelper.createGroup(favGroup));
}
private File getInternalFile() { private File getInternalFile() {
return context.getFileStreamPath(FILE_TO_BACKUP); return context.getFileStreamPath(FILE_TO_BACKUP);
@ -127,7 +137,7 @@ public class FavouritesDbHelper {
cachedFavoritePoints.remove(p); cachedFavoritePoints.remove(p);
} }
for (FavoriteGroup gr : groupsToSync) { for (FavoriteGroup gr : groupsToSync) {
context.getMapMarkersHelper().syncGroupAsync(new MarkersSyncGroup(gr.name, gr.name, MarkersSyncGroup.FAVORITES_TYPE)); syncWithMarkers(gr);
} }
} }
if (groupsToDelete != null) { if (groupsToDelete != null) {
@ -135,7 +145,7 @@ public class FavouritesDbHelper {
flatGroups.remove(g.name); flatGroups.remove(g.name);
favoriteGroups.remove(g); favoriteGroups.remove(g);
cachedFavoritePoints.removeAll(g.points); cachedFavoritePoints.removeAll(g.points);
context.getMapMarkersHelper().removeMarkersSyncGroup(g.name); removeFromMarkers(g);
} }
} }
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
@ -150,7 +160,7 @@ public class FavouritesDbHelper {
FavoriteGroup group = flatGroups.get(p.getCategory()); FavoriteGroup group = flatGroups.get(p.getCategory());
if (group != null) { if (group != null) {
group.points.remove(p); group.points.remove(p);
context.getMapMarkersHelper().syncGroupAsync(new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE)); syncWithMarkers(group);
} }
cachedFavoritePoints.remove(p); cachedFavoritePoints.remove(p);
} }
@ -180,7 +190,7 @@ public class FavouritesDbHelper {
sortAll(); sortAll();
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
} }
context.getMapMarkersHelper().syncGroupAsync(new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE)); syncWithMarkers(group);
return true; return true;
} }
@ -275,7 +285,7 @@ public class FavouritesDbHelper {
} }
sortAll(); sortAll();
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
context.getMapMarkersHelper().syncGroupAsync(new MarkersSyncGroup(category, category, MarkersSyncGroup.FAVORITES_TYPE)); syncWithMarkers(getOrCreateGroup(p, 0));
return true; return true;
} }
@ -283,7 +293,7 @@ public class FavouritesDbHelper {
p.setLatitude(lat); p.setLatitude(lat);
p.setLongitude(lon); p.setLongitude(lon);
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
context.getMapMarkersHelper().syncGroupAsync(new MarkersSyncGroup(p.getCategory(), p.getCategory(), MarkersSyncGroup.FAVORITES_TYPE)); syncWithMarkers(getOrCreateGroup(p, 0));
return true; return true;
} }
@ -352,7 +362,7 @@ public class FavouritesDbHelper {
if (remove) { if (remove) {
flatGroups.remove(group.name); flatGroups.remove(group.name);
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
context.getMapMarkersHelper().removeMarkersSyncGroup(group.name); removeFromMarkers(group);
return true; return true;
} }
return false; return false;
@ -603,14 +613,13 @@ public class FavouritesDbHelper {
} }
public void editFavouriteGroup(FavoriteGroup group, String newName, int color, boolean visible) { public void editFavouriteGroup(FavoriteGroup group, String newName, int color, boolean visible) {
MapMarkersHelper markersHelper = context.getMapMarkersHelper();
if (color != 0 && group.color != color) { if (color != 0 && group.color != color) {
FavoriteGroup gr = flatGroups.get(group.name); FavoriteGroup gr = flatGroups.get(group.name);
group.color = color; group.color = color;
for (FavouritePoint p : gr.points) { for (FavouritePoint p : gr.points) {
p.setColor(color); p.setColor(color);
} }
markersHelper.syncGroupAsync(new MarkersSyncGroup(gr.name, gr.name, MarkersSyncGroup.FAVORITES_TYPE)); syncWithMarkers(gr);
} }
if (group.visible != visible) { if (group.visible != visible) {
FavoriteGroup gr = flatGroups.get(group.name); FavoriteGroup gr = flatGroups.get(group.name);
@ -618,11 +627,11 @@ public class FavouritesDbHelper {
for (FavouritePoint p : gr.points) { for (FavouritePoint p : gr.points) {
p.setVisible(visible); p.setVisible(visible);
} }
markersHelper.syncGroupAsync(new MarkersSyncGroup(gr.name, gr.name, MarkersSyncGroup.FAVORITES_TYPE)); syncWithMarkers(gr);
} }
if (!group.name.equals(newName)) { if (!group.name.equals(newName)) {
FavoriteGroup gr = flatGroups.remove(group.name); FavoriteGroup gr = flatGroups.remove(group.name);
markersHelper.removeMarkersSyncGroup(group.name); removeFromMarkers(gr);
gr.name = newName; gr.name = newName;
FavoriteGroup renamedGroup = flatGroups.get(gr.name); FavoriteGroup renamedGroup = flatGroups.get(gr.name);
boolean existing = renamedGroup != null; boolean existing = renamedGroup != null;
@ -638,9 +647,8 @@ public class FavouritesDbHelper {
renamedGroup.points.add(p); renamedGroup.points.add(p);
} }
} }
MarkersSyncGroup syncGroup = new MarkersSyncGroup(renamedGroup.name, renamedGroup.name, MarkersSyncGroup.FAVORITES_TYPE); addToMarkers(renamedGroup);
markersHelper.addMarkersSyncGroup(syncGroup); syncWithMarkers(renamedGroup);
markersHelper.syncGroupAsync(syncGroup);
} }
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
} }

View file

@ -6,7 +6,6 @@ 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.AndroidUtils;
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;
@ -546,15 +545,15 @@ public class GpxSelectionHelper {
File gpx = new File(gpxFile.path); File gpx = new File(gpxFile.path);
if (gpx.exists()) { if (gpx.exists()) {
MapMarkersHelper mapMarkersHelper = app.getMapMarkersHelper(); MapMarkersHelper mapMarkersHelper = app.getMapMarkersHelper();
MarkersSyncGroup syncGroup = new MarkersSyncGroup(gpx.getAbsolutePath(), AndroidUtils.trimExtension(gpx.getName()), MarkersSyncGroup.GPX_TYPE); MarkersSyncGroup syncGroup = MapMarkersHelper.createGroup(gpx);
boolean enabled = true; boolean enabled = true;
if (createOrDeleteGroup) { if (createOrDeleteGroup) {
boolean show = getSelectedFileByPath(gpx.getAbsolutePath()) != null; boolean show = getSelectedFileByPath(gpx.getAbsolutePath()) != null;
enabled = mapMarkersHelper.isGroupSynced(gpx.getAbsolutePath()); enabled = mapMarkersHelper.isGroupSynced(syncGroup.getId());
if (show && !enabled) { if (show && !enabled) {
mapMarkersHelper.addMarkersSyncGroup(syncGroup); mapMarkersHelper.addMarkersSyncGroup(syncGroup);
} else if (!show && mapMarkersHelper.isGroupDisabled(gpx.getAbsolutePath())) { } else if (!show && mapMarkersHelper.isGroupDisabled(syncGroup.getId())) {
mapMarkersHelper.removeMarkersSyncGroup(gpx.getAbsolutePath()); mapMarkersHelper.removeMarkersSyncGroup(syncGroup.getId());
} }
} }
mapMarkersHelper.syncGroupAsync(syncGroup, enabled, callback); mapMarkersHelper.syncGroupAsync(syncGroup, enabled, callback);

View file

@ -7,6 +7,7 @@ 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.AndroidUtils;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.data.FavouritePoint; import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
@ -232,11 +233,6 @@ public class MapMarkersHelper {
}); });
} }
@Nullable
public MarkersSyncGroup getGroup(String id) {
return markersDbHelper.getGroup(id);
}
public boolean isGroupSynced(String id) { public boolean isGroupSynced(String id) {
return markersDbHelper.getGroup(id) != null; return markersDbHelper.getGroup(id) != null;
} }
@ -273,11 +269,9 @@ public class MapMarkersHelper {
} }
public boolean isSynced(SelectedGpxFile gpxFile) { public boolean isSynced(SelectedGpxFile gpxFile) {
GPXFile gpx = gpxFile.getGpxFile(); List<WptPt> gpxPoints = gpxFile.getGpxFile().getPoints();
List<WptPt> gpxPoints = gpx.getPoints();
for (WptPt wptPt : gpxPoints) { for (WptPt wptPt : gpxPoints) {
MapMarker mapMarker = getMapMarker(wptPt); if (getMapMarker(wptPt) != null) {
if (mapMarker != null) {
return true; return true;
} }
} }
@ -288,6 +282,7 @@ public class MapMarkersHelper {
return getMapMarker(favouritePoint) != null; return getMapMarker(favouritePoint) != null;
} }
@Nullable
public MapMarker getMapMarker(WptPt wptPt) { public MapMarker getMapMarker(WptPt wptPt) {
for (MapMarker marker : mapMarkers) { for (MapMarker marker : mapMarkers) {
if (marker.wptPt == wptPt) { if (marker.wptPt == wptPt) {
@ -297,6 +292,7 @@ public class MapMarkersHelper {
return null; return null;
} }
@Nullable
public MapMarker getMapMarker(FavouritePoint favouritePoint) { public MapMarker getMapMarker(FavouritePoint favouritePoint) {
for (MapMarker marker : mapMarkers) { for (MapMarker marker : mapMarkers) {
if (marker.favouritePoint == favouritePoint) { if (marker.favouritePoint == favouritePoint) {
@ -879,7 +875,7 @@ public class MapMarkersHelper {
if (marker.groupName != null) { if (marker.groupName != null) {
group.setName(marker.groupName); group.setName(marker.groupName);
group.setGroupKey(marker.groupKey); group.setGroupKey(marker.groupKey);
MapMarkersHelper.MarkersSyncGroup syncGroup = getGroup(marker.groupKey); MarkersSyncGroup syncGroup = markersDbHelper.getGroup(marker.groupKey);
if (syncGroup != null) { if (syncGroup != null) {
group.setType(syncGroup.getType()); group.setType(syncGroup.getType());
} else { } else {
@ -938,7 +934,7 @@ public class MapMarkersHelper {
group = new MapMarkersGroup(); group = new MapMarkersGroup();
group.setName(marker.groupName); group.setName(marker.groupName);
group.setGroupKey(marker.groupKey); group.setGroupKey(marker.groupKey);
MapMarkersHelper.MarkersSyncGroup syncGroup = getGroup(marker.groupKey); MarkersSyncGroup syncGroup = markersDbHelper.getGroup(marker.groupKey);
if (syncGroup != null) { if (syncGroup != null) {
group.setType(syncGroup.getType()); group.setType(syncGroup.getType());
} else { } else {
@ -1020,6 +1016,14 @@ public class MapMarkersHelper {
return null; return null;
} }
public static MarkersSyncGroup createGroup(@NonNull FavoriteGroup favGroup) {
return new MarkersSyncGroup(favGroup.name, favGroup.name, MarkersSyncGroup.FAVORITES_TYPE);
}
public static MarkersSyncGroup createGroup(@NonNull File gpx) {
return new MarkersSyncGroup(gpx.getAbsolutePath(), AndroidUtils.trimExtension(gpx.getName()), MarkersSyncGroup.GPX_TYPE);
}
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
// accessors to active markers: // accessors to active markers:

View file

@ -156,49 +156,31 @@ public class EditFavoriteGroupDialogFragment extends MenuBottomSheetDialogFragme
if (group.points.size() > 0) { if (group.points.size() > 0) {
items.add(new DividerHalfItem(getContext())); items.add(new DividerHalfItem(getContext()));
}
final MapMarkersHelper markersHelper = app.getMapMarkersHelper(); final MapMarkersHelper markersHelper = app.getMapMarkersHelper();
final MarkersSyncGroup syncGroup = final MarkersSyncGroup syncGroup = MapMarkersHelper.createGroup(group);
new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE); final boolean synced = markersHelper.isGroupSynced(syncGroup.getId());
boolean groupSyncedWithMarkers = markersHelper.isGroupSynced(syncGroup.getId());
if (app.getSettings().USE_MAP_MARKERS.get() && group.points.size() > 0 && !groupSyncedWithMarkers) { BaseBottomSheetItem markersGroupItem = new SimpleBottomSheetItem.Builder()
BaseBottomSheetItem addToMarkersItem = new SimpleBottomSheetItem.Builder() .setIcon(getContentIcon(synced ? R.drawable.ic_action_delete_dark : R.drawable.ic_action_flag_dark))
.setIcon(getContentIcon(R.drawable.ic_action_flag_dark)) .setTitle(getString(synced ? R.string.remove_from_map_markers : R.string.shared_string_add_to_map_markers))
.setTitle(getString(R.string.shared_string_add_to_map_markers))
.setLayoutId(R.layout.bottom_sheet_item_simple)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
markersHelper.addMarkersSyncGroup(syncGroup);
markersHelper.syncGroupAsync(syncGroup);
dismiss();
MapActivity.launchMapActivityMoveToTop(getActivity());
}
})
.create();
items.add(addToMarkersItem);
}
if (app.getSettings().USE_MAP_MARKERS.get() && groupSyncedWithMarkers) {
BaseBottomSheetItem removeFromMarkersItem = new SimpleBottomSheetItem.Builder()
.setIcon(getContentIcon(R.drawable.ic_action_delete_dark))
.setTitle(getString(R.string.remove_from_map_markers))
.setLayoutId(R.layout.bottom_sheet_item_simple) .setLayoutId(R.layout.bottom_sheet_item_simple)
.setOnClickListener(new View.OnClickListener() { .setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
if (synced) {
markersHelper.removeMarkersSyncGroup(syncGroup.getId()); markersHelper.removeMarkersSyncGroup(syncGroup.getId());
} else {
markersHelper.addMarkersSyncGroup(syncGroup);
markersHelper.syncGroupAsync(syncGroup);
}
dismiss(); dismiss();
MapActivity.launchMapActivityMoveToTop(getActivity()); MapActivity.launchMapActivityMoveToTop(getActivity());
} }
}) })
.create(); .create();
items.add(removeFromMarkersItem); items.add(markersGroupItem);
}
if (group.points.size() > 0) {
BaseBottomSheetItem shareItem = new SimpleBottomSheetItem.Builder() BaseBottomSheetItem shareItem = new SimpleBottomSheetItem.Builder()
.setIcon(getContentIcon(R.drawable.ic_action_gshare_dark)) .setIcon(getContentIcon(R.drawable.ic_action_gshare_dark))
.setTitle(getString(R.string.shared_string_share)) .setTitle(getString(R.string.shared_string_share))

View file

@ -424,14 +424,12 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment {
private void selectMapMarkersImpl() { private void selectMapMarkersImpl() {
if (getSelectedFavoritesCount() > 0) { if (getSelectedFavoritesCount() > 0) {
if (getSettings().USE_MAP_MARKERS.get()) {
MapMarkersHelper markersHelper = getMyApplication().getMapMarkersHelper(); MapMarkersHelper markersHelper = getMyApplication().getMapMarkersHelper();
List<LatLon> points = new LinkedList<>(); List<LatLon> points = new ArrayList<>();
List<PointDescription> names = new LinkedList<>(); 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());
MarkersSyncGroup syncGr = MarkersSyncGroup syncGr = MapMarkersHelper.createGroup(favGr);
new MarkersSyncGroup(favGr.name, favGr.name, MarkersSyncGroup.FAVORITES_TYPE);
if (entry.getValue().size() == favGr.points.size()) { if (entry.getValue().size() == favGr.points.size()) {
markersHelper.addMarkersSyncGroup(syncGr); markersHelper.addMarkersSyncGroup(syncGr);
markersHelper.syncGroupAsync(syncGr); markersHelper.syncGroupAsync(syncGr);
@ -446,18 +444,6 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment {
} }
} }
MapActivity.launchMapActivityMoveToTop(getActivity()); MapActivity.launchMapActivityMoveToTop(getActivity());
} else {
final TargetPointsHelper targetPointsHelper = getMyApplication().getTargetPointsHelper();
for (FavouritePoint fp : getSelectedFavorites()) {
targetPointsHelper.navigateToPoint(new LatLon(fp.getLatitude(), fp.getLongitude()), false,
targetPointsHelper.getIntermediatePoints().size() + 1,
new PointDescription(PointDescription.POINT_TYPE_FAVORITE, fp.getName()));
}
if (getMyApplication().getRoutingHelper().isRouteCalculated()) {
targetPointsHelper.updateRouteAndRefresh(true);
}
IntermediatePointsDialog.openIntermediatePointsDialog(getActivity(), getMyApplication(), true);
}
} }
} }

View file

@ -671,10 +671,9 @@ public class TrackPointFragment extends OsmandExpandableListFragment {
private void selectMapMarkersImpl() { private void selectMapMarkersImpl() {
if (getSelectedItemsCount() > 0) { if (getSelectedItemsCount() > 0) {
if (getSettings().USE_MAP_MARKERS.get()) {
MapMarkersHelper markersHelper = app.getMapMarkersHelper(); MapMarkersHelper markersHelper = app.getMapMarkersHelper();
List<LatLon> points = new LinkedList<>(); List<LatLon> points = new ArrayList<>();
List<PointDescription> names = new LinkedList<>(); List<PointDescription> names = new ArrayList<>();
for (Map.Entry<GpxDisplayItemType, Set<GpxDisplayItem>> entry : selectedItems.entrySet()) { for (Map.Entry<GpxDisplayItemType, Set<GpxDisplayItem>> entry : selectedItems.entrySet()) {
if (entry.getKey() != GpxDisplayItemType.TRACK_POINTS) { if (entry.getKey() != GpxDisplayItemType.TRACK_POINTS) {
for (GpxDisplayItem i : entry.getValue()) { for (GpxDisplayItem i : entry.getValue()) {
@ -687,20 +686,6 @@ public class TrackPointFragment extends OsmandExpandableListFragment {
} }
} }
MapActivity.launchMapActivityMoveToTop(getActivity()); MapActivity.launchMapActivityMoveToTop(getActivity());
} else {
final TargetPointsHelper targetPointsHelper = getMyApplication().getTargetPointsHelper();
for (GpxDisplayItem i : getSelectedItems()) {
if (i.locationStart != null) {
targetPointsHelper.navigateToPoint(new LatLon(i.locationStart.lat, i.locationStart.lon), false,
targetPointsHelper.getIntermediatePoints().size() + 1,
new PointDescription(PointDescription.POINT_TYPE_FAVORITE, i.name));
}
}
if (getMyApplication().getRoutingHelper().isRouteCalculated()) {
targetPointsHelper.updateRouteAndRefresh(true);
}
IntermediatePointsDialog.openIntermediatePointsDialog(getActivity(), getMyApplication(), true);
}
} }
} }