Merge pull request #5856 from osmandapp/ShareLocationImprovements
Share location improvements
This commit is contained in:
commit
3f7a968e8a
5 changed files with 122 additions and 49 deletions
|
@ -47,7 +47,8 @@ private const val TITLES_REPLACED_WITH_IDS = "changed_to_chat_id"
|
|||
|
||||
class TelegramSettings(private val app: TelegramApplication) {
|
||||
|
||||
var chatLivePeriods = mutableMapOf<Long, Long>()
|
||||
private var chatLivePeriods = mutableMapOf<Long, Long>()
|
||||
var chatShareLocStartSec = 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()
|
||||
|
||||
chatShareLocStartSec = chatShareLocStartSec.filter { (key, _) ->
|
||||
presentChatIds.contains(key)
|
||||
}.toMutableMap()
|
||||
}
|
||||
|
||||
fun shareLocationToChat(
|
||||
|
@ -101,19 +106,44 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
else -> livePeriod
|
||||
}
|
||||
chatLivePeriods[chatId] = lp
|
||||
chatShareLocStartSec[chatId] = (System.currentTimeMillis()/1000)
|
||||
shareLocationChats.add(chatId)
|
||||
} else {
|
||||
shareLocationChats.remove(chatId)
|
||||
chatLivePeriods.remove(chatId)
|
||||
chatShareLocStartSec.remove(chatId)
|
||||
}
|
||||
this.shareLocationChats = shareLocationChats.toHashSet()
|
||||
}
|
||||
|
||||
fun getChatLivePeriod(chatId: Long) = chatLivePeriods[chatId]
|
||||
|
||||
fun getChatLivePeriods(): Map<Long, Long> {
|
||||
return chatLivePeriods.filter {
|
||||
getChatLiveMessageExpireTime(it.key) > 0
|
||||
}
|
||||
}
|
||||
|
||||
fun getChatShareLocStartSec(chatId: Long) = chatShareLocStartSec[chatId]
|
||||
|
||||
fun getChatLiveMessageExpireTime(chatId: Long): Long {
|
||||
val startTime = getChatShareLocStartSec(chatId)
|
||||
val livePeriod = getChatLivePeriod(chatId)
|
||||
return if (startTime != null && livePeriod != null) {
|
||||
livePeriod - ((System.currentTimeMillis() / 1000) - startTime)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
fun updateChatShareLocStartSec(chatId: Long, startTime: Long) {
|
||||
chatShareLocStartSec[chatId] = startTime
|
||||
}
|
||||
|
||||
fun stopSharingLocationToChats() {
|
||||
this.shareLocationChats = emptySet()
|
||||
this.chatLivePeriods.clear()
|
||||
this.chatShareLocStartSec.clear()
|
||||
}
|
||||
|
||||
fun showChatOnMap(chatId: Long, show: Boolean) {
|
||||
|
|
|
@ -38,7 +38,7 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
|||
lastLocation = location
|
||||
|
||||
if (location != null && app.isInternetConnectionAvailable) {
|
||||
val chatLivePeriods = app.settings.chatLivePeriods
|
||||
val chatLivePeriods = app.settings.getChatLivePeriods()
|
||||
if (chatLivePeriods.isNotEmpty()) {
|
||||
app.telegramHelper.sendLiveLocationMessage(chatLivePeriods, location.latitude, location.longitude)
|
||||
}
|
||||
|
|
|
@ -540,6 +540,23 @@ class TelegramHelper private constructor() {
|
|||
return false
|
||||
}
|
||||
|
||||
fun stopSendingLiveLocationToChat(chatId: Long) {
|
||||
val msgId = chatLiveMessages[chatId]?.id
|
||||
if (msgId != null && msgId != 0L) {
|
||||
client?.send(
|
||||
TdApi.EditMessageLiveLocation(chatId, msgId, null, null),
|
||||
liveLocationMessageUpdatesHandler
|
||||
)
|
||||
}
|
||||
needRefreshActiveLiveLocationMessages = true
|
||||
}
|
||||
|
||||
fun stopSendingLiveLocationMessages() {
|
||||
chatLiveMessages.forEach { chatId, _ ->
|
||||
stopSendingLiveLocationToChat(chatId)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getActiveLiveLocationMessages(onComplete: (() -> Unit)?) {
|
||||
requestingActiveLiveLocationMessages = true
|
||||
client?.send(TdApi.GetActiveLiveLocationMessages()) { obj ->
|
||||
|
|
|
@ -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 {
|
||||
|
@ -70,6 +73,8 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
|
||||
private var sharingMode = false
|
||||
|
||||
private var updateEnable: Boolean = false
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
|
@ -180,6 +185,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
sharingMode = isChecked
|
||||
app.settings.stopSharingLocationToChats()
|
||||
app.shareLocationHelper.stopSharingLocation()
|
||||
telegramHelper.stopSendingLiveLocationMessages()
|
||||
updateContent()
|
||||
}
|
||||
}
|
||||
|
@ -201,10 +207,13 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
super.onResume()
|
||||
telegramHelper.addChatLiveMessagesListener(this)
|
||||
updateContent()
|
||||
updateEnable = true
|
||||
startHandler()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
updateEnable = false
|
||||
telegramHelper.removeChatLiveMessagesListener(this)
|
||||
}
|
||||
|
||||
|
@ -280,6 +289,29 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
}
|
||||
}
|
||||
|
||||
private fun startHandler() {
|
||||
val updateAdapter = Handler()
|
||||
updateAdapter.postDelayed({
|
||||
if (sharingMode && updateEnable) {
|
||||
val iterator = adapter.chats.iterator()
|
||||
while (iterator.hasNext()) {
|
||||
val chat = iterator.next()
|
||||
if (app.settings.getChatLiveMessageExpireTime(chat.id) <= 0) {
|
||||
app.settings.shareLocationToChat(chat.id, false)
|
||||
iterator.remove()
|
||||
}
|
||||
}
|
||||
if (adapter.chats.isNotEmpty()) {
|
||||
adapter.notifyDataSetChanged()
|
||||
} else {
|
||||
sharingMode = false
|
||||
updateContent()
|
||||
}
|
||||
}
|
||||
startHandler()
|
||||
}, ADAPTER_UPDATE_INTERVAL_MIL)
|
||||
}
|
||||
|
||||
private fun animateStartSharingBtn(show: Boolean) {
|
||||
if (startSharingBtn.visibility == View.VISIBLE) {
|
||||
val scale = if (show) 1f else 0f
|
||||
|
@ -393,8 +425,15 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
for (chatId in chatList) {
|
||||
val chat = telegramHelper.getChat(chatId)
|
||||
if (chat != null) {
|
||||
if (settings.isSharingLocationToChat(chatId) && !sharingMode) {
|
||||
continue
|
||||
if (settings.isSharingLocationToChat(chatId)) {
|
||||
if (sharingMode) {
|
||||
val message = telegramHelper.getChatLiveMessages()[chat.id]
|
||||
if (message != null) {
|
||||
settings.updateChatShareLocStartSec(chatId, message.date.toLong())
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
} else if (telegramHelper.isPrivateChat(chat)) {
|
||||
if ((chat.type as TdApi.ChatTypePrivate).userId == currentUser?.id) {
|
||||
continue
|
||||
|
@ -405,7 +444,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
}
|
||||
adapter.chats = chats
|
||||
}
|
||||
|
||||
|
||||
inner class MyLocationListAdapter : RecyclerView.Adapter<MyLocationListAdapter.BaseViewHolder>() {
|
||||
var chats = mutableListOf<TdApi.Chat>()
|
||||
set(value) {
|
||||
|
@ -484,7 +523,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
setOnCheckedChangeListener { _, isChecked ->
|
||||
if (!isChecked) {
|
||||
app.settings.shareLocationToChat(chat.id, false)
|
||||
app.shareLocationHelper.stopSharingLocation()
|
||||
telegramHelper.stopSendingLiveLocationToChat(chat.id)
|
||||
removeItem(chat)
|
||||
}
|
||||
}
|
||||
|
@ -498,50 +537,36 @@ 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
|
||||
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
|
||||
}
|
||||
app.settings.shareLocationToChat(chat.id, true, newLivePeriod)
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
}
|
||||
holder.stopSharingDescr?.apply {
|
||||
visibility = View.VISIBLE
|
||||
text = "${getText(R.string.stop_at)}:"
|
||||
}
|
||||
|
||||
holder.stopSharingFirstPart?.apply {
|
||||
visibility = View.VISIBLE
|
||||
text = OsmandFormatter.getFormattedTime(expiresIn)
|
||||
}
|
||||
|
||||
holder.stopSharingSecondPart?.apply {
|
||||
visibility = View.VISIBLE
|
||||
text = "(${getString(R.string.in_time,
|
||||
OsmandFormatter.getFormattedDuration(context!!, expiresIn.toLong(), true))})"
|
||||
}
|
||||
if (expiresIn == 0) {
|
||||
removeItem(chat)
|
||||
}
|
||||
val expiresIn = app.settings.getChatLiveMessageExpireTime(chat.id)
|
||||
|
||||
holder.textInArea?.apply {
|
||||
visibility = View.VISIBLE
|
||||
text = "${getText(R.string.plus)} ${OsmandFormatter.getFormattedDuration(context!!,
|
||||
MESSAGE_ADD_ACTIVE_TIME_SEC)}"
|
||||
setOnClickListener {
|
||||
val newLivePeriod = app.settings.getChatLiveMessageExpireTime(chat.id) + MESSAGE_ADD_ACTIVE_TIME_SEC
|
||||
telegramHelper.stopSendingLiveLocationToChat(chat.id)
|
||||
app.settings.shareLocationToChat(chat.id, true, newLivePeriod)
|
||||
app.forceUpdateMyLocation()
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
} else {
|
||||
holder.textInArea?.visibility = View.INVISIBLE
|
||||
holder.stopSharingDescr?.visibility = View.INVISIBLE
|
||||
holder.stopSharingFirstPart?.visibility = View.INVISIBLE
|
||||
holder.stopSharingSecondPart?.visibility = View.INVISIBLE
|
||||
}
|
||||
|
||||
holder.stopSharingDescr?.apply {
|
||||
visibility = View.VISIBLE
|
||||
text = "${getText(R.string.stop_at)}:"
|
||||
}
|
||||
|
||||
holder.stopSharingFirstPart?.apply {
|
||||
visibility = View.VISIBLE
|
||||
text = OsmandFormatter.getFormattedTime(expiresIn)
|
||||
}
|
||||
|
||||
holder.stopSharingSecondPart?.apply {
|
||||
visibility = View.VISIBLE
|
||||
text = "(${getString(R.string.in_time,
|
||||
OsmandFormatter.getFormattedDuration(context!!, expiresIn, true))})"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -551,6 +576,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
|
|||
if (chats.isEmpty()) {
|
||||
sharingMode = false
|
||||
updateContent()
|
||||
app.shareLocationHelper.stopSharingLocation()
|
||||
} else {
|
||||
adapter.notifyDataSetChanged()
|
||||
}
|
||||
|
|
|
@ -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