mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-02 12:07:28 +00:00
Max selections support
This commit is contained in:
+1
-1
@@ -15,6 +15,6 @@ interface SendPollResponseAction {
|
||||
suspend fun execute(
|
||||
timeline: Timeline,
|
||||
pollStartId: EventId,
|
||||
answerId: String
|
||||
answerIds: List<String>
|
||||
): Result<Unit>
|
||||
}
|
||||
|
||||
+5
-1
@@ -18,6 +18,7 @@ import kotlinx.collections.immutable.ImmutableList
|
||||
* @property question the poll question.
|
||||
* @property answerItems the list of answers.
|
||||
* @property pollKind the kind of poll.
|
||||
* @property maxSelections the maximum number of selections allowed.
|
||||
* @property isPollEditable whether the poll is editable.
|
||||
* @property isPollEnded whether the poll is ended.
|
||||
* @property isMine whether the poll has been created by me.
|
||||
@@ -27,7 +28,10 @@ data class PollContentState(
|
||||
val question: String,
|
||||
val answerItems: ImmutableList<PollAnswerItem>,
|
||||
val pollKind: PollKind,
|
||||
val maxSelections: ULong,
|
||||
val isPollEditable: Boolean,
|
||||
val isPollEnded: Boolean,
|
||||
val isMine: Boolean,
|
||||
)
|
||||
) {
|
||||
val isMultipleSelection: Boolean get() = maxSelections > 1u
|
||||
}
|
||||
|
||||
+2
@@ -84,6 +84,7 @@ fun aPollContentState(
|
||||
hasVotes: Boolean = true,
|
||||
question: String = aPollQuestion(),
|
||||
pollKind: PollKind = PollKind.Disclosed,
|
||||
maxSelections: ULong = 1u,
|
||||
answerItems: ImmutableList<PollAnswerItem> = aPollAnswerItemList(
|
||||
isEnded = isEnded,
|
||||
showVotes = showVotes,
|
||||
@@ -94,6 +95,7 @@ fun aPollContentState(
|
||||
question = question,
|
||||
answerItems = answerItems,
|
||||
pollKind = pollKind,
|
||||
maxSelections = maxSelections,
|
||||
isPollEditable = isMine && !isEnded && isPollEditable,
|
||||
isPollEnded = isEnded,
|
||||
isMine = isMine,
|
||||
|
||||
+62
-5
@@ -40,7 +40,7 @@ import kotlinx.collections.immutable.ImmutableList
|
||||
@Composable
|
||||
fun PollContentView(
|
||||
state: PollContentState,
|
||||
onSelectAnswer: (pollStartId: EventId, answerId: String) -> Unit,
|
||||
onSelectAnswer: (pollStartId: EventId, answerIds: List<String>) -> Unit,
|
||||
onEditPoll: (pollStartId: EventId) -> Unit,
|
||||
onEndPoll: (pollStartId: EventId) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
@@ -50,6 +50,8 @@ fun PollContentView(
|
||||
question = state.question,
|
||||
answerItems = state.answerItems,
|
||||
pollKind = state.pollKind,
|
||||
isMultipleSelection = state.isMultipleSelection,
|
||||
maxSelections = state.maxSelections,
|
||||
isPollEditable = state.isPollEditable,
|
||||
isPollEnded = state.isPollEnded,
|
||||
isMine = state.isMine,
|
||||
@@ -66,10 +68,12 @@ fun PollContentView(
|
||||
question: String,
|
||||
answerItems: ImmutableList<PollAnswerItem>,
|
||||
pollKind: PollKind,
|
||||
isMultipleSelection: Boolean,
|
||||
maxSelections: ULong,
|
||||
isPollEditable: Boolean,
|
||||
isPollEnded: Boolean,
|
||||
isMine: Boolean,
|
||||
onSelectAnswer: (pollStartId: EventId, answerId: String) -> Unit,
|
||||
onSelectAnswer: (pollStartId: EventId, answerIds: List<String>) -> Unit,
|
||||
onEditPoll: (pollStartId: EventId) -> Unit,
|
||||
onEndPoll: (pollStartId: EventId) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
@@ -77,7 +81,23 @@ fun PollContentView(
|
||||
val votesCount = remember(answerItems) { answerItems.sumOf { it.votesCount } }
|
||||
|
||||
fun onSelectAnswer(pollAnswer: PollAnswer) {
|
||||
eventId?.let { onSelectAnswer(it, pollAnswer.id) }
|
||||
eventId?.let {
|
||||
if (isMultipleSelection) {
|
||||
val newSelections = answerItems.map { item ->
|
||||
if (item.answer.id == pollAnswer.id) {
|
||||
item.copy(isSelected = !item.isSelected)
|
||||
} else {
|
||||
item
|
||||
}
|
||||
}.filter { it.isSelected }.map { it.answer.id }
|
||||
// Only send the response if the selection count is within the limit
|
||||
if (newSelections.size <= maxSelections.toInt()) {
|
||||
onSelectAnswer(it, newSelections)
|
||||
}
|
||||
} else {
|
||||
onSelectAnswer(it, listOf(pollAnswer.id))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun onEditPoll() {
|
||||
@@ -107,7 +127,11 @@ fun PollContentView(
|
||||
) {
|
||||
PollTitleView(title = question, isPollEnded = isPollEnded)
|
||||
|
||||
PollAnswers(answerItems = answerItems, onSelectAnswer = ::onSelectAnswer)
|
||||
PollAnswers(
|
||||
answerItems = answerItems,
|
||||
isMultipleSelection = isMultipleSelection,
|
||||
onSelectAnswer = ::onSelectAnswer,
|
||||
)
|
||||
|
||||
if (isPollEnded || pollKind == PollKind.Disclosed) {
|
||||
DisclosedPollBottomNotice(votesCount = votesCount)
|
||||
@@ -130,8 +154,10 @@ fun PollContentView(
|
||||
@Composable
|
||||
private fun PollAnswers(
|
||||
answerItems: ImmutableList<PollAnswerItem>,
|
||||
isMultipleSelection: Boolean,
|
||||
onSelectAnswer: (PollAnswer) -> Unit,
|
||||
) {
|
||||
val role = if (isMultipleSelection) Role.Checkbox else Role.RadioButton
|
||||
Column(
|
||||
modifier = Modifier.selectableGroup(),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp),
|
||||
@@ -144,7 +170,7 @@ private fun PollAnswers(
|
||||
selected = it.isSelected,
|
||||
enabled = it.isEnabled,
|
||||
onClick = { onSelectAnswer(it.answer) },
|
||||
role = Role.RadioButton,
|
||||
role = role,
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -207,6 +233,8 @@ internal fun PollContentViewUndisclosedPreview() = ElementPreview {
|
||||
question = "What type of food should we have at the party?",
|
||||
answerItems = aPollAnswerItemList(showVotes = false),
|
||||
pollKind = PollKind.Undisclosed,
|
||||
isMultipleSelection = false,
|
||||
maxSelections = 1u,
|
||||
isPollEnded = false,
|
||||
isPollEditable = false,
|
||||
isMine = false,
|
||||
@@ -224,6 +252,27 @@ internal fun PollContentViewDisclosedPreview() = ElementPreview {
|
||||
question = "What type of food should we have at the party?",
|
||||
answerItems = aPollAnswerItemList(),
|
||||
pollKind = PollKind.Disclosed,
|
||||
isMultipleSelection = false,
|
||||
maxSelections = 1u,
|
||||
isPollEnded = false,
|
||||
isPollEditable = false,
|
||||
isMine = false,
|
||||
onSelectAnswer = { _, _ -> },
|
||||
onEditPoll = {},
|
||||
onEndPoll = {},
|
||||
)
|
||||
}
|
||||
|
||||
@PreviewsDayNight
|
||||
@Composable
|
||||
internal fun PollContentViewMultipleSelectionPreview() = ElementPreview {
|
||||
PollContentView(
|
||||
eventId = EventId("\$anEventId"),
|
||||
question = "What type of food should we have at the party?",
|
||||
answerItems = aPollAnswerItemList(),
|
||||
pollKind = PollKind.Disclosed,
|
||||
isMultipleSelection = true,
|
||||
maxSelections = 3u,
|
||||
isPollEnded = false,
|
||||
isPollEditable = false,
|
||||
isMine = false,
|
||||
@@ -241,6 +290,8 @@ internal fun PollContentViewEndedPreview() = ElementPreview {
|
||||
question = "What type of food should we have at the party?",
|
||||
answerItems = aPollAnswerItemList(isEnded = true),
|
||||
pollKind = PollKind.Disclosed,
|
||||
isMultipleSelection = false,
|
||||
maxSelections = 1u,
|
||||
isPollEnded = true,
|
||||
isPollEditable = false,
|
||||
isMine = false,
|
||||
@@ -258,6 +309,8 @@ internal fun PollContentViewCreatorEditablePreview() = ElementPreview {
|
||||
question = "What type of food should we have at the party?",
|
||||
answerItems = aPollAnswerItemList(hasVotes = false, isEnded = false),
|
||||
pollKind = PollKind.Disclosed,
|
||||
isMultipleSelection = false,
|
||||
maxSelections = 1u,
|
||||
isPollEnded = false,
|
||||
isPollEditable = true,
|
||||
isMine = true,
|
||||
@@ -275,6 +328,8 @@ internal fun PollContentViewCreatorPreview() = ElementPreview {
|
||||
question = "What type of food should we have at the party?",
|
||||
answerItems = aPollAnswerItemList(isEnded = false),
|
||||
pollKind = PollKind.Disclosed,
|
||||
isMultipleSelection = false,
|
||||
maxSelections = 1u,
|
||||
isPollEnded = false,
|
||||
isPollEditable = false,
|
||||
isMine = true,
|
||||
@@ -292,6 +347,8 @@ internal fun PollContentViewCreatorEndedPreview() = ElementPreview {
|
||||
question = "What type of food should we have at the party?",
|
||||
answerItems = aPollAnswerItemList(isEnded = true),
|
||||
pollKind = PollKind.Disclosed,
|
||||
isMultipleSelection = false,
|
||||
maxSelections = 1u,
|
||||
isPollEnded = true,
|
||||
isPollEditable = false,
|
||||
isMine = true,
|
||||
|
||||
Reference in New Issue
Block a user