Introducing a use case to configure and start a Session

This commit is contained in:
Maxime NATUREL 2022-10-03 14:49:37 +02:00
parent ce70ab8f0f
commit 1e6fb99e3c
7 changed files with 62 additions and 22 deletions

View File

@ -19,10 +19,10 @@ package im.vector.app.core.di
import android.content.Context
import arrow.core.Option
import im.vector.app.ActiveSessionDataSource
import im.vector.app.core.extensions.configureAndStart
import im.vector.app.core.extensions.startSyncing
import im.vector.app.core.pushers.UnifiedPushHelper
import im.vector.app.core.services.GuardServiceStarter
import im.vector.app.core.session.ConfigureAndStartSessionUseCase
import im.vector.app.features.call.webrtc.WebRtcCallManager
import im.vector.app.features.crypto.keysrequest.KeyRequestHandler
import im.vector.app.features.crypto.verification.IncomingVerificationRequestHandler
@ -50,6 +50,7 @@ class ActiveSessionHolder @Inject constructor(
private val sessionInitializer: SessionInitializer,
private val applicationContext: Context,
private val authenticationService: AuthenticationService,
private val configureAndStartSessionUseCase: ConfigureAndStartSessionUseCase,
) {
private var activeSessionReference: AtomicReference<Session?> = AtomicReference()
@ -109,7 +110,7 @@ class ActiveSessionHolder @Inject constructor(
}
?: sessionInitializer.tryInitialize(readCurrentSession = { activeSessionReference.get() }) { session ->
setActiveSession(session)
session.configureAndStart(applicationContext, startSyncing = startSync)
configureAndStartSessionUseCase.execute(session, startSyncing = startSync)
}
}

View File

