Skip to content

Commit bb35a84

Browse files
committed
Review changes
1 parent b35dc7c commit bb35a84

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

app/src/main/java/com/yacgroup/yacguide/network/SectorParser.kt

+22-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.yacgroup.yacguide.utils.ParserUtils
2828
import com.yacgroup.yacguide.utils.DateUtils
2929
import org.json.JSONArray
3030
import org.json.JSONException
31+
import org.json.JSONObject
3132
import java.lang.RuntimeException
3233
import java.util.*
3334
import kotlin.jvm.Throws
@@ -137,7 +138,7 @@ class SectorParser(private val _db: DatabaseWrapper) : JSONWebParser() {
137138
for (i in 0 until jsonComments.length()) {
138139
val jsonComment = jsonComments.getJSONObject(i)
139140
val user = jsonComment.getString("username")
140-
val date = _parseCommentDate(jsonComment.getString("datum"))
141+
val date = _getCommentDate(jsonComment)
141142
val comment = RegionComment(
142143
id = ParserUtils.jsonField2Int(jsonComment, "komment_ID"),
143144
qualityId = ParserUtils.jsonField2Int(jsonComment, "qual"),
@@ -236,7 +237,7 @@ class SectorParser(private val _db: DatabaseWrapper) : JSONWebParser() {
236237
val rockId = ParserUtils.jsonField2Int(jsonComment, "gipfelid")
237238
val sectorId = ParserUtils.jsonField2Int(jsonComment, "sektorid")
238239
val user = jsonComment.getString("username")
239-
val date = _parseCommentDate(jsonComment.getString("datum"))
240+
val date = _getCommentDate(jsonComment)
240241
when {
241242
routeId != 0 -> {
242243
val comment = RouteComment(
@@ -285,8 +286,24 @@ class SectorParser(private val _db: DatabaseWrapper) : JSONWebParser() {
285286
}
286287

287288
/**
288-
* Parse comment date-times in the format yyyy-MM-dd HH:mm:ss
289-
* and return only the date in the format yyyy-MM-dd.
289+
* Return the change date from the given JSON object.
290+
*
291+
* If the change date is empty, try the creation date.
290292
*/
291-
private fun _parseCommentDate(date: String): String = DateUtils.formatDate(date.split(" ")[0])
293+
private fun _getCommentDate(jsonObject: JSONObject): String {
294+
val changeDate = jsonObject.getString("adatum")
295+
val creationDate = jsonObject.getString("datum")
296+
val date = if (changeDate.isNotBlank()) {
297+
changeDate
298+
} else if (creationDate.isNotBlank()) {
299+
creationDate
300+
} else {
301+
""
302+
}
303+
// The date is expected in the format yyyy-MM-dd HH:mm:ss.
304+
// Return only the date in the format yyyy-MM-dd.
305+
date.split(" ").let {
306+
return if (it.isNotEmpty()) DateUtils.formatDate(it[0]) else ""
307+
}
308+
}
292309
}

0 commit comments

Comments
 (0)