From b11da02c8b36a697ce7df9e64948459eaeea8d51 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 18 Mar 2020 22:56:57 +0600 Subject: [PATCH] refactor UpdatesUtils --- .../api/InternalUtils/UpdatesUtils.kt | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesUtils.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesUtils.kt index ea6b42d275..b7eb65f209 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesUtils.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesUtils.kt @@ -21,22 +21,23 @@ internal fun List.lastUpdateIdentifier(): UpdateIdentifier? { internal fun List.convertWithMediaGroupUpdates(): List { val resultUpdates = mutableListOf() - val mediaGroups = mutableMapOf>() + val mediaGroups = mutableMapOf>() for (update in this) { - if (update !is BaseMessageUpdate) { - resultUpdates.add(update) - continue - } - val asEditMediaGroupMessage = update.toEditMediaGroupUpdate() - if (asEditMediaGroupMessage != null) { - resultUpdates.add(asEditMediaGroupMessage) - } else { - val data = update.data - if (data is MediaGroupMessage) { - (mediaGroups[data.mediaGroupId] ?: mutableListOf().also { mediaGroups[data.mediaGroupId] = it }).add(update) - } else { - resultUpdates.add(update) + when (update) { + is BaseEditMessageUpdate -> resultUpdates.add( + update.toEditMediaGroupUpdate() + ) + is BaseSentMessageUpdate -> { + val data = update.data + if (data is MediaGroupMessage) { + mediaGroups.getOrPut(data.mediaGroupId) { + mutableListOf() + }.add(update) + } else { + resultUpdates.add(update) + } } + else -> resultUpdates.add(update) } } mediaGroups.values.map { @@ -48,7 +49,7 @@ internal fun List.convertWithMediaGroupUpdates(): List { return resultUpdates } -internal fun List.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let { +internal fun List.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let { if (isEmpty()) { return@let null } @@ -60,10 +61,10 @@ internal fun List.toSentMediaGroupUpdate(): SentMediaGroupUpd } } -internal fun BaseMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate? = (this as? EditMediaGroupUpdate) ?: let { +internal fun BaseEditMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate = (this as? EditMediaGroupUpdate) ?: let { when (this) { is EditMessageUpdate -> EditMessageMediaGroupUpdate(this) is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this) - else -> null + else -> error("Unsupported type of ${BaseEditMessageUpdate::class.simpleName}") } }