mirror of
https://github.com/element-hq/element-android
synced 2024-11-27 20:06:51 +03:00
Replace IsCurrentSessionUseCase by using DeviceFullInfo field
This commit is contained in:
parent
04ad2daa80
commit
8d870b8220
5 changed files with 14 additions and 126 deletions
|
@ -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 ->
|
||||
updateToolbar(state.isCurrentSession)
|
||||
updateToolbar(state)
|
||||
updateEntryDetails(state.deviceId)
|
||||
updateSessionInfo(state)
|
||||
updateLoading(state.isLoading)
|
||||
}
|
||||
|
||||
private fun updateToolbar(isCurrentSession: Boolean) {
|
||||
val titleResId = if (isCurrentSession) R.string.device_manager_current_session_title else R.string.device_manager_session_title
|
||||
(activity as? AppCompatActivity)
|
||||
?.supportActionBar
|
||||
?.setTitle(titleResId)
|
||||
private fun updateToolbar(viewState: SessionOverviewViewState) {
|
||||
if (viewState.deviceInfo is Success) {
|
||||
val titleResId =
|
||||
if (viewState.deviceInfo.invoke().isCurrentDevice) R.string.device_manager_current_session_title else R.string.device_manager_session_title
|
||||
(activity as? AppCompatActivity)
|
||||
?.supportActionBar
|
||||
?.setTitle(titleResId)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateEntryDetails(deviceId: String) {
|
||||
|
@ -158,10 +161,11 @@ class SessionOverviewFragment :
|
|||
private fun updateSessionInfo(viewState: SessionOverviewViewState) {
|
||||
if (viewState.deviceInfo is Success) {
|
||||
views.sessionOverviewInfo.isVisible = true
|
||||
val isCurrentSession = viewState.isCurrentSession
|
||||
val deviceInfo = viewState.deviceInfo.invoke()
|
||||
val isCurrentSession = deviceInfo.isCurrentDevice
|
||||
val infoViewState = SessionInfoViewState(
|
||||
isCurrentSession = isCurrentSession,
|
||||
deviceFullInfo = viewState.deviceInfo.invoke(),
|
||||
deviceFullInfo = deviceInfo,
|
||||
isVerifyButtonVisible = isCurrentSession || viewState.isCurrentSessionTrusted,
|
||||
isDetailsButtonVisible = false,
|
||||
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.resources.StringProvider
|
||||
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.VectorSessionsListViewModel
|
||||
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.UserInteractiveAuthInterceptor
|
||||
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.session.crypto.model.RoomEncryptionTrustLevel
|
||||
import org.matrix.android.sdk.api.session.uia.DefaultBaseAuth
|
||||
|
@ -51,7 +51,6 @@ import kotlin.coroutines.Continuation
|
|||
|
||||
class SessionOverviewViewModel @AssistedInject constructor(
|
||||
@Assisted val initialState: SessionOverviewViewState,
|
||||
private val isCurrentSessionUseCase: IsCurrentSessionUseCase,
|
||||
private val stringProvider: StringProvider,
|
||||
private val getDeviceFullInfoUseCase: GetDeviceFullInfoUseCase,
|
||||
private val checkIfCurrentSessionCanBeVerifiedUseCase: CheckIfCurrentSessionCanBeVerifiedUseCase,
|
||||
|
@ -72,17 +71,10 @@ class SessionOverviewViewModel @AssistedInject constructor(
|
|||
}
|
||||
|
||||
init {
|
||||
setState {
|
||||
copy(isCurrentSession = isCurrentSession(deviceId))
|
||||
}
|
||||
observeSessionInfo(initialState.deviceId)
|
||||
observeCurrentSessionInfo()
|
||||
}
|
||||
|
||||
private fun isCurrentSession(deviceId: String): Boolean {
|
||||
return isCurrentSessionUseCase.execute(deviceId)
|
||||
}
|
||||
|
||||
private fun observeSessionInfo(deviceId: String) {
|
||||
getDeviceFullInfoUseCase.execute(deviceId)
|
||||
.onEach { setState { copy(deviceInfo = Success(it)) } }
|
||||
|
@ -114,7 +106,7 @@ class SessionOverviewViewModel @AssistedInject constructor(
|
|||
}
|
||||
|
||||
private fun handleVerifySessionAction() = withState { viewState ->
|
||||
if (viewState.isCurrentSession) {
|
||||
if (viewState.deviceInfo.invoke()?.isCurrentDevice.orFalse()) {
|
||||
handleVerifyCurrentSession()
|
||||
} else {
|
||||
handleVerifyOtherSession(viewState.deviceId)
|
||||
|
|
|
@ -23,7 +23,6 @@ import im.vector.app.features.settings.devices.v2.DeviceFullInfo
|
|||
|
||||
data class SessionOverviewViewState(
|
||||
val deviceId: String,
|
||||
val isCurrentSession: Boolean = false,
|
||||
val isCurrentSessionTrusted: Boolean = false,
|
||||
val deviceInfo: Async<DeviceFullInfo> = Uninitialized,
|
||||
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 a new issue