method fix

This commit is contained in:
Dmitriy Ruban 2019-12-23 15:56:50 +02:00
parent fe0452b3f4
commit 4456f5151c
2 changed files with 20 additions and 15 deletions

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="buffer_time_descr">Time after which buffered messages will be deleted</string>
<string name="buffer_time_descr">Time after which buffered location messages will be deleted</string>
<string name="buffer_time">Buffer time</string>
<string name="time_zone_descr">Select time zone to show in your location messages.</string>
<string name="time_zone">Time zone</string>

View file

@ -23,6 +23,8 @@ class LocationMessages(val app: TelegramApplication) {
private val dbHelper: SQLiteHelper
private var lastRemoveTime: Long? = null
init {
dbHelper = SQLiteHelper(app)
readBufferedMessages()
@ -91,7 +93,7 @@ class LocationMessages(val app: TelegramApplication) {
fun addBufferedMessage(message: BufferMessage) {
log.debug("addBufferedMessage $message")
val messages = mutableListOf(*this.bufferedMessages.toTypedArray())
val messages = this.bufferedMessages.toMutableList()
messages.add(message)
this.bufferedMessages = messages
dbHelper.addBufferedMessage(message)
@ -141,30 +143,33 @@ class LocationMessages(val app: TelegramApplication) {
fun removeBufferedMessage(message: BufferMessage) {
log.debug("removeBufferedMessage $message")
val messages = mutableListOf(*this.bufferedMessages.toTypedArray())
val messages = this.bufferedMessages.toMutableList()
messages.remove(message)
this.bufferedMessages = messages
dbHelper.removeBufferedMessage(message)
}
private fun removeOldBufferedMessages() {
if (this.bufferedMessages.isNotEmpty()) {
val bufferTime = app.settings.bufferTime * 1000
val currentTime = System.currentTimeMillis()
val cleanedList = arrayListOf<BufferMessage>()
this.bufferedMessages.forEach { message ->
val diffTime = currentTime - message.time
if (diffTime > bufferTime) {
log.debug("remove old buffered message: $message with diff in time: $diffTime")
if (this.bufferedMessages.isNotEmpty() && isTimeToDelete(currentTime)) {
val bufferTime = app.settings.bufferTime * 1000
val messages = this.bufferedMessages.toMutableList()
val expiredList = messages.filter {
currentTime - it.time > bufferTime
}
expiredList.forEach { message ->
dbHelper.removeBufferedMessage(message)
} else {
cleanedList.add(message)
}
}
this.bufferedMessages = cleanedList
}
messages.removeAll(expiredList)
this.bufferedMessages = messages.toList()
lastRemoveTime = currentTime
}
}
private fun isTimeToDelete(currentTime: Long) = if (lastRemoveTime != null) {
currentTime - lastRemoveTime!! > 60000L
} else true
private fun readBufferedMessages() {
this.bufferedMessages = dbHelper.getBufferedMessages()
removeOldBufferedMessages()