Skip to content

Commit

Permalink
fix: disable back press when app lock team enforced (WPB-5644) (#2481)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: alexandreferris <[email protected]>
  • Loading branch information
3 people authored Nov 29, 2023
1 parent 7019f0d commit 2dd5d83
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.wire.android.ui.home.appLock.set

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -61,6 +62,7 @@ import com.wire.android.ui.common.spacers.HorizontalSpace
import com.wire.android.ui.common.spacers.VerticalSpace
import com.wire.android.ui.common.textfield.WirePasswordTextField
import com.wire.android.ui.common.textfield.WireTextFieldState
import com.wire.android.ui.common.topappbar.NavigationIconType
import com.wire.android.ui.common.topappbar.WireCenterAlignedTopAppBar
import com.wire.android.ui.theme.WireTheme
import com.wire.android.ui.theme.wireColorScheme
Expand Down Expand Up @@ -109,6 +111,7 @@ fun SetLockCodeScreenContent(
topBar = {
WireCenterAlignedTopAppBar(
onNavigationPressed = onBackPress,
navigationIconType = if (state.isEditable) NavigationIconType.Back else null,
elevation = dimensions().spacing0x,
title = stringResource(id = R.string.settings_set_lock_screen_title)
)
Expand Down Expand Up @@ -173,6 +176,12 @@ fun SetLockCodeScreenContent(
}
}
}

BackHandler {
if (state.isEditable) {
onBackPress()
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ data class SetLockCodeViewState(
val password: TextFieldValue = TextFieldValue(),
val passwordValidation: ValidatePasswordResult = ValidatePasswordResult.Invalid(),
val timeout: Duration = ObserveAppLockConfigUseCase.DEFAULT_APP_LOCK_TIMEOUT,
val done: Boolean = false
val done: Boolean = false,
val isEditable: Boolean = true
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.android.util.sha256
import com.wire.kalium.logic.feature.applock.MarkTeamAppLockStatusAsNotifiedUseCase
import com.wire.kalium.logic.feature.auth.ValidatePasswordUseCase
import com.wire.kalium.logic.feature.featureConfig.IsAppLockEditableUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
Expand All @@ -40,7 +41,8 @@ class SetLockScreenViewModel @Inject constructor(
private val validatePassword: ValidatePasswordUseCase,
private val globalDataStore: GlobalDataStore,
private val dispatchers: DispatcherProvider,
private val observeAppLockConfigUseCase: ObserveAppLockConfigUseCase,
private val observeAppLockConfig: ObserveAppLockConfigUseCase,
private val isAppLockEditable: IsAppLockEditableUseCase,
private val markTeamAppLockStatusAsNotified: MarkTeamAppLockStatusAsNotifiedUseCase
) : ViewModel() {

Expand All @@ -49,10 +51,12 @@ class SetLockScreenViewModel @Inject constructor(

init {
viewModelScope.launch {
observeAppLockConfigUseCase()
val isEditable = isAppLockEditable()
observeAppLockConfig()
.collectLatest {
state = state.copy(
timeout = it.timeout
timeout = it.timeout,
isEditable = isEditable
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ import com.wire.android.feature.ObserveAppLockConfigUseCase
import com.wire.kalium.logic.feature.applock.MarkTeamAppLockStatusAsNotifiedUseCase
import com.wire.kalium.logic.feature.auth.ValidatePasswordResult
import com.wire.kalium.logic.feature.auth.ValidatePasswordUseCase
import com.wire.kalium.logic.feature.featureConfig.IsAppLockEditableUseCase
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.verify
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(CoroutineTestExtension::class)
class SetLockScreenViewModelTest {

@Test
fun `given new password input, when valid,then should update state`() {
fun `given new password input, when valid,then should update state`() = runTest {
val (arrangement, viewModel) = Arrangement()
.withValidPassword()
.arrange()
Expand All @@ -53,7 +55,7 @@ class SetLockScreenViewModelTest {
}

@Test
fun `given new password input, when invalid,then should update state`() {
fun `given new password input, when invalid,then should update state`() = runTest {
val (arrangement, viewModel) = Arrangement()
.withInvalidPassword()
.arrange()
Expand All @@ -80,12 +82,16 @@ class SetLockScreenViewModelTest {
@MockK
private lateinit var markTeamAppLockStatusAsNotified: MarkTeamAppLockStatusAsNotifiedUseCase

@MockK
private lateinit var isAppLockEditable: IsAppLockEditableUseCase

init {
MockKAnnotations.init(this, relaxUnitFun = true)
coEvery { globalDataStore.setUserAppLock(any()) } returns Unit
coEvery { observeAppLockConfig() } returns flowOf(
AppLockConfig.Disabled(ObserveAppLockConfigUseCase.DEFAULT_APP_LOCK_TIMEOUT)
)
coEvery { isAppLockEditable() } returns true
}

fun withValidPassword() = apply {
Expand All @@ -101,6 +107,7 @@ class SetLockScreenViewModelTest {
globalDataStore,
TestDispatcherProvider(),
observeAppLockConfig,
isAppLockEditable,
markTeamAppLockStatusAsNotified
)

Expand Down

0 comments on commit 2dd5d83

Please sign in to comment.