@ -27,17 +27,6 @@ import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupState
import org.matrix.android.sdk.api.session.sync.FilterService
import timber.log.Timber
fun Session.configureAndStart(context: Context, startSyncing: Boolean = true) {
Timber.i("Configure and start session for $myUserId. startSyncing: $startSyncing")
open()
filterService().setFilter(FilterService.FilterPreset.ElementFilter)
if (startSyncing) {
startSyncing(context)
}
pushersService().refreshPushers()
context.singletonEntryPoint().webRtcCallManager().checkForProtocolsSupportIfNeeded()
}
fun Session.startSyncing(context: Context) {
val applicationContext = context.applicationContext
if (!syncService().hasAlreadySynced()) {

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* 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 im.vector.app.core.session
import android.content.Context
import dagger.hilt.android.qualifiers.ApplicationContext
import im.vector.app.core.extensions.startSyncing
import im.vector.app.features.call.webrtc.WebRtcCallManager
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.sync.FilterService
import timber.log.Timber
import javax.inject.Inject
class ConfigureAndStartSessionUseCase @Inject constructor(
@ApplicationContext private val context: Context,
private val webRtcCallManager: WebRtcCallManager,
) {
// TODO add unit tests
fun execute(session: Session, startSyncing: Boolean = true) {
Timber.i("Configure and start session for ${session.myUserId}. startSyncing: $startSyncing")
session.open()
session.filterService().setFilter(FilterService.FilterPreset.ElementFilter)
if (startSyncing) {
session.startSyncing(context)
}
session.pushersService().refreshPushers()
webRtcCallManager.checkForProtocolsSupportIfNeeded()
}
}

View File

@ -30,9 +30,9 @@ import im.vector.app.R
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.di.MavericksAssistedViewModelFactory
import im.vector.app.core.di.hiltMavericksViewModelFactory
import im.vector.app.core.extensions.configureAndStart
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.core.resources.StringProvider
import im.vector.app.core.session.ConfigureAndStartSessionUseCase
import im.vector.app.core.utils.ensureTrailingSlash
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
@ -64,7 +64,8 @@ class LoginViewModel @AssistedInject constructor(
private val homeServerConnectionConfigFactory: HomeServerConnectionConfigFactory,
private val reAuthHelper: ReAuthHelper,
private val stringProvider: StringProvider,
private val homeServerHistoryService: HomeServerHistoryService
private val homeServerHistoryService: HomeServerHistoryService,
private val configureAndStartSessionUseCase: ConfigureAndStartSessionUseCase,
) : VectorViewModel<LoginViewState, LoginAction, LoginViewEvents>(initialState) {
@AssistedFactory
@ -732,7 +733,7 @@ class LoginViewModel @AssistedInject constructor(
activeSessionHolder.setActiveSession(session)
authenticationService.reset()
session.configureAndStart(applicationContext)
configureAndStartSessionUseCase.execute(session)
setState {
copy(
asyncLoginAction = Success(Unit)

View File

@ -26,13 +26,13 @@ import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.di.MavericksAssistedViewModelFactory
import im.vector.app.core.di.hiltMavericksViewModelFactory
import im.vector.app.core.extensions.cancelCurrentOnSet
import im.vector.app.core.extensions.configureAndStart
import im.vector.app.core.extensions.inferNoConnectivity
import im.vector.app.core.extensions.isMatrixId
import im.vector.app.core.extensions.toReducedUrl
import im.vector.app.core.extensions.vectorStore
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.core.resources.StringProvider
import im.vector.app.core.session.ConfigureAndStartSessionUseCase
import im.vector.app.core.utils.ensureProtocol
import im.vector.app.core.utils.ensureTrailingSlash
import im.vector.app.features.VectorFeatures
@ -91,6 +91,7 @@ class OnboardingViewModel @AssistedInject constructor(
private val vectorOverrides: VectorOverrides,
private val registrationActionHandler: RegistrationActionHandler,
private val sdkIntProvider: BuildVersionSdkIntProvider,
private val configureAndStartSessionUseCase: ConfigureAndStartSessionUseCase,
) : VectorViewModel<OnboardingViewState, OnboardingAction, OnboardingViewEvents>(initialState) {
@AssistedFactory
@ -616,7 +617,7 @@ class OnboardingViewModel @AssistedInject constructor(
activeSessionHolder.setActiveSession(session)
authenticationService.reset()
session.configureAndStart(applicationContext)
configureAndStartSessionUseCase.execute(session)
when (authenticationDescription) {
is AuthenticationDescription.Register -> {

View File

@ -20,6 +20,7 @@ import android.net.Uri
import android.os.Build
import com.airbnb.mvrx.test.MavericksTestRule
import im.vector.app.R
import im.vector.app.core.session.ConfigureAndStartSessionUseCase
import im.vector.app.features.login.LoginConfig
import im.vector.app.features.login.LoginMode
import im.vector.app.features.login.ReAuthHelper
@ -50,6 +51,7 @@ import im.vector.app.test.fixtures.a401ServerError
import im.vector.app.test.fixtures.aHomeServerCapabilities
import im.vector.app.test.fixtures.anUnrecognisedCertificateError
import im.vector.app.test.test
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Before
@ -111,6 +113,7 @@ class OnboardingViewModelTest {
private val fakeStartAuthenticationFlowUseCase = FakeStartAuthenticationFlowUseCase()
private val fakeHomeServerHistoryService = FakeHomeServerHistoryService()
private val fakeLoginWizard = FakeLoginWizard()
private val fakeConfigureAndStartSessionUseCase = mockk<ConfigureAndStartSessionUseCase>()
private var initialState = OnboardingViewState()
private lateinit var viewModel: OnboardingViewModel
@ -1093,6 +1096,7 @@ class OnboardingViewModelTest {
FakeVectorOverrides(),
fakeRegistrationActionHandler.instance,
TestBuildVersionSdkIntProvider().also { it.value = Build.VERSION_CODES.O },
fakeConfigureAndStartSessionUseCase,
).also {
viewModel = it
initialState = state
@ -1132,7 +1136,7 @@ class OnboardingViewModelTest {
private fun givenInitialisesSession(session: Session) {
fakeActiveSessionHolder.expectSetsActiveSession(session)
fakeAuthenticationService.expectReset()
fakeSession.expectStartsSyncing()
fakeSession.expectStartsSyncing(fakeConfigureAndStartSessionUseCase)
}
private fun givenRegistrationResultFor(action: RegisterAction, result: RegistrationActionHandler.Result) {

View File

@ -16,9 +16,9 @@
package im.vector.app.test.fakes
import im.vector.app.core.extensions.configureAndStart
import im.vector.app.core.extensions.startSyncing
import im.vector.app.core.extensions.vectorStore
import im.vector.app.core.session.ConfigureAndStartSessionUseCase
import im.vector.app.features.session.VectorSessionStore
import im.vector.app.test.testCoroutineDispatchers
import io.mockk.coEvery
@ -71,9 +71,9 @@ class FakeSession(
}
}
fun expectStartsSyncing() {
fun expectStartsSyncing(configureAndStartSessionUseCase: ConfigureAndStartSessionUseCase) {
coJustRun {
this@FakeSession.configureAndStart(any(), startSyncing = true)
configureAndStartSessionUseCase.execute(this@FakeSession, startSyncing = true)
this@FakeSession.startSyncing(any())
}
}