Introduce `SessionState` to be able to check the Session state from several places.

This commit is contained in:
Benoit Marty 2022-05-30 18:26:44 +02:00 committed by Benoit Marty
parent d3784c8019
commit 1bad8f7741
3 changed files with 42 additions and 11 deletions

View File

@ -131,12 +131,11 @@ internal class DefaultSession @Inject constructor(
private val toDeviceService: Lazy<ToDeviceService>,
private val eventStreamService: Lazy<EventStreamService>,
@UnauthenticatedWithCertificate
private val unauthenticatedWithCertificateOkHttpClient: Lazy<OkHttpClient>
private val unauthenticatedWithCertificateOkHttpClient: Lazy<OkHttpClient>,
private val sessionState: SessionState,
) : Session,
GlobalErrorHandler.Listener {
private var isOpen = false
private val uiHandler = createUIHandler()
override val isOpenable: Boolean
@ -144,8 +143,7 @@ internal class DefaultSession @Inject constructor(
@MainThread
override fun open() {
assert(!isOpen)
isOpen = true
sessionState.setIsOpen(true)
globalErrorHandler.listener = this
cryptoService.get().ensureDevice()
uiHandler.post {
@ -159,7 +157,7 @@ internal class DefaultSession @Inject constructor(
}
override fun close() {
assert(isOpen)
assert(sessionState.isOpen)
syncService.get().stopSync()
// timelineEventDecryptor.destroy()
uiHandler.post {
@ -170,7 +168,7 @@ internal class DefaultSession @Inject constructor(
}
cryptoService.get().close()
globalErrorHandler.listener = null
isOpen = false
sessionState.setIsOpen(false)
}
override suspend fun clearCache() {

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.internal.session
import javax.inject.Inject
@SessionScope
internal class SessionState @Inject constructor() {
var isOpen = false
private set
/**
* Set the new state. Throw if already in the new state.
*/
fun setIsOpen(newState: Boolean) {
assert(newState != isOpen)
isOpen = newState
}
}

View File

@ -21,6 +21,7 @@ import org.matrix.android.sdk.api.session.sync.SyncRequestState
import org.matrix.android.sdk.api.session.sync.SyncService
import org.matrix.android.sdk.internal.di.SessionId
import org.matrix.android.sdk.internal.di.WorkManagerProvider
import org.matrix.android.sdk.internal.session.SessionState
import org.matrix.android.sdk.internal.session.sync.job.SyncThread
import org.matrix.android.sdk.internal.session.sync.job.SyncWorker
import timber.log.Timber
@ -33,6 +34,7 @@ internal class DefaultSyncService @Inject constructor(
private val syncThreadProvider: Provider<SyncThread>,
private val syncTokenStore: SyncTokenStore,
private val syncRequestStateTracker: SyncRequestStateTracker,
private val sessionState: SessionState,
) : SyncService {
private var syncThread: SyncThread? = null
@ -50,8 +52,7 @@ internal class DefaultSyncService @Inject constructor(
override fun startSync(fromForeground: Boolean) {
Timber.i("Starting sync thread")
// TODO How to check that now?
// assert(isOpen)
assert(sessionState.isOpen)
val localSyncThread = getSyncThread()
localSyncThread.setInitialForeground(fromForeground)
if (!localSyncThread.isAlive) {
@ -63,8 +64,7 @@ internal class DefaultSyncService @Inject constructor(
}
override fun stopSync() {
// TODO How to check that now?
// assert(isOpen)
assert(sessionState.isOpen)
syncThread?.kill()
syncThread = null
}