lastChatsInfo in settings

This commit is contained in:
Dmitriy Ruban 2019-12-23 13:06:01 +02:00
parent 22be792a53
commit 0e4a5f3cd4

View file

@ -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 GPS_UPDATE_EXPIRED_TIME = 60 * 3L // 3 minutes
private const val LAST_CHATS_INFO_KEY = "last_chats_info"
class TelegramSettings(private val app: TelegramApplication) { class TelegramSettings(private val app: TelegramApplication) {
private val log = PlatformUtil.getLog(TelegramSettings::class.java) private val log = PlatformUtil.getLog(TelegramSettings::class.java)
@ -113,6 +115,7 @@ class TelegramSettings(private val app: TelegramApplication) {
private var hiddenOnMapChats: Set<Long> = emptySet() private var hiddenOnMapChats: Set<Long> = emptySet()
private var shareDevices: Set<DeviceBot> = emptySet() private var shareDevices: Set<DeviceBot> = emptySet()
private var liveTracksInfo = emptyList<LiveTrackInfo>() private var liveTracksInfo = emptyList<LiveTrackInfo>()
private var lastChatsInfo = ConcurrentHashMap<Long, LastChatInfo>()
var sharingStatusChanges = ConcurrentLinkedQueue<SharingStatus>() var sharingStatusChanges = ConcurrentLinkedQueue<SharingStatus>()
@ -651,6 +654,11 @@ class TelegramSettings(private val app: TelegramApplication) {
edit.putString(LIVE_TRACKS_KEY, jsonArrayLiveTracks.toString()) edit.putString(LIVE_TRACKS_KEY, jsonArrayLiveTracks.toString())
} }
val jArrayLastInfo = convertLastChatsInfoToJson()
if (jArrayLastInfo != null) {
edit.putString(LAST_CHATS_INFO_KEY, jArrayLastInfo.toString())
}
edit.apply() edit.apply()
} }
@ -678,6 +686,12 @@ class TelegramSettings(private val app: TelegramApplication) {
e.printStackTrace() e.printStackTrace()
} }
try {
parseLastChatsInfo(JSONArray(prefs.getString(LAST_CHATS_INFO_KEY, "")))
} catch (e: JSONException) {
log.error(e)
}
parseShareDevices(prefs.getString(SHARE_DEVICES_KEY, "")) parseShareDevices(prefs.getString(SHARE_DEVICES_KEY, ""))
val sendMyLocDef = SEND_MY_LOC_VALUES_SEC[SEND_MY_LOC_DEFAULT_INDEX] 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) { private fun parseShareChatsInfo(json: JSONArray) {
for (i in 0 until json.length()) { for (i in 0 until json.length()) {
val obj = json.getJSONObject(i) val obj = json.getJSONObject(i)
@ -883,6 +913,17 @@ class TelegramSettings(private val app: TelegramApplication) {
shareDevices = OsmandApiUtils.parseJsonContents(json).toHashSet() 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 getLiveNowChats() = app.telegramHelper.getMessagesByChatIds(locHistoryTime).keys
private fun updatePrefs() { private fun updatePrefs() {
@ -1371,4 +1412,15 @@ class TelegramSettings(private val app: TelegramApplication) {
internal const val SENT_MESSAGES_KEY = "sentMessages" 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"
}
}
} }