2018-06-09 11:20:21 +02:00
|
|
|
package net.osmand.telegram
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants
|
|
|
|
import net.osmand.telegram.utils.OsmandFormatter.SpeedConstants
|
|
|
|
|
|
|
|
class TelegramSettings(private val app: TelegramApplication) {
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
private const val SETTINGS_NAME = "osmand_telegram_settings"
|
|
|
|
|
2018-06-09 21:37:00 +02:00
|
|
|
private const val SHARE_LOCATION_CHATS_KEY = "share_location_chats"
|
|
|
|
private const val SHOW_ON_MAP_CHATS_KEY = "show_on_map_chats"
|
2018-06-09 11:20:21 +02:00
|
|
|
|
2018-06-09 21:37:00 +02:00
|
|
|
private const val METRICS_CONSTANTS_KEY = "metrics_constants"
|
|
|
|
private const val SPEED_CONSTANTS_KEY = "speed_constants"
|
2018-06-09 11:20:21 +02:00
|
|
|
|
2018-06-09 21:37:00 +02:00
|
|
|
private const val SHOW_NOTIFICATION_ALWAYS_KEY = "show_notification_always"
|
2018-06-09 11:20:21 +02:00
|
|
|
}
|
|
|
|
|
2018-06-09 21:37:00 +02:00
|
|
|
private var shareLocationChats: Set<String> = emptySet()
|
|
|
|
private var showOnMapChats: Set<String> = emptySet()
|
2018-06-09 11:20:21 +02:00
|
|
|
|
|
|
|
var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS
|
|
|
|
var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR
|
|
|
|
|
|
|
|
var showNotificationAlways = true
|
|
|
|
|
|
|
|
init {
|
|
|
|
read()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun hasAnyChatToShareLocation(): Boolean {
|
|
|
|
return shareLocationChats.isNotEmpty()
|
|
|
|
}
|
|
|
|
|
2018-06-09 21:37:00 +02:00
|
|
|
fun isSharingLocationToChat(chatTitle: String): Boolean {
|
|
|
|
return shareLocationChats.contains(chatTitle)
|
2018-06-09 11:20:21 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 14:20:11 +02:00
|
|
|
fun hasAnyChatToShowOnMap(): Boolean {
|
|
|
|
return showOnMapChats.isNotEmpty()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun isShowingChatOnMap(chatTitle: String): Boolean {
|
|
|
|
return showOnMapChats.contains(chatTitle)
|
|
|
|
}
|
|
|
|
|
2018-06-09 21:37:00 +02:00
|
|
|
fun removeNonexistingChats(presentChatTitles: List<String>) {
|
|
|
|
val shareLocationChats = shareLocationChats.toMutableList()
|
|
|
|
shareLocationChats.intersect(presentChatTitles)
|
|
|
|
this.shareLocationChats = shareLocationChats.toHashSet()
|
2018-06-10 14:20:11 +02:00
|
|
|
|
|
|
|
val showOnMapChats = showOnMapChats.toMutableList()
|
|
|
|
showOnMapChats.intersect(presentChatTitles)
|
|
|
|
this.showOnMapChats = showOnMapChats.toHashSet()
|
2018-06-09 21:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun shareLocationToChat(chatTitle: String, share: Boolean) {
|
2018-06-09 11:20:21 +02:00
|
|
|
val shareLocationChats = shareLocationChats.toMutableList()
|
|
|
|
if (share) {
|
2018-06-09 21:37:00 +02:00
|
|
|
shareLocationChats.add(chatTitle)
|
2018-06-09 11:20:21 +02:00
|
|
|
} else {
|
2018-06-09 21:37:00 +02:00
|
|
|
shareLocationChats.remove(chatTitle)
|
2018-06-09 11:20:21 +02:00
|
|
|
}
|
|
|
|
this.shareLocationChats = shareLocationChats.toHashSet()
|
|
|
|
}
|
|
|
|
|
2018-06-10 14:20:11 +02:00
|
|
|
fun showChatOnMap(chatTitle: String, show: Boolean) {
|
|
|
|
val showOnMapChats = showOnMapChats.toMutableList()
|
|
|
|
if (show) {
|
|
|
|
showOnMapChats.add(chatTitle)
|
|
|
|
} else {
|
|
|
|
showOnMapChats.remove(chatTitle)
|
|
|
|
}
|
|
|
|
this.showOnMapChats = showOnMapChats.toHashSet()
|
|
|
|
}
|
|
|
|
|
2018-06-09 11:20:21 +02:00
|
|
|
fun getShareLocationChats() = ArrayList(shareLocationChats)
|
2018-06-10 14:20:11 +02:00
|
|
|
fun getShowOnMapChats() = ArrayList(showOnMapChats)
|
2018-06-09 11:20:21 +02:00
|
|
|
|
|
|
|
fun save() {
|
|
|
|
val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE)
|
|
|
|
val edit = prefs.edit()
|
|
|
|
|
|
|
|
val shareLocationChatsSet = mutableSetOf<String>()
|
|
|
|
val shareLocationChats = ArrayList(shareLocationChats)
|
2018-06-09 21:37:00 +02:00
|
|
|
for (chatTitle in shareLocationChats) {
|
|
|
|
shareLocationChatsSet.add(chatTitle)
|
2018-06-09 11:20:21 +02:00
|
|
|
}
|
|
|
|
edit.putStringSet(SHARE_LOCATION_CHATS_KEY, shareLocationChatsSet)
|
|
|
|
|
|
|
|
val showOnMapChatsSet = mutableSetOf<String>()
|
|
|
|
val showOnMapChats = ArrayList(showOnMapChats)
|
2018-06-09 21:37:00 +02:00
|
|
|
for (chatTitle in showOnMapChats) {
|
|
|
|
showOnMapChatsSet.add(chatTitle)
|
2018-06-09 11:20:21 +02:00
|
|
|
}
|
|
|
|
edit.putStringSet(SHOW_ON_MAP_CHATS_KEY, showOnMapChatsSet)
|
|
|
|
|
|
|
|
edit.putString(METRICS_CONSTANTS_KEY, metricsConstants.name)
|
|
|
|
edit.putString(SPEED_CONSTANTS_KEY, speedConstants.name)
|
|
|
|
|
|
|
|
edit.putBoolean(SHOW_NOTIFICATION_ALWAYS_KEY, showNotificationAlways)
|
|
|
|
|
|
|
|
edit.apply()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun read() {
|
|
|
|
val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE)
|
|
|
|
|
2018-06-09 21:37:00 +02:00
|
|
|
val shareLocationChats = mutableSetOf<String>()
|
2018-06-09 11:20:21 +02:00
|
|
|
val shareLocationChatsSet = prefs.getStringSet(SHARE_LOCATION_CHATS_KEY, mutableSetOf())
|
2018-06-09 21:37:00 +02:00
|
|
|
for (chatTitle in shareLocationChatsSet) {
|
|
|
|
shareLocationChats.add(chatTitle)
|
2018-06-09 11:20:21 +02:00
|
|
|
}
|
|
|
|
this.shareLocationChats = shareLocationChats
|
|
|
|
|
2018-06-09 21:37:00 +02:00
|
|
|
val showOnMapChats = mutableSetOf<String>()
|
2018-06-09 11:20:21 +02:00
|
|
|
val showOnMapChatsSet = prefs.getStringSet(SHOW_ON_MAP_CHATS_KEY, mutableSetOf())
|
2018-06-09 21:37:00 +02:00
|
|
|
for (chatTitle in showOnMapChatsSet) {
|
|
|
|
showOnMapChats.add(chatTitle)
|
2018-06-09 11:20:21 +02:00
|
|
|
}
|
|
|
|
this.showOnMapChats = showOnMapChats
|
|
|
|
|
|
|
|
metricsConstants = MetricsConstants.valueOf(prefs.getString(METRICS_CONSTANTS_KEY, MetricsConstants.KILOMETERS_AND_METERS.name))
|
|
|
|
speedConstants = SpeedConstants.valueOf(prefs.getString(SPEED_CONSTANTS_KEY, SpeedConstants.KILOMETERS_PER_HOUR.name))
|
|
|
|
|
|
|
|
showNotificationAlways = prefs.getBoolean(SHOW_NOTIFICATION_ALWAYS_KEY, true)
|
|
|
|
}
|
|
|
|
}
|