Skip to content

Commit

Permalink
Merge pull request #18391 from wordpress-mobile/feature/enable-accoun…
Browse files Browse the repository at this point in the history
…t-closure--18381-add-confirmation-dialog

Add a confirmation dialog for account closure (addendum)
  • Loading branch information
mkevins authored May 9, 2023
2 parents bdc3897 + 8bb341f commit 5060a01
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand Down Expand Up @@ -297,7 +298,8 @@ class AccountSettingsViewModel @Inject constructor(
object Dismissed: AccountClosureUiState()

sealed class Opened: AccountClosureUiState() {
data class Default(val username: String?): Opened()
data class Default(val username: String?, val isPending: Boolean = false): Opened()

object Atomic: Opened()
}
}
Expand All @@ -315,6 +317,18 @@ class AccountSettingsViewModel @Inject constructor(
_accountClosureUiState.value = Dismissed
}

fun closeAccount() {
(accountClosureUiState.value as? Default)?.let { uiState ->
_accountClosureUiState.value = uiState.copy(isPending = true)

launch {
@Suppress("MagicNumber")
delay(3000)
_accountClosureUiState.value = uiState.copy(isPending = false)
}
}
}

override fun onCleared() {
pushAccountSettingsUseCase.onCleared()
super.onCleared()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import org.wordpress.android.ui.compose.theme.AppTheme
fun AccountClosureDialog(
onDismissRequest: () -> Unit,
currentUsername: String,
onConfirm: () -> Unit,
isPending: Boolean,
) {
var username by remember { mutableStateOf("") }
val padding = 10.dp
Expand Down Expand Up @@ -81,7 +83,8 @@ fun AccountClosureDialog(
text = stringResource(R.string.confirm),
modifier = Modifier.weight(1f),
enabled = username.isNotEmpty() && username == currentUsername,
onClick = {},
isPending = isPending,
onClick = onConfirm,
colors = ButtonDefaults.buttonColors(
contentColor = MaterialTheme.colors.error,
backgroundColor = Color.Transparent,
Expand All @@ -100,6 +103,8 @@ fun PreviewAccountClosureDialog() {
AccountClosureDialog(
onDismissRequest = {},
currentUsername = "previewUser",
onConfirm = {},
isPending = false,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ fun AccountClosureUi(viewModel: AccountSettingsViewModel, onHelpRequested: () ->
AccountClosureDialog(
onDismissRequest = { viewModel.dismissAccountClosureDialog() },
currentUsername,
onConfirm = { viewModel.closeAccount() },
isPending = it.isPending,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.wordpress.android.ui.prefs.accountsettings.components

import androidx.compose.foundation.layout.size
import androidx.compose.material.Button
import androidx.compose.material.ButtonColors
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.OutlinedButton
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -39,6 +41,7 @@ fun FlatOutlinedButton(
modifier: Modifier = Modifier,
colors: ButtonColors = ButtonDefaults.buttonColors(),
enabled: Boolean = true,
isPending: Boolean = false,
) = OutlinedButton(
modifier = modifier,
onClick = onClick,
Expand All @@ -47,7 +50,14 @@ fun FlatOutlinedButton(
defaultElevation = 0.dp,
pressedElevation = 0.dp,
),
enabled = enabled,
enabled = enabled && !isPending,
) {
Text(text)
if (isPending) {
CircularProgressIndicator(
strokeWidth = 2.dp,
modifier = Modifier.size(20.dp),
)
} else {
Text(text)
}
}

0 comments on commit 5060a01

Please sign in to comment.