OsmAnd/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt

153 lines
4.9 KiB
Kotlin
Raw Normal View History

package net.osmand.telegram
import android.content.Context
import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants
import net.osmand.telegram.utils.OsmandFormatter.SpeedConstants
private const val SETTINGS_NAME = "osmand_telegram_settings"
private const val SHARE_LOCATION_CHATS_KEY = "share_location_chats"
private const val SHOW_ON_MAP_CHATS_KEY = "show_on_map_chats"
2018-06-11 18:57:33 +02:00
private const val METRICS_CONSTANTS_KEY = "metrics_constants"
private const val SPEED_CONSTANTS_KEY = "speed_constants"
private const val SEND_MY_LOCATION_INTERVAL_KEY = "send_my_location_interval"
private const val SEND_MY_LOCATION_INTERVAL_DEFAULT = 5L * 1000 // 5 seconds
2018-06-11 18:57:33 +02:00
private const val USER_LOCATION_EXPIRE_TIME_KEY = "user_location_expire_time"
2018-07-13 13:02:59 +02:00
private const val CHANGED_TO_CHAT_ID_KEY = "changed_to_chat_id"
private const val USER_LOCATION_EXPIRE_TIME_DEFAULT = 15L * 60 * 1000 // 15 minutes
class TelegramSettings(private val app: TelegramApplication) {
2018-06-11 18:57:33 +02:00
2018-07-13 13:02:59 +02:00
private var shareLocationChats: Set<Long> = emptySet()
private var showOnMapChats: Set<Long> = emptySet()
2018-06-11 18:57:33 +02:00
var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS
var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR
2018-06-13 16:18:54 +02:00
var sendMyLocationInterval = SEND_MY_LOCATION_INTERVAL_DEFAULT
var userLocationExpireTime = USER_LOCATION_EXPIRE_TIME_DEFAULT
2018-07-13 13:02:59 +02:00
var afterMovingChatTitleToChatId = false
2018-06-11 18:57:33 +02:00
init {
read()
}
2018-07-04 15:00:51 +02:00
fun hasAnyChatToShareLocation() = shareLocationChats.isNotEmpty()
2018-06-11 18:57:33 +02:00
2018-07-13 13:02:59 +02:00
fun isSharingLocationToChat(id: Long) = shareLocationChats.contains(id)
2018-06-11 18:57:33 +02:00
2018-07-04 15:00:51 +02:00
fun hasAnyChatToShowOnMap() = showOnMapChats.isNotEmpty()
2018-06-11 18:57:33 +02:00
2018-07-13 13:02:59 +02:00
fun isShowingChatOnMap(id: Long) = showOnMapChats.contains(id)
2018-06-11 18:57:33 +02:00
2018-07-13 13:02:59 +02:00
fun removeNonexistingChats(presentChatIds: List<Long>) {
2018-06-11 18:57:33 +02:00
val shareLocationChats = shareLocationChats.toMutableList()
2018-07-13 13:02:59 +02:00
shareLocationChats.intersect(presentChatIds)
2018-06-11 18:57:33 +02:00
this.shareLocationChats = shareLocationChats.toHashSet()
val showOnMapChats = showOnMapChats.toMutableList()
2018-07-13 13:02:59 +02:00
showOnMapChats.intersect(presentChatIds)
2018-06-11 18:57:33 +02:00
this.showOnMapChats = showOnMapChats.toHashSet()
}
2018-07-13 13:02:59 +02:00
fun shareLocationToChat(id: Long, share: Boolean) {
2018-06-11 18:57:33 +02:00
val shareLocationChats = shareLocationChats.toMutableList()
if (share) {
2018-07-13 13:02:59 +02:00
shareLocationChats.add(id)
2018-06-11 18:57:33 +02:00
} else {
2018-07-13 13:02:59 +02:00
shareLocationChats.remove(id)
2018-06-11 18:57:33 +02:00
}
this.shareLocationChats = shareLocationChats.toHashSet()
}
fun stopSharingLocationToChats() {
this.shareLocationChats = emptySet()
}
2018-07-13 13:02:59 +02:00
fun showChatOnMap(id: Long, show: Boolean) {
2018-06-11 18:57:33 +02:00
val showOnMapChats = showOnMapChats.toMutableList()
if (show) {
2018-07-13 13:02:59 +02:00
showOnMapChats.add(id)
2018-06-11 18:57:33 +02:00
} else {
2018-07-13 13:02:59 +02:00
showOnMapChats.remove(id)
2018-06-11 18:57:33 +02:00
}
this.showOnMapChats = showOnMapChats.toHashSet()
}
fun getShareLocationChats() = ArrayList(shareLocationChats)
2018-07-04 15:00:51 +02:00
2018-06-11 18:57:33 +02:00
fun getShowOnMapChats() = ArrayList(showOnMapChats)
fun getShowOnMapChatsCount() = showOnMapChats.size
fun save() {
val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE)
val edit = prefs.edit()
val shareLocationChatsSet = mutableSetOf<String>()
val shareLocationChats = ArrayList(shareLocationChats)
2018-07-13 13:02:59 +02:00
for (chatId in shareLocationChats) {
shareLocationChatsSet.add(chatId.toString())
2018-06-11 18:57:33 +02:00
}
edit.putStringSet(SHARE_LOCATION_CHATS_KEY, shareLocationChatsSet)
val showOnMapChatsSet = mutableSetOf<String>()
val showOnMapChats = ArrayList(showOnMapChats)
2018-07-13 13:02:59 +02:00
for (chatId in showOnMapChats) {
showOnMapChatsSet.add(chatId.toString())
2018-06-11 18:57:33 +02:00
}
edit.putStringSet(SHOW_ON_MAP_CHATS_KEY, showOnMapChatsSet)
edit.putString(METRICS_CONSTANTS_KEY, metricsConstants.name)
edit.putString(SPEED_CONSTANTS_KEY, speedConstants.name)
2018-07-13 13:02:59 +02:00
edit.putBoolean(CHANGED_TO_CHAT_ID_KEY, afterMovingChatTitleToChatId)
2018-06-11 18:57:33 +02:00
2018-06-13 16:18:54 +02:00
edit.putLong(SEND_MY_LOCATION_INTERVAL_KEY, sendMyLocationInterval)
2018-06-11 18:57:33 +02:00
edit.apply()
}
fun read() {
val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE)
2018-07-13 13:02:59 +02:00
afterMovingChatTitleToChatId =
prefs.getBoolean(CHANGED_TO_CHAT_ID_KEY, false)
2018-06-11 18:57:33 +02:00
2018-07-13 13:02:59 +02:00
val shareLocationChats = mutableSetOf<Long>()
2018-06-11 18:57:33 +02:00
val shareLocationChatsSet = prefs.getStringSet(SHARE_LOCATION_CHATS_KEY, mutableSetOf())
2018-07-13 13:27:01 +02:00
2018-07-13 13:02:59 +02:00
val showOnMapChats = mutableSetOf<Long>()
2018-06-11 18:57:33 +02:00
val showOnMapChatsSet = prefs.getStringSet(SHOW_ON_MAP_CHATS_KEY, mutableSetOf())
2018-07-13 13:02:59 +02:00
if (!afterMovingChatTitleToChatId) {
showOnMapChatsSet.clear()
shareLocationChatsSet.clear()
afterMovingChatTitleToChatId = true
}
2018-07-13 13:27:01 +02:00
for (chatId in shareLocationChatsSet) {
shareLocationChats.add(chatId.toLong())
}
this.shareLocationChats = shareLocationChats
for (chatId in showOnMapChatsSet) {
2018-07-13 13:02:59 +02:00
showOnMapChats.add(chatId.toLong())
2018-06-11 18:57:33 +02:00
}
this.showOnMapChats = showOnMapChats
2018-07-04 15:00:51 +02:00
metricsConstants = MetricsConstants.valueOf(
2018-07-13 13:27:01 +02:00
prefs.getString(METRICS_CONSTANTS_KEY, MetricsConstants.KILOMETERS_AND_METERS.name)
2018-07-04 15:00:51 +02:00
)
speedConstants = SpeedConstants.valueOf(
2018-07-13 13:27:01 +02:00
prefs.getString(SPEED_CONSTANTS_KEY, SpeedConstants.KILOMETERS_PER_HOUR.name)
2018-07-04 15:00:51 +02:00
)
sendMyLocationInterval =
prefs.getLong(SEND_MY_LOCATION_INTERVAL_KEY, SEND_MY_LOCATION_INTERVAL_DEFAULT)
userLocationExpireTime =
prefs.getLong(USER_LOCATION_EXPIRE_TIME_KEY, USER_LOCATION_EXPIRE_TIME_DEFAULT)
2018-06-11 18:57:33 +02:00
}
}