From c262e8a151bd447a6fc47ff092b77ab6ad7acbd5 Mon Sep 17 00:00:00 2001 From: veliymolfar Date: Thu, 16 Jan 2020 12:29:44 +0200 Subject: [PATCH] replace periods time with last sharing time --- .../net/osmand/telegram/TelegramSettings.kt | 58 ++----------------- .../telegram/ui/MyLocationTabFragment.kt | 2 +- 2 files changed, 7 insertions(+), 53 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 227215c03c..c8b0a179b2 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -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): 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,13 +932,8 @@ class TelegramSettings(private val app: TelegramApplication) { val obj = json.getJSONObject(i) val lastInfo = LastChatInfo().apply { chatId = obj.optLong(LastChatInfo.CHAT_ID_KEY) - periods = LinkedList() - 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().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?, time: Long): LinkedList { - if (periods?.isNotEmpty() != null) { - return if (periods.size < 5) { - periods.addFirst(time) - periods - } else { - periods.removeLast() - periods.addFirst(time) - periods - } - } - return LinkedList().apply { addFirst(time) } - } - - fun calcLivePeriod(periods: LinkedList): 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() + var period = -1L companion object { internal const val CHAT_ID_KEY = "chatId" - internal const val PERIODS_KEY = "periods" + internal const val PERIOD_KEY = "period" } } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt index 154116af0b..fab7f1db77 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt @@ -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