Allow polls to be edited (#1869)

Polls can be edited if they do not have any votes

---------

Co-authored-by: ElementBot <benoitm+elementbot@element.io>
This commit is contained in:
jonnyandrew 2023-11-24 16:47:58 +00:00 committed by GitHub
parent 4e52244b86
commit 8fcec4a006
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
50 changed files with 827 additions and 173 deletions

View file

@ -19,6 +19,8 @@ package io.element.android.features.poll.api
import io.element.android.libraries.matrix.api.poll.PollAnswer
import kotlinx.collections.immutable.persistentListOf
fun aPollQuestion() = "What type of food should we have at the party?"
fun aPollAnswerItemList(
hasVotes: Boolean = true,
isEnded: Boolean = false,
@ -30,7 +32,7 @@ fun aPollAnswerItemList(
isEnabled = !isEnded,
isWinner = isEnded,
votesCount = if (hasVotes) 5 else 0,
percentage = 0.5f
percentage = if (hasVotes) 0.5f else 0f
),
aPollAnswerItem(
answer = PollAnswer("option_2", "Chinese \uD83C\uDDE8\uD83C\uDDF3"),
@ -47,9 +49,14 @@ fun aPollAnswerItemList(
isWinner = false,
isSelected = true,
votesCount = if (hasVotes) 1 else 0,
percentage = 0.1f
percentage = if (hasVotes) 0.1f else 0f
),
aPollAnswerItem(
isDisclosed = isDisclosed,
isEnabled = !isEnded,
votesCount = if (hasVotes) 4 else 0,
percentage = if (hasVotes) 0.4f else 0f,
),
aPollAnswerItem(isDisclosed = isDisclosed, isEnabled = !isEnded),
)
fun aPollAnswerItem(

View file

@ -55,6 +55,7 @@ fun PollContentView(
question: String,
answerItems: ImmutableList<PollAnswerItem>,
pollKind: PollKind,
isPollEditable: Boolean,
isPollEnded: Boolean,
isMine: Boolean,
onAnswerSelected: (pollStartId: EventId, answerId: String) -> Unit,
@ -103,8 +104,8 @@ fun PollContentView(
if (isMine) {
CreatorView(
votesCount = 1, // TODO Polls: set to `votesCount` when edit poll screen is implemented.
isPollEnded = isPollEnded,
isPollEditable = isPollEditable,
onPollEdit = ::onPollEdit,
onPollEnd = { showConfirmation = true },
modifier = Modifier.fillMaxWidth(),
@ -197,26 +198,25 @@ private fun ColumnScope.UndisclosedPollBottomNotice(
@Composable
private fun CreatorView(
@Suppress("SameParameterValue") votesCount: Int, // TODO Polls: remove @Suppress when edit poll screen is implemented.
isPollEnded: Boolean,
isPollEditable: Boolean,
onPollEdit: () -> Unit,
onPollEnd: () -> Unit,
modifier: Modifier = Modifier
) {
if (!isPollEnded) {
if (votesCount == 0) {
when {
isPollEditable ->
Button(
text = stringResource(id = CommonStrings.action_edit_poll),
onClick = onPollEdit,
modifier = modifier,
)
} else {
!isPollEnded ->
Button(
text = stringResource(id = CommonStrings.action_end_poll),
onClick = onPollEnd,
modifier = modifier,
)
}
}
}
@ -229,6 +229,7 @@ internal fun PollContentUndisclosedPreview() = ElementPreview {
answerItems = aPollAnswerItemList(isDisclosed = false),
pollKind = PollKind.Undisclosed,
isPollEnded = false,
isPollEditable = false,
isMine = false,
onAnswerSelected = { _, _ -> },
onPollEdit = {},
@ -245,6 +246,7 @@ internal fun PollContentDisclosedPreview() = ElementPreview {
answerItems = aPollAnswerItemList(),
pollKind = PollKind.Disclosed,
isPollEnded = false,
isPollEditable = false,
isMine = false,
onAnswerSelected = { _, _ -> },
onPollEdit = {},
@ -261,6 +263,7 @@ internal fun PollContentEndedPreview() = ElementPreview {
answerItems = aPollAnswerItemList(isEnded = true),
pollKind = PollKind.Disclosed,
isPollEnded = true,
isPollEditable = false,
isMine = false,
onAnswerSelected = { _, _ -> },
onPollEdit = {},
@ -270,13 +273,14 @@ internal fun PollContentEndedPreview() = ElementPreview {
@PreviewsDayNight
@Composable
internal fun PollContentCreatorNoVotesPreview() = ElementPreview {
internal fun PollContentCreatorEditablePreview() = ElementPreview {
PollContentView(
eventId = EventId("\$anEventId"),
question = "What type of food should we have at the party?",
answerItems = aPollAnswerItemList(hasVotes = false, isEnded = false),
pollKind = PollKind.Disclosed,
isPollEnded = false,
isPollEditable = true,
isMine = true,
onAnswerSelected = { _, _ -> },
onPollEdit = {},
@ -293,6 +297,7 @@ internal fun PollContentCreatorPreview() = ElementPreview {
answerItems = aPollAnswerItemList(isEnded = false),
pollKind = PollKind.Disclosed,
isPollEnded = false,
isPollEditable = false,
isMine = true,
onAnswerSelected = { _, _ -> },
onPollEdit = {},
@ -309,6 +314,7 @@ internal fun PollContentCreatorEndedPreview() = ElementPreview {
answerItems = aPollAnswerItemList(isEnded = true),
pollKind = PollKind.Disclosed,
isPollEnded = true,
isPollEditable = false,
isMine = true,
onAnswerSelected = { _, _ -> },
onPollEdit = {},

View file

@ -21,5 +21,14 @@ import com.bumble.appyx.core.node.Node
import io.element.android.libraries.architecture.FeatureEntryPoint
interface CreatePollEntryPoint : FeatureEntryPoint {
fun createNode(parentNode: Node, buildContext: BuildContext): Node
data class Params(
val mode: CreatePollMode,
)
fun nodeBuilder(parentNode: Node, buildContext: BuildContext): NodeBuilder
interface NodeBuilder {
fun params(params: Params): NodeBuilder
fun build(): Node
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.features.poll.api.create
import io.element.android.libraries.matrix.api.core.EventId
sealed interface CreatePollMode {
data object NewPoll : CreatePollMode
data class EditPoll(val eventId: EventId) : CreatePollMode
}