replace periods time with last sharing time

This commit is contained in:
veliymolfar 2020-01-16 12:29:44 +02:00
parent ef78945310
commit c262e8a151
2 changed files with 7 additions and 53 deletions

View file

@ -851,22 +851,7 @@ class TelegramSettings(private val app: TelegramApplication) {
lastChatsInfo.forEach { lastInfo ->
val obj = JSONObject()
obj.put(LastChatInfo.CHAT_ID_KEY, lastInfo.chatId)
obj.put(LastChatInfo.PERIODS_KEY, convertPeriodsToJson(lastInfo.periods))
jArray.put(obj)
}
jArray
} catch (e: JSONException) {
log.error(e)
null
}
}
private fun convertPeriodsToJson(periods: LinkedList<Long>): JSONArray? {
return try {
val jArray = JSONArray()
for (i in 0 until periods.count()) {
val obj = JSONObject()
obj.put(i.toString(), periods[i])
obj.put(LastChatInfo.PERIOD_KEY, lastInfo.period)
jArray.put(obj)
}
jArray
@ -947,12 +932,7 @@ class TelegramSettings(private val app: TelegramApplication) {
val obj = json.getJSONObject(i)
val lastInfo = LastChatInfo().apply {
chatId = obj.optLong(LastChatInfo.CHAT_ID_KEY)
periods = LinkedList<Long>()
val jsonArray = obj.getJSONArray(LastChatInfo.PERIODS_KEY)
for (j in 0 until jsonArray.length()) {
val o = jsonArray.get(j) as JSONObject
periods.addLast(o.optLong(j.toString()))
}
period = obj.optLong(LastChatInfo.PERIOD_KEY)
}
lastChatsInfo.addLast(lastInfo)
}
@ -964,16 +944,14 @@ class TelegramSettings(private val app: TelegramApplication) {
addItemToSuggested(id, time)
} else {
val index = lastChatsInfo.indexOf(lastInfo)
lastChatsInfo[index].periods = addTimeToPeriods(lastChatsInfo[index].periods, time)
lastChatsInfo[index].period = time
}
}
private fun addItemToSuggested(id: Long, time: Long) {
val newLastInfo = LastChatInfo().apply {
chatId = id
periods = LinkedList<Long>().apply {
addFirst(time)
}
period = time
}
if (lastChatsInfo.size < 5) {
lastChatsInfo.addFirst(newLastInfo)
@ -983,30 +961,6 @@ class TelegramSettings(private val app: TelegramApplication) {
}
}
private fun addTimeToPeriods(periods: LinkedList<Long>?, time: Long): LinkedList<Long> {
if (periods?.isNotEmpty() != null) {
return if (periods.size < 5) {
periods.addFirst(time)
periods
} else {
periods.removeLast()
periods.addFirst(time)
periods
}
}
return LinkedList<Long>().apply { addFirst(time) }
}
fun calcLivePeriod(periods: LinkedList<Long>): Long {
val sortedPeriods = periods.toLongArray()
sortedPeriods.sort()
return if (sortedPeriods.size % 2 == 0) {
(sortedPeriods[sortedPeriods.size / 2] + sortedPeriods[sortedPeriods.size / 2 - 1]) / 2
} else {
sortedPeriods[sortedPeriods.size / 2]
}
}
private fun getLiveNowChats() = app.telegramHelper.getMessagesByChatIds(locHistoryTime).keys
private fun updatePrefs() {
@ -1512,11 +1466,11 @@ class TelegramSettings(private val app: TelegramApplication) {
class LastChatInfo {
var chatId = -1L
var periods = LinkedList<Long>()
var period = -1L
companion object {
internal const val CHAT_ID_KEY = "chatId"
internal const val PERIODS_KEY = "periods"
internal const val PERIOD_KEY = "period"
}
}
}

View file

@ -580,7 +580,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
val lastInfo = lastChatsInfo.find { it.chatId == chatId }
if (chat != null && lastInfo != null) {
val index = lastChatsInfo.indexOf(lastInfo)
lastItems.add(LastChat(chat, settings.calcLivePeriod(lastChatsInfo[index].periods)))
lastItems.add(LastChat(chat, lastChatsInfo[index].period))
}
}
return lastItems