1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +00:00

update LiveLocation logic

This commit is contained in:
InsanusMokrassar 2020-02-07 22:36:15 +06:00
parent 8fd6a09763
commit d6bbb0cadc

View File

@ -14,36 +14,30 @@ import com.soywiz.klock.TimeSpan
import io.ktor.utils.io.core.Closeable import io.ktor.utils.io.core.Closeable
import kotlinx.coroutines.* import kotlinx.coroutines.*
private val livePeriodDelayDouble = ((livePeriodLimit.last - 60L) * 1000L).toDouble() private val livePeriodDelayMillis = (livePeriodLimit.last - 60L) * 1000L
class LiveLocation internal constructor( class LiveLocation internal constructor(
private val scope: CoroutineScope,
private val requestsExecutor: RequestsExecutor, private val requestsExecutor: RequestsExecutor,
scope: CoroutineScope,
autoCloseTimeDelay: Double,
initMessage: ContentMessage<LocationContent> initMessage: ContentMessage<LocationContent>
) : Closeable { ) : Closeable {
var isClosed: Boolean = false private val doWhenClose = {
private set scope.launch {
private var autoCloseTime = DateTime.now() + TimeSpan(livePeriodDelayDouble) requestsExecutor.stopLiveLocation(message)
}
}
private val autoCloseTime = DateTime.now() + TimeSpan(autoCloseTimeDelay)
val leftUntilCloseMillis: TimeSpan val leftUntilCloseMillis: TimeSpan
get() = autoCloseTime - DateTime.now() get() = autoCloseTime - DateTime.now()
private var updateJob: Job? = null
var isClosed: Boolean = false
private set
get() = field || leftUntilCloseMillis.millisecondsLong < 0L
private var message: ContentMessage<LocationContent> = initMessage private var message: ContentMessage<LocationContent> = initMessage
set(value) {
field = value
updateJob ?.cancel()
updateJob = scope.launch {
autoCloseTime = DateTime.now() + TimeSpan(livePeriodDelayDouble)
delay(leftUntilCloseMillis.millisecondsLong)
updateJob = null
close()
}
}
val lastLocation: Location val lastLocation: Location
get() = message.content.location get() = message.content.location
init {
message = initMessage // required to init updateJob
}
suspend fun updateLocation( suspend fun updateLocation(
location: Location, location: Location,
replyMarkup: InlineKeyboardMarkup? = null replyMarkup: InlineKeyboardMarkup? = null
@ -65,10 +59,7 @@ class LiveLocation internal constructor(
return return
} }
isClosed = true isClosed = true
updateJob ?.cancel() doWhenClose()
scope.launch {
requestsExecutor.stopLiveLocation(message)
}
} }
} }
@ -77,6 +68,7 @@ suspend fun RequestsExecutor.startLiveLocation(
chatId: ChatIdentifier, chatId: ChatIdentifier,
latitude: Double, latitude: Double,
longitude: Double, longitude: Double,
firstTimeUntilCloseMillis: Long = livePeriodDelayMillis,
disableNotification: Boolean = false, disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null, replyToMessageId: MessageIdentifier? = null,
replyMarkup: KeyboardMarkup? = null replyMarkup: KeyboardMarkup? = null
@ -94,8 +86,9 @@ suspend fun RequestsExecutor.startLiveLocation(
) )
return LiveLocation( return LiveLocation(
scope,
this, this,
scope,
firstTimeUntilCloseMillis.toDouble(),
locationMessage locationMessage
) )
} }
@ -105,31 +98,34 @@ suspend fun RequestsExecutor.startLiveLocation(
chat: Chat, chat: Chat,
latitude: Double, latitude: Double,
longitude: Double, longitude: Double,
firstTimeUntilCloseMillis: Long = livePeriodDelayMillis,
disableNotification: Boolean = false, disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null, replyToMessageId: MessageIdentifier? = null,
replyMarkup: KeyboardMarkup? = null replyMarkup: KeyboardMarkup? = null
): LiveLocation = startLiveLocation( ): LiveLocation = startLiveLocation(
scope, chat.id, latitude, longitude, disableNotification, replyToMessageId, replyMarkup scope, chat.id, latitude, longitude, firstTimeUntilCloseMillis, disableNotification, replyToMessageId, replyMarkup
) )
suspend fun RequestsExecutor.startLiveLocation( suspend fun RequestsExecutor.startLiveLocation(
scope: CoroutineScope, scope: CoroutineScope,
chatId: ChatId, chatId: ChatId,
location: Location, location: Location,
firstTimeUntilCloseMillis: Long = livePeriodDelayMillis,
disableNotification: Boolean = false, disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null, replyToMessageId: MessageIdentifier? = null,
replyMarkup: KeyboardMarkup? = null replyMarkup: KeyboardMarkup? = null
): LiveLocation = startLiveLocation( ): LiveLocation = startLiveLocation(
scope, chatId, location.latitude, location.longitude, disableNotification, replyToMessageId, replyMarkup scope, chatId, location.latitude, location.longitude, firstTimeUntilCloseMillis, disableNotification, replyToMessageId, replyMarkup
) )
suspend fun RequestsExecutor.startLiveLocation( suspend fun RequestsExecutor.startLiveLocation(
scope: CoroutineScope, scope: CoroutineScope,
chat: Chat, chat: Chat,
location: Location, location: Location,
firstTimeUntilCloseMillis: Long = livePeriodDelayMillis,
disableNotification: Boolean = false, disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null, replyToMessageId: MessageIdentifier? = null,
replyMarkup: KeyboardMarkup? = null replyMarkup: KeyboardMarkup? = null
): LiveLocation = startLiveLocation( ): LiveLocation = startLiveLocation(
scope, chat.id, location.latitude, location.longitude, disableNotification, replyToMessageId, replyMarkup scope, chat.id, location.latitude, location.longitude, firstTimeUntilCloseMillis, disableNotification, replyToMessageId, replyMarkup
) )