diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt index 3dab26a1..b812fca8 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt @@ -12,8 +12,10 @@ import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface import pl.szczodrzynski.edziennik.api.v2.models.ApiError import pl.szczodrzynski.edziennik.api.v2.prepare import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanData +import pl.szczodrzynski.edziennik.api.v2.vulcan.data.api.VulcanApiMessagesChangeStatus import pl.szczodrzynski.edziennik.api.v2.vulcan.firstlogin.VulcanFirstLogin import pl.szczodrzynski.edziennik.api.v2.vulcan.login.VulcanLogin +import pl.szczodrzynski.edziennik.api.v2.vulcan.login.VulcanLoginApi import pl.szczodrzynski.edziennik.api.v2.vulcanLoginMethods import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull @@ -63,7 +65,11 @@ class Vulcan(val app: App, val profile: Profile?, val loginStore: LoginStore, va } override fun getMessage(message: MessageFull) { - + VulcanLoginApi(data) { + VulcanApiMessagesChangeStatus(data, message) { + completed() + } + } } override fun markAllAnnouncementsAsRead() { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiMessagesChangeStatus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiMessagesChangeStatus.kt new file mode 100644 index 00000000..098edfb2 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiMessagesChangeStatus.kt @@ -0,0 +1,61 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-11-12 + */ + +package pl.szczodrzynski.edziennik.api.v2.vulcan.data.api + +import pl.szczodrzynski.edziennik.api.v2.VULCAN_API_ENDPOINT_MESSAGES_CHANGE_STATUS +import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan +import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanApi +import pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_SENT +import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull +import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata + +class VulcanApiMessagesChangeStatus( + override val data: DataVulcan, + private val messageObject: MessageFull, + val onSuccess: () -> Unit +) : VulcanApi(data) { + companion object { + const val TAG = "VulcanApiMessagesChangeStatus" + } + + init { + data.profile?.also { profile -> + apiGet(TAG, VULCAN_API_ENDPOINT_MESSAGES_CHANGE_STATUS, parameters = mapOf( + "WiadomoscId" to messageObject.id, + "FolderWiadomosci" to "Odebrane", + "Status" to "Widoczna", + "LoginId" to data.studentLoginId, + "IdUczen" to data.studentId + )) { _, _ -> + + if (!messageObject.seen) { + data.messageMetadataList.add(Metadata( + profileId, + Metadata.TYPE_MESSAGE, + messageObject.id, + true, + true, + messageObject.addedDate + )) + } + + if (messageObject.type != TYPE_SENT) { + val messageRecipientObject = MessageRecipient( + profileId, + -1, + -1, + System.currentTimeMillis(), + messageObject.id + ) + + data.messageRecipientList.add(messageRecipientObject) + } + + 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 295db235..3ce5e202 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 @@ -14,6 +14,8 @@ 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.data.db.modules.teachers.Teacher +import pl.szczodrzynski.edziennik.utils.Utils import pl.szczodrzynski.edziennik.utils.models.Date class VulcanApiMessagesInbox(override val data: DataVulcan, val onSuccess: () -> Unit) : VulcanApi(data) { @@ -43,7 +45,25 @@ class VulcanApiMessagesInbox(override val data: DataVulcan, val onSuccess: () -> val senderLoginId = message.getString("NadawcaId") ?: return@forEach val senderId = data.teacherList - .singleOrNull { it.loginId == senderLoginId }?.id ?: return@forEach + .singleOrNull { it.loginId == senderLoginId }?.id ?: { + val senderName = message.getString("Nadawca") ?: "" + val senderNameSplit = senderName.split(" ") + + if (senderNameSplit.size >= 2) { + val senderLastName = senderNameSplit[0] + val senderFirstName = senderNameSplit[1] + + val teacherObject = Teacher( + profileId, + -1 * Utils.crc16(senderName.toByteArray()).toLong(), + senderFirstName, + senderLastName, + senderLoginId + ) + data.teacherList.put(teacherObject.id, teacherObject) + teacherObject.id + } else -1 + }.invoke() val sentDate = message.getLong("DataWyslaniaUnixEpoch")?.let { it * 1000 } ?: -1 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 index 26e97e22..390c1efc 100644 --- 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 @@ -54,12 +54,12 @@ class VulcanApiMessagesSent(override val data: DataVulcan, val onSuccess: () -> ) message.getJsonArray("Adresaci")?.asJsonObjectList() - ?.forEachIndexed { _, recipient -> + ?.onEach { recipient -> val recipientLoginId = recipient.getString("LoginId") - ?: return@forEachIndexed + ?: return@onEach val recipientId = data.teacherList.singleOrNull { it.loginId == recipientLoginId }?.id - ?: return@forEachIndexed + ?: return@onEach val readDate: Long = when (readBy) { 0 -> 0