Untitled

 avatar
unknown
kotlin
2 years ago
1.2 kB
1
Indexable
    fun convertToDomain(response: NotificationsResponse): List<NotificationEntity> {
        return response.notifications.map { convertResponseToEntity(it) }
    }

    private fun convertResponseToEntity(responseItem: NotificationItemResponse): NotificationEntity {
        val pushBody = responseItem.payload?.mapValues {
            val value = it.value
            val strValue = if (value is JsonObject) {
                value.jsonObject.toString()
            } else {
                value.jsonPrimitive.content
            }
            try {
                kSerialization.decodeFromString<JsonObject>(strValue).toMap()
                    .mapValues { entry -> entry.value.jsonPrimitive.content }
            } catch (throwable: Throwable) {
                strValue
            }
        }
        return NotificationEntity(
            pushId = responseItem.id,
            title = responseItem.title ?: "",
            text = responseItem.text ?: "",
            mediaUrl = responseItem.mediaUrl ?: "",
            isNew = !responseItem.isRead,
            updateTimestamp = responseItem.updateTimestamp,
            pushBody = pushBody,
            notificationFromCenter = !responseItem.withMobilePush
        )
    }