fix to load newest conversations (online first)
Before, old conversations that were left still occurred in the list (only adding+updating was done, but never deleting) also, the list is up to date when coming back from chat. Otherwise there may be unread messages shown for a short moment which were already read. Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Родитель
beb7b150be
Коммит
e951b3d53a
|
@ -50,12 +50,13 @@ class OfflineFirstConversationsRepository @Inject constructor(
|
|||
|
||||
override fun getRooms(): Job =
|
||||
scope.launch {
|
||||
repeat(2) {
|
||||
val list = getListOfConversations(user.id!!)
|
||||
if (list.isNotEmpty()) {
|
||||
_roomListFlow.emit(list)
|
||||
}
|
||||
sync()
|
||||
val resultsFromSync = sync()
|
||||
if (!resultsFromSync.isNullOrEmpty()) {
|
||||
val conversations = resultsFromSync.map(ConversationEntity::asModel)
|
||||
_roomListFlow.emit(conversations)
|
||||
} else {
|
||||
val conversationsFromDb = getListOfConversations(user.id!!)
|
||||
_roomListFlow.emit(conversationsFromDb)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,10 +67,12 @@ class OfflineFirstConversationsRepository @Inject constructor(
|
|||
model.let { _conversationFlow.emit(model) }
|
||||
}
|
||||
|
||||
private suspend fun sync() {
|
||||
private suspend fun sync(): List<ConversationEntity>? {
|
||||
var conversationsFromSync: List<ConversationEntity>? = null
|
||||
|
||||
if (!monitor.isOnline.first()) {
|
||||
Log.d(OfflineFirstChatRepository.TAG, "Device is offline, can't load conversations from server")
|
||||
return
|
||||
return null
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -78,13 +81,25 @@ class OfflineFirstConversationsRepository @Inject constructor(
|
|||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.blockingSingle()
|
||||
|
||||
val list = conversationsList.map {
|
||||
conversationsFromSync = conversationsList.map {
|
||||
it.asEntity(user.id!!)
|
||||
}
|
||||
dao.upsertConversations(list)
|
||||
|
||||
deleteLeftConversations(conversationsFromSync)
|
||||
dao.upsertConversations(conversationsFromSync)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Something went wrong when fetching conversations", e)
|
||||
}
|
||||
return conversationsFromSync
|
||||
}
|
||||
|
||||
private suspend fun deleteLeftConversations(conversationsFromSync: List<ConversationEntity>) {
|
||||
val oldConversationsFromDb = dao.getConversationsForUser(user.id!!).first()
|
||||
|
||||
val conversationsToDelete = oldConversationsFromDb.filterNot { conversationsFromSync.contains(it) }
|
||||
val conversationIdsToDelete = conversationsToDelete.map { it.internalId }
|
||||
|
||||
dao.deleteConversations(conversationIdsToDelete)
|
||||
}
|
||||
|
||||
private suspend fun getListOfConversations(accountId: Long): List<ConversationModel> =
|
||||
|
|
|
@ -34,16 +34,16 @@ interface ConversationsDao {
|
|||
WHERE internalId in (:conversationIds)
|
||||
"""
|
||||
)
|
||||
fun deleteConversation(conversationIds: List<Long>)
|
||||
fun deleteConversations(conversationIds: List<String>)
|
||||
|
||||
@Update
|
||||
fun updateConversation(conversationEntity: ConversationEntity)
|
||||
|
||||
@Query(
|
||||
"""
|
||||
DELETE FROM conversations
|
||||
WHERE internalId LIKE :pattern
|
||||
DELETE FROM Conversations
|
||||
WHERE accountId = :accountId
|
||||
"""
|
||||
)
|
||||
fun clearAllConversationsForUser(pattern: String)
|
||||
fun clearAllConversationsForUser(accountId: Long)
|
||||
}
|
||||
|
|
|
@ -194,7 +194,7 @@ public class AccountRemovalWorker extends Worker {
|
|||
String accountId = Objects.requireNonNull(user.getId()).toString();
|
||||
String pattern = accountId + "@%"; // ... LIKE "<accountId>@%"
|
||||
chatMessagesDao.clearAllMessagesForUser(pattern);
|
||||
conversationsDao.clearAllConversationsForUser(pattern);
|
||||
conversationsDao.clearAllConversationsForUser(user.getId());
|
||||
chatBlocksDao.clearChatBlocksForUser(pattern);
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче