From 42b56fa4a2b04f988defc313252c81a86e8598ab Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sun, 13 Oct 2019 16:17:39 +0200 Subject: [PATCH] [APIv2/Librus] Add getting announcements --- .../api/v2/librus/data/LibrusData.kt | 4 ++ .../librus/data/api/LibrusApiAnnouncements.kt | 68 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAnnouncements.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt index c4e867f9..3420730b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt @@ -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() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAnnouncements.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAnnouncements.kt new file mode 100644 index 00000000..cb0c3014 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAnnouncements.kt @@ -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() + } + } +}