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 -> lastChatsInfo.forEach { lastInfo ->
val obj = JSONObject() val obj = JSONObject()
obj.put(LastChatInfo.CHAT_ID_KEY, lastInfo.chatId) obj.put(LastChatInfo.CHAT_ID_KEY, lastInfo.chatId)
obj.put(LastChatInfo.PERIODS_KEY, convertPeriodsToJson(lastInfo.periods)) obj.put(LastChatInfo.PERIOD_KEY, lastInfo.period)
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])
jArray.put(obj) jArray.put(obj)
} }
jArray jArray
@ -947,13 +932,8 @@ class TelegramSettings(private val app: TelegramApplication) {
val obj = json.getJSONObject(i) val obj = json.getJSONObject(i)
val lastInfo = LastChatInfo().apply { val lastInfo = LastChatInfo().apply {
chatId = obj.optLong(LastChatInfo.CHAT_ID_KEY) chatId = obj.optLong(LastChatInfo.CHAT_ID_KEY)
periods = LinkedList<Long>() period = obj.optLong(LastChatInfo.PERIOD_KEY)
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()))
} }
}
lastChatsInfo.addLast(lastInfo) lastChatsInfo.addLast(lastInfo)
} }
} }
@ -964,16 +944,14 @@ class TelegramSettings(private val app: TelegramApplication) {
addItemToSuggested(id, time) addItemToSuggested(id, time)
} else { } else {
val index = lastChatsInfo.indexOf(lastInfo) val index = lastChatsInfo.indexOf(lastInfo)
lastChatsInfo[index].periods = addTimeToPeriods(lastChatsInfo[index].periods, time) lastChatsInfo[index].period = time
} }
} }
private fun addItemToSuggested(id: Long, time: Long) { private fun addItemToSuggested(id: Long, time: Long) {
val newLastInfo = LastChatInfo().apply { val newLastInfo = LastChatInfo().apply {
chatId = id chatId = id
periods = LinkedList<Long>().apply { period = time
addFirst(time)
}
} }
if (lastChatsInfo.size < 5) { if (lastChatsInfo.size < 5) {
lastChatsInfo.addFirst(newLastInfo) 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 getLiveNowChats() = app.telegramHelper.getMessagesByChatIds(locHistoryTime).keys
private fun updatePrefs() { private fun updatePrefs() {
@ -1512,11 +1466,11 @@ class TelegramSettings(private val app: TelegramApplication) {
class LastChatInfo { class LastChatInfo {
var chatId = -1L var chatId = -1L
var periods = LinkedList<Long>() var period = -1L
companion object { companion object {
internal const val CHAT_ID_KEY = "chatId" 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 } val lastInfo = lastChatsInfo.find { it.chatId == chatId }
if (chat != null && lastInfo != null) { if (chat != null && lastInfo != null) {
val index = lastChatsInfo.indexOf(lastInfo) val index = lastChatsInfo.indexOf(lastInfo)
lastItems.add(LastChat(chat, settings.calcLivePeriod(lastChatsInfo[index].periods))) lastItems.add(LastChat(chat, lastChatsInfo[index].period))
} }
} }
return lastItems return lastItems