Skip to content

Commit 9b756d4

Browse files
committed
Fixed bugs w/ lastKnown and async work and refactored
Signed-off-by: rapterjet2004 <[email protected]>
1 parent 18bf7bb commit 9b756d4

File tree

4 files changed

+54
-47
lines changed

4 files changed

+54
-47
lines changed

app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ interface ChatMessageRepository : LifecycleAwareManager {
5959
withNetworkParams: Bundle
6060
): Job
6161

62-
fun updateRoomMessages(roomToken: String, limit: Int)
62+
suspend fun updateRoomMessages(internalConversationId: String, limit: Int)
6363

6464
/**
6565
* Long polls the server for any updates to the chat, if found, it synchronizes

app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt

+14-15
Original file line numberDiff line numberDiff line change
@@ -258,22 +258,21 @@ class OfflineFirstChatRepository @Inject constructor(
258258
updateUiForLastCommonRead()
259259
}
260260

261-
override fun updateRoomMessages(roomToken: String, limit: Int) {
262-
scope.launch {
263-
Log.d(TAG, "---- updateRoomMessages ------------")
264-
val fieldMap = getFieldMap(
265-
lookIntoFuture = false,
266-
timeout = 0,
267-
includeLastKnown = false,
268-
setReadMarker = true,
269-
lastKnown = null,
270-
limit = limit
271-
)
261+
override suspend fun updateRoomMessages(internalConversationId: String, limit: Int) {
262+
val lastKnown = chatDao.getNewestMessageId(internalConversationId)
263+
264+
Log.d(TAG, "---- updateRoomMessages ------------ with lastKnown: $lastKnown")
265+
val fieldMap = getFieldMap(
266+
lookIntoFuture = true,
267+
timeout = 0,
268+
includeLastKnown = false,
269+
setReadMarker = true,
270+
lastKnown = lastKnown.toInt(),
271+
)
272272

273-
val networkParams = bundleOf()
274-
networkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)
275-
sync(networkParams)
276-
}
273+
val networkParams = bundleOf()
274+
networkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)
275+
sync(networkParams)
277276
}
278277

279278
override fun initMessagePolling(initialMessageId: Long): Job =

app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt

+1-29
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ class ConversationsListActivity :
388388
conversationsListViewModel.getRoomsFlow
389389
.onEach { list ->
390390
// Refreshes conversation messages in the background asynchronously
391-
list.refreshMessages()
391+
conversationsListViewModel.updateRoomMessages(credentials!!, conversationItems, list)
392392

393393
// Update Conversations
394394
conversationItems.clear()
@@ -463,34 +463,6 @@ class ConversationsListActivity :
463463
updateFilterConversationButtonColor()
464464
}
465465

466-
private fun List<ConversationModel>.refreshMessages() {
467-
val previous = conversationItems.associate {
468-
(it as ConversationItem)
469-
val unreadMessages = it.model.unreadMessages
470-
val roomToken = it.model.token
471-
Pair(roomToken, unreadMessages)
472-
}
473-
474-
val current = this.associateWith { model ->
475-
val unreadMessages = model.unreadMessages
476-
unreadMessages
477-
}
478-
479-
val result = current.map { (model, unreadMessages) ->
480-
val previousUnreadMessages = previous[model.token] // Check if this conversation exists in last list
481-
previousUnreadMessages?.let {
482-
Pair(model, unreadMessages - previousUnreadMessages)
483-
}
484-
}.filterNotNull()
485-
486-
val baseUrl = userManager.currentUser.blockingGet().baseUrl!!
487-
for (pair in result) {
488-
if (pair.second > 0) {
489-
conversationsListViewModel.updateRoomMessages(pair.first, pair.second, credentials!!, baseUrl)
490-
}
491-
}
492-
}
493-
494466
private fun filter(conversation: ConversationModel): Boolean {
495467
var result = true
496468
for ((k, v) in filterState) {

app/src/main/java/com/nextcloud/talk/conversationlist/viewmodels/ConversationsListViewModel.kt

+38-2
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@ import android.util.Log
1010
import androidx.lifecycle.LiveData
1111
import androidx.lifecycle.MutableLiveData
1212
import androidx.lifecycle.ViewModel
13+
import androidx.lifecycle.viewModelScope
14+
import com.nextcloud.talk.adapters.items.ConversationItem
1315
import com.nextcloud.talk.chat.data.ChatMessageRepository
1416
import com.nextcloud.talk.conversationlist.data.OfflineConversationsRepository
1517
import com.nextcloud.talk.invitation.data.InvitationsModel
1618
import com.nextcloud.talk.invitation.data.InvitationsRepository
1719
import com.nextcloud.talk.models.domain.ConversationModel
1820
import com.nextcloud.talk.users.UserManager
1921
import com.nextcloud.talk.utils.ApiUtils
22+
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem
2023
import io.reactivex.Observer
2124
import io.reactivex.android.schedulers.AndroidSchedulers
2225
import io.reactivex.disposables.Disposable
2326
import io.reactivex.schedulers.Schedulers
27+
import kotlinx.coroutines.Dispatchers
2428
import kotlinx.coroutines.flow.catch
2529
import kotlinx.coroutines.flow.onEach
30+
import kotlinx.coroutines.launch
2631
import javax.inject.Inject
2732

2833
class ConversationsListViewModel @Inject constructor(
@@ -88,10 +93,41 @@ class ConversationsListViewModel @Inject constructor(
8893
repository.getRooms()
8994
}
9095

91-
fun updateRoomMessages(model: ConversationModel, limit: Int, credentials: String, baseUrl: String) {
96+
fun updateRoomMessages(credentials: String, oldList: MutableList<AbstractFlexibleItem<*>>, list: List<ConversationModel>) {
97+
val previous = oldList.associate {
98+
(it as ConversationItem)
99+
val unreadMessages = it.model.unreadMessages
100+
val roomToken = it.model.token
101+
Pair(roomToken, unreadMessages)
102+
}
103+
104+
val current = list.associateWith { model ->
105+
val unreadMessages = model.unreadMessages
106+
unreadMessages
107+
}
108+
109+
val result = current.map { (model, unreadMessages) ->
110+
val previousUnreadMessages = previous[model.token] // Check if this conversation exists in last list
111+
previousUnreadMessages?.let {
112+
Pair(model, unreadMessages - previousUnreadMessages)
113+
}
114+
}.filterNotNull()
115+
val baseUrl = userManager.currentUser.blockingGet().baseUrl!!
116+
117+
viewModelScope.launch(Dispatchers.IO) {
118+
for (pair in result) {
119+
if (pair.second > 0) {
120+
updateRoomMessage(pair.first, pair.second, credentials, baseUrl)
121+
}
122+
}
123+
}
124+
125+
}
126+
127+
private suspend fun updateRoomMessage(model: ConversationModel, limit: Int, credentials: String, baseUrl: String) {
92128
val urlForChatting = ApiUtils.getUrlForChat(1, baseUrl, model.token) // FIXME v1?
93129
chatRepository.setData(model, credentials, urlForChatting)
94-
chatRepository.updateRoomMessages(model.token, limit)
130+
chatRepository.updateRoomMessages(model.internalId, limit)
95131
}
96132

97133
inner class FederatedInvitationsObserver : Observer<InvitationsModel> {

0 commit comments

Comments
 (0)