add ChatLiveMessagesListener to MainActivity

This commit is contained in:
Chumva 2018-08-15 15:00:09 +03:00
parent f1c4741076
commit 117837aa54
2 changed files with 22 additions and 16 deletions

View file

@ -32,7 +32,7 @@ private const val PERMISSION_REQUEST_LOCATION = 1
private const val MY_LOCATION_TAB_POS = 0
private const val LIVE_NOW_TAB_POS = 1
class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListener {
class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListener, ChatLiveMessagesListener {
private val log = PlatformUtil.getLog(TelegramHelper::class.java)
@ -143,6 +143,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
if (telegramHelper.listener != this) {
telegramHelper.listener = this
}
telegramHelper.addChatLiveMessagesListener(this)
app.locationProvider.checkIfLastKnownLocationIsValid()
@ -159,6 +160,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
override fun onPause() {
super.onPause()
telegramHelper.listener = null
telegramHelper.removeChatLiveMessagesListener(this)
app.locationProvider.pauseAllUpdates()
@ -238,6 +240,10 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
}
}
override fun onChatLiveMessagesUpdated(messages: List<TdApi.Message>) {
updateExistingLiveMessages()
}
override fun switchButtonsVisibility(visible: Boolean) {
val buttonsVisibility = if (visible) View.VISIBLE else View.GONE
if (buttonsBar.visibility != buttonsVisibility) {
@ -307,6 +313,16 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
show()
}
}
fun updateExistingLiveMessages() {
telegramHelper.getChatLiveMessages().values.forEach {
if (settings.isSharingLocationToChat(it.chatId)
&& (settings.getChatShareLocStartSec(it.chatId) == null || settings.getChatLivePeriod(it.chatId) == null)) {
settings.shareLocationToChat(it.chatId, true, (it.content as TdApi.MessageLocation).livePeriod.toLong())
settings.updateChatShareLocStartSec(it.chatId, it.date.toLong())
}
}
}
private fun runOnUi(action: (() -> Unit)) {
if (!paused) {

View file

@ -33,7 +33,7 @@ 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, ChatLiveMessagesListener {
class MyLocationTabFragment : Fragment(), TelegramListener {
private var textMarginSmall: Int = 0
private var textMarginBig: Int = 0
@ -206,7 +206,6 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
override fun onResume() {
super.onResume()
telegramHelper.addChatLiveMessagesListener(this)
telegramHelper.getActiveLiveLocationMessages(null)
updateContent()
updateEnable = true
@ -216,7 +215,6 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
override fun onPause() {
super.onPause()
updateEnable = false
telegramHelper.removeChatLiveMessagesListener(this)
}
override fun onSaveInstanceState(outState: Bundle) {
@ -272,10 +270,6 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
override fun onSendLiveLocationError(code: Int, message: String) {
}
override fun onChatLiveMessagesUpdated(messages: List<TdApi.Message>) {
updateExistingLiveMessages(messages)
}
fun onPrimaryBtnClick() {
if (selectedChats.isNotEmpty()) {
val fm = fragmentManager ?: return
@ -296,7 +290,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
updateAdapter.postDelayed({
if (updateEnable) {
if (sharingMode) {
updateExistingLiveMessages(telegramHelper.getChatLiveMessages().values.toList())
updateExistingLiveMessages()
val iterator = adapter.chats.iterator()
while (iterator.hasNext()) {
val chat = iterator.next()
@ -455,13 +449,9 @@ class MyLocationTabFragment : Fragment(), TelegramListener, ChatLiveMessagesList
}
}
private fun updateExistingLiveMessages(messages: List<TdApi.Message>) {
messages.forEach {
if (settings.isSharingLocationToChat(it.chatId)
&& (settings.getChatShareLocStartSec(it.chatId) == null || settings.getChatLivePeriod(it.chatId) == null)) {
settings.shareLocationToChat(it.chatId, true, (it.content as TdApi.MessageLocation).livePeriod.toLong())
settings.updateChatShareLocStartSec(it.chatId, it.date.toLong())
}
private fun updateExistingLiveMessages() {
if (activity is MainActivity) {
(activity as MainActivity).updateExistingLiveMessages()
}
sharingMode = settings.hasAnyChatToShareLocation()
if (!shareLocationHelper.sharingLocation && sharingMode && AndroidUtils.isLocationPermissionAvailable(app)) {