add chatLiveStartTimeSec and updates for adapter
This commit is contained in:
parent
ad7bd6d1c5
commit
965f908ace
3 changed files with 91 additions and 45 deletions
|
@ -48,6 +48,7 @@ private const val TITLES_REPLACED_WITH_IDS = "changed_to_chat_id"
|
|||
class TelegramSettings(private val app: TelegramApplication) {
|
||||
|
||||
var chatLivePeriods = mutableMapOf<Long, Long>()
|
||||
var chatLiveStartTimeSec = mutableMapOf<Long, Long>()
|
||||
|
||||
private var shareLocationChats: Set<Long> = emptySet()
|
||||
private var showOnMapChats: Set<Long> = emptySet()
|
||||
|
@ -86,6 +87,10 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
chatLivePeriods = chatLivePeriods.filter { (key, _) ->
|
||||
presentChatIds.contains(key)
|
||||
}.toMutableMap()
|
||||
|
||||
chatLiveStartTimeSec = chatLiveStartTimeSec.filter { (key, _) ->
|
||||
presentChatIds.contains(key)
|
||||
}.toMutableMap()
|
||||
}
|
||||
|
||||
fun shareLocationToChat(
|
||||
|
@ -101,19 +106,28 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
else -> livePeriod
|
||||
}
|
||||
chatLivePeriods[chatId] = lp
|
||||
chatLiveStartTimeSec[chatId] = (System.currentTimeMillis()/1000)
|
||||
shareLocationChats.add(chatId)
|
||||
} else {
|
||||
shareLocationChats.remove(chatId)
|
||||
chatLivePeriods.remove(chatId)
|
||||
chatLiveStartTimeSec.remove(chatId)
|
||||
}
|
||||
this.shareLocationChats = shareLocationChats.toHashSet()
|
||||
}
|
||||
|
||||
fun getChatLivePeriod(chatId: Long) = chatLivePeriods[chatId]
|
||||
|
||||
fun getChatLiveMessageStartTime(chatId: Long) = chatLiveStartTimeSec[chatId]
|
||||
|
||||
fun updateChatLiveMessageStartTime(chatId: Long, startTime: Long) {
|
||||
chatLiveStartTimeSec[chatId] = startTime
|
||||
}
|
||||
|
||||
fun stopSharingLocationToChats() {
|
||||
this.shareLocationChats = emptySet()
|
||||
this.chatLivePeriods.clear()
|
||||
this.chatLiveStartTimeSec.clear()
|
||||
}
|
||||
|
||||
fun showChatOnMap(chatId: Long, show: Boolean) {
|
||||
|
|
|
@ -6,6 +6,7 @@ import android.content.Intent
|
|||
import android.graphics.drawable.GradientDrawable
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.support.design.widget.AppBarLayout
|
||||
import android.support.v4.app.Fragment
|
||||
import android.support.v4.content.ContextCompat
|
||||
|
@ -28,6 +29,8 @@ private const val SELECTED_CHATS_KEY = "selected_chats"
|
|||
private const val SHARE_LOCATION_CHAT = 1
|
||||
private const val DEFAULT_CHAT = 0
|
||||
|
||||
private const val ADAPTER_UPDATE_INTERVAL_MIL = 30 * 1000L // 30 sec
|
||||
|
||||
private const val MESSAGE_ADD_ACTIVE_TIME_SEC = 30 * 60L // 30 min
|
||||
|
||||
class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesListener {
|
||||
|
@ -202,6 +205,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
super.onResume()
|
||||
telegramHelper.addChatLiveMessagesListener(this)
|
||||
updateContent()
|
||||
startAdapterUpdate()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
|
@ -281,6 +285,31 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
}
|
||||
}
|
||||
|
||||
private fun startAdapterUpdate() {
|
||||
val handler = Handler()
|
||||
handler.postDelayed(object : Runnable {
|
||||
override fun run() {
|
||||
if (sharingMode) {
|
||||
val iterator = adapter.chats.iterator()
|
||||
while (iterator.hasNext()) {
|
||||
val chat = iterator.next()
|
||||
if (getChatLiveMessageExpireTime(chat.id) <= 0) {
|
||||
app.settings.shareLocationToChat(chat.id, false)
|
||||
iterator.remove()
|
||||
}
|
||||
}
|
||||
if (adapter.chats.isNotEmpty()) {
|
||||
adapter.notifyDataSetChanged()
|
||||
} else {
|
||||
sharingMode = false
|
||||
updateContent()
|
||||
}
|
||||
}
|
||||
handler.postDelayed(this, ADAPTER_UPDATE_INTERVAL_MIL)
|
||||
}
|
||||
}, ADAPTER_UPDATE_INTERVAL_MIL)
|
||||
}
|
||||
|
||||
private fun animateStartSharingBtn(show: Boolean) {
|
||||
if (startSharingBtn.visibility == View.VISIBLE) {
|
||||
val scale = if (show) 1f else 0f
|
||||
|
@ -394,8 +423,15 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
for (chatId in chatList) {
|
||||
val chat = telegramHelper.getChat(chatId)
|
||||
if (chat != null) {
|
||||
if (settings.isSharingLocationToChat(chatId) && !sharingMode) {
|
||||
if (settings.isSharingLocationToChat(chatId)) {
|
||||
if (sharingMode) {
|
||||
val message = telegramHelper.getChatLiveMessages()[chat.id]
|
||||
if (message != null) {
|
||||
settings.updateChatLiveMessageStartTime(chatId, message.date.toLong())
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
} else if (telegramHelper.isPrivateChat(chat)) {
|
||||
if ((chat.type as TdApi.ChatTypePrivate).userId == currentUser?.id) {
|
||||
continue
|
||||
|
@ -407,6 +443,16 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
adapter.chats = chats
|
||||
}
|
||||
|
||||
private fun getChatLiveMessageExpireTime(chatId: Long): Long {
|
||||
val startTime = settings.getChatLiveMessageStartTime(chatId)
|
||||
val livePeriod = settings.getChatLivePeriod(chatId)
|
||||
return if (startTime != null && livePeriod != null) {
|
||||
livePeriod - ((System.currentTimeMillis() / 1000) - startTime)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
inner class MyLocationListAdapter : RecyclerView.Adapter<MyLocationListAdapter.BaseViewHolder>() {
|
||||
var chats = mutableListOf<TdApi.Chat>()
|
||||
set(value) {
|
||||
|
@ -499,26 +545,22 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
text = "${getText(R.string.sharing_time)}:"
|
||||
}
|
||||
}
|
||||
val message = telegramHelper.getChatLiveMessages()[chat.id]
|
||||
if (message != null) {
|
||||
val content = message.content
|
||||
if (content is TdApi.MessageLocation) {
|
||||
val expiresIn = content.expiresIn
|
||||
|
||||
val expiresIn = getChatLiveMessageExpireTime(chat.id)
|
||||
|
||||
holder.textInArea?.apply {
|
||||
visibility = View.VISIBLE
|
||||
text = "${getText(R.string.plus)} ${OsmandFormatter.getFormattedDuration(context!!,
|
||||
MESSAGE_ADD_ACTIVE_TIME_SEC)}"
|
||||
setOnClickListener {
|
||||
var newLivePeriod = app.settings.getChatLivePeriod(chat.id)
|
||||
if (newLivePeriod != null) {
|
||||
newLivePeriod += MESSAGE_ADD_ACTIVE_TIME_SEC
|
||||
} else {
|
||||
newLivePeriod = MESSAGE_ADD_ACTIVE_TIME_SEC
|
||||
}
|
||||
val newLivePeriod = getChatLiveMessageExpireTime(chat.id) + MESSAGE_ADD_ACTIVE_TIME_SEC
|
||||
telegramHelper.stopSendingLiveLocationToChat(chat.id)
|
||||
app.settings.shareLocationToChat(chat.id, true, newLivePeriod)
|
||||
app.forceUpdateMyLocation()
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
}
|
||||
|
||||
holder.stopSharingDescr?.apply {
|
||||
visibility = View.VISIBLE
|
||||
text = "${getText(R.string.stop_at)}:"
|
||||
|
@ -532,17 +574,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
holder.stopSharingSecondPart?.apply {
|
||||
visibility = View.VISIBLE
|
||||
text = "(${getString(R.string.in_time,
|
||||
OsmandFormatter.getFormattedDuration(context!!, expiresIn.toLong(), true))})"
|
||||
}
|
||||
if (expiresIn == 0) {
|
||||
removeItem(chat)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
holder.textInArea?.visibility = View.INVISIBLE
|
||||
holder.stopSharingDescr?.visibility = View.INVISIBLE
|
||||
holder.stopSharingFirstPart?.visibility = View.INVISIBLE
|
||||
holder.stopSharingSecondPart?.visibility = View.INVISIBLE
|
||||
OsmandFormatter.getFormattedDuration(context!!, expiresIn, true))})"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ object OsmandFormatter {
|
|||
}
|
||||
}
|
||||
|
||||
fun getFormattedTime(seconds: Int): String {
|
||||
fun getFormattedTime(seconds: Long): String {
|
||||
val calendar = Calendar.getInstance()
|
||||
calendar.timeInMillis = System.currentTimeMillis() + (seconds * 1000)
|
||||
return if (isSameDay(calendar, Calendar.getInstance())) {
|
||||
|
|
Loading…
Reference in a new issue