From 33c009befe3bd6362c8bc1e2a1933bfd8bc48bec Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sat, 2 Nov 2019 00:05:14 +0100 Subject: [PATCH] [APIv2/Vulcan] Add getting received messages --- .../edziennik/api/v2/models/Data.kt | 13 +++- .../api/v2/vulcan/data/VulcanData.kt | 4 + .../v2/vulcan/data/api/VulcanApiAttendance.kt | 7 +- .../api/v2/vulcan/data/api/VulcanApiEvents.kt | 10 +-- .../vulcan/data/api/VulcanApiMessagesInbox.kt | 75 +++++++++++++++++++ 5 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiMessagesInbox.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt index d96bae78..8e3865b8 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt @@ -60,6 +60,16 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) val profileId get() = profile?.id ?: -1 + val syncStartDate: Date + get() = when (profile?.empty) { + true -> profile.getSemesterStart(profile.currentSemester) + else -> Date.getToday().stepForward(0, -1, 0) + } + + val syncEndDate: Date + get() = profile?.getSemesterEnd(profile.currentSemester) + ?: Date.getToday().stepForward(0, 1, 0) + /** * A callback passed to all [Feature]s and [LoginMethod]s */ @@ -316,8 +326,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) db.notificationDao().addAll(notifications) onSuccess() } - } - catch (e: Exception) { + } catch (e: Exception) { error(ApiError(TAG, EXCEPTION_NOTIFY_AND_SYNC) .withThrowable(e)) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt index 4fa2ad1e..c3364192 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt @@ -65,6 +65,10 @@ class VulcanData(val data: DataVulcan, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_endpoint_attendance) VulcanApiAttendance(data) { onSuccess() } } + ENDPOINT_VULCAN_API_MESSAGES_INBOX -> { + data.startProgress(R.string.edziennik_progress_endpoint_messages_inbox) + VulcanApiMessagesInbox(data) { onSuccess() } + } else -> onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiAttendance.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiAttendance.kt index 1722cfed..20a8082f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiAttendance.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiAttendance.kt @@ -22,12 +22,9 @@ class VulcanApiAttendance(override val data: DataVulcan, val onSuccess: () -> Un data.db.attendanceTypeDao().getAllNow(profileId).toSparseArray(data.attendanceTypes) { it.id } } - val startDate: String = profile.getSemesterStart(profile.currentSemester).stringY_m_d - val endDate: String = profile.getSemesterEnd(profile.currentSemester).stringY_m_d - apiGet(TAG, VULCAN_API_ENDPOINT_ATTENDANCE, parameters = mapOf( - "DataPoczatkowa" to startDate, - "DataKoncowa" to endDate, + "DataPoczatkowa" to data.syncStartDate.stringY_m_d, + "DataKoncowa" to data.syncEndDate.stringY_m_d, "IdOddzial" to data.studentClassId, "IdUczen" to data.studentId, "IdOkresKlasyfikacyjny" to data.studentSemesterId diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiEvents.kt index d971118c..37578e6f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiEvents.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiEvents.kt @@ -26,19 +26,13 @@ class VulcanApiEvents(override val data: DataVulcan, private val isHomework: Boo init { data.profile?.also { profile -> - val startDate: String = when (profile.empty) { - true -> profile.getSemesterStart(profile.currentSemester).stringY_m_d - else -> Date.getToday().stepForward(0, -1, 0).stringY_m_d - } - val endDate: String = profile.getSemesterEnd(profile.currentSemester).stringY_m_d - val endpoint = when (isHomework) { true -> VULCAN_API_ENDPOINT_HOMEWORK else -> VULCAN_API_ENDPOINT_EVENTS } apiGet(TAG, endpoint, parameters = mapOf( - "DataPoczatkowa" to startDate, - "DataKoncowa" to endDate, + "DataPoczatkowa" to data.syncStartDate.stringY_m_d, + "DataKoncowa" to data.syncEndDate.stringY_m_d, "IdOddzial" to data.studentClassId, "IdUczen" to data.studentId, "IdOkresKlasyfikacyjny" to data.studentSemesterId diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiMessagesInbox.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiMessagesInbox.kt new file mode 100644 index 00000000..50ba3411 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiMessagesInbox.kt @@ -0,0 +1,75 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-11-01 + */ + +package pl.szczodrzynski.edziennik.api.v2.vulcan.data.api + +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.api.v2.VULCAN_API_ENDPOINT_MESSAGES_RECEIVED +import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan +import pl.szczodrzynski.edziennik.api.v2.vulcan.ENDPOINT_VULCAN_API_MESSAGES_INBOX +import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanApi +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.data.db.modules.messages.Message +import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata + +class VulcanApiMessagesInbox(override val data: DataVulcan, val onSuccess: () -> Unit) : VulcanApi(data) { + companion object { + const val TAG = "VulcanApiMessagesInbox" + } + + init { + apiGet(TAG, VULCAN_API_ENDPOINT_MESSAGES_RECEIVED, parameters = mapOf( + "DataPoczatkowa" to data.syncStartDate.inUnix, + "DataKoncowa" to data.syncEndDate.inUnix, + "LoginId" to data.studentLoginId, + "IdUczen" to data.studentId + )) { json, _ -> + json.getJsonArray("Data").asJsonObjectList()?.forEach { message -> + val id = message.getLong("WiadomoscId") ?: return@forEach + val subject = message.getString("Tytul") ?: "" + val body = message.getString("Tresc") ?: "" + + val senderLoginId = message.getString("NadawcaId") ?: return@forEach + val senderId = data.teacherList + .singleOrNull { it.loginId == senderLoginId }?.id ?: return@forEach + + val addedDate = message.getLong("DataWyslaniaUnixEpoch")?.let { it * 1000 } ?: -1 + val readDate = message.getLong("DataPrzeczytaniaUnixEpoch")?.let { it * 1000 } ?: -1 + + val messageObject = Message( + profileId, + id, + subject, + body, + Message.TYPE_RECEIVED, + senderId, + -1 + ) + + val messageRecipientObject = MessageRecipient( + profileId, + -1, + -1, + readDate, + id + ) + + data.messageList.add(messageObject) + data.messageRecipientList.add(messageRecipientObject) + data.metadataList.add(Metadata( + profileId, + Metadata.TYPE_MESSAGE, + id, + readDate > 0, + readDate > 0, + addedDate + )) + } + + data.setSyncNext(ENDPOINT_VULCAN_API_MESSAGES_INBOX, SYNC_ALWAYS) + onSuccess() + } + } +}