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