From c71f9c81cd49c14528fe4fcece7c0f2044c1c158 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Fri, 20 May 2022 12:29:27 +0100 Subject: [PATCH] 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 --- .../java/im/vector/app/core/extensions/Job.kt | 33 +++++++++++++++++++ .../onboarding/OnboardingViewModel.kt | 21 +++++------- 2 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/core/extensions/Job.kt diff --git a/vector/src/main/java/im/vector/app/core/extensions/Job.kt b/vector/src/main/java/im/vector/app/core/extensions/Job.kt new file mode 100644 index 0000000000..d9a4332ef2 --- /dev/null +++ b/vector/src/main/java/im/vector/app/core/extensions/Job.kt @@ -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 = object : ReadWriteProperty { + 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 + } +} diff --git a/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt b/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt index a6c16415d0..a7caa9d0c2 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt @@ -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 } }