refactor chatIdsToDuration

This commit is contained in:
Chumva 2018-07-13 16:28:37 +03:00
parent 1033e61c0c
commit 9357222b58
2 changed files with 19 additions and 37 deletions

View file

@ -18,6 +18,7 @@ private const val SEND_MY_LOCATION_INTERVAL_DEFAULT = 5L * 1000 // 5 seconds
private const val USER_LOCATION_EXPIRE_TIME_KEY = "user_location_expire_time"
private const val USER_LOCATION_EXPIRE_TIME_DEFAULT = 15L * 60 * 1000 // 15 minutes
private const val DEFAULT_VISIBLE_TIME_SECONDS = 60 * 60L // 1 hour
private const val TITLES_REPLACED_WITH_IDS = "changed_to_chat_id"
@ -26,7 +27,7 @@ class TelegramSettings(private val app: TelegramApplication) {
private var shareLocationChats: Set<Long> = emptySet()
private var showOnMapChats: Set<Long> = emptySet()
private var chatIdsToDuration: Map<Long, Long> = emptyMap()
private var chatIdsToDuration = mutableMapOf<Long, Long>()
var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS
var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR
@ -56,47 +57,33 @@ class TelegramSettings(private val app: TelegramApplication) {
showOnMapChats.intersect(presentChatIds)
this.showOnMapChats = showOnMapChats.toHashSet()
val chatIdsToDuration = HashMap<Long, Long>()
this.chatIdsToDuration.forEach { chatId, duration ->
if (presentChatIds.contains(chatId)) {
chatIdsToDuration[chatId] = duration
}
}
this.chatIdsToDuration = chatIdsToDuration.toMap()
chatIdsToDuration = chatIdsToDuration.filter { (key, _) ->
presentChatIds.contains(key)
}.toMutableMap()
}
fun shareLocationToChat(chatId: Long, share: Boolean) {
fun shareLocationToChat(chatId: Long, share: Boolean, duration: Long = DEFAULT_VISIBLE_TIME_SECONDS) {
val shareLocationChats = shareLocationChats.toMutableList()
val chatIdsToDuration = chatIdsToDuration.toMutableMap()
if (share) {
val lp: Long = when {
duration < TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong()
duration > TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong()
else -> duration
}
chatIdsToDuration[chatId] = lp
shareLocationChats.add(chatId)
chatIdsToDuration[chatId] = DEFAULT_VISIBLE_TIME_SECONDS
} else {
shareLocationChats.remove(chatId)
chatIdsToDuration.remove(chatId)
}
this.shareLocationChats = shareLocationChats.toHashSet()
this.chatIdsToDuration = chatIdsToDuration.toMap()
}
fun updateChatIdToDuration(chatId: Long, duration: Long) {
val chatIdsToDuration = chatIdsToDuration.toMutableMap()
val lp: Long = when {
duration < TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong()
duration > TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong()
else -> duration
}
chatIdsToDuration[chatId] = lp
this.chatIdsToDuration = chatIdsToDuration.toMap()
}
fun getChatExpireTime(chatId: Long): Long? {
return chatIdsToDuration[chatId]
}
fun getChatExpireTime(chatId: Long) = chatIdsToDuration[chatId]
fun stopSharingLocationToChats() {
this.shareLocationChats = emptySet()
this.chatIdsToDuration = emptyMap()
this.chatIdsToDuration.clear()
}
fun showChatOnMap(chatId: Long, show: Boolean) {

View file

@ -78,19 +78,14 @@ class SetTimeDialogFragment : DialogFragment() {
text = getString(R.string.shared_string_share)
setOnClickListener {
chatIdsToDuration.forEach { chatId, expireTime ->
settings.shareLocationToChat(chatId, true)
settings.updateChatIdToDuration(chatId, expireTime)
settings.shareLocationToChat(chatId, true, expireTime)
}
if (settings.hasAnyChatToShareLocation()) {
if (!AndroidUtils.isLocationPermissionAvailable(view.context)) {
AndroidUtils.requestLocationPermission(activity!!)
} else {
app.shareLocationHelper.startSharingLocation()
}
if (!AndroidUtils.isLocationPermissionAvailable(view.context)) {
AndroidUtils.requestLocationPermission(activity!!)
} else {
app.shareLocationHelper.stopSharingLocation()
app.shareLocationHelper.startSharingLocation()
dismiss()
}
dismiss()
}
}