From 9b756d46db668007fbac5fa12c5b123b799010d6 Mon Sep 17 00:00:00 2001 From: rapterjet2004 Date: Tue, 10 Dec 2024 11:16:06 -0600 Subject: [PATCH] Fixed bugs w/ lastKnown and async work and refactored Signed-off-by: rapterjet2004 --- .../talk/chat/data/ChatMessageRepository.kt | 2 +- .../network/OfflineFirstChatRepository.kt | 29 +++++++------- .../ConversationsListActivity.kt | 30 +------------- .../viewmodels/ConversationsListViewModel.kt | 40 ++++++++++++++++++- 4 files changed, 54 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt b/app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt index c7bf685f7d..e59cddbfa2 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt @@ -59,7 +59,7 @@ interface ChatMessageRepository : LifecycleAwareManager { withNetworkParams: Bundle ): Job - fun updateRoomMessages(roomToken: String, limit: Int) + suspend fun updateRoomMessages(internalConversationId: String, limit: Int) /** * Long polls the server for any updates to the chat, if found, it synchronizes diff --git a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt index 23eb0c63bd..89544aaed3 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt @@ -258,22 +258,21 @@ class OfflineFirstChatRepository @Inject constructor( updateUiForLastCommonRead() } - override fun updateRoomMessages(roomToken: String, limit: Int) { - scope.launch { - Log.d(TAG, "---- updateRoomMessages ------------") - val fieldMap = getFieldMap( - lookIntoFuture = false, - timeout = 0, - includeLastKnown = false, - setReadMarker = true, - lastKnown = null, - limit = limit - ) + override suspend fun updateRoomMessages(internalConversationId: String, limit: Int) { + val lastKnown = chatDao.getNewestMessageId(internalConversationId) + + Log.d(TAG, "---- updateRoomMessages ------------ with lastKnown: $lastKnown") + val fieldMap = getFieldMap( + lookIntoFuture = true, + timeout = 0, + includeLastKnown = false, + setReadMarker = true, + lastKnown = lastKnown.toInt(), + ) - val networkParams = bundleOf() - networkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap) - sync(networkParams) - } + val networkParams = bundleOf() + networkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap) + sync(networkParams) } override fun initMessagePolling(initialMessageId: Long): Job = diff --git a/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt b/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt index 6a56e8ffca..4baf4a94f1 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt @@ -388,7 +388,7 @@ class ConversationsListActivity : conversationsListViewModel.getRoomsFlow .onEach { list -> // Refreshes conversation messages in the background asynchronously - list.refreshMessages() + conversationsListViewModel.updateRoomMessages(credentials!!, conversationItems, list) // Update Conversations conversationItems.clear() @@ -463,34 +463,6 @@ class ConversationsListActivity : updateFilterConversationButtonColor() } - private fun List.refreshMessages() { - val previous = conversationItems.associate { - (it as ConversationItem) - val unreadMessages = it.model.unreadMessages - val roomToken = it.model.token - Pair(roomToken, unreadMessages) - } - - val current = this.associateWith { model -> - val unreadMessages = model.unreadMessages - unreadMessages - } - - val result = current.map { (model, unreadMessages) -> - val previousUnreadMessages = previous[model.token] // Check if this conversation exists in last list - previousUnreadMessages?.let { - Pair(model, unreadMessages - previousUnreadMessages) - } - }.filterNotNull() - - val baseUrl = userManager.currentUser.blockingGet().baseUrl!! - for (pair in result) { - if (pair.second > 0) { - conversationsListViewModel.updateRoomMessages(pair.first, pair.second, credentials!!, baseUrl) - } - } - } - private fun filter(conversation: ConversationModel): Boolean { var result = true for ((k, v) in filterState) { diff --git a/app/src/main/java/com/nextcloud/talk/conversationlist/viewmodels/ConversationsListViewModel.kt b/app/src/main/java/com/nextcloud/talk/conversationlist/viewmodels/ConversationsListViewModel.kt index a80ddf824a..1b52594c0d 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationlist/viewmodels/ConversationsListViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationlist/viewmodels/ConversationsListViewModel.kt @@ -10,6 +10,8 @@ import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.nextcloud.talk.adapters.items.ConversationItem import com.nextcloud.talk.chat.data.ChatMessageRepository import com.nextcloud.talk.conversationlist.data.OfflineConversationsRepository import com.nextcloud.talk.invitation.data.InvitationsModel @@ -17,12 +19,15 @@ import com.nextcloud.talk.invitation.data.InvitationsRepository import com.nextcloud.talk.models.domain.ConversationModel import com.nextcloud.talk.users.UserManager import com.nextcloud.talk.utils.ApiUtils +import eu.davidea.flexibleadapter.items.AbstractFlexibleItem import io.reactivex.Observer import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.launch import javax.inject.Inject class ConversationsListViewModel @Inject constructor( @@ -88,10 +93,41 @@ class ConversationsListViewModel @Inject constructor( repository.getRooms() } - fun updateRoomMessages(model: ConversationModel, limit: Int, credentials: String, baseUrl: String) { + fun updateRoomMessages(credentials: String, oldList: MutableList>, list: List) { + val previous = oldList.associate { + (it as ConversationItem) + val unreadMessages = it.model.unreadMessages + val roomToken = it.model.token + Pair(roomToken, unreadMessages) + } + + val current = list.associateWith { model -> + val unreadMessages = model.unreadMessages + unreadMessages + } + + val result = current.map { (model, unreadMessages) -> + val previousUnreadMessages = previous[model.token] // Check if this conversation exists in last list + previousUnreadMessages?.let { + Pair(model, unreadMessages - previousUnreadMessages) + } + }.filterNotNull() + val baseUrl = userManager.currentUser.blockingGet().baseUrl!! + + viewModelScope.launch(Dispatchers.IO) { + for (pair in result) { + if (pair.second > 0) { + updateRoomMessage(pair.first, pair.second, credentials, baseUrl) + } + } + } + + } + + private suspend fun updateRoomMessage(model: ConversationModel, limit: Int, credentials: String, baseUrl: String) { val urlForChatting = ApiUtils.getUrlForChat(1, baseUrl, model.token) // FIXME v1? chatRepository.setData(model, credentials, urlForChatting) - chatRepository.updateRoomMessages(model.token, limit) + chatRepository.updateRoomMessages(model.internalId, limit) } inner class FederatedInvitationsObserver : Observer {