1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-01 07:25:23 +00:00
tgbotapi/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/LocationContent.kt

154 lines
5.4 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.MessageId
2022-11-07 18:11:14 +00:00
import dev.inmo.tgbotapi.types.MessageThreadId
2024-01-07 16:42:46 +00:00
import dev.inmo.tgbotapi.types.ReplyParameters
import dev.inmo.tgbotapi.types.business_connection.BusinessConnectionId
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 kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.encoding.*
2020-10-04 11:06:30 +00:00
2021-09-21 12:57:12 +00:00
/**
* [MessageContent] with [location]. This interface contains [copy] method for cases when you do not want to use some
* class casts for copying of content
*
* @see LocationContentSerializer
* @see Location
*/
@Serializable(LocationContentSerializer::class)
sealed interface LocationContent : MessageContent {
2020-10-04 11:06:30 +00:00
val location: Location
2021-09-21 12:49:29 +00:00
fun copy(location: Location = this.location) {
when (this) {
is LiveLocationContent -> LiveLocationContent(
(location as? LiveLocation) ?: this.location.copy(
longitude = location.longitude,
latitude = location.latitude,
horizontalAccuracy = location.horizontalAccuracy
)
)
is StaticLocationContent -> StaticLocationContent(
(location as? StaticLocation) ?: this.location.copy(
longitude = location.longitude,
latitude = location.latitude,
horizontalAccuracy = location.horizontalAccuracy
)
)
}
}
companion object {
operator fun invoke(location: Location): LocationContent {
return when (location) {
is StaticLocation -> StaticLocationContent(location)
is LiveLocation -> LiveLocationContent(location)
}
}
}
}
2021-09-21 12:57:12 +00:00
/**
* [KSerializer] for [LocationContent]
*/
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)
}
}
2021-09-21 12:57:12 +00:00
/**
* [LocationContent] which represents content with [LiveLocation]. In case you are tracking this content throw message
* changes, may evolve to [StaticLocationContent]
*
* @see dev.inmo.tgbotapi.extensions.behaviour_builder.utils.followLocation
*/
2023-08-31 20:41:16 +00:00
@Serializable
data class LiveLocationContent(
override val location: LiveLocation
) : LocationContent {
2020-10-04 11:06:30 +00:00
override fun createResend(
chatId: ChatIdentifier,
2022-11-07 18:11:14 +00:00
messageThreadId: MessageThreadId?,
businessConnectionId: BusinessConnectionId?,
2020-10-04 11:06:30 +00:00
disableNotification: Boolean,
2022-01-01 14:13:22 +00:00
protectContent: Boolean,
2024-01-07 16:42:46 +00:00
replyParameters: ReplyParameters?,
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,
2022-11-07 18:11:14 +00:00
messageThreadId,
businessConnectionId,
disableNotification,
2022-01-01 14:13:22 +00:00
protectContent,
2024-01-07 16:42:46 +00:00
replyParameters,
replyMarkup
) as SendMessageRequest<ContentMessage<LiveLocationContent>>
}
2021-09-21 12:57:12 +00:00
/**
* Just a [LocationContent] with [StaticLocation] [location]. It could be [LiveLocationContent] in previous time in case
* when somebody has sent [LiveLocation] in chat and then stop to broadcast location
*/
2023-08-31 20:41:16 +00:00
@Serializable
data class StaticLocationContent(
override val location: StaticLocation
) : LocationContent {
override fun createResend(
chatId: ChatIdentifier,
2022-11-07 18:11:14 +00:00
messageThreadId: MessageThreadId?,
businessConnectionId: BusinessConnectionId?,
disableNotification: Boolean,
2022-01-01 14:13:22 +00:00
protectContent: Boolean,
2024-01-07 16:42:46 +00:00
replyParameters: ReplyParameters?,
replyMarkup: KeyboardMarkup?
): Request<ContentMessage<StaticLocationContent>> = SendStaticLocation(
chatId,
location.latitude,
location.longitude,
2022-11-07 18:11:14 +00:00
messageThreadId,
businessConnectionId,
disableNotification,
2022-01-01 14:13:22 +00:00
protectContent,
2024-01-07 16:42:46 +00:00
replyParameters,
replyMarkup
) as SendMessageRequest<ContentMessage<StaticLocationContent>>
}