From ca1b1969420d20f6cd22e98d9512a6b626243a7e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 May 2024 19:43:54 +0600 Subject: [PATCH] updates in LiveFlowLocation behaviours --- .../extensions/api/LiveFlowLocation.kt | 23 ++++++++++++------- .../extensions/api/LiveLocationProvider.kt | 3 ++- .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 3 ++- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/LiveFlowLocation.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/LiveFlowLocation.kt index 1a6b2704e6..892c63a4fe 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/LiveFlowLocation.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/LiveFlowLocation.kt @@ -50,22 +50,30 @@ suspend fun TelegramBot.handleLiveLocation( sentMessageFlow: FlowCollector>? = null ) { var currentLiveLocationMessage: ContentMessage? = null - val updateMessageJob = CoroutineScope(currentCoroutineContext().LinkedSupervisorJob()).launchSafelyWithoutExceptions(start = CoroutineStart.LAZY) { - while (isActive) { - delay(liveTimeMillis) - // Remove previous location message info to resend live location message - currentLiveLocationMessage = null + val updateMessageJob = if (liveTimeMillis == indefiniteLivePeriodDelayMillis) { // do not launch refreshing of message for indefinite live locations + null + } else { + CoroutineScope(currentCoroutineContext().LinkedSupervisorJob()).launchSafelyWithoutExceptions(start = CoroutineStart.LAZY) { + while (isActive) { + delay(liveTimeMillis) + // Remove previous location message info to resend live location message + currentLiveLocationMessage = null + } } } locationsFlow.collect { val capturedLiveLocationMessage = currentLiveLocationMessage if (capturedLiveLocationMessage == null) { - updateMessageJob.start() + updateMessageJob ?.start() currentLiveLocationMessage = send( chatId, it.latitude, it.longitude, - ceil(liveTimeMillis.toDouble() / 1000).toInt(), + if (liveTimeMillis == indefiniteLivePeriodDelayMillis) { + LiveLocation.INDEFINITE_LIVE_PERIOD + } else { + ceil(liveTimeMillis.toDouble() / 1000).toInt() + }, it.horizontalAccuracy, it.heading, it.proximityAlertRadius, @@ -83,7 +91,6 @@ suspend fun TelegramBot.handleLiveLocation( message = capturedLiveLocationMessage, latitude = it.latitude, longitude = it.longitude, - livePeriod = null, horizontalAccuracy = it.horizontalAccuracy, heading = it.heading, proximityAlertRadius = it.proximityAlertRadius, diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/LiveLocationProvider.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/LiveLocationProvider.kt index 8f686e5346..14cf0d3382 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/LiveLocationProvider.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/LiveLocationProvider.kt @@ -23,7 +23,8 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import kotlin.math.ceil -val defaultLivePeriodDelayMillis = (livePeriodLimit.last - 60L) * 1000L +const val indefiniteLivePeriodDelayMillis = LiveLocation.INDEFINITE_LIVE_PERIOD * 1000L +const val defaultLivePeriodDelayMillis = indefiniteLivePeriodDelayMillis /** * @see startLiveLocation 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 542339a84f..286e0e0b2b 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 @@ -1,5 +1,6 @@ package dev.inmo.tgbotapi.types +import dev.inmo.tgbotapi.types.location.LiveLocation import dev.inmo.tgbotapi.utils.BuiltinMimeTypes typealias ForwardSignature = String @@ -47,7 +48,7 @@ val pollOptionTextLength = 1 .. 100 val pollQuestionTextLength = 1 .. 300 val pollOptionsLimit = 2 .. 10 -val livePeriodLimit = 60 .. 86400 +val livePeriodLimit = 60 .. LiveLocation.INDEFINITE_LIVE_PERIOD val inlineQueryAnswerResultsLimit = 0 .. 50