Skip to content

Commit

Permalink
fix: delete button appears briefly when opening account screen (WPB-7…
Browse files Browse the repository at this point in the history
…426) (#3283)
  • Loading branch information
ohassine authored Aug 6, 2024
1 parent 226dd4c commit 59c23d9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -71,6 +73,8 @@ class MyAccountViewModel @Inject constructor(
var isE2EIEnabled by Delegates.notNull<Boolean>()

init {
initScreenState()

runBlocking {
hasSAMLCred = when (val result = isPasswordRequired()) {
is IsPasswordRequiredUseCase.Result.Failure -> false
Expand All @@ -88,9 +92,6 @@ class MyAccountViewModel @Inject constructor(
isEditNameAllowed = managedByWire && !isE2EIEnabled,
isEditHandleAllowed = managedByWire && !isE2EIEnabled
)
viewModelScope.launch {
fetchSelfUser()
}

viewModelScope.launch {
if (!hasSAMLCred) {
Expand All @@ -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(
Expand All @@ -112,21 +125,32 @@ 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 ->
myAccountState = myAccountState.copy(
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 59c23d9

Please sign in to comment.