mirror of
https://github.com/nextcloud/talk-android
synced 2025-07-09 13:59:48 +01:00
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:
parent
beb7b150be
commit
e951b3d53a
@ -50,12 +50,13 @@ class OfflineFirstConversationsRepository @Inject constructor(
|
|||||||
|
|
||||||
override fun getRooms(): Job =
|
override fun getRooms(): Job =
|
||||||
scope.launch {
|
scope.launch {
|
||||||
repeat(2) {
|
val resultsFromSync = sync()
|
||||||
val list = getListOfConversations(user.id!!)
|
if (!resultsFromSync.isNullOrEmpty()) {
|
||||||
if (list.isNotEmpty()) {
|
val conversations = resultsFromSync.map(ConversationEntity::asModel)
|
||||||
_roomListFlow.emit(list)
|
_roomListFlow.emit(conversations)
|
||||||
}
|
} else {
|
||||||
sync()
|
val conversationsFromDb = getListOfConversations(user.id!!)
|
||||||
|
_roomListFlow.emit(conversationsFromDb)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,10 +67,12 @@ class OfflineFirstConversationsRepository @Inject constructor(
|
|||||||
model.let { _conversationFlow.emit(model) }
|
model.let { _conversationFlow.emit(model) }
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun sync() {
|
private suspend fun sync(): List<ConversationEntity>? {
|
||||||
|
var conversationsFromSync: List<ConversationEntity>? = null
|
||||||
|
|
||||||
if (!monitor.isOnline.first()) {
|
if (!monitor.isOnline.first()) {
|
||||||
Log.d(OfflineFirstChatRepository.TAG, "Device is offline, can't load conversations from server")
|
Log.d(OfflineFirstChatRepository.TAG, "Device is offline, can't load conversations from server")
|
||||||
return
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -78,13 +81,25 @@ class OfflineFirstConversationsRepository @Inject constructor(
|
|||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.blockingSingle()
|
.blockingSingle()
|
||||||
|
|
||||||
val list = conversationsList.map {
|
conversationsFromSync = conversationsList.map {
|
||||||
it.asEntity(user.id!!)
|
it.asEntity(user.id!!)
|
||||||
}
|
}
|
||||||
dao.upsertConversations(list)
|
|
||||||
|
deleteLeftConversations(conversationsFromSync)
|
||||||
|
dao.upsertConversations(conversationsFromSync)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "Something went wrong when fetching conversations", e)
|
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> =
|
private suspend fun getListOfConversations(accountId: Long): List<ConversationModel> =
|
||||||
|
@ -34,16 +34,16 @@ interface ConversationsDao {
|
|||||||
WHERE internalId in (:conversationIds)
|
WHERE internalId in (:conversationIds)
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
fun deleteConversation(conversationIds: List<Long>)
|
fun deleteConversations(conversationIds: List<String>)
|
||||||
|
|
||||||
@Update
|
@Update
|
||||||
fun updateConversation(conversationEntity: ConversationEntity)
|
fun updateConversation(conversationEntity: ConversationEntity)
|
||||||
|
|
||||||
@Query(
|
@Query(
|
||||||
"""
|
"""
|
||||||
DELETE FROM conversations
|
DELETE FROM Conversations
|
||||||
WHERE internalId LIKE :pattern
|
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 accountId = Objects.requireNonNull(user.getId()).toString();
|
||||||
String pattern = accountId + "@%"; // ... LIKE "<accountId>@%"
|
String pattern = accountId + "@%"; // ... LIKE "<accountId>@%"
|
||||||
chatMessagesDao.clearAllMessagesForUser(pattern);
|
chatMessagesDao.clearAllMessagesForUser(pattern);
|
||||||
conversationsDao.clearAllConversationsForUser(pattern);
|
conversationsDao.clearAllConversationsForUser(user.getId());
|
||||||
chatBlocksDao.clearChatBlocksForUser(pattern);
|
chatBlocksDao.clearChatBlocksForUser(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user