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 MY_LOCATION_TAB_POS = 0
private const val LIVE_NOW_TAB_POS = 1 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) private val log = PlatformUtil.getLog(TelegramHelper::class.java)
@ -143,6 +143,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
if (telegramHelper.listener != this) { if (telegramHelper.listener != this) {
telegramHelper.listener = this telegramHelper.listener = this
} }
telegramHelper.addChatLiveMessagesListener(this)
app.locationProvider.checkIfLastKnownLocationIsValid() app.locationProvider.checkIfLastKnownLocationIsValid()
@ -159,6 +160,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
telegramHelper.listener = null telegramHelper.listener = null
telegramHelper.removeChatLiveMessagesListener(this)
app.locationProvider.pauseAllUpdates() 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) { override fun switchButtonsVisibility(visible: Boolean) {
val buttonsVisibility = if (visible) View.VISIBLE else View.GONE val buttonsVisibility = if (visible) View.VISIBLE else View.GONE
if (buttonsBar.visibility != buttonsVisibility) { if (buttonsBar.visibility != buttonsVisibility) {
@ -308,6 +314,16 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
} }
} }
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)) { private fun runOnUi(action: (() -> Unit)) {
if (!paused) { if (!paused) {
runOnUiThread(action) runOnUiThread(action)

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