Skip to content

Commit

Permalink
Fixed bugs w/ lastKnown and async work and refactored
Browse files Browse the repository at this point in the history
Signed-off-by: rapterjet2004 <[email protected]>
  • Loading branch information
rapterjet2004 committed Dec 10, 2024
1 parent 18bf7bb commit 9b756d4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -463,34 +463,6 @@ class ConversationsListActivity :
updateFilterConversationButtonColor()
}

private fun List<ConversationModel>.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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ 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
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(
Expand Down Expand Up @@ -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<AbstractFlexibleItem<*>>, list: List<ConversationModel>) {
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<InvitationsModel> {
Expand Down

0 comments on commit 9b756d4

Please sign in to comment.