Skip to content

Commit 6ee450c

Browse files
mahibiAndyScherzinger
authored andcommitted
Avoid duplicated fetches when opening a chat (also calms down the loading bar)
Fix 1: When entering a chat, in onResume in ChatViewModel, fetchNewMessagesWithRetry was called. This is too aggressive pulling and should only be used when waiting for uploads to be finished. It is also not satisfied when the response is empty and will try again. UX wise this caused that the loading bar was shown for a long time. fetchNewMessagesWithRetry was replaced with chatRepository.fetchNewMessages() which does a single fetch which is enough in this case. Fix 2: As onResume is executed on every opening of a chat, it added unnecessary requests on top of the loadInitialMessages handling. As a guard to only execute it when coming back from background, isReturningFromBackground was introduced. In this way the scenario from #6313 is still fulfilled but too many requests on startup are avoided. Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
1 parent 697fdfb commit 6ee450c

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

‎app/src/main/java/com/nextcloud/talk/chat/viewmodels/ChatViewModel.kt‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,16 @@ class ChatViewModel @AssistedInject constructor(
221221

222222
override fun onResume(owner: LifecycleOwner) {
223223
super.onResume(owner)
224+
val isReturningFromBackground = ::currentLifeCycleFlag.isInitialized
224225
currentLifeCycleFlag = LifeCycleFlag.RESUMED
225226
mediaRecorderManager.handleOnResume()
226227
chatRepository.handleOnResume()
227228
mediaPlayerManager.handleOnResume()
228-
viewModelScope.launch { fetchNewMessagesWithRetry() }
229+
if (isReturningFromBackground) {
230+
viewModelScope.launch {
231+
chatRepository.fetchNewMessages()
232+
}
233+
}
229234
}
230235

231236
override fun onPause(owner: LifecycleOwner) {

0 commit comments

Comments
 (0)