-
-
Notifications
You must be signed in to change notification settings - Fork 358
/
ResurveyUtils.kt
73 lines (64 loc) · 2.6 KB
/
ResurveyUtils.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package de.westnordost.streetcomplete.data.meta
import de.westnordost.streetcomplete.data.osm.changes.StringMapChangesBuilder
import java.lang.Exception
import java.text.SimpleDateFormat
import java.util.*
import java.util.Calendar.MILLISECOND
/** Returns all the known keys used for recording the date at which the tag with the given key
* should be checked again. */
fun getLastCheckDateKeys(key: String): Sequence<String> = sequenceOf(
"$key:check_date", "check_date:$key",
"$key:lastcheck", "lastcheck:$key",
"$key:last_checked", "last_checked:$key"
)
fun Date.toCheckDateString(): String = OSM_CHECK_DATE_FORMAT.format(this)
fun String.toCheckDate(): Date? {
val groups = OSM_CHECK_DATE_REGEX.matchEntire(this)?.groupValues ?: return null
val year = groups[1].toIntOrNull() ?: return null
val month = groups[2].toIntOrNull() ?: return null
val day = groups[3].toIntOrNull() ?: 1
val calendar = Calendar.getInstance()
return try {
// -1 because this is the month index
calendar.set(year, month-1, day, 0, 0, 0)
calendar.set(MILLISECOND, 0)
calendar.time
} catch (e: Exception) {
null
}
}
/** adds or modifies the given tag. If the updated tag is the same as before, sets the check date
* tag to today instead. */
fun StringMapChangesBuilder.updateWithCheckDate(key: String, value: String) {
val previousValue = getPreviousValue(key)
if (previousValue == value) {
updateCheckDateForKey(key)
} else {
addOrModify(key, value)
deleteCheckDatesForKey(key)
}
}
/** Set/update solely the check date to today for the given key */
fun StringMapChangesBuilder.updateCheckDateForKey(key: String) {
addOrModify("$SURVEY_MARK_KEY:$key", Date().toCheckDateString())
// remove old check date keys (except the one we want to set)
getLastCheckDateKeys(key).forEach {
if (it != "$SURVEY_MARK_KEY:$key") deleteIfExists(it)
}
}
/** Delete any check date keys for the given key */
fun StringMapChangesBuilder.deleteCheckDatesForKey(key: String) {
getLastCheckDateKeys(key).forEach { deleteIfExists(it) }
}
/** Date format of the tags used for recording the date at which the element or tag with the given
* key should be checked again. */
private val OSM_CHECK_DATE_FORMAT = SimpleDateFormat("yyyy-MM-dd", Locale.US)
// not using date format because we want to be able to understand 2000-11 as well
private val OSM_CHECK_DATE_REGEX = Regex("([0-9]{4})-([0-9]{2})(?:-([0-9]{2}))?")
val LAST_CHECK_DATE_KEYS = listOf(
"check_date",
"lastcheck",
"last_checked",
"survey:date",
"survey_date"
)