Update live locations list after receiving new location messages

This commit is contained in:
Alex Sytnyk 2018-07-02 12:15:57 +03:00
parent add6499830
commit 01a8fc7994
3 changed files with 36 additions and 11 deletions

View file

@ -17,7 +17,7 @@ import net.osmand.telegram.helpers.TelegramHelper.TelegramIncomingMessagesListen
import net.osmand.telegram.notifications.TelegramNotification.NotificationType
import net.osmand.telegram.utils.AndroidUtils
import org.drinkless.td.libcore.telegram.TdApi
import java.util.ArrayList
import java.util.*
import java.util.concurrent.Executors
class TelegramService : Service(), LocationListener, TelegramIncomingMessagesListener {
@ -78,7 +78,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
setupServiceErrorInterval()
app.telegramService = this
app.telegramHelper.incomingMessagesListener = this
app.telegramHelper.addIncomingMessagesListener(this)
if (isUsedByMyLocation(usedBy)) {
initLocationUpdates()
@ -108,7 +108,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
super.onDestroy()
val app = app()
app.telegramHelper.stopLiveMessagesUpdates()
app.telegramHelper.incomingMessagesListener = null
app.telegramHelper.removeIncomingMessagesListener(this)
app.telegramService = null
usedBy = 0

View file

@ -13,6 +13,7 @@ import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
import kotlin.collections.HashSet
class TelegramHelper private constructor() {
@ -73,7 +74,15 @@ class TelegramHelper private constructor() {
private var updateLiveMessagesExecutor: ScheduledExecutorService? = null
var listener: TelegramListener? = null
var incomingMessagesListener: TelegramIncomingMessagesListener? = null
private val incomingMessagesListeners = HashSet<TelegramIncomingMessagesListener>()
fun addIncomingMessagesListener(listener: TelegramIncomingMessagesListener) {
incomingMessagesListeners.add(listener)
}
fun removeIncomingMessagesListener(listener: TelegramIncomingMessagesListener) {
incomingMessagesListeners.remove(listener)
}
fun getChatList(): TreeSet<OrderedChat> {
synchronized(chatList) {
@ -261,7 +270,7 @@ class TelegramHelper private constructor() {
val updateLiveMessagesExecutor = Executors.newSingleThreadScheduledExecutor()
this.updateLiveMessagesExecutor = updateLiveMessagesExecutor
updateLiveMessagesExecutor.scheduleWithFixedDelay({
incomingMessagesListener?.updateLocationMessages()
incomingMessagesListeners.forEach { it.updateLocationMessages() }
}, UPDATE_LIVE_MESSAGES_INTERVAL_SEC, UPDATE_LIVE_MESSAGES_INTERVAL_SEC, TimeUnit.SECONDS);
}
@ -376,7 +385,7 @@ class TelegramHelper private constructor() {
usersLiveMessages[message.id] = message
}
}
incomingMessagesListener?.onReceiveChatLocationMessages(chatTitle, *messages)
incomingMessagesListeners.forEach { it.onReceiveChatLocationMessages(chatTitle, *messages) }
}
else -> listener?.onTelegramError(-1, "Receive wrong response from TDLib: $obj")
}
@ -787,7 +796,9 @@ class TelegramHelper private constructor() {
}
val chatTitle = chats[message.chatId]?.title
if (chatTitle != null) {
incomingMessagesListener?.onReceiveChatLocationMessages(chatTitle, message)
incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(chatTitle, message)
}
}
}
@ -799,7 +810,9 @@ class TelegramHelper private constructor() {
usersLiveMessages[message.id] = message
val chatTitle = chats[message.chatId]?.title
if (chatTitle != null) {
incomingMessagesListener?.onReceiveChatLocationMessages(chatTitle, message)
incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(chatTitle, message)
}
}
}
}

View file

@ -13,15 +13,14 @@ import android.widget.TextView
import android.widget.Toast
import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.helpers.TelegramHelper.TelegramAuthorizationState
import net.osmand.telegram.helpers.TelegramHelper.TelegramListener
import net.osmand.telegram.helpers.TelegramHelper.*
import net.osmand.telegram.helpers.TelegramUiHelper
import org.drinkless.td.libcore.telegram.TdApi
private const val CHAT_VIEW_TYPE = 0
private const val CONTACT_VIEW_TYPE = 1
class LiveNowTabFragment : Fragment(), TelegramListener {
class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessagesListener {
private val app: TelegramApplication
get() = activity?.application as TelegramApplication
@ -48,6 +47,12 @@ class LiveNowTabFragment : Fragment(), TelegramListener {
override fun onResume() {
super.onResume()
updateList()
telegramHelper.addIncomingMessagesListener(this)
}
override fun onPause() {
super.onPause()
telegramHelper.removeIncomingMessagesListener(this)
}
override fun onTelegramStatusChanged(
@ -88,6 +93,13 @@ class LiveNowTabFragment : Fragment(), TelegramListener {
override fun onSendLiveLocationError(code: Int, message: String) {
}
override fun onReceiveChatLocationMessages(chatTitle: String, vararg messages: TdApi.Message) {
updateList()
}
override fun updateLocationMessages() {
}
private fun updateList() {
val res = mutableListOf<Any>()
for ((id, messages) in telegramHelper.getMessagesByChatIds()) {