Skip to content

Commit

Permalink
disable the delete buttons while we are processing the delete request
Browse files Browse the repository at this point in the history
  • Loading branch information
frett committed Nov 10, 2023
1 parent 5dba4c0 commit 503798a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ private fun DeleteAccountLayoutDisplayPreview() {
GodToolsTheme { DeleteAccountLayout(DeleteAccountScreen.State.Display { }) }
}

@Preview
@Composable
private fun DeleteAccountLayoutDeletingPreview() {
GodToolsTheme { DeleteAccountLayout(DeleteAccountScreen.State.Deleting { }) }
}

@Preview
@Composable
private fun DeleteAccountLayoutErrorPreview() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fun DeleteAccountLayout(state: State, modifier: Modifier = Modifier) {
modifier = Modifier.padding(horizontal = MARGIN_HORIZONTAL, top = 8.dp, bottom = 32.dp)
)

val actionsEnabled = state !is State.Error
val actionsEnabled = state !is State.Deleting && state !is State.Error
OutlinedButton(
enabled = actionsEnabled,
onClick = { state.eventSink(Event.DeleteAccount) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ class DeleteAccountPresenter @AssistedInject constructor(
@Composable
override fun present(): DeleteAccountScreen.State {
val coroutineScope = rememberCoroutineScope()

var deleting by remember { mutableStateOf(false) }
var error by rememberRetained { mutableStateOf(false) }

val eventSink: (Event) -> Unit = remember {
{
when (it) {
Event.DeleteAccount -> {
deleting = true
coroutineScope.launch {
if (accountManager.deleteAccount()) {
navigator.pop()
} else {
error = true
deleting = false
}
}
}
Expand All @@ -47,6 +51,7 @@ class DeleteAccountPresenter @AssistedInject constructor(

return when {
error -> DeleteAccountScreen.State.Error(eventSink)
deleting -> DeleteAccountScreen.State.Deleting(eventSink)
else -> DeleteAccountScreen.State.Display(eventSink)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlinx.parcelize.Parcelize
data object DeleteAccountScreen : Screen {
sealed class State : CircuitUiState {
data class Display(override val eventSink: (Event) -> Unit) : State()
data class Deleting(override val eventSink: (Event) -> Unit) : State()
data class Error(override val eventSink: (Event) -> Unit) : State()

abstract val eventSink: (Event) -> Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ class DeleteAccountLayoutTest {
}
// endregion State Display

// region State Deleting
@Test
fun `State Deleting - Action - Close Icon`() {
composeTestRule.run {
setContent { DeleteAccountLayout(State.Deleting(events)) }
onNodeWithTag(TEST_TAG_ICON_CLOSE)
.assertIsEnabled()
.performClick()
}

events.assertEvent(Event.Close)
}

@Test
fun `State Deleting - Action - Disabled Delete & Cancel Buttons`() {
composeTestRule.run {
setContent { DeleteAccountLayout(State.Deleting(events)) }
onNodeWithTag(TEST_TAG_BUTTON_DELETE).assertIsNotEnabled()
onNodeWithTag(TEST_TAG_BUTTON_CANCEL).assertIsNotEnabled()
}

events.assertNoEvents()
}
// endregion State Deleting

// region State Error
@Test
fun `State Error - Action - Close Icon`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class DeleteAccountPresenterTest {
assertIs<State.Display>(awaitItem())
.eventSink(Event.DeleteAccount)

assertIs<State.Deleting>(awaitItem())
deleteAccountResponse.send(true)
coVerify { accountManager.deleteAccount() }
navigator.awaitPop()
Expand All @@ -58,6 +59,7 @@ class DeleteAccountPresenterTest {
assertIs<State.Display>(awaitItem())
.eventSink(Event.DeleteAccount)

assertIs<State.Deleting>(awaitItem())
deleteAccountResponse.send(false)
coVerify { accountManager.deleteAccount() }

Expand All @@ -83,4 +85,21 @@ class DeleteAccountPresenterTest {

confirmVerified(accountManager)
}

@Test
fun `Cancel Delete Account - While Deleting`() = runTest {
presenter.test {
assertIs<State.Display>(awaitItem())
.eventSink(Event.DeleteAccount)

assertIs<State.Deleting>(awaitItem())
.eventSink(Event.Close)
coVerify { accountManager.deleteAccount() }
navigator.awaitPop()

cancelAndIgnoreRemainingEvents()
}

confirmVerified(accountManager)
}
}

0 comments on commit 503798a

Please sign in to comment.