method fix
This commit is contained in:
parent
fe0452b3f4
commit
4456f5151c
2 changed files with 20 additions and 15 deletions
|
@ -1,6 +1,6 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<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="buffer_time">Buffer time</string>
|
||||||
<string name="time_zone_descr">Select time zone to show in your location messages.</string>
|
<string name="time_zone_descr">Select time zone to show in your location messages.</string>
|
||||||
<string name="time_zone">Time zone</string>
|
<string name="time_zone">Time zone</string>
|
||||||
|
|
|
@ -23,6 +23,8 @@ class LocationMessages(val app: TelegramApplication) {
|
||||||
|
|
||||||
private val dbHelper: SQLiteHelper
|
private val dbHelper: SQLiteHelper
|
||||||
|
|
||||||
|
private var lastRemoveTime: Long? = null
|
||||||
|
|
||||||
init {
|
init {
|
||||||
dbHelper = SQLiteHelper(app)
|
dbHelper = SQLiteHelper(app)
|
||||||
readBufferedMessages()
|
readBufferedMessages()
|
||||||
|
@ -91,7 +93,7 @@ class LocationMessages(val app: TelegramApplication) {
|
||||||
|
|
||||||
fun addBufferedMessage(message: BufferMessage) {
|
fun addBufferedMessage(message: BufferMessage) {
|
||||||
log.debug("addBufferedMessage $message")
|
log.debug("addBufferedMessage $message")
|
||||||
val messages = mutableListOf(*this.bufferedMessages.toTypedArray())
|
val messages = this.bufferedMessages.toMutableList()
|
||||||
messages.add(message)
|
messages.add(message)
|
||||||
this.bufferedMessages = messages
|
this.bufferedMessages = messages
|
||||||
dbHelper.addBufferedMessage(message)
|
dbHelper.addBufferedMessage(message)
|
||||||
|
@ -141,30 +143,33 @@ class LocationMessages(val app: TelegramApplication) {
|
||||||
|
|
||||||
fun removeBufferedMessage(message: BufferMessage) {
|
fun removeBufferedMessage(message: BufferMessage) {
|
||||||
log.debug("removeBufferedMessage $message")
|
log.debug("removeBufferedMessage $message")
|
||||||
val messages = mutableListOf(*this.bufferedMessages.toTypedArray())
|
val messages = this.bufferedMessages.toMutableList()
|
||||||
messages.remove(message)
|
messages.remove(message)
|
||||||
this.bufferedMessages = messages
|
this.bufferedMessages = messages
|
||||||
dbHelper.removeBufferedMessage(message)
|
dbHelper.removeBufferedMessage(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun removeOldBufferedMessages() {
|
private fun removeOldBufferedMessages() {
|
||||||
if (this.bufferedMessages.isNotEmpty()) {
|
val currentTime = System.currentTimeMillis()
|
||||||
|
if (this.bufferedMessages.isNotEmpty() && isTimeToDelete(currentTime)) {
|
||||||
val bufferTime = app.settings.bufferTime * 1000
|
val bufferTime = app.settings.bufferTime * 1000
|
||||||
val currentTime = System.currentTimeMillis()
|
val messages = this.bufferedMessages.toMutableList()
|
||||||
val cleanedList = arrayListOf<BufferMessage>()
|
val expiredList = messages.filter {
|
||||||
this.bufferedMessages.forEach { message ->
|
currentTime - it.time > bufferTime
|
||||||
val diffTime = currentTime - message.time
|
|
||||||
if (diffTime > bufferTime) {
|
|
||||||
log.debug("remove old buffered message: $message with diff in time: $diffTime")
|
|
||||||
dbHelper.removeBufferedMessage(message)
|
|
||||||
} else {
|
|
||||||
cleanedList.add(message)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
this.bufferedMessages = cleanedList
|
expiredList.forEach { message ->
|
||||||
|
dbHelper.removeBufferedMessage(message)
|
||||||
|
}
|
||||||
|
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() {
|
private fun readBufferedMessages() {
|
||||||
this.bufferedMessages = dbHelper.getBufferedMessages()
|
this.bufferedMessages = dbHelper.getBufferedMessages()
|
||||||
removeOldBufferedMessages()
|
removeOldBufferedMessages()
|
||||||
|
|
Loading…
Reference in a new issue