Skip to content

Commit

Permalink
do not try to pull chat messages when offline or paused
Browse files Browse the repository at this point in the history
Otherwise, it resulted in a lot of flickering because _lastCommonReadFlow was emitted every 500ms. And anyway if we know we are offline or paused then it doesn't make sense to execute the sync method.

chain which caused the flickering was:
updateUiForLastCommonRead (_lastCommonReadFlow) -> updateReadStatusOfAllMessages - notifyDataSetChanged -> onBindViewHolder -> IncomingLinkPreviewMessageViewHolder

Signed-off-by: Marcel Hibbe <[email protected]>
  • Loading branch information
mahibi authored and backportbot[bot] committed Sep 12, 2024
1 parent c00eade commit ad71cf9
Showing 1 changed file with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,29 +199,31 @@ class OfflineFirstChatRepository @Inject constructor(
val networkParams = Bundle()

while (true) {
if (!monitor.isOnline.first() || itIsPaused) Thread.sleep(HALF_SECOND)

// sync database with server (This is a long blocking call because long polling (lookIntoFuture) is set)
networkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)

val resultsFromSync = sync(networkParams)
if (!resultsFromSync.isNullOrEmpty()) {
val chatMessages = resultsFromSync.map(ChatMessageEntity::asModel)
val pair = Pair(true, chatMessages)
_messageFlow.emit(pair)
}
if (!monitor.isOnline.first() || itIsPaused) {
Thread.sleep(HALF_SECOND)
} else {
// sync database with server (This is a long blocking call because long polling (lookIntoFuture) is set)
networkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)

val resultsFromSync = sync(networkParams)
if (!resultsFromSync.isNullOrEmpty()) {
val chatMessages = resultsFromSync.map(ChatMessageEntity::asModel)
val pair = Pair(true, chatMessages)
_messageFlow.emit(pair)
}

updateUiForLastCommonRead()
updateUiForLastCommonRead()

val newestMessage = chatDao.getNewestMessageId(internalConversationId).toInt()
val newestMessage = chatDao.getNewestMessageId(internalConversationId).toInt()

// update field map vars for next cycle
fieldMap = getFieldMap(
lookIntoFuture = true,
includeLastKnown = false,
setReadMarker = true,
lastKnown = newestMessage
)
// update field map vars for next cycle
fieldMap = getFieldMap(
lookIntoFuture = true,
includeLastKnown = false,
setReadMarker = true,
lastKnown = newestMessage
)
}
}
}

Expand Down

0 comments on commit ad71cf9

Please sign in to comment.