1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-26 11:38:09 +00:00
tgbotapi/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/LocationContent.kt

107 lines
3.8 KiB
Kotlin
Raw Normal View History

2020-10-04 11:06:30 +00:00
package dev.inmo.tgbotapi.types.message.content
import dev.inmo.tgbotapi.requests.abstracts.Request
2020-11-05 18:12:14 +00:00
import dev.inmo.tgbotapi.requests.send.SendLiveLocation
import dev.inmo.tgbotapi.requests.send.SendStaticLocation
import dev.inmo.tgbotapi.requests.send.abstracts.SendMessageRequest
2020-11-05 18:12:14 +00:00
import dev.inmo.tgbotapi.types.ChatIdentifier
import dev.inmo.tgbotapi.types.MessageIdentifier
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
2020-11-04 19:12:14 +00:00
import dev.inmo.tgbotapi.types.location.*
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.encoding.*
2020-10-04 11:06:30 +00:00
@Serializable(LocationContentSerializer::class)
sealed interface LocationContent : MessageContent {
2020-10-04 11:06:30 +00:00
val location: Location
companion object {
operator fun invoke(location: Location): LocationContent {
return when (location) {
is StaticLocation -> StaticLocationContent(location)
is LiveLocation -> LiveLocationContent(location)
}
}
}
}
@Serializer(LocationContent::class)
object LocationContentSerializer : KSerializer<LocationContent> {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("LocationContent") {
element(LocationContent::location.name, LocationSerializer.descriptor)
}
override fun deserialize(decoder: Decoder): LocationContent {
lateinit var location: Location
decoder.decodeStructure(descriptor) {
while (true) {
when (val index = decodeElementIndex(descriptor)) {
0 -> location = decodeSerializableElement(descriptor, index, LocationSerializer)
CompositeDecoder.DECODE_DONE -> break
else -> error("Unexpected index: $index")
}
}
}
return LocationContent(location)
}
override fun serialize(encoder: Encoder, value: LocationContent) {
encoder.beginStructure(descriptor).apply {
encodeSerializableElement(descriptor, 0, LocationSerializer, value.location)
}.endStructure(descriptor)
}
}
@Serializable(LocationContentSerializer::class)
data class LiveLocationContent(
override val location: LiveLocation
) : LocationContent {
2020-10-04 11:06:30 +00:00
override fun createResend(
chatId: ChatIdentifier,
disableNotification: Boolean,
replyToMessageId: MessageIdentifier?,
2020-11-05 18:05:05 +00:00
allowSendingWithoutReply: Boolean?,
2020-10-04 11:06:30 +00:00
replyMarkup: KeyboardMarkup?
): Request<ContentMessage<LiveLocationContent>> = SendLiveLocation(
chatId,
location.latitude,
location.longitude,
location.livePeriod,
location.horizontalAccuracy,
location.heading,
location.proximityAlertRadius,
disableNotification,
replyToMessageId,
allowSendingWithoutReply,
replyMarkup
) as SendMessageRequest<ContentMessage<LiveLocationContent>>
}
@Serializable(LocationContentSerializer::class)
data class StaticLocationContent(
override val location: StaticLocation
) : LocationContent {
override fun createResend(
chatId: ChatIdentifier,
disableNotification: Boolean,
replyToMessageId: MessageIdentifier?,
allowSendingWithoutReply: Boolean?,
replyMarkup: KeyboardMarkup?
): Request<ContentMessage<StaticLocationContent>> = SendStaticLocation(
chatId,
location.latitude,
location.longitude,
disableNotification,
replyToMessageId,
allowSendingWithoutReply,
replyMarkup
) as SendMessageRequest<ContentMessage<StaticLocationContent>>
}