From 0a01d2567e4de22fc7c6d1387edaf2cb0adfecb0 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 4 Jul 2025 11:53:16 +0600 Subject: [PATCH] add checklist --- .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 3 + .../tgbotapi/types/checklists/Checklist.kt | 62 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/checklists/Checklist.kt diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index b14b023515..53aabe0333 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -310,6 +310,9 @@ const val iconColorField = "icon_color" const val emojiListField = "emoji_list" const val completedByUserField = "completed_by_user" const val completionDateField = "completion_date" +const val titleEntitiesField = "title_entities" +const val othersCanAddTasksField = "others_can_add_tasks" +const val othersCanMarkTasksAsDoneField = "others_can_mark_tasks_as_done" const val requestContactField = "request_contact" const val requestLocationField = "request_location" diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/checklists/Checklist.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/checklists/Checklist.kt new file mode 100644 index 0000000000..dd531ff73a --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/checklists/Checklist.kt @@ -0,0 +1,62 @@ +package dev.inmo.tgbotapi.types.checklists + +import dev.inmo.tgbotapi.types.message.RawMessageEntity +import dev.inmo.tgbotapi.types.message.asTextSources +import dev.inmo.tgbotapi.types.message.textsources.TextSource +import dev.inmo.tgbotapi.types.message.toRawMessageEntities +import dev.inmo.tgbotapi.utils.extensions.makeSourceString +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +@Serializable(Checklist.Companion::class) +data class Checklist( + val titleTextSources: List, + val tasks: List, + val othersCanAddTasks: Boolean = false, + val othersCanCompleteTasks: Boolean = false, +) { + val title: String by lazy { + titleTextSources.makeSourceString() + } + + companion object : KSerializer { + @Serializable + private class RawChecklist( + val title: String, + val title_entities: List = emptyList(), + val tasks: List, + val others_can_add_tasks: Boolean = false, + val others_can_mark_tasks_as_done: Boolean = false, + ) + override val descriptor: SerialDescriptor = RawChecklist.serializer().descriptor + + override fun serialize( + encoder: Encoder, + value: Checklist + ) { + RawChecklist.serializer().serialize( + encoder, + RawChecklist( + title = value.title, + title_entities = value.titleTextSources.toRawMessageEntities(), + tasks = value.tasks, + others_can_add_tasks = value.othersCanAddTasks, + others_can_mark_tasks_as_done = value.othersCanCompleteTasks, + ) + ) + } + + override fun deserialize(decoder: Decoder): Checklist { + val raw = RawChecklist.serializer().deserialize(decoder) + return Checklist( + titleTextSources = raw.title_entities.asTextSources(raw.title), + tasks = raw.tasks, + othersCanAddTasks = raw.others_can_add_tasks, + othersCanCompleteTasks = raw.others_can_mark_tasks_as_done + ) + } + } +}