From 0e4a5f3cd47539bbac48a14199aa54c2b04a9f26 Mon Sep 17 00:00:00 2001 From: Dmitriy Ruban Date: Mon, 23 Dec 2019 13:06:01 +0200 Subject: [PATCH] lastChatsInfo in settings --- .../net/osmand/telegram/TelegramSettings.kt | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 71ae9f6215..09a2ed2aa4 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -105,6 +105,8 @@ private const val WAITING_TDLIB_TIME = 30 // 2 seconds private const val GPS_UPDATE_EXPIRED_TIME = 60 * 3L // 3 minutes +private const val LAST_CHATS_INFO_KEY = "last_chats_info" + class TelegramSettings(private val app: TelegramApplication) { private val log = PlatformUtil.getLog(TelegramSettings::class.java) @@ -113,6 +115,7 @@ class TelegramSettings(private val app: TelegramApplication) { private var hiddenOnMapChats: Set = emptySet() private var shareDevices: Set = emptySet() private var liveTracksInfo = emptyList() + private var lastChatsInfo = ConcurrentHashMap() var sharingStatusChanges = ConcurrentLinkedQueue() @@ -651,6 +654,11 @@ class TelegramSettings(private val app: TelegramApplication) { edit.putString(LIVE_TRACKS_KEY, jsonArrayLiveTracks.toString()) } + val jArrayLastInfo = convertLastChatsInfoToJson() + if (jArrayLastInfo != null) { + edit.putString(LAST_CHATS_INFO_KEY, jArrayLastInfo.toString()) + } + edit.apply() } @@ -678,6 +686,12 @@ class TelegramSettings(private val app: TelegramApplication) { e.printStackTrace() } + try { + parseLastChatsInfo(JSONArray(prefs.getString(LAST_CHATS_INFO_KEY, ""))) + } catch (e: JSONException) { + log.error(e) + } + parseShareDevices(prefs.getString(SHARE_DEVICES_KEY, "")) val sendMyLocDef = SEND_MY_LOC_VALUES_SEC[SEND_MY_LOC_DEFAULT_INDEX] @@ -817,6 +831,22 @@ class TelegramSettings(private val app: TelegramApplication) { } } + private fun convertLastChatsInfoToJson(): JSONArray? { + return try { + val jArray = JSONArray() + lastChatsInfo.forEach { (chatId, lastInfo) -> + val obj = JSONObject() + obj.put(LastChatInfo.CHAT_ID_KEY, chatId) + obj.put(LastChatInfo.LIVE_PERIOD_KEY, lastInfo.livePeriod) + jArray.put(obj) + } + jArray + } catch (e: JSONException) { + log.error(e) + null + } + } + private fun parseShareChatsInfo(json: JSONArray) { for (i in 0 until json.length()) { val obj = json.getJSONObject(i) @@ -883,6 +913,17 @@ class TelegramSettings(private val app: TelegramApplication) { shareDevices = OsmandApiUtils.parseJsonContents(json).toHashSet() } + private fun parseLastChatsInfo(json: JSONArray) { + for (i in 0 until json.length()) { + val obj = json.getJSONObject(i) + val lastInfo = LastChatInfo().apply { + chatId = obj.optLong(LastChatInfo.CHAT_ID_KEY) + livePeriod = obj.getLong(LastChatInfo.LIVE_PERIOD_KEY) + } + lastChatsInfo[lastInfo.chatId] = lastInfo + } + } + private fun getLiveNowChats() = app.telegramHelper.getMessagesByChatIds(locHistoryTime).keys private fun updatePrefs() { @@ -1371,4 +1412,15 @@ class TelegramSettings(private val app: TelegramApplication) { internal const val SENT_MESSAGES_KEY = "sentMessages" } } + + class LastChatInfo { + + var chatId = -1L + var livePeriod = -1L + + companion object { + internal const val CHAT_ID_KEY = "chatId" + internal const val LIVE_PERIOD_KEY = "livePeriod" + } + } } \ No newline at end of file