Provide a session scope, to launch coroutines without using the ViewModelScope

It could be used elsewhere, will change other location in another commit.
This commit is contained in:
Benoit Marty 2021-04-19 10:54:54 +02:00 committed by Benoit Marty
parent 95639927a7
commit 82ee2567b7
3 changed files with 23 additions and 2 deletions

View File

@ -272,4 +272,9 @@ interface Session :
* Maintenance API, allows to print outs info on DB size to logcat
*/
fun logDbUsageInfo()
/**
* Launch a coroutine using the session scope
*/
fun launch(block: suspend () -> Unit )
}

View File

@ -19,12 +19,16 @@ package org.matrix.android.sdk.internal.session
import androidx.annotation.MainThread
import dagger.Lazy
import io.realm.RealmConfiguration
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import org.matrix.android.sdk.api.auth.data.SessionParams
import org.matrix.android.sdk.api.failure.GlobalError
import org.matrix.android.sdk.api.federation.FederationService
import org.matrix.android.sdk.api.pushrules.PushRuleService
import org.matrix.android.sdk.api.session.initsync.InitialSyncProgressService
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.account.AccountService
import org.matrix.android.sdk.api.session.accountdata.AccountDataService
@ -38,6 +42,7 @@ import org.matrix.android.sdk.api.session.file.ContentDownloadStateTracker
import org.matrix.android.sdk.api.session.file.FileService
import org.matrix.android.sdk.api.session.group.GroupService
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilitiesService
import org.matrix.android.sdk.api.session.initsync.InitialSyncProgressService
import org.matrix.android.sdk.api.session.integrationmanager.IntegrationManagerService
import org.matrix.android.sdk.api.session.media.MediaService
import org.matrix.android.sdk.api.session.permalinks.PermalinkService
@ -155,10 +160,13 @@ internal class DefaultSession @Inject constructor(
override val isOpenable: Boolean
get() = sessionParamsStore.get(sessionId)?.isTokenValid ?: false
private var sessionScope: CoroutineScope? = null
@MainThread
override fun open() {
assert(!isOpen)
isOpen = true
sessionScope = CoroutineScope(SupervisorJob())
cryptoService.get().ensureDevice()
uiHandler.post {
lifecycleObservers.forEach { it.onSessionStarted() }
@ -199,6 +207,8 @@ internal class DefaultSession @Inject constructor(
override fun close() {
assert(isOpen)
sessionScope?.coroutineContext?.cancelChildren(CancellationException("Closing session"))
sessionScope = null
stopSync()
// timelineEventDecryptor.destroy()
uiHandler.post {
@ -301,4 +311,10 @@ internal class DefaultSession @Inject constructor(
override fun logDbUsageInfo() {
RealmDebugTools(realmConfiguration).logInfo("Session")
}
override fun launch(block: suspend () -> Unit) {
sessionScope?.launch {
block()
}
}
}

View File

@ -1318,7 +1318,7 @@ class RoomDetailViewModel @AssistedInject constructor(
}
}
bufferedMostRecentDisplayedEvent.root.eventId?.let { eventId ->
viewModelScope.launch {
session.launch {
tryOrNull { room.setReadReceipt(eventId) }
}
}