cancel scope when onStop is reached

This is necessary especially to cancel the long polling when configuration change was made, e.g. screen was rotated. Otherwise multiple long polling requests would be running after configuration changes.

Because it not possible to launch a new coroutine in a scope that was canceled, it is necessary to re-initialize the scope.

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2025-01-22 13:50:30 +01:00
parent 504846593b
commit b6e341bbf1
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
4 changed files with 15 additions and 11 deletions

View File

@ -680,12 +680,10 @@ class ChatActivity :
val urlForChatting =
ApiUtils.getUrlForChat(chatApiVersion, conversationUser?.baseUrl, roomToken)
if (adapter?.isEmpty == true) {
chatViewModel.loadMessages(
withCredentials = credentials!!,
withUrl = urlForChatting
)
}
chatViewModel.loadMessages(
withCredentials = credentials!!,
withUrl = urlForChatting
)
} else {
Log.w(
TAG,

View File

@ -46,7 +46,7 @@ interface ChatMessageRepository : LifecycleAwareManager {
fun setData(conversationModel: ConversationModel, credentials: String, urlForChatting: String)
fun loadInitialMessages(withNetworkParams: Bundle): Job
fun initScopeAndLoadInitialMessages(withNetworkParams: Bundle)
/**
* Loads messages from local storage. If the messages are not found, then it

View File

@ -45,6 +45,7 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.retryWhen
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.io.IOException
import javax.inject.Inject
@ -111,7 +112,7 @@ class OfflineFirstChatRepository @Inject constructor(
private var newXChatLastCommonRead: Int? = null
private var itIsPaused = false
private val scope = CoroutineScope(Dispatchers.IO)
private lateinit var scope: CoroutineScope
lateinit var internalConversationId: String
private lateinit var conversationModel: ConversationModel
@ -125,7 +126,12 @@ class OfflineFirstChatRepository @Inject constructor(
internalConversationId = conversationModel.internalId
}
override fun loadInitialMessages(withNetworkParams: Bundle): Job =
override fun initScopeAndLoadInitialMessages(withNetworkParams: Bundle) {
scope = CoroutineScope(Dispatchers.IO)
loadInitialMessages(withNetworkParams)
}
private fun loadInitialMessages(withNetworkParams: Bundle): Job =
scope.launch {
Log.d(TAG, "---- loadInitialMessages ------------")
newXChatLastCommonRead = conversationModel.lastCommonReadMessage
@ -793,7 +799,7 @@ class OfflineFirstChatRepository @Inject constructor(
}
override fun handleOnStop() {
// unused atm
scope.cancel()
}
override fun handleChatOnBackPress() {

View File

@ -395,7 +395,7 @@ class ChatViewModel @Inject constructor(
val bundle = Bundle()
bundle.putString(BundleKeys.KEY_CHAT_URL, withUrl)
bundle.putString(BundleKeys.KEY_CREDENTIALS, withCredentials)
chatRepository.loadInitialMessages(
chatRepository.initScopeAndLoadInitialMessages(
withNetworkParams = bundle
)
}