CreatePollEvents -> CreatePollEvent

This commit is contained in:
Benoit Marty 2025-12-03 18:13:18 +01:00
parent 82a32371cc
commit e6e67a02b7
5 changed files with 76 additions and 76 deletions

View file

@ -10,15 +10,15 @@ package io.element.android.features.poll.impl.create
import io.element.android.libraries.matrix.api.poll.PollKind
sealed interface CreatePollEvents {
data object Save : CreatePollEvents
data class Delete(val confirmed: Boolean) : CreatePollEvents
data class SetQuestion(val question: String) : CreatePollEvents
data class SetAnswer(val index: Int, val text: String) : CreatePollEvents
data object AddAnswer : CreatePollEvents
data class RemoveAnswer(val index: Int) : CreatePollEvents
data class SetPollKind(val pollKind: PollKind) : CreatePollEvents
data object NavBack : CreatePollEvents
data object ConfirmNavBack : CreatePollEvents
data object HideConfirmation : CreatePollEvents
sealed interface CreatePollEvent {
data object Save : CreatePollEvent
data class Delete(val confirmed: Boolean) : CreatePollEvent
data class SetQuestion(val question: String) : CreatePollEvent
data class SetAnswer(val index: Int, val text: String) : CreatePollEvent
data object AddAnswer : CreatePollEvent
data class RemoveAnswer(val index: Int) : CreatePollEvent
data class SetPollKind(val pollKind: PollKind) : CreatePollEvent
data object NavBack : CreatePollEvent
data object ConfirmNavBack : CreatePollEvent
data object HideConfirmation : CreatePollEvent
}

View file

@ -97,9 +97,9 @@ class CreatePollPresenter(
val scope = rememberCoroutineScope()
fun handleEvent(event: CreatePollEvents) {
fun handleEvent(event: CreatePollEvent) {
when (event) {
is CreatePollEvents.Save -> scope.launch {
is CreatePollEvent.Save -> scope.launch {
if (canSave) {
repository.savePoll(
existingPollId = when (mode) {
@ -123,7 +123,7 @@ class CreatePollPresenter(
Timber.d("Cannot create poll")
}
}
is CreatePollEvents.Delete -> {
is CreatePollEvent.Delete -> {
if (mode !is CreatePollMode.EditPoll) {
return
}
@ -139,25 +139,25 @@ class CreatePollPresenter(
navigateUp()
}
}
is CreatePollEvents.AddAnswer -> {
is CreatePollEvent.AddAnswer -> {
poll = poll.withNewAnswer()
}
is CreatePollEvents.RemoveAnswer -> {
is CreatePollEvent.RemoveAnswer -> {
poll = poll.withAnswerRemoved(event.index)
}
is CreatePollEvents.SetAnswer -> {
is CreatePollEvent.SetAnswer -> {
poll = poll.withAnswerChanged(event.index, event.text)
}
is CreatePollEvents.SetPollKind -> {
is CreatePollEvent.SetPollKind -> {
poll = poll.copy(isDisclosed = event.pollKind.isDisclosed)
}
is CreatePollEvents.SetQuestion -> {
is CreatePollEvent.SetQuestion -> {
poll = poll.copy(question = event.question)
}
is CreatePollEvents.NavBack -> {
is CreatePollEvent.NavBack -> {
navigateUp()
}
CreatePollEvents.ConfirmNavBack -> {
CreatePollEvent.ConfirmNavBack -> {
val shouldConfirm = isDirty
if (shouldConfirm) {
showBackConfirmation = true
@ -165,7 +165,7 @@ class CreatePollPresenter(
navigateUp()
}
}
is CreatePollEvents.HideConfirmation -> {
is CreatePollEvent.HideConfirmation -> {
showBackConfirmation = false
showDeleteConfirmation = false
}

View file

@ -20,7 +20,7 @@ data class CreatePollState(
val pollKind: PollKind,
val showBackConfirmation: Boolean,
val showDeleteConfirmation: Boolean,
val eventSink: (CreatePollEvents) -> Unit,
val eventSink: (CreatePollEvent) -> Unit,
) {
enum class Mode {
New,

View file

@ -62,21 +62,21 @@ fun CreatePollView(
) {
val coroutineScope = rememberCoroutineScope()
val navBack = { state.eventSink(CreatePollEvents.ConfirmNavBack) }
val navBack = { state.eventSink(CreatePollEvent.ConfirmNavBack) }
BackHandler(onBack = navBack)
if (state.showBackConfirmation) {
SaveChangesDialog(
onSaveClick = { state.eventSink(CreatePollEvents.Save) },
onDiscardClick = { state.eventSink(CreatePollEvents.NavBack) },
onDismiss = { state.eventSink(CreatePollEvents.HideConfirmation) },
onSaveClick = { state.eventSink(CreatePollEvent.Save) },
onDiscardClick = { state.eventSink(CreatePollEvent.NavBack) },
onDismiss = { state.eventSink(CreatePollEvent.HideConfirmation) },
)
}
if (state.showDeleteConfirmation) {
ConfirmationDialog(
title = stringResource(id = R.string.screen_edit_poll_delete_confirmation_title),
content = stringResource(id = R.string.screen_edit_poll_delete_confirmation),
onSubmitClick = { state.eventSink(CreatePollEvents.Delete(confirmed = true)) },
onDismiss = { state.eventSink(CreatePollEvents.HideConfirmation) }
onSubmitClick = { state.eventSink(CreatePollEvent.Delete(confirmed = true)) },
onDismiss = { state.eventSink(CreatePollEvent.HideConfirmation) }
)
}
val questionFocusRequester = remember { FocusRequester() }
@ -91,7 +91,7 @@ fun CreatePollView(
mode = state.mode,
saveEnabled = state.canSave,
onBackClick = navBack,
onSaveClick = { state.eventSink(CreatePollEvents.Save) }
onSaveClick = { state.eventSink(CreatePollEvent.Save) }
)
},
) { paddingValues ->
@ -112,7 +112,7 @@ fun CreatePollView(
label = stringResource(id = R.string.screen_create_poll_question_desc),
value = state.question,
onValueChange = {
state.eventSink(CreatePollEvents.SetQuestion(it))
state.eventSink(CreatePollEvent.SetQuestion(it))
},
modifier = Modifier
.focusRequester(questionFocusRequester)
@ -131,7 +131,7 @@ fun CreatePollView(
TextField(
value = answer.text,
onValueChange = {
state.eventSink(CreatePollEvents.SetAnswer(index, it))
state.eventSink(CreatePollEvent.SetAnswer(index, it))
},
modifier = Modifier
.then(if (isLastItem) Modifier.focusRequester(answerFocusRequester) else Modifier)
@ -145,7 +145,7 @@ fun CreatePollView(
imageVector = CompoundIcons.Delete(),
contentDescription = stringResource(R.string.screen_create_poll_delete_option_a11y, answer.text),
modifier = Modifier.clickable(answer.canDelete) {
state.eventSink(CreatePollEvents.RemoveAnswer(index))
state.eventSink(CreatePollEvent.RemoveAnswer(index))
},
)
},
@ -161,7 +161,7 @@ fun CreatePollView(
),
style = ListItemStyle.Primary,
onClick = {
state.eventSink(CreatePollEvents.AddAnswer)
state.eventSink(CreatePollEvent.AddAnswer)
coroutineScope.launch(Dispatchers.Main) {
lazyListState.animateScrollToItem(state.answers.size + 1)
answerFocusRequester.requestFocus()
@ -181,7 +181,7 @@ fun CreatePollView(
),
onClick = {
state.eventSink(
CreatePollEvents.SetPollKind(
CreatePollEvent.SetPollKind(
if (state.pollKind == PollKind.Disclosed) PollKind.Undisclosed else PollKind.Disclosed
)
)
@ -191,7 +191,7 @@ fun CreatePollView(
ListItem(
headlineContent = { Text(text = stringResource(id = CommonStrings.action_delete_poll)) },
style = ListItemStyle.Destructive,
onClick = { state.eventSink(CreatePollEvents.Delete(confirmed = false)) },
onClick = { state.eventSink(CreatePollEvent.Delete(confirmed = false)) },
)
}
}