provides a dedicated job for the email verification polling to allow it to be cancelled when resetting the auth flow

- extracts an auto cancelling job delegate
This commit is contained in:
Adam Brown 2022-05-20 12:29:27 +01:00
parent 2378643071
commit c71f9c81cd
2 changed files with 41 additions and 13 deletions

View file

@ -0,0 +1,33 @@
/*
* 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.extensions
import kotlinx.coroutines.Job
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
/**
* Property delegate for automatically cancelling the current job when setting a new value.
*/
fun cancelCurrentOnSet(): ReadWriteProperty<Any?, Job?> = object : ReadWriteProperty<Any?, Job?> {
private var currentJob: Job? = null
override fun getValue(thisRef: Any?, property: KProperty<*>): Job? = currentJob
override fun setValue(thisRef: Any?, property: KProperty<*>, value: Job?) {
currentJob?.cancel()
currentJob = value
}
}

View file

@ -25,6 +25,7 @@ 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.cancelCurrentOnSet
import im.vector.app.core.extensions.configureAndStart
import im.vector.app.core.extensions.vectorStore
import im.vector.app.core.platform.VectorViewModel
@ -124,12 +125,8 @@ class OnboardingViewModel @AssistedInject constructor(
private var loginConfig: LoginConfig? = null
private var currentJob: Job? = null
set(value) {
// Cancel any previous Job
field?.cancel()
field = value
}
private var emailVerificationPollingJob: Job? by cancelCurrentOnSet()
private var currentJob: Job? by cancelCurrentOnSet()
override fun handle(action: OnboardingAction) {
when (action) {
@ -264,13 +261,10 @@ class OnboardingViewModel @AssistedInject constructor(
setState { copy(isLoading = false) }
}
// Allow email verification polling to coexist with other jobs
when (action) {
is RegisterAction.SendAgainThreePid -> {
// avoid cancelling the current job which is polling for verification complete
}
else -> {
currentJob = job
}
is RegisterAction.CheckIfEmailHasBeenValidated -> emailVerificationPollingJob = job
else -> currentJob = job
}
}
@ -317,6 +311,7 @@ class OnboardingViewModel @AssistedInject constructor(
private fun handleResetAction(action: OnboardingAction.ResetAction) {
// Cancel any request
currentJob = null
emailVerificationPollingJob = null
when (action) {
OnboardingAction.ResetHomeServerType -> {
@ -800,7 +795,7 @@ class OnboardingViewModel @AssistedInject constructor(
}
private fun cancelWaitForEmailValidation() {
currentJob = null
emailVerificationPollingJob = null
}
}