[APIv2/Librus] Add getting announcements

This commit is contained in:
Kacper Ziubryniewicz 2019-10-13 16:17:39 +02:00
parent fb945470c0
commit 42b56fa4a2
2 changed files with 72 additions and 0 deletions

View File

@ -63,6 +63,10 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
data.startProgress(R.string.sync_action_syncing_attendance)
LibrusApiAttendances(data) { onSuccess() }
}
ENDPOINT_LIBRUS_API_ANNOUNCEMENTS -> {
data.startProgress(R.string.sync_action_syncing_announcements)
LibrusApiAnnouncements(data) { onSuccess() }
}
else -> onSuccess()
}
}

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) Kacper Ziubryniewicz 2019-10-13
*/
package pl.szczodrzynski.edziennik.api.v2.librus.data.api
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_ANNOUNCEMENTS
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.announcements.Announcement
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
import pl.szczodrzynski.edziennik.utils.Utils
import pl.szczodrzynski.edziennik.utils.models.Date
class LibrusApiAnnouncements(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiAnnouncements"
}
init {
apiGet(TAG, "SchoolNotices") { json ->
val announcements = json.getJsonArray("SchoolNotices")
announcements?.forEach { announcementEl ->
val announcement = announcementEl.asJsonObject
val id = Utils.crc16(announcement.getString("Id")?.toByteArray()
?: return@forEach).toLong()
val subject = announcement.getString("Subject") ?: ""
val text = announcement.getString("Content") ?: ""
val startDate = Date.fromY_m_d(announcement.getString("StartDate")
?: return@forEach)
val endDate = Date.fromY_m_d(announcement.getString("EndDate") ?: return@forEach)
val teacherId = announcement.getJsonObject("AddedBy")?.getLong("Id") ?: -1
val announcementObject = Announcement(
profileId,
id,
subject,
text,
startDate,
endDate,
teacherId
)
val addedDate = Date.fromIso(announcement.getString("CreationDate")
?: return@forEach)
val read = announcement.getBoolean("WasRead") ?: false
data.announcementList.add(announcementObject)
data.metadataList.add(Metadata(
profileId,
Metadata.TYPE_ANNOUNCEMENT,
id,
read,
read,
addedDate
))
}
data.setSyncNext(ENDPOINT_LIBRUS_API_ANNOUNCEMENTS, SYNC_ALWAYS)
onSuccess()
}
}
}