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"?> <?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>

View file

@ -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 bufferTime = app.settings.bufferTime * 1000
val currentTime = System.currentTimeMillis() val currentTime = System.currentTimeMillis()
val cleanedList = arrayListOf<BufferMessage>() if (this.bufferedMessages.isNotEmpty() && isTimeToDelete(currentTime)) {
this.bufferedMessages.forEach { message -> val bufferTime = app.settings.bufferTime * 1000
val diffTime = currentTime - message.time val messages = this.bufferedMessages.toMutableList()
if (diffTime > bufferTime) { val expiredList = messages.filter {
log.debug("remove old buffered message: $message with diff in time: $diffTime") currentTime - it.time > bufferTime
}
expiredList.forEach { message ->
dbHelper.removeBufferedMessage(message) dbHelper.removeBufferedMessage(message)
} else { }
cleanedList.add(message) messages.removeAll(expiredList)
} this.bufferedMessages = messages.toList()
} lastRemoveTime = currentTime
this.bufferedMessages = cleanedList
} }
} }
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()