Replace IsCurrentSessionUseCase by using DeviceFullInfo field
This commit is contained in:
parent
04ad2daa80
commit
8d870b8220
@ -1,30 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.features.settings.devices.v2
|
|
||||||
|
|
||||||
import im.vector.app.core.di.ActiveSessionHolder
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
class IsCurrentSessionUseCase @Inject constructor(
|
|
||||||
private val activeSessionHolder: ActiveSessionHolder,
|
|
||||||
) {
|
|
||||||
|
|
||||||
fun execute(deviceId: String): Boolean {
|
|
||||||
val currentDeviceId = activeSessionHolder.getSafeActiveSession()?.sessionParams?.deviceId.orEmpty()
|
|
||||||
return deviceId.isNotEmpty() && deviceId == currentDeviceId
|
|
||||||
}
|
|
||||||
}
|
|
@ -136,17 +136,20 @@ class SessionOverviewFragment :
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun invalidate() = withState(viewModel) { state ->
|
override fun invalidate() = withState(viewModel) { state ->
|
||||||
updateToolbar(state.isCurrentSession)
|
updateToolbar(state)
|
||||||
updateEntryDetails(state.deviceId)
|
updateEntryDetails(state.deviceId)
|
||||||
updateSessionInfo(state)
|
updateSessionInfo(state)
|
||||||
updateLoading(state.isLoading)
|
updateLoading(state.isLoading)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateToolbar(isCurrentSession: Boolean) {
|
private fun updateToolbar(viewState: SessionOverviewViewState) {
|
||||||
val titleResId = if (isCurrentSession) R.string.device_manager_current_session_title else R.string.device_manager_session_title
|
if (viewState.deviceInfo is Success) {
|
||||||
(activity as? AppCompatActivity)
|
val titleResId =
|
||||||
?.supportActionBar
|
if (viewState.deviceInfo.invoke().isCurrentDevice) R.string.device_manager_current_session_title else R.string.device_manager_session_title
|
||||||
?.setTitle(titleResId)
|
(activity as? AppCompatActivity)
|
||||||
|
?.supportActionBar
|
||||||
|
?.setTitle(titleResId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateEntryDetails(deviceId: String) {
|
private fun updateEntryDetails(deviceId: String) {
|
||||||
@ -158,10 +161,11 @@ class SessionOverviewFragment :
|
|||||||
private fun updateSessionInfo(viewState: SessionOverviewViewState) {
|
private fun updateSessionInfo(viewState: SessionOverviewViewState) {
|
||||||
if (viewState.deviceInfo is Success) {
|
if (viewState.deviceInfo is Success) {
|
||||||
views.sessionOverviewInfo.isVisible = true
|
views.sessionOverviewInfo.isVisible = true
|
||||||
val isCurrentSession = viewState.isCurrentSession
|
val deviceInfo = viewState.deviceInfo.invoke()
|
||||||
|
val isCurrentSession = deviceInfo.isCurrentDevice
|
||||||
val infoViewState = SessionInfoViewState(
|
val infoViewState = SessionInfoViewState(
|
||||||
isCurrentSession = isCurrentSession,
|
isCurrentSession = isCurrentSession,
|
||||||
deviceFullInfo = viewState.deviceInfo.invoke(),
|
deviceFullInfo = deviceInfo,
|
||||||
isVerifyButtonVisible = isCurrentSession || viewState.isCurrentSessionTrusted,
|
isVerifyButtonVisible = isCurrentSession || viewState.isCurrentSessionTrusted,
|
||||||
isDetailsButtonVisible = false,
|
isDetailsButtonVisible = false,
|
||||||
isLearnMoreLinkVisible = true,
|
isLearnMoreLinkVisible = true,
|
||||||
|
@ -27,7 +27,6 @@ import im.vector.app.core.di.MavericksAssistedViewModelFactory
|
|||||||
import im.vector.app.core.di.hiltMavericksViewModelFactory
|
import im.vector.app.core.di.hiltMavericksViewModelFactory
|
||||||
import im.vector.app.core.resources.StringProvider
|
import im.vector.app.core.resources.StringProvider
|
||||||
import im.vector.app.features.auth.PendingAuthHandler
|
import im.vector.app.features.auth.PendingAuthHandler
|
||||||
import im.vector.app.features.settings.devices.v2.IsCurrentSessionUseCase
|
|
||||||
import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase
|
import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase
|
||||||
import im.vector.app.features.settings.devices.v2.VectorSessionsListViewModel
|
import im.vector.app.features.settings.devices.v2.VectorSessionsListViewModel
|
||||||
import im.vector.app.features.settings.devices.v2.signout.InterceptSignoutFlowResponseUseCase
|
import im.vector.app.features.settings.devices.v2.signout.InterceptSignoutFlowResponseUseCase
|
||||||
@ -42,6 +41,7 @@ import kotlinx.coroutines.launch
|
|||||||
import org.matrix.android.sdk.api.auth.UIABaseAuth
|
import org.matrix.android.sdk.api.auth.UIABaseAuth
|
||||||
import org.matrix.android.sdk.api.auth.UserInteractiveAuthInterceptor
|
import org.matrix.android.sdk.api.auth.UserInteractiveAuthInterceptor
|
||||||
import org.matrix.android.sdk.api.auth.registration.RegistrationFlowResponse
|
import org.matrix.android.sdk.api.auth.registration.RegistrationFlowResponse
|
||||||
|
import org.matrix.android.sdk.api.extensions.orFalse
|
||||||
import org.matrix.android.sdk.api.failure.Failure
|
import org.matrix.android.sdk.api.failure.Failure
|
||||||
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
|
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
|
||||||
import org.matrix.android.sdk.api.session.uia.DefaultBaseAuth
|
import org.matrix.android.sdk.api.session.uia.DefaultBaseAuth
|
||||||
@ -51,7 +51,6 @@ import kotlin.coroutines.Continuation
|
|||||||
|
|
||||||
class SessionOverviewViewModel @AssistedInject constructor(
|
class SessionOverviewViewModel @AssistedInject constructor(
|
||||||
@Assisted val initialState: SessionOverviewViewState,
|
@Assisted val initialState: SessionOverviewViewState,
|
||||||
private val isCurrentSessionUseCase: IsCurrentSessionUseCase,
|
|
||||||
private val stringProvider: StringProvider,
|
private val stringProvider: StringProvider,
|
||||||
private val getDeviceFullInfoUseCase: GetDeviceFullInfoUseCase,
|
private val getDeviceFullInfoUseCase: GetDeviceFullInfoUseCase,
|
||||||
private val checkIfCurrentSessionCanBeVerifiedUseCase: CheckIfCurrentSessionCanBeVerifiedUseCase,
|
private val checkIfCurrentSessionCanBeVerifiedUseCase: CheckIfCurrentSessionCanBeVerifiedUseCase,
|
||||||
@ -72,17 +71,10 @@ class SessionOverviewViewModel @AssistedInject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
setState {
|
|
||||||
copy(isCurrentSession = isCurrentSession(deviceId))
|
|
||||||
}
|
|
||||||
observeSessionInfo(initialState.deviceId)
|
observeSessionInfo(initialState.deviceId)
|
||||||
observeCurrentSessionInfo()
|
observeCurrentSessionInfo()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isCurrentSession(deviceId: String): Boolean {
|
|
||||||
return isCurrentSessionUseCase.execute(deviceId)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun observeSessionInfo(deviceId: String) {
|
private fun observeSessionInfo(deviceId: String) {
|
||||||
getDeviceFullInfoUseCase.execute(deviceId)
|
getDeviceFullInfoUseCase.execute(deviceId)
|
||||||
.onEach { setState { copy(deviceInfo = Success(it)) } }
|
.onEach { setState { copy(deviceInfo = Success(it)) } }
|
||||||
@ -114,7 +106,7 @@ class SessionOverviewViewModel @AssistedInject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun handleVerifySessionAction() = withState { viewState ->
|
private fun handleVerifySessionAction() = withState { viewState ->
|
||||||
if (viewState.isCurrentSession) {
|
if (viewState.deviceInfo.invoke()?.isCurrentDevice.orFalse()) {
|
||||||
handleVerifyCurrentSession()
|
handleVerifyCurrentSession()
|
||||||
} else {
|
} else {
|
||||||
handleVerifyOtherSession(viewState.deviceId)
|
handleVerifyOtherSession(viewState.deviceId)
|
||||||
|
@ -23,7 +23,6 @@ import im.vector.app.features.settings.devices.v2.DeviceFullInfo
|
|||||||
|
|
||||||
data class SessionOverviewViewState(
|
data class SessionOverviewViewState(
|
||||||
val deviceId: String,
|
val deviceId: String,
|
||||||
val isCurrentSession: Boolean = false,
|
|
||||||
val isCurrentSessionTrusted: Boolean = false,
|
val isCurrentSessionTrusted: Boolean = false,
|
||||||
val deviceInfo: Async<DeviceFullInfo> = Uninitialized,
|
val deviceInfo: Async<DeviceFullInfo> = Uninitialized,
|
||||||
val isLoading: Boolean = false,
|
val isLoading: Boolean = false,
|
||||||
|
@ -1,77 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.features.settings.devices.v2
|
|
||||||
|
|
||||||
import im.vector.app.test.fakes.FakeActiveSessionHolder
|
|
||||||
import io.mockk.verify
|
|
||||||
import org.amshove.kluent.shouldBe
|
|
||||||
import org.junit.Test
|
|
||||||
import org.matrix.android.sdk.api.auth.data.SessionParams
|
|
||||||
|
|
||||||
private const val A_SESSION_ID_1 = "session-id-1"
|
|
||||||
private const val A_SESSION_ID_2 = "session-id-2"
|
|
||||||
|
|
||||||
class IsCurrentSessionUseCaseTest {
|
|
||||||
|
|
||||||
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
|
|
||||||
|
|
||||||
private val isCurrentSessionUseCase = IsCurrentSessionUseCase(
|
|
||||||
activeSessionHolder = fakeActiveSessionHolder.instance,
|
|
||||||
)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `given the session id of the current session when checking if id is current session then result is true`() {
|
|
||||||
// Given
|
|
||||||
val sessionParams = givenIdForCurrentSession(A_SESSION_ID_1)
|
|
||||||
|
|
||||||
// When
|
|
||||||
val result = isCurrentSessionUseCase.execute(A_SESSION_ID_1)
|
|
||||||
|
|
||||||
// Then
|
|
||||||
result shouldBe true
|
|
||||||
verify { sessionParams.deviceId }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `given a session id different from the current session id when checking if id is current session then result is false`() {
|
|
||||||
// Given
|
|
||||||
val sessionParams = givenIdForCurrentSession(A_SESSION_ID_1)
|
|
||||||
|
|
||||||
// When
|
|
||||||
val result = isCurrentSessionUseCase.execute(A_SESSION_ID_2)
|
|
||||||
|
|
||||||
// Then
|
|
||||||
result shouldBe false
|
|
||||||
verify { sessionParams.deviceId }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `given no current active session when checking if id is current session then result is false`() {
|
|
||||||
// Given
|
|
||||||
fakeActiveSessionHolder.givenGetSafeActiveSessionReturns(null)
|
|
||||||
|
|
||||||
// When
|
|
||||||
val result = isCurrentSessionUseCase.execute(A_SESSION_ID_1)
|
|
||||||
|
|
||||||
// Then
|
|
||||||
result shouldBe false
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun givenIdForCurrentSession(sessionId: String): SessionParams {
|
|
||||||
return fakeActiveSessionHolder.fakeSession.givenSessionId(sessionId)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user