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

View file

@ -6,7 +6,6 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import net.osmand.AndroidUtils;
import net.osmand.IProgress;
import net.osmand.data.LatLon;
import net.osmand.plus.GPXDatabase.GpxDataItem;
@ -546,15 +545,15 @@ public class GpxSelectionHelper {
File gpx = new File(gpxFile.path);
if (gpx.exists()) {
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;
if (createOrDeleteGroup) {
boolean show = getSelectedFileByPath(gpx.getAbsolutePath()) != null;
enabled = mapMarkersHelper.isGroupSynced(gpx.getAbsolutePath());
enabled = mapMarkersHelper.isGroupSynced(syncGroup.getId());
if (show && !enabled) {
mapMarkersHelper.addMarkersSyncGroup(syncGroup);
} else if (!show && mapMarkersHelper.isGroupDisabled(gpx.getAbsolutePath())) {
mapMarkersHelper.removeMarkersSyncGroup(gpx.getAbsolutePath());
} else if (!show && mapMarkersHelper.isGroupDisabled(syncGroup.getId())) {
mapMarkersHelper.removeMarkersSyncGroup(syncGroup.getId());
}
}
mapMarkersHelper.syncGroupAsync(syncGroup, enabled, callback);

View file

@ -7,6 +7,7 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import net.osmand.AndroidUtils;
import net.osmand.IndexConstants;
import net.osmand.data.FavouritePoint;
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) {
return markersDbHelper.getGroup(id) != null;
}
@ -273,11 +269,9 @@ public class MapMarkersHelper {
}
public boolean isSynced(SelectedGpxFile gpxFile) {
GPXFile gpx = gpxFile.getGpxFile();
List<WptPt> gpxPoints = gpx.getPoints();
List<WptPt> gpxPoints = gpxFile.getGpxFile().getPoints();
for (WptPt wptPt : gpxPoints) {
MapMarker mapMarker = getMapMarker(wptPt);
if (mapMarker != null) {
if (getMapMarker(wptPt) != null) {
return true;
}
}
@ -288,6 +282,7 @@ public class MapMarkersHelper {
return getMapMarker(favouritePoint) != null;
}
@Nullable
public MapMarker getMapMarker(WptPt wptPt) {
for (MapMarker marker : mapMarkers) {
if (marker.wptPt == wptPt) {
@ -297,6 +292,7 @@ public class MapMarkersHelper {
return null;
}
@Nullable
public MapMarker getMapMarker(FavouritePoint favouritePoint) {
for (MapMarker marker : mapMarkers) {
if (marker.favouritePoint == favouritePoint) {
@ -879,7 +875,7 @@ public class MapMarkersHelper {
if (marker.groupName != null) {
group.setName(marker.groupName);
group.setGroupKey(marker.groupKey);
MapMarkersHelper.MarkersSyncGroup syncGroup = getGroup(marker.groupKey);
MarkersSyncGroup syncGroup = markersDbHelper.getGroup(marker.groupKey);
if (syncGroup != null) {
group.setType(syncGroup.getType());
} else {
@ -938,7 +934,7 @@ public class MapMarkersHelper {
group = new MapMarkersGroup();
group.setName(marker.groupName);
group.setGroupKey(marker.groupKey);
MapMarkersHelper.MarkersSyncGroup syncGroup = getGroup(marker.groupKey);
MarkersSyncGroup syncGroup = markersDbHelper.getGroup(marker.groupKey);
if (syncGroup != null) {
group.setType(syncGroup.getType());
} else {
@ -1020,6 +1016,14 @@ public class MapMarkersHelper {
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:

View file

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

View file

@ -424,40 +424,26 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment {
private void selectMapMarkersImpl() {
if (getSelectedFavoritesCount() > 0) {
if (getSettings().USE_MAP_MARKERS.get()) {
MapMarkersHelper markersHelper = getMyApplication().getMapMarkersHelper();
List<LatLon> points = new LinkedList<>();
List<PointDescription> names = new LinkedList<>();
for (Map.Entry<String, Set<FavouritePoint>> entry : favoritesSelected.entrySet()) {
FavoriteGroup favGr = helper.getGroup(entry.getKey());
MarkersSyncGroup syncGr =
new MarkersSyncGroup(favGr.name, favGr.name, MarkersSyncGroup.FAVORITES_TYPE);
if (entry.getValue().size() == favGr.points.size()) {
markersHelper.addMarkersSyncGroup(syncGr);
markersHelper.syncGroupAsync(syncGr);
} else {
for (FavouritePoint fp : entry.getValue()) {
points.add(new LatLon(fp.getLatitude(), fp.getLongitude()));
names.add(new PointDescription(PointDescription.POINT_TYPE_MAP_MARKER, fp.getName()));
}
markersHelper.addMapMarkers(points, names, syncGr);
points.clear();
names.clear();
MapMarkersHelper markersHelper = getMyApplication().getMapMarkersHelper();
List<LatLon> points = new ArrayList<>();
List<PointDescription> names = new ArrayList<>();
for (Map.Entry<String, Set<FavouritePoint>> entry : favoritesSelected.entrySet()) {
FavoriteGroup favGr = helper.getGroup(entry.getKey());
MarkersSyncGroup syncGr = MapMarkersHelper.createGroup(favGr);
if (entry.getValue().size() == favGr.points.size()) {
markersHelper.addMarkersSyncGroup(syncGr);
markersHelper.syncGroupAsync(syncGr);
} else {
for (FavouritePoint fp : entry.getValue()) {
points.add(new LatLon(fp.getLatitude(), fp.getLongitude()));
names.add(new PointDescription(PointDescription.POINT_TYPE_MAP_MARKER, fp.getName()));
}
markersHelper.addMapMarkers(points, names, syncGr);
points.clear();
names.clear();
}
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);
}
MapActivity.launchMapActivityMoveToTop(getActivity());
}
}

View file

@ -671,36 +671,21 @@ public class TrackPointFragment extends OsmandExpandableListFragment {
private void selectMapMarkersImpl() {
if (getSelectedItemsCount() > 0) {
if (getSettings().USE_MAP_MARKERS.get()) {
MapMarkersHelper markersHelper = app.getMapMarkersHelper();
List<LatLon> points = new LinkedList<>();
List<PointDescription> names = new LinkedList<>();
for (Map.Entry<GpxDisplayItemType, Set<GpxDisplayItem>> entry : selectedItems.entrySet()) {
if (entry.getKey() != GpxDisplayItemType.TRACK_POINTS) {
for (GpxDisplayItem i : entry.getValue()) {
if (i.locationStart != null) {
points.add(new LatLon(i.locationStart.lat, i.locationStart.lon));
names.add(new PointDescription(PointDescription.POINT_TYPE_MAP_MARKER, i.name));
}
MapMarkersHelper markersHelper = app.getMapMarkersHelper();
List<LatLon> points = new ArrayList<>();
List<PointDescription> names = new ArrayList<>();
for (Map.Entry<GpxDisplayItemType, Set<GpxDisplayItem>> entry : selectedItems.entrySet()) {
if (entry.getKey() != GpxDisplayItemType.TRACK_POINTS) {
for (GpxDisplayItem i : entry.getValue()) {
if (i.locationStart != null) {
points.add(new LatLon(i.locationStart.lat, i.locationStart.lon));
names.add(new PointDescription(PointDescription.POINT_TYPE_MAP_MARKER, i.name));
}
markersHelper.addMapMarkers(points, names, null);
}
markersHelper.addMapMarkers(points, names, null);
}
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);
}
MapActivity.launchMapActivityMoveToTop(getActivity());
}
}