From 9e9cc432cecda5a11aa1dc2af28c771e06d7c724 Mon Sep 17 00:00:00 2001 From: Oussama Hassine Date: Tue, 6 Aug 2024 16:52:50 +0200 Subject: [PATCH] fix: delete button appears briefly when opening account screen (WPB-7426) (#3283) (cherry picked from commit 59c23d9fb594f3f6fdb555afb753e5b4316b9257) --- .../home/settings/account/MyAccountScreen.kt | 2 +- .../home/settings/account/MyAccountState.kt | 1 + .../settings/account/MyAccountViewModel.kt | 34 ++++++++++++++++--- .../account/MyAccountViewModelTest.kt | 6 ++++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/wire/android/ui/home/settings/account/MyAccountScreen.kt b/app/src/main/kotlin/com/wire/android/ui/home/settings/account/MyAccountScreen.kt index 29e550a1da7..8bb430429b1 100644 --- a/app/src/main/kotlin/com/wire/android/ui/home/settings/account/MyAccountScreen.kt +++ b/app/src/main/kotlin/com/wire/android/ui/home/settings/account/MyAccountScreen.kt @@ -97,7 +97,7 @@ fun MyAccountScreen( navigateToChangeEmail = { navigator.navigate(NavigationCommand(ChangeEmailScreenDestination)) } ), forgotPasswordUrl = this.changePasswordUrl, - canDeleteAccount = viewModel.myAccountState.teamName.isNullOrBlank(), + canDeleteAccount = viewModel.myAccountState.canDeleteAccount, onDeleteAccountClicked = deleteAccountViewModel::onDeleteAccountClicked, onDeleteAccountConfirmed = deleteAccountViewModel::onDeleteAccountDialogConfirmed, onDeleteAccountDismissed = deleteAccountViewModel::onDeleteAccountDialogDismissed, diff --git a/app/src/main/kotlin/com/wire/android/ui/home/settings/account/MyAccountState.kt b/app/src/main/kotlin/com/wire/android/ui/home/settings/account/MyAccountState.kt index 1b319148563..08a8903e02c 100644 --- a/app/src/main/kotlin/com/wire/android/ui/home/settings/account/MyAccountState.kt +++ b/app/src/main/kotlin/com/wire/android/ui/home/settings/account/MyAccountState.kt @@ -22,6 +22,7 @@ data class MyAccountState( val fullName: String = "", val userName: String = "", val email: String = "", + val canDeleteAccount: Boolean = false, val teamName: String? = null, val domain: String = "", val changePasswordUrl: String? = null, diff --git a/app/src/main/kotlin/com/wire/android/ui/home/settings/account/MyAccountViewModel.kt b/app/src/main/kotlin/com/wire/android/ui/home/settings/account/MyAccountViewModel.kt index dde795ca0aa..a35360b4dcf 100644 --- a/app/src/main/kotlin/com/wire/android/ui/home/settings/account/MyAccountViewModel.kt +++ b/app/src/main/kotlin/com/wire/android/ui/home/settings/account/MyAccountViewModel.kt @@ -33,6 +33,7 @@ import com.wire.kalium.logic.feature.user.GetSelfUserUseCase import com.wire.kalium.logic.feature.user.IsE2EIEnabledUseCase import com.wire.kalium.logic.feature.user.IsPasswordRequiredUseCase import com.wire.kalium.logic.feature.user.IsReadOnlyAccountUseCase +import com.wire.kalium.logic.feature.user.IsSelfATeamMemberUseCase import com.wire.kalium.logic.feature.user.SelfServerConfigUseCase import com.wire.kalium.logic.functional.getOrNull import dagger.hilt.android.lifecycle.HiltViewModel @@ -51,6 +52,7 @@ class MyAccountViewModel @Inject constructor( savedStateHandle: SavedStateHandle, private val getSelf: GetSelfUserUseCase, private val getSelfTeam: GetUpdatedSelfTeamUseCase, + private val isSelfATeamMember: IsSelfATeamMemberUseCase, private val serverConfig: SelfServerConfigUseCase, private val isPasswordRequired: IsPasswordRequiredUseCase, private val isReadOnlyAccount: IsReadOnlyAccountUseCase, @@ -71,6 +73,8 @@ class MyAccountViewModel @Inject constructor( var isE2EIEnabled by Delegates.notNull() init { + initScreenState() + runBlocking { hasSAMLCred = when (val result = isPasswordRequired()) { is IsPasswordRequiredUseCase.Result.Failure -> false @@ -88,9 +92,6 @@ class MyAccountViewModel @Inject constructor( isEditNameAllowed = managedByWire && !isE2EIEnabled, isEditHandleAllowed = managedByWire && !isE2EIEnabled ) - viewModelScope.launch { - fetchSelfUser() - } viewModelScope.launch { if (!hasSAMLCred) { @@ -99,6 +100,18 @@ class MyAccountViewModel @Inject constructor( } } + private fun initScreenState() { + viewModelScope.launch { + initCanDeleteAccountValue() + } + viewModelScope.launch { + fetchSelfUser() + } + viewModelScope.launch { + fetchSelfUserTeam() + } + } + private suspend fun loadChangePasswordUrl() { when (val result = withContext(dispatchers.io()) { serverConfig() }) { is SelfServerConfigUseCase.Result.Failure -> appLogger.e( @@ -112,7 +125,6 @@ class MyAccountViewModel @Inject constructor( private suspend fun fetchSelfUser() { viewModelScope.launch { - val selfTeam = getSelfTeam().getOrNull() val self = getSelf().flowOn(dispatchers.io()).shareIn(this, SharingStarted.WhileSubscribed(1)) self.collect { user -> @@ -120,13 +132,25 @@ class MyAccountViewModel @Inject constructor( fullName = user.name.orEmpty(), userName = user.handle.orEmpty(), email = user.email.orEmpty(), - teamName = selfTeam?.name.orEmpty(), domain = user.id.domain ) } } } + private suspend fun initCanDeleteAccountValue() { + myAccountState = myAccountState.copy( + canDeleteAccount = !isSelfATeamMember(), + ) + } + + private suspend fun fetchSelfUserTeam() { + val selfTeam = getSelfTeam().getOrNull() + myAccountState = myAccountState.copy( + teamName = selfTeam?.name.orEmpty(), + ) + } + companion object { /** * This is a build time flag that allows to enable/disable the change email feature. diff --git a/app/src/test/kotlin/com/wire/android/ui/home/settings/account/MyAccountViewModelTest.kt b/app/src/test/kotlin/com/wire/android/ui/home/settings/account/MyAccountViewModelTest.kt index 226b26d100c..1d70084ecc0 100644 --- a/app/src/test/kotlin/com/wire/android/ui/home/settings/account/MyAccountViewModelTest.kt +++ b/app/src/test/kotlin/com/wire/android/ui/home/settings/account/MyAccountViewModelTest.kt @@ -32,6 +32,7 @@ import com.wire.kalium.logic.feature.user.IsE2EIEnabledUseCase import com.wire.kalium.logic.feature.user.IsPasswordRequiredUseCase import com.wire.kalium.logic.feature.user.IsPasswordRequiredUseCase.Result.Success import com.wire.kalium.logic.feature.user.IsReadOnlyAccountUseCase +import com.wire.kalium.logic.feature.user.IsSelfATeamMemberUseCase import com.wire.kalium.logic.feature.user.SelfServerConfigUseCase import com.wire.kalium.logic.functional.Either import io.mockk.Called @@ -224,11 +225,15 @@ class MyAccountViewModelTest { @MockK lateinit var isE2EIEnabledUseCase: IsE2EIEnabledUseCase + @MockK + lateinit var isSelfATeamMember: IsSelfATeamMemberUseCase + private val viewModel by lazy { MyAccountViewModel( savedStateHandle, getSelfUserUseCase, getSelfTeamUseCase, + isSelfATeamMember, selfServerConfigUseCase, isPasswordRequiredUseCase, isReadOnlyAccountUseCase, @@ -242,6 +247,7 @@ class MyAccountViewModelTest { coEvery { getSelfUserUseCase() } returns flowOf(TestUser.SELF_USER.copy(teamId = TeamId(TestTeam.TEAM.id))) coEvery { getSelfTeamUseCase() } returns Either.Right(TestTeam.TEAM) coEvery { selfServerConfigUseCase() } returns SelfServerConfigUseCase.Result.Success(newServerConfig(1)) + coEvery { isSelfATeamMember() } returns true } fun withUserRequiresPasswordResult(result: IsPasswordRequiredUseCase.Result = Success(true)) = apply {