Migrate from settings to db in progress

This commit is contained in:
Alexander Sytnyk 2017-09-06 13:40:02 +03:00
parent b4e8e87901
commit cafbf8a25f
7 changed files with 58 additions and 148 deletions

View file

@ -797,7 +797,7 @@ public class OsmandAidlApi {
List<MapMarker> mapMarkers = markersHelper.getMapMarkers(); List<MapMarker> mapMarkers = markersHelper.getMapMarkers();
for (MapMarker m : mapMarkers) { for (MapMarker m : mapMarkers) {
if (m.getOnlyName().equals(marker.getName()) && latLon.equals(new LatLon(m.getLatitude(), m.getLongitude()))) { if (m.getOnlyName().equals(marker.getName()) && latLon.equals(new LatLon(m.getLatitude(), m.getLongitude()))) {
markersHelper.removeMapMarker(m); markersHelper.moveMapMarkerToHistory(m);
refreshMap(); refreshMap();
return true; return true;
} }

View file

@ -6,6 +6,7 @@ import android.support.annotation.Nullable;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.LocationPoint; import net.osmand.data.LocationPoint;
import net.osmand.data.PointDescription; import net.osmand.data.PointDescription;
import net.osmand.plus.mapmarkers.MapMarkersDbHelper;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.util.ArrayList; import java.util.ArrayList;
@ -19,6 +20,7 @@ public class MapMarkersHelper {
private OsmandSettings settings; private OsmandSettings settings;
private List<MapMarkerChangedListener> listeners = new ArrayList<>(); private List<MapMarkerChangedListener> listeners = new ArrayList<>();
private OsmandApplication ctx; private OsmandApplication ctx;
private MapMarkersDbHelper markersDbHelper;
private boolean startFromMyLocation; private boolean startFromMyLocation;
public interface MapMarkerChangedListener { public interface MapMarkerChangedListener {
@ -144,8 +146,9 @@ public class MapMarkersHelper {
public MapMarkersHelper(OsmandApplication ctx) { public MapMarkersHelper(OsmandApplication ctx) {
this.ctx = ctx; this.ctx = ctx;
settings = ctx.getSettings(); settings = ctx.getSettings();
markersDbHelper = ctx.getMapMarkersDbHelper();
startFromMyLocation = settings.ROUTE_MAP_MARKERS_START_MY_LOC.get(); startFromMyLocation = settings.ROUTE_MAP_MARKERS_START_MY_LOC.get();
readFromSettings(); loadMarkers();
} }
public boolean isStartFromMyLocation() { public boolean isStartFromMyLocation() {
@ -159,55 +162,25 @@ public class MapMarkersHelper {
public void lookupAddressAll() { public void lookupAddressAll() {
for (MapMarker mapMarker : mapMarkers) { for (MapMarker mapMarker : mapMarkers) {
lookupAddress(mapMarker, false); lookupAddress(mapMarker);
} }
for (MapMarker mapMarker : mapMarkersHistory) { for (MapMarker mapMarker : mapMarkersHistory) {
lookupAddress(mapMarker, true); lookupAddress(mapMarker);
} }
} }
private void readFromSettings() { private void loadMarkers() {
mapMarkers.clear(); mapMarkers.clear();
mapMarkersHistory.clear(); mapMarkersHistory.clear();
List<LatLon> ips = settings.getMapMarkersPoints(); mapMarkers.addAll(markersDbHelper.getActiveMarkers());
List<String> desc = settings.getMapMarkersPointDescriptions(ips.size()); mapMarkersHistory.addAll(markersDbHelper.getMarkersHistory());
List<Integer> colors = settings.getMapMarkersColors(ips.size());
List<Boolean> selections = settings.getMapMarkersSelections(ips.size());
List<Long> creationDates = settings.getMapMarkersCreationDates(ips.size());
int colorIndex = 0;
for (int i = 0; i < ips.size(); i++) {
if (colors.size() > i) {
colorIndex = colors.get(i);
}
MapMarker mapMarker = new MapMarker(ips.get(i),
PointDescription.deserializeFromString(desc.get(i), ips.get(i)), colorIndex,
selections.get(i), i);
mapMarker.creationDate = creationDates.get(i);
mapMarkers.add(mapMarker);
}
ips = settings.getMapMarkersHistoryPoints();
desc = settings.getMapMarkersHistoryPointDescriptions(ips.size());
colors = settings.getMapMarkersHistoryColors(ips.size());
creationDates = settings.getMapMarkersHistoryCreationDates(ips.size());
for (int i = 0; i < ips.size(); i++) {
if (colors.size() > i) {
colorIndex = colors.get(i);
}
MapMarker mapMarker = new MapMarker(ips.get(i),
PointDescription.deserializeFromString(desc.get(i), ips.get(i)),
colorIndex, false, i);
mapMarker.history = true;
mapMarker.creationDate = creationDates.get(i);
mapMarkersHistory.add(mapMarker);
}
if (!ctx.isApplicationInitializing()) { if (!ctx.isApplicationInitializing()) {
lookupAddressAll(); lookupAddressAll();
} }
} }
private void lookupAddress(final MapMarker mapMarker, final boolean history) { private void lookupAddress(final MapMarker mapMarker) {
if (mapMarker != null && mapMarker.pointDescription.isSearchingAddress(ctx)) { if (mapMarker != null && mapMarker.pointDescription.isSearchingAddress(ctx)) {
cancelPointAddressRequests(mapMarker.point); cancelPointAddressRequests(mapMarker.point);
GeocodingLookupService.AddressLookupRequest lookupRequest = new GeocodingLookupService.AddressLookupRequest(mapMarker.point, new GeocodingLookupService.OnAddressLookupResult() { GeocodingLookupService.AddressLookupRequest lookupRequest = new GeocodingLookupService.AddressLookupRequest(mapMarker.point, new GeocodingLookupService.OnAddressLookupResult() {
@ -218,13 +191,7 @@ public class MapMarkersHelper {
} else { } else {
mapMarker.pointDescription.setName(address); mapMarker.pointDescription.setName(address);
} }
if (history) { markersDbHelper.updateMarker(mapMarker);
settings.updateMapMarkerHistory(mapMarker.point.getLatitude(), mapMarker.point.getLongitude(),
mapMarker.pointDescription, mapMarker.colorIndex, mapMarker.creationDate);
} else {
settings.updateMapMarker(mapMarker.point.getLatitude(), mapMarker.point.getLongitude(),
mapMarker.pointDescription, mapMarker.colorIndex, mapMarker.selected, mapMarker.creationDate);
}
updateMarker(mapMarker); updateMarker(mapMarker);
} }
}, null); }, null);
@ -232,15 +199,27 @@ public class MapMarkersHelper {
} }
} }
public void removeMapMarker(int index) { public void moveMapMarkerToHistory(MapMarker marker) {
settings.deleteMapMarker(index); if (marker != null) {
MapMarker mapMarker = mapMarkers.remove(index); marker.history = true;
cancelPointAddressRequests(mapMarker.point); markersDbHelper.moveMarkerToHistory(marker);
int ind = 0; mapMarkers.remove(marker);
for (MapMarker marker : mapMarkers) { mapMarkersHistory.add(marker);
marker.index = ind++; cancelPointAddressRequests(marker.point);
refresh();
}
}
public void restoreMarkerFromHistory(MapMarker marker, int position) {
if (marker != null) {
MapMarker next = position >= mapMarkers.size() ? null : mapMarkers.get(position);
marker.history = false;
mapMarkersHistory.remove(marker);
mapMarkers.add(position, marker);
markersDbHelper.restoreMapMarkerFromHistory(marker);
markersDbHelper.changeActiveMarkerPosition(marker, next);
refresh();
} }
refresh();
} }
public List<MapMarker> getMapMarkers() { public List<MapMarker> getMapMarkers() {
@ -309,28 +288,19 @@ public class MapMarkersHelper {
public void removeActiveMarkers() { public void removeActiveMarkers() {
cancelAddressRequests(); cancelAddressRequests();
for (int i = mapMarkers.size() - 1; i >= 0; i--) { markersDbHelper.moveAllActiveMarkersToHistory();
MapMarker marker = mapMarkers.get(i); mapMarkers.clear();
addMapMarkerHistory(marker); mapMarkersHistory = markersDbHelper.getMarkersHistory();
}
settings.clearActiveMapMarkers();
readFromSettings();
refresh(); refresh();
} }
public void removeMarkersHistory() { public void removeMarkersHistory() {
cancelAddressRequests(); cancelAddressRequests();
settings.clearMapMarkersHistory(); markersDbHelper.clearAllMarkersHistory();
readFromSettings(); mapMarkersHistory.clear();
refresh(); refresh();
} }
public void addMapMarker(MapMarker marker, int index) {
settings.insertMapMarker(marker.getLatitude(), marker.getLongitude(), marker.pointDescription,
marker.colorIndex, marker.selected, marker.creationDate, index);
readFromSettings();
}
public void addMapMarker(LatLon point, PointDescription historyName) { public void addMapMarker(LatLon point, PointDescription historyName) {
List<LatLon> points = new ArrayList<>(1); List<LatLon> points = new ArrayList<>(1);
List<PointDescription> historyNames = new ArrayList<>(1); List<PointDescription> historyNames = new ArrayList<>(1);
@ -342,13 +312,6 @@ public class MapMarkersHelper {
public void addMapMarkers(List<LatLon> points, List<PointDescription> historyNames) { public void addMapMarkers(List<LatLon> points, List<PointDescription> historyNames) {
if (points.size() > 0) { if (points.size() > 0) {
int colorIndex = -1; int colorIndex = -1;
double[] latitudes = new double[points.size()];
double[] longitudes = new double[points.size()];
List<PointDescription> pointDescriptions = new ArrayList<>();
int[] colorIndexes = new int[points.size()];
int[] positions = new int[points.size()];
boolean[] selections = new boolean[points.size()];
int[] indexes = new int[points.size()];
for (int i = 0; i < points.size(); i++) { for (int i = 0; i < points.size(); i++) {
LatLon point = points.get(i); LatLon point = points.get(i);
PointDescription historyName = historyNames.get(i); PointDescription historyName = historyNames.get(i);
@ -371,33 +334,18 @@ public class MapMarkersHelper {
colorIndex = (colorIndex + 1) % MAP_MARKERS_COLORS_COUNT; colorIndex = (colorIndex + 1) % MAP_MARKERS_COLORS_COUNT;
} }
latitudes[i] = point.getLatitude(); MapMarker marker = new MapMarker(point, pointDescription, colorIndex, false, 0);
longitudes[i] = point.getLongitude(); markersDbHelper.addMarker(marker);
pointDescriptions.add(pointDescription);
colorIndexes[i] = colorIndex;
positions[i] = -1 - i;
selections[i] = false;
indexes[i] = 0;
} }
/* adding map marker to second topbar's row loadMarkers();
if (sortedMapMarkers.size() > 0) {
MapMarker firstMarker = sortedMapMarkers.get(0);
settings.updateMapMarker(firstMarker.getLatitude(), firstMarker.getLongitude(),
firstMarker.pointDescription, firstMarker.colorIndex, -points.size(), firstMarker.selected);
}
*/
settings.insertMapMarkers(latitudes, longitudes, pointDescriptions, colorIndexes, positions,
selections, indexes);
readFromSettings();
} }
} }
public void updateMapMarker(MapMarker marker, boolean refresh) { public void updateMapMarker(MapMarker marker, boolean refresh) {
if (marker != null) { if (marker != null) {
settings.updateMapMarker(marker.getLatitude(), marker.getLongitude(), markersDbHelper.updateMarker(marker);
marker.pointDescription, marker.colorIndex, marker.selected, marker.creationDate);
if (refresh) { if (refresh) {
readFromSettings(); loadMarkers();
refresh(); refresh();
} }
} }
@ -405,35 +353,9 @@ public class MapMarkersHelper {
public void moveMapMarker(@Nullable MapMarker marker, LatLon latLon) { public void moveMapMarker(@Nullable MapMarker marker, LatLon latLon) {
if (marker != null) { if (marker != null) {
settings.moveMapMarker(new LatLon(marker.getLatitude(), marker.getLongitude()), latLon,
marker.pointDescription, marker.colorIndex, marker.selected, marker.creationDate);
marker.point = new LatLon(latLon.getLatitude(), latLon.getLongitude()); marker.point = new LatLon(latLon.getLatitude(), latLon.getLongitude());
readFromSettings(); markersDbHelper.updateMarker(marker);
refresh(); loadMarkers();
}
}
public void removeMapMarker(MapMarker marker) {
if (marker != null) {
settings.deleteMapMarker(marker.index);
readFromSettings();
refresh();
}
}
public void addMapMarkerHistory(MapMarker marker) {
if (marker != null) {
settings.insertMapMarkerHistory(marker.getLatitude(), marker.getLongitude(),
marker.pointDescription, marker.colorIndex, marker.creationDate, 0);
readFromSettings();
refresh();
}
}
public void removeMapMarkerHistory(MapMarker marker) {
if (marker != null) {
settings.deleteMapMarkerHistory(marker.index);
readFromSettings();
refresh(); refresh();
} }
} }
@ -470,7 +392,7 @@ public class MapMarkersHelper {
} }
if (markers != null || markersHistory != null) { if (markers != null || markersHistory != null) {
readFromSettings(); loadMarkers();
refresh(); refresh();
} }
} }

View file

@ -10,7 +10,6 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.MapMarkerDialogHelper; import net.osmand.plus.helpers.MapMarkerDialogHelper;
import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.MenuController; import net.osmand.plus.mapcontextmenu.MenuController;
import net.osmand.plus.mapillary.MapillaryPlugin;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
public class MapMarkerMenuController extends MenuController { public class MapMarkerMenuController extends MenuController {
@ -25,8 +24,7 @@ public class MapMarkerMenuController extends MenuController {
leftTitleButtonController = new TitleButtonController() { leftTitleButtonController = new TitleButtonController() {
@Override @Override
public void buttonPressed() { public void buttonPressed() {
markersHelper.removeMapMarker(getMapMarker().index); markersHelper.moveMapMarkerToHistory(getMapMarker());
markersHelper.addMapMarkerHistory(getMapMarker());
getMapActivity().getContextMenu().close(); getMapActivity().getContextMenu().close();
} }
}; };

View file

@ -227,7 +227,7 @@ public class MapMarkersDbHelper {
private void buildLinkedList(LongSparseArray<MapMarker> markers, List<MapMarker> res, MapMarker marker) { private void buildLinkedList(LongSparseArray<MapMarker> markers, List<MapMarker> res, MapMarker marker) {
if (marker != null) { if (marker != null) {
res.add(0, marker); res.add(marker);
MapMarker prev = markers.get(marker.id); MapMarker prev = markers.get(marker.id);
if (prev != null) { if (prev != null) {
buildLinkedList(markers, res, prev); buildLinkedList(markers, res, prev);
@ -271,7 +271,7 @@ public class MapMarkersDbHelper {
} }
} }
public void moveActiveMarkerToHistory(MapMarker marker) { public void moveMarkerToHistory(MapMarker marker) {
SQLiteConnection db = openConnection(false); SQLiteConnection db = openConnection(false);
if (db != null) { if (db != null) {
try { try {
@ -281,7 +281,7 @@ public class MapMarkersDbHelper {
"WHERE " + MARKERS_COL_NEXT_KEY + " = ?", new Object[]{marker.nextKey, marker.id}); "WHERE " + MARKERS_COL_NEXT_KEY + " = ?", new Object[]{marker.nextKey, marker.id});
db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " +
MARKERS_COL_ACTIVE + " = ? " + MARKERS_COL_ACTIVE + " = ?, " +
MARKERS_COL_VISITED + " = ? " + MARKERS_COL_VISITED + " = ? " +
"WHERE " + MARKERS_COL_ID + " = ?", new Object[]{0, marker.visitedDate, marker.id}); "WHERE " + MARKERS_COL_ID + " = ?", new Object[]{0, marker.visitedDate, marker.id});
} finally { } finally {
@ -309,11 +309,12 @@ public class MapMarkersDbHelper {
SQLiteConnection db = openConnection(false); SQLiteConnection db = openConnection(false);
if (db != null) { if (db != null) {
try { try {
List<MapMarker> active = getActiveMarkers();
db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " +
MARKERS_COL_ACTIVE + " = ?, " + MARKERS_COL_ACTIVE + " = ?, " +
MARKERS_COL_NEXT_KEY + " = ? " + MARKERS_COL_NEXT_KEY + " = ? " +
"WHERE " + MARKERS_COL_ID + " = ?", "WHERE " + MARKERS_COL_ID + " = ?",
new Object[]{1, getActiveMarkers().get(0).id, marker.id}); new Object[]{1, active.size() > 0 ? active.get(0).id : TAIL_NEXT_VALUE, marker.id});
} finally { } finally {
db.close(); db.close();
} }

View file

@ -68,7 +68,7 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
} }
@Override @Override
public void onBindViewHolder(final MapMarkerItemViewHolder holder, int pos) { public void onBindViewHolder(final MapMarkerItemViewHolder holder, final int pos) {
IconsCache iconsCache = mapActivity.getMyApplication().getIconsCache(); IconsCache iconsCache = mapActivity.getMyApplication().getIconsCache();
MapMarker marker = markers.get(pos); MapMarker marker = markers.get(pos);
@ -99,27 +99,17 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
return; return;
} }
final MapMarker marker = markers.get(position); final MapMarker marker = markers.get(position);
final boolean[] undone = new boolean[1];
mapActivity.getMyApplication().getMapMarkersHelper().removeMapMarker(marker.index); mapActivity.getMyApplication().getMapMarkersHelper().moveMapMarkerToHistory(marker);
notifyItemRemoved(position); notifyItemRemoved(position);
Snackbar.make(holder.itemView, R.string.item_removed, Snackbar.LENGTH_LONG) Snackbar.make(holder.itemView, R.string.item_removed, Snackbar.LENGTH_LONG)
.setAction(R.string.shared_string_undo, new View.OnClickListener() { .setAction(R.string.shared_string_undo, new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
undone[0] = true; mapActivity.getMyApplication().getMapMarkersHelper().restoreMarkerFromHistory(marker, position);
mapActivity.getMyApplication().getMapMarkersHelper().addMapMarker(marker, position);
notifyItemInserted(position); notifyItemInserted(position);
} }
})
.addCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar transientBottomBar, int event) {
if (!undone[0]) {
mapActivity.getMyApplication().getMapMarkersHelper().addMapMarkerHistory(marker);
}
}
}).show(); }).show();
} }
}); });

View file

@ -51,6 +51,8 @@ public class MapMarkersHistoryAdapter extends RecyclerView.Adapter<MapMarkerItem
holder.title.setText(marker.getName(app)); holder.title.setText(marker.getName(app));
holder.description.setText(marker.visitedDate + "");
holder.optionsBtn.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_refresh_dark)); holder.optionsBtn.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_refresh_dark));
holder.optionsBtn.setOnClickListener(new View.OnClickListener() { holder.optionsBtn.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -60,8 +62,7 @@ public class MapMarkersHistoryAdapter extends RecyclerView.Adapter<MapMarkerItem
return; return;
} }
MapMarker marker = markers.get(position); MapMarker marker = markers.get(position);
app.getMapMarkersHelper().removeMapMarkerHistory(marker); app.getMapMarkersHelper().restoreMarkerFromHistory(marker, 0);
app.getMapMarkersHelper().addMapMarker(marker, 0);
notifyItemRemoved(position); notifyItemRemoved(position);
} }
}); });

View file

@ -131,9 +131,7 @@ public class MapMarkersWidgetsFactory {
private void removeMarker(int index) { private void removeMarker(int index) {
if (helper.getMapMarkers().size() > index) { if (helper.getMapMarkers().size() > index) {
MapMarker marker = helper.getMapMarkers().get(index); helper.moveMapMarkerToHistory(helper.getMapMarkers().get(index));
helper.removeMapMarker(marker.index);
helper.addMapMarkerHistory(marker);
} }
} }