From 8c0632258640a929f0586c1db80209614779727c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 15 Feb 2020 16:19:12 +0600 Subject: [PATCH] now LiveLocation is deprecated in TelegramBotAPI and placed into TelegramBotAPI-extensions-api --- CHANGELOG.md | 1 - .../extensions/api/LiveLocation.kt | 57 ++++++++++++++++++- .../TelegramBotAPI/requests/LiveLocation.kt | 3 +- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 341accfd72..1538909d55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,7 +66,6 @@ __API Extensions__ __Telegram Bot API__ * All `RequestsExecutor` extensions related to Telegram Bots API was replaced into `API Extensions` project -* `LiveLocation` now have public constructor, but it is still not recommended to use directly ## 0.22.0 diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/LiveLocation.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/LiveLocation.kt index 0fc869784c..144dc30d3c 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/LiveLocation.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/LiveLocation.kt @@ -1,15 +1,70 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.api import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor -import com.github.insanusmokrassar.TelegramBotAPI.requests.LiveLocation +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.edit.LiveLocation.editLiveLocation +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.edit.LiveLocation.stopLiveLocation import com.github.insanusmokrassar.TelegramBotAPI.requests.send.SendLocation import com.github.insanusmokrassar.TelegramBotAPI.types.* +import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.LocationContent +import com.soywiz.klock.DateTime +import com.soywiz.klock.TimeSpan +import io.ktor.utils.io.core.Closeable import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.launch import kotlin.math.ceil private val livePeriodDelayMillis = (livePeriodLimit.last - 60L) * 1000L +class LiveLocation internal constructor( + private val requestsExecutor: RequestsExecutor, + scope: CoroutineScope, + autoCloseTimeDelay: Double, + initMessage: ContentMessage +) : Closeable { + private val doWhenClose = { + scope.launch { + requestsExecutor.stopLiveLocation(message) + } + } + private val autoCloseTime = DateTime.now() + TimeSpan(autoCloseTimeDelay) + val leftUntilCloseMillis: TimeSpan + get() = autoCloseTime - DateTime.now() + + var isClosed: Boolean = false + private set + get() = field || leftUntilCloseMillis.millisecondsLong < 0L + + private var message: ContentMessage = initMessage + val lastLocation: Location + get() = message.content.location + + suspend fun updateLocation( + location: Location, + replyMarkup: InlineKeyboardMarkup? = null + ): Location { + if (!isClosed) { + message = requestsExecutor.editLiveLocation( + message, + location, + replyMarkup + ) + return lastLocation + } else { + error("LiveLocation is closed") + } + } + + override fun close() { + if (isClosed) { + return + } + isClosed = true + doWhenClose() + } +} suspend fun RequestsExecutor.startLiveLocation( scope: CoroutineScope, diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/LiveLocation.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/LiveLocation.kt index c762441253..7da413e3f1 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/LiveLocation.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/LiveLocation.kt @@ -18,7 +18,8 @@ import kotlinx.coroutines.launch import kotlin.math.ceil private val livePeriodDelayMillis = (livePeriodLimit.last - 60L) * 1000L -class LiveLocation( +@Deprecated("Deprecated due to extracting into separated library") +class LiveLocation internal constructor( private val requestsExecutor: RequestsExecutor, scope: CoroutineScope, autoCloseTimeDelay: Double,