From 23d55ec5717f0745540b4311db38ee3219e79590 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Tue, 5 Nov 2019 17:30:24 +0100 Subject: [PATCH] [APIv2/Vulcan] Add getting sent messages --- .../api/v2/vulcan/data/VulcanData.kt | 4 + .../vulcan/data/api/VulcanApiMessagesInbox.kt | 119 +++++++++--------- .../vulcan/data/api/VulcanApiMessagesSent.kt | 99 +++++++++++++++ .../v2/vulcan/data/api/VulcanApiTemplate.kt | 10 +- 4 files changed, 170 insertions(+), 62 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiMessagesSent.kt 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 4b6c69dc..e7bcff7f 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 @@ -68,6 +68,10 @@ class VulcanData(val data: DataVulcan, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_endpoint_messages_inbox) VulcanApiMessagesInbox(data) { onSuccess() } } + ENDPOINT_VULCAN_API_MESSAGES_SENT -> { + data.startProgress(R.string.edziennik_progress_endpoint_messages_outbox) + VulcanApiMessagesSent(data) { onSuccess() } + } else -> onSuccess() } } 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 index ef1cacfe..085db051 100644 --- 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 @@ -11,6 +11,7 @@ import pl.szczodrzynski.edziennik.api.v2.vulcan.ENDPOINT_VULCAN_API_MESSAGES_INB 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.Message.TYPE_RECEIVED import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata import pl.szczodrzynski.edziennik.utils.models.Date @@ -20,64 +21,68 @@ class VulcanApiMessagesInbox(override val data: DataVulcan, val onSuccess: () -> const val TAG = "VulcanApiMessagesInbox" } - init { data.profile?.also { profile -> + 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 - - apiGet(TAG, VULCAN_API_ENDPOINT_MESSAGES_RECEIVED, parameters = mapOf( - "DataPoczatkowa" to startDate, - "DataKoncowa" to endDate, - "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 - )) + 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 - data.setSyncNext(ENDPOINT_VULCAN_API_MESSAGES_INBOX, SYNC_ALWAYS) - onSuccess() - } - } ?: onSuccess()} + apiGet(TAG, VULCAN_API_ENDPOINT_MESSAGES_RECEIVED, parameters = mapOf( + "DataPoczatkowa" to startDate, + "DataKoncowa" to endDate, + "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 sentDate = message.getLong("DataWyslaniaUnixEpoch")?.let { it * 1000 } + ?: -1 + val readDate = message.getLong("DataPrzeczytaniaUnixEpoch")?.let { it * 1000 } + ?: -1 + + val messageObject = Message( + profileId, + id, + subject, + body, + 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, + sentDate + )) + } + + data.setSyncNext(ENDPOINT_VULCAN_API_MESSAGES_INBOX, SYNC_ALWAYS) + onSuccess() + } + } ?: onSuccess() + } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiMessagesSent.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiMessagesSent.kt new file mode 100644 index 00000000..a56e4f67 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiMessagesSent.kt @@ -0,0 +1,99 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-11-5 + */ + +package pl.szczodrzynski.edziennik.api.v2.vulcan.data.api + +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_MESSAGES +import pl.szczodrzynski.edziennik.api.v2.VULCAN_API_ENDPOINT_MESSAGES_SENT +import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan +import pl.szczodrzynski.edziennik.api.v2.vulcan.ENDPOINT_VULCAN_API_MESSAGES_SENT +import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanApi +import pl.szczodrzynski.edziennik.data.db.modules.messages.Message +import pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_SENT +import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata +import pl.szczodrzynski.edziennik.utils.models.Date + +class VulcanApiMessagesSent(override val data: DataVulcan, val onSuccess: () -> Unit) : VulcanApi(data) { + companion object { + const val TAG = "VulcanApiMessagesSent" + } + + 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 + + apiGet(TAG, VULCAN_API_ENDPOINT_MESSAGES_SENT, parameters = mapOf( + "DataPoczatkowa" to startDate, + "DataKoncowa" to endDate, + "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 readBy = message.getInt("Przeczytane") ?: 0 + val unreadBy = message.getInt("Nieprzeczytane") ?: 0 + val sentDate = message.getLong("DataWyslaniaUnixEpoch")?.let { it * 1000 } ?: -1 + + val messageObject = Message( + profileId, + id, + subject, + body, + TYPE_SENT, + -1, + -1 + ) + + message.getJsonArray("Adresaci")?.asJsonObjectList() + ?.forEachIndexed { _, recipient -> + + val recipientLoginId = recipient.getString("LoginId") + ?: return@forEachIndexed + val recipientId = data.teacherList.singleOrNull { it.loginId == recipientLoginId }?.id + ?: return@forEachIndexed + + val readDate: Long = when (readBy) { + 0 -> 0 + else -> when (unreadBy) { + 0 -> 1 + else -> -1 + } + } + + val messageRecipientObject = MessageRecipient( + profileId, + recipientId, + -1, + readDate, + id + ) + + data.messageRecipientList.add(messageRecipientObject) + } + + data.messageList.add(messageObject) + data.metadataList.add(Metadata( + profileId, + Metadata.TYPE_MESSAGE, + id, + true, + true, + sentDate + )) + } + + data.setSyncNext(ENDPOINT_VULCAN_API_MESSAGES_SENT, 1 * DAY, DRAWER_ITEM_MESSAGES) + onSuccess() + } + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiTemplate.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiTemplate.kt index 2c2bb824..c62a0de8 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiTemplate.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiTemplate.kt @@ -6,8 +6,6 @@ package pl.szczodrzynski.edziennik.api.v2.vulcan.data.api import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanApi -import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS -import pl.szczodrzynski.edziennik.getJsonArray class VulcanApiTemplate(override val data: DataVulcan, val onSuccess: () -> Unit) : VulcanApi(data) { companion object { @@ -15,10 +13,12 @@ class VulcanApiTemplate(override val data: DataVulcan, val onSuccess: () -> Unit } init { - /* apiGet(TAG, VULCAN_API_ENDPOINT_) { json, _ -> + /* data.profile?.also { profile -> + apiGet(TAG, VULCAN_API_ENDPOINT_) { json, _ -> - data.setSyncNext(ENDPOINT_VULCAN_API_, SYNC_ALWAYS) - onSuccess() + data.setSyncNext(ENDPOINT_VULCAN_API_, SYNC_ALWAYS) + onSuccess() + } } */ } }