[API/Usos] Fix detecting term start and end date.

This commit is contained in:
Kuba Szczodrzyński 2022-10-23 23:11:49 +02:00
parent 8424414317
commit 6628b97faf
No known key found for this signature in database
GPG Key ID: 70CB8A85BA1633CB

View File

@ -41,24 +41,18 @@ class UsosApiTerms(
} }
private fun processResponse(json: JsonArray): Boolean { private fun processResponse(json: JsonArray): Boolean {
val dates = mutableSetOf<Date>() val today = Date.getToday()
for (term in json.asJsonObjectList()) { for (term in json.asJsonObjectList()) {
if (!term.getBoolean("is_active", false)) if (!term.getBoolean("is_active", false))
continue continue
val startDate = term.getString("start_date")?.let { Date.fromY_m_d(it) } val startDate = term.getString("start_date")?.let { Date.fromY_m_d(it) } ?: continue
val finishDate = term.getString("finish_date")?.let { Date.fromY_m_d(it) } val finishDate = term.getString("finish_date")?.let { Date.fromY_m_d(it) } ?: continue
if (startDate != null) if (today in startDate..finishDate) {
dates += startDate profile?.studentSchoolYearStart = startDate.year
if (finishDate != null) profile?.dateSemester1Start = startDate
dates += finishDate profile?.dateSemester2Start = finishDate
}
} }
val datesSorted = dates.sorted()
if (datesSorted.size != 3)
return false
profile?.studentSchoolYearStart = datesSorted[0].year
profile?.dateSemester1Start = datesSorted[0]
profile?.dateSemester2Start = datesSorted[1]
profile?.dateYearEnd = datesSorted[2]
return true return true
} }
} }