Add some code

This commit is contained in:
Alexander Sytnyk 2017-09-08 14:33:10 +03:00
parent 2b264697ad
commit c42d8b8114
2 changed files with 62 additions and 19 deletions

View file

@ -43,6 +43,7 @@ public class MapMarkersHelper {
public long creationDate; public long creationDate;
public long visitedDate; public long visitedDate;
public long nextKey; public long nextKey;
public long groupKey = -1;
public MapMarker(LatLon point, PointDescription name, int colorIndex, public MapMarker(LatLon point, PointDescription name, int colorIndex,
boolean selected, int index) { boolean selected, int index) {
@ -191,15 +192,8 @@ public class MapMarkersHelper {
Collections.sort(markers, new Comparator<MapMarker>() { Collections.sort(markers, new Comparator<MapMarker>() {
@Override @Override
public int compare(MapMarker mapMarker1, MapMarker mapMarker2) { public int compare(MapMarker mapMarker1, MapMarker mapMarker2) {
long firstMarkerDate; long firstMarkerDate = history ? mapMarker1.visitedDate : mapMarker1.creationDate;
long secondMarkerDate; long secondMarkerDate = history ? mapMarker2.visitedDate : mapMarker2.creationDate;
if (history) {
firstMarkerDate = mapMarker1.visitedDate;
secondMarkerDate = mapMarker2.visitedDate;
} else {
firstMarkerDate = mapMarker1.creationDate;
secondMarkerDate = mapMarker2.creationDate;
}
if (firstMarkerDate > secondMarkerDate) { if (firstMarkerDate > secondMarkerDate) {
return -1; return -1;
} else if (firstMarkerDate == secondMarkerDate) { } else if (firstMarkerDate == secondMarkerDate) {
@ -230,6 +224,14 @@ public class MapMarkersHelper {
} }
} }
public long createGroupIfNeeded(String name) {
return markersDbHelper.createGroupIfNeeded(name);
}
public String getGroupName(long id) {
return markersDbHelper.getGroupName(id);
}
public void moveMapMarkerToHistory(MapMarker marker) { public void moveMapMarkerToHistory(MapMarker marker) {
if (marker != null) { if (marker != null) {
cancelPointAddressRequests(marker.point); cancelPointAddressRequests(marker.point);

View file

@ -43,7 +43,7 @@ public class MapMarkersDbHelper {
MARKERS_COL_ACTIVE + " int, " + // 1 = true, 0 = false MARKERS_COL_ACTIVE + " int, " + // 1 = true, 0 = false
MARKERS_COL_ADDED + " long, " + MARKERS_COL_ADDED + " long, " +
MARKERS_COL_VISITED + " long, " + MARKERS_COL_VISITED + " long, " +
MARKERS_COL_GROUP_KEY + " int, " + MARKERS_COL_GROUP_KEY + " long, " +
MARKERS_COL_COLOR + " int, " + MARKERS_COL_COLOR + " int, " +
MARKERS_COL_NEXT_KEY + " long);"; MARKERS_COL_NEXT_KEY + " long);";
@ -61,15 +61,18 @@ public class MapMarkersDbHelper {
" FROM " + MARKERS_TABLE_NAME; " FROM " + MARKERS_TABLE_NAME;
private static final String GROUPS_TABLE_NAME = "map_markers_groups"; private static final String GROUPS_TABLE_NAME = "map_markers_groups";
private static final String GROUPS_COL_ID = "group_id";
private static final String GROUPS_COL_NAME = "group_name"; private static final String GROUPS_COL_NAME = "group_name";
private static final String GROUPS_COL_TYPE = "group_type";
private static final String GROUPS_COL_ADDED = "group_added";
private static final String GROUPS_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " + private static final String GROUPS_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " +
GROUPS_TABLE_NAME + " (" + GROUPS_TABLE_NAME + " (" +
GROUPS_COL_NAME + " TEXT, " + GROUPS_COL_ID + " long PRIMARY KEY, " +
GROUPS_COL_TYPE + " TEXT, " + GROUPS_COL_NAME + " TEXT);";
GROUPS_COL_ADDED + " long);";
private static final String GROUPS_TABLE_SELECT = "SELECT " +
GROUPS_COL_ID + ", " +
GROUPS_COL_NAME +
" FROM " + GROUPS_TABLE_NAME;
private static final int TAIL_NEXT_VALUE = 0; private static final int TAIL_NEXT_VALUE = 0;
private static final int HISTORY_NEXT_VALUE = -1; private static final int HISTORY_NEXT_VALUE = -1;
@ -148,6 +151,26 @@ public class MapMarkersDbHelper {
} }
} }
public long createGroupIfNeeded(String name) {
long res = -1;
SQLiteConnection db = openConnection(false);
if (db != null) {
try {
SQLiteCursor query = db.rawQuery(GROUPS_TABLE_SELECT + " WHERE " + GROUPS_COL_NAME + " = ?",
new String[]{name});
if (query.moveToFirst()) {
res = query.getLong(0);
} else {
res = Long.parseLong(String.valueOf(System.currentTimeMillis()) + String.valueOf(new Random().nextInt(900) + 100));
db.execSQL("INSERT INTO " + GROUPS_TABLE_NAME + " VALUES (?, ?)", new Object[]{res, name});
}
} finally {
db.close();
}
}
return res;
}
public void addMarker(MapMarker marker) { public void addMarker(MapMarker marker) {
addMarker(marker, false); addMarker(marker, false);
} }
@ -179,7 +202,7 @@ public class MapMarkersDbHelper {
String descr = PointDescription.serializeToString(marker.getOriginalPointDescription()); String descr = PointDescription.serializeToString(marker.getOriginalPointDescription());
int active = marker.history ? 0 : 1; int active = marker.history ? 0 : 1;
long visited = saveExisting ? currentTime : 0; long visited = saveExisting ? currentTime : 0;
int groupKey = 0; long groupKey = marker.groupKey;
int colorIndex = marker.colorIndex; int colorIndex = marker.colorIndex;
PointDescription pointDescription = marker.getOriginalPointDescription(); PointDescription pointDescription = marker.getOriginalPointDescription();
@ -198,6 +221,23 @@ public class MapMarkersDbHelper {
marker.history ? HISTORY_NEXT_VALUE : TAIL_NEXT_VALUE}); marker.history ? HISTORY_NEXT_VALUE : TAIL_NEXT_VALUE});
} }
public String getGroupName(long id) {
String res = "";
SQLiteConnection db = openConnection(true);
if (db != null) {
try {
SQLiteCursor query = db.rawQuery(GROUPS_TABLE_SELECT + " WHERE " + GROUPS_COL_ID + " = ?",
new String[]{String.valueOf(id)});
if (query.moveToFirst()) {
res = query.getString(1);
}
} finally {
db.close();
}
}
return res;
}
public List<MapMarker> getActiveMarkers() { public List<MapMarker> getActiveMarkers() {
List<MapMarker> res = new LinkedList<>(); List<MapMarker> res = new LinkedList<>();
LongSparseArray<MapMarker> markers = new LongSparseArray<>(); LongSparseArray<MapMarker> markers = new LongSparseArray<>();
@ -229,18 +269,19 @@ public class MapMarkersDbHelper {
boolean active = query.getInt(4) == 1; boolean active = query.getInt(4) == 1;
long added = query.getLong(5); long added = query.getLong(5);
long visited = query.getLong(6); long visited = query.getLong(6);
int groupKey = query.getInt(7); long groupKey = query.getInt(7);
int colorIndex = query.getInt(8); int colorIndex = query.getInt(8);
long nextKey = query.getLong(9); long nextKey = query.getLong(9);
LatLon latLon = new LatLon(lat, lon); LatLon latLon = new LatLon(lat, lon);
MapMarker marker = new MapMarker(latLon, PointDescription.deserializeFromString(desc, latLon), MapMarker marker = new MapMarker(latLon, PointDescription.deserializeFromString(desc, latLon),
colorIndex, false, 0); colorIndex, false, 0);
marker.id = id;
marker.history = !active;
marker.creationDate = added; marker.creationDate = added;
marker.visitedDate = visited; marker.visitedDate = visited;
marker.history = !active; marker.groupKey = groupKey;
marker.nextKey = nextKey; marker.nextKey = nextKey;
marker.id = id;
return marker; return marker;
} }