LocationContent -> LiveLocationContent / StaticLocationContent

This commit is contained in:
InsanusMokrassar 2021-09-12 17:15:58 +06:00
parent 1fbaf396aa
commit 46cc37be62
5 changed files with 108 additions and 30 deletions

View File

@ -7,6 +7,7 @@
* `MicroUtils`: `0.5.25` -> `0.5.26`
* `Core`:
* New `BotAction` implementation - `CustomBotAction`
* `LocationContent` has been divided to two different types: `LiveLocationContent` and `StaticLocationContent`
## 0.35.8

View File

@ -6,7 +6,7 @@ import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass
import dev.inmo.tgbotapi.types.message.content.LocationContent
import dev.inmo.tgbotapi.types.message.content.*
import dev.inmo.tgbotapi.utils.throwRangeError
import kotlinx.serialization.*

View File

@ -3,46 +3,104 @@ package dev.inmo.tgbotapi.types.message.content
import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.requests.send.SendLiveLocation
import dev.inmo.tgbotapi.requests.send.SendStaticLocation
import dev.inmo.tgbotapi.requests.send.abstracts.SendMessageRequest
import dev.inmo.tgbotapi.types.ChatIdentifier
import dev.inmo.tgbotapi.types.MessageIdentifier
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.location.*
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
import kotlinx.serialization.Serializable
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.encoding.*
@Serializable
data class LocationContent(
@Serializable(LocationContentSerializer::class)
sealed interface LocationContent : MessageContent {
val location: Location
) : MessageContent {
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 {
override fun createResend(
chatId: ChatIdentifier,
disableNotification: Boolean,
replyToMessageId: MessageIdentifier?,
allowSendingWithoutReply: Boolean?,
replyMarkup: KeyboardMarkup?
): Request<ContentMessage<LocationContent>> = when (location) {
is StaticLocation -> SendStaticLocation(
chatId,
location.latitude,
location.longitude,
disableNotification,
replyToMessageId,
allowSendingWithoutReply,
replyMarkup
)
is LiveLocation -> SendLiveLocation(
chatId,
location.latitude,
location.longitude,
location.livePeriod,
location.horizontalAccuracy,
location.heading,
location.proximityAlertRadius,
disableNotification,
replyToMessageId,
allowSendingWithoutReply,
replyMarkup
)
}
}
): 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>>
}

View File

@ -27,6 +27,7 @@ class BotActionTests {
FindLocationAction -> example.botAction.actionName
RecordVideoNoteAction -> example.botAction.actionName
UploadVideoNoteAction -> example.botAction.actionName
is CustomBotAction -> example.botAction.actionName
}
)
}

View File

@ -2506,6 +2506,24 @@ inline fun ResendableContent.asLocationContent(): LocationContent? = this as? Lo
@PreviewFeature
inline fun ResendableContent.requireLocationContent(): LocationContent = this as LocationContent
@PreviewFeature
inline fun <T> ResendableContent.whenLiveLocationContent(block: (LiveLocationContent) -> T) = asLiveLocationContent() ?.let(block)
@PreviewFeature
inline fun ResendableContent.asLiveLocationContent(): LiveLocationContent? = this as? LiveLocationContent
@PreviewFeature
inline fun ResendableContent.requireLiveLocationContent(): LiveLocationContent = this as LiveLocationContent
@PreviewFeature
inline fun <T> ResendableContent.whenStaticLocationContent(block: (StaticLocationContent) -> T) = asStaticLocationContent() ?.let(block)
@PreviewFeature
inline fun ResendableContent.asStaticLocationContent(): StaticLocationContent? = this as? StaticLocationContent
@PreviewFeature
inline fun ResendableContent.requireStaticLocationContent(): StaticLocationContent = this as StaticLocationContent
@PreviewFeature
inline fun <T> ResendableContent.whenPollContent(block: (PollContent) -> T) = asPollContent() ?.let(block)