From ad33ae102399cb2a621ff61998e52864422a212b Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 2 Oct 2018 14:58:55 +0300 Subject: [PATCH] Add increasing values for adding sharing time --- .../net/osmand/telegram/TelegramSettings.kt | 32 ++++++++++++++++++- .../telegram/ui/MyLocationTabFragment.kt | 11 +++---- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 0753799ed3..e46c70d4f4 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -26,6 +26,7 @@ private val LOC_HISTORY_VALUES_SEC = listOf( 12 * 60 * 60L, 24 * 60 * 60L ) +private val MESSAGE_ADD_ACTIVE_TIME_VALUES_SEC = listOf(15 * 60L, 30 * 60L, 60 * 60L, 180 * 60L) private const val SEND_MY_LOC_DEFAULT_INDEX = 6 private const val STALE_LOC_DEFAULT_INDEX = 4 @@ -55,6 +56,8 @@ class TelegramSettings(private val app: TelegramApplication) { private var chatLivePeriods = mutableMapOf() private var chatShareLocStartSec = mutableMapOf() + + private var chatShareAddActiveTime = mutableMapOf() private var shareLocationChats: Set = emptySet() private var hiddenOnMapChats: Set = emptySet() @@ -99,6 +102,10 @@ class TelegramSettings(private val app: TelegramApplication) { presentChatIds.contains(key) }.toMutableMap() + chatShareAddActiveTime = chatShareAddActiveTime.filter { (key, _) -> + presentChatIds.contains(key) + }.toMutableMap() + chatShareLocStartSec = chatShareLocStartSec.filter { (key, _) -> presentChatIds.contains(key) }.toMutableMap() @@ -107,7 +114,8 @@ class TelegramSettings(private val app: TelegramApplication) { fun shareLocationToChat( chatId: Long, share: Boolean, - livePeriod: Long = DEFAULT_VISIBLE_TIME_SECONDS + livePeriod: Long = DEFAULT_VISIBLE_TIME_SECONDS, + addActiveTime: Long = MESSAGE_ADD_ACTIVE_TIME_VALUES_SEC[0] ) { val shareLocationChats = shareLocationChats.toMutableList() if (share) { @@ -118,17 +126,34 @@ class TelegramSettings(private val app: TelegramApplication) { } chatLivePeriods[chatId] = lp chatShareLocStartSec[chatId] = (System.currentTimeMillis() / 1000) + chatShareAddActiveTime[chatId] = addActiveTime shareLocationChats.add(chatId) } else { shareLocationChats.remove(chatId) chatLivePeriods.remove(chatId) chatShareLocStartSec.remove(chatId) + chatShareAddActiveTime.remove(chatId) } this.shareLocationChats = shareLocationChats.toHashSet() } fun getChatLivePeriod(chatId: Long) = chatLivePeriods[chatId] + fun getChatAddActiveTime(chatId: Long) = chatShareAddActiveTime[chatId] ?: MESSAGE_ADD_ACTIVE_TIME_VALUES_SEC[0] + + fun getChatNextAddActiveTime(chatId: Long): Long { + return if (chatShareAddActiveTime.containsKey(chatId)) { + var index = MESSAGE_ADD_ACTIVE_TIME_VALUES_SEC.indexOf(chatShareAddActiveTime[chatId]) + if (MESSAGE_ADD_ACTIVE_TIME_VALUES_SEC.lastIndex > index) { + MESSAGE_ADD_ACTIVE_TIME_VALUES_SEC[++index] + } else { + MESSAGE_ADD_ACTIVE_TIME_VALUES_SEC[index] + } + } else { + MESSAGE_ADD_ACTIVE_TIME_VALUES_SEC[0] + } + } + fun getChatLivePeriods(): Map { return chatLivePeriods.filter { getChatLiveMessageExpireTime(it.key) > 0 @@ -151,10 +176,15 @@ class TelegramSettings(private val app: TelegramApplication) { chatShareLocStartSec[chatId] = startTime } + fun updateChatAddActiveTime(chatId: Long, newTime: Long) { + chatShareAddActiveTime[chatId] = newTime + } + fun stopSharingLocationToChats() { this.shareLocationChats = emptySet() this.chatLivePeriods.clear() this.chatShareLocStartSec.clear() + this.chatShareAddActiveTime.clear() } fun showChatOnMap(chatId: Long, show: Boolean) { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt index 7a0e051cd9..3485a50211 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt @@ -30,8 +30,6 @@ private const val DEFAULT_CHAT = 0 private const val ADAPTER_UPDATE_INTERVAL_MIL = 5 * 1000L // 5 sec -private const val MESSAGE_ADD_ACTIVE_TIME_SEC = 30 * 60L // 30 min - class MyLocationTabFragment : Fragment(), TelegramListener { private var textMarginSmall: Int = 0 @@ -578,13 +576,14 @@ class MyLocationTabFragment : Fragment(), TelegramListener { holder.textInArea?.apply { visibility = View.VISIBLE - text = "${getText(R.string.plus)} ${OsmandFormatter.getFormattedDuration(context!!, - MESSAGE_ADD_ACTIVE_TIME_SEC)}" + text = "${getText(R.string.plus)} ${OsmandFormatter.getFormattedDuration( + context!!, settings.getChatAddActiveTime(chat.id))}" setOnClickListener { - val newLivePeriod = settings.getChatLiveMessageExpireTime(chat.id) + MESSAGE_ADD_ACTIVE_TIME_SEC + val chatNextAddTime = settings.getChatNextAddActiveTime(chat.id) + val newLivePeriod = settings.getChatLiveMessageExpireTime(chat.id) + settings.getChatAddActiveTime(chat.id) settings.shareLocationToChat(chat.id, false) telegramHelper.stopSendingLiveLocationToChat(chat.id) - settings.shareLocationToChat(chat.id, true, newLivePeriod) + settings.shareLocationToChat(chat.id, true, newLivePeriod, chatNextAddTime) app.forceUpdateMyLocation() notifyItemChanged(position) }