Pin code : add simple grace period
This commit is contained in:
@@ -21,6 +21,7 @@ import kotlinx.coroutines.flow.StateFlow
|
||||
interface PinStateService {
|
||||
val pinState: StateFlow<PinState>
|
||||
|
||||
suspend fun lock()
|
||||
suspend fun entersForeground()
|
||||
suspend fun entersBackground()
|
||||
suspend fun unlock()
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
class PinAuthenticationPresenter @Inject constructor(
|
||||
private val pinStateService PinStateService,
|
||||
private val pinStateService: PinStateService,
|
||||
private val coroutineScope: CoroutineScope,
|
||||
) : Presenter<PinAuthenticationState> {
|
||||
|
||||
@@ -33,7 +33,7 @@ class PinAuthenticationPresenter @Inject constructor(
|
||||
|
||||
fun handleEvents(event: PinAuthenticationEvents) {
|
||||
when (event) {
|
||||
PinAuthenticationEvents.Unlock -> coroutineScope.launch { pinStateDataSource.unlock() }
|
||||
PinAuthenticationEvents.Unlock -> coroutineScope.launch { pinStateService.unlock() }
|
||||
}
|
||||
}
|
||||
return PinAuthenticationState(
|
||||
|
||||
@@ -23,10 +23,16 @@ import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.di.SingleIn
|
||||
import io.element.android.libraries.featureflag.api.FeatureFlagService
|
||||
import io.element.android.libraries.featureflag.api.FeatureFlags
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val GRACE_PERIOD_IN_MILLIS = 90 * 1000L
|
||||
|
||||
@SingleIn(AppScope::class)
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DefaultPinStateService @Inject constructor(
|
||||
@@ -36,15 +42,24 @@ class DefaultPinStateService @Inject constructor(
|
||||
private val _pinState = MutableStateFlow<PinState>(PinState.Unlocked)
|
||||
override val pinState: StateFlow<PinState> = _pinState
|
||||
|
||||
private var lockJob: Job? = null
|
||||
|
||||
override suspend fun unlock() {
|
||||
if (featureFlagService.isFeatureEnabled(FeatureFlags.PinUnlock)) {
|
||||
_pinState.value = PinState.Unlocked
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun lock() {
|
||||
if (featureFlagService.isFeatureEnabled(FeatureFlags.PinUnlock)) {
|
||||
_pinState.value = PinState.Locked
|
||||
override suspend fun entersForeground() {
|
||||
lockJob?.cancel()
|
||||
}
|
||||
|
||||
override suspend fun entersBackground(): Unit = coroutineScope {
|
||||
lockJob = launch {
|
||||
if (featureFlagService.isFeatureEnabled(FeatureFlags.PinUnlock)) {
|
||||
delay(GRACE_PERIOD_IN_MILLIS)
|
||||
_pinState.value = PinState.Locked
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user