Skip to content

Commit

Permalink
Merge pull request #2609 from element-hq/feature/bma/pollTrophy
Browse files Browse the repository at this point in the history
Poll trophy
  • Loading branch information
bmarty authored Mar 27, 2024
2 parents d3f8962 + ea73c51 commit 347da7b
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 55 deletions.
1 change: 1 addition & 0 deletions changelog.d/2608.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make completed poll more clearly visible
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import io.element.android.libraries.matrix.api.poll.PollAnswer
* @property isSelected whether the user has selected this answer.
* @property isEnabled whether the answer can be voted.
* @property isWinner whether this is the winner answer in the poll.
* @property isDisclosed whether the votes for this answer should be disclosed.
* @property showVotes whether the votes for this answer should be displayed.
* @property votesCount the number of votes for this answer.
* @property percentage the percentage of votes for this answer.
*/
Expand All @@ -34,7 +34,7 @@ data class PollAnswerItem(
val isSelected: Boolean,
val isEnabled: Boolean,
val isWinner: Boolean,
val isDisclosed: Boolean,
val showVotes: Boolean,
val votesCount: Int,
val percentage: Float,
)
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import io.element.android.libraries.designsystem.theme.components.LinearProgress
import io.element.android.libraries.designsystem.theme.components.Text
import io.element.android.libraries.designsystem.theme.progressIndicatorTrackColor
import io.element.android.libraries.designsystem.toEnabledColor
import io.element.android.libraries.designsystem.utils.CommonDrawables
import io.element.android.libraries.ui.strings.CommonPlurals

@Composable
Expand Down Expand Up @@ -79,17 +80,36 @@ internal fun PollAnswerView(
text = answerItem.answer.text,
style = if (answerItem.isWinner) ElementTheme.typography.fontBodyLgMedium else ElementTheme.typography.fontBodyLgRegular,
)
if (answerItem.isDisclosed) {
Text(
modifier = Modifier.align(Alignment.Bottom),
text = pluralStringResource(
id = CommonPlurals.common_poll_votes_count,
count = answerItem.votesCount,
answerItem.votesCount
),
style = if (answerItem.isWinner) ElementTheme.typography.fontBodySmMedium else ElementTheme.typography.fontBodySmRegular,
color = if (answerItem.isWinner) ElementTheme.colors.textPrimary else ElementTheme.colors.textSecondary,
if (answerItem.showVotes) {
val text = pluralStringResource(
id = CommonPlurals.common_poll_votes_count,
count = answerItem.votesCount,
answerItem.votesCount
)
Row(
modifier = Modifier.align(Alignment.Bottom),
verticalAlignment = Alignment.CenterVertically,
) {
if (answerItem.isWinner) {
Icon(
resourceId = CommonDrawables.ic_winner,
contentDescription = null,
tint = ElementTheme.colors.iconAccentTertiary,
)
Spacer(modifier = Modifier.width(2.dp))
Text(
text = text,
style = ElementTheme.typography.fontBodySmMedium,
color = ElementTheme.colors.textPrimary,
)
} else {
Text(
text = text,
style = ElementTheme.typography.fontBodySmRegular,
color = ElementTheme.colors.textSecondary,
)
}
}
}
}
Spacer(modifier = Modifier.height(10.dp))
Expand All @@ -98,7 +118,7 @@ internal fun PollAnswerView(
color = if (answerItem.isWinner) ElementTheme.colors.textSuccessPrimary else answerItem.isEnabled.toEnabledColor(),
progress = {
when {
answerItem.isDisclosed -> answerItem.percentage
answerItem.showVotes -> answerItem.percentage
answerItem.isSelected -> 1f
else -> 0f
}
Expand All @@ -114,54 +134,54 @@ internal fun PollAnswerView(
@Composable
internal fun PollAnswerDisclosedNotSelectedPreview() = ElementPreview {
PollAnswerView(
answerItem = aPollAnswerItem(isDisclosed = true, isSelected = false),
answerItem = aPollAnswerItem(showVotes = true, isSelected = false),
)
}

@PreviewsDayNight
@Composable
internal fun PollAnswerDisclosedSelectedPreview() = ElementPreview {
PollAnswerView(
answerItem = aPollAnswerItem(isDisclosed = true, isSelected = true),
answerItem = aPollAnswerItem(showVotes = true, isSelected = true),
)
}

@PreviewsDayNight
@Composable
internal fun PollAnswerUndisclosedNotSelectedPreview() = ElementPreview {
PollAnswerView(
answerItem = aPollAnswerItem(isDisclosed = false, isSelected = false),
answerItem = aPollAnswerItem(showVotes = false, isSelected = false),
)
}

@PreviewsDayNight
@Composable
internal fun PollAnswerUndisclosedSelectedPreview() = ElementPreview {
PollAnswerView(
answerItem = aPollAnswerItem(isDisclosed = false, isSelected = true),
answerItem = aPollAnswerItem(showVotes = false, isSelected = true),
)
}

@PreviewsDayNight
@Composable
internal fun PollAnswerEndedWinnerNotSelectedPreview() = ElementPreview {
PollAnswerView(
answerItem = aPollAnswerItem(isDisclosed = true, isSelected = false, isEnabled = false, isWinner = true),
answerItem = aPollAnswerItem(showVotes = true, isSelected = false, isEnabled = false, isWinner = true),
)
}

@PreviewsDayNight
@Composable
internal fun PollAnswerEndedWinnerSelectedPreview() = ElementPreview {
PollAnswerView(
answerItem = aPollAnswerItem(isDisclosed = true, isSelected = true, isEnabled = false, isWinner = true),
answerItem = aPollAnswerItem(showVotes = true, isSelected = true, isEnabled = false, isWinner = true),
)
}

@PreviewsDayNight
@Composable
internal fun PollAnswerEndedSelectedPreview() = ElementPreview {
PollAnswerView(
answerItem = aPollAnswerItem(isDisclosed = true, isSelected = true, isEnabled = false, isWinner = false),
answerItem = aPollAnswerItem(showVotes = true, isSelected = true, isEnabled = false, isWinner = false),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,35 @@ fun aPollQuestion() = "What type of food should we have at the party?"
fun aPollAnswerItemList(
hasVotes: Boolean = true,
isEnded: Boolean = false,
isDisclosed: Boolean = true,
showVotes: Boolean = true,
) = persistentListOf(
aPollAnswerItem(
answer = PollAnswer("option_1", "Italian \uD83C\uDDEE\uD83C\uDDF9"),
isDisclosed = isDisclosed,
showVotes = showVotes,
isEnabled = !isEnded,
isWinner = isEnded,
votesCount = if (hasVotes) 5 else 0,
percentage = if (hasVotes) 0.5f else 0f
),
aPollAnswerItem(
answer = PollAnswer("option_2", "Chinese \uD83C\uDDE8\uD83C\uDDF3"),
isDisclosed = isDisclosed,
showVotes = showVotes,
isEnabled = !isEnded,
isWinner = false,
votesCount = 0,
percentage = 0f
),
aPollAnswerItem(
answer = PollAnswer("option_3", "Brazilian \uD83C\uDDE7\uD83C\uDDF7"),
isDisclosed = isDisclosed,
showVotes = showVotes,
isEnabled = !isEnded,
isWinner = false,
isSelected = true,
votesCount = if (hasVotes) 1 else 0,
percentage = if (hasVotes) 0.1f else 0f
),
aPollAnswerItem(
isDisclosed = isDisclosed,
showVotes = showVotes,
isEnabled = !isEnded,
votesCount = if (hasVotes) 4 else 0,
percentage = if (hasVotes) 0.4f else 0f,
Expand All @@ -70,15 +70,15 @@ fun aPollAnswerItem(
isSelected: Boolean = false,
isEnabled: Boolean = true,
isWinner: Boolean = false,
isDisclosed: Boolean = true,
showVotes: Boolean = true,
votesCount: Int = 4,
percentage: Float = 0.4f,
) = PollAnswerItem(
answer = answer,
isSelected = isSelected,
isEnabled = isEnabled,
isWinner = isWinner,
isDisclosed = isDisclosed,
showVotes = showVotes,
votesCount = votesCount,
percentage = percentage
)
Expand All @@ -87,14 +87,14 @@ fun aPollContentState(
eventId: EventId? = null,
isMine: Boolean = false,
isEnded: Boolean = false,
isDisclosed: Boolean = true,
showVotes: Boolean = true,
isPollEditable: Boolean = true,
hasVotes: Boolean = true,
question: String = aPollQuestion(),
pollKind: PollKind = PollKind.Disclosed,
answerItems: ImmutableList<PollAnswerItem> = aPollAnswerItemList(
isEnded = isEnded,
isDisclosed = isDisclosed,
showVotes = showVotes,
hasVotes = hasVotes
),
) = PollContentState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ internal fun PollContentUndisclosedPreview() = ElementPreview {
PollContentView(
eventId = EventId("\$anEventId"),
question = "What type of food should we have at the party?",
answerItems = aPollAnswerItemList(isDisclosed = false),
answerItems = aPollAnswerItemList(showVotes = false),
pollKind = PollKind.Undisclosed,
isPollEnded = false,
isPollEditable = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class DefaultPollContentStateFactory @Inject constructor(
isSelected = isSelected,
isEnabled = !isPollEnded,
isWinner = isWinner,
isDisclosed = content.kind.isDisclosed || isPollEnded,
showVotes = content.kind.isDisclosed || isPollEnded,
votesCount = answerVoteCount,
percentage = percentage,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class PollContentStateFactoryTest {
val state = factory.create(eventTimelineItem, aPollContent(PollKind.Undisclosed))
val expectedState = aPollContentState(pollKind = PollKind.Undisclosed).let {
it.copy(
answerItems = it.answerItems.map { answerItem -> answerItem.copy(isDisclosed = false) }.toImmutableList()
answerItems = it.answerItems.map { answerItem -> answerItem.copy(showVotes = false) }.toImmutableList()
)
}
assertThat(state).isEqualTo(expectedState)
Expand All @@ -147,10 +147,10 @@ class PollContentStateFactoryTest {
val expectedState = aPollContentState(
pollKind = PollKind.Undisclosed,
answerItems = listOf(
aPollAnswerItem(answer = A_POLL_ANSWER_1, isDisclosed = false, votesCount = 3, percentage = 0.3f),
aPollAnswerItem(answer = A_POLL_ANSWER_2, isDisclosed = false, isSelected = true, votesCount = 6, percentage = 0.6f),
aPollAnswerItem(answer = A_POLL_ANSWER_3, isDisclosed = false),
aPollAnswerItem(answer = A_POLL_ANSWER_4, isDisclosed = false, votesCount = 1, percentage = 0.1f),
aPollAnswerItem(answer = A_POLL_ANSWER_1, showVotes = false, votesCount = 3, percentage = 0.3f),
aPollAnswerItem(answer = A_POLL_ANSWER_2, showVotes = false, isSelected = true, votesCount = 6, percentage = 0.6f),
aPollAnswerItem(answer = A_POLL_ANSWER_3, showVotes = false),
aPollAnswerItem(answer = A_POLL_ANSWER_4, showVotes = false, votesCount = 1, percentage = 0.1f),
),
)
assertThat(state).isEqualTo(expectedState)
Expand All @@ -164,7 +164,7 @@ class PollContentStateFactoryTest {
pollKind = PollKind.Undisclosed
).let {
it.copy(
answerItems = it.answerItems.map { answerItem -> answerItem.copy(isDisclosed = true, isEnabled = false) }.toImmutableList(),
answerItems = it.answerItems.map { answerItem -> answerItem.copy(showVotes = true, isEnabled = false) }.toImmutableList(),
)
}
assertThat(state).isEqualTo(expectedState)
Expand Down Expand Up @@ -258,15 +258,15 @@ class PollContentStateFactoryTest {
isSelected: Boolean = false,
isEnabled: Boolean = true,
isWinner: Boolean = false,
isDisclosed: Boolean = true,
showVotes: Boolean = true,
votesCount: Int = 0,
percentage: Float = 0f,
) = PollAnswerItem(
answer = answer,
isSelected = isSelected,
isEnabled = isEnabled,
isWinner = isWinner,
isDisclosed = isDisclosed,
showVotes = showVotes,
votesCount = votesCount,
percentage = percentage,
)
Expand Down
28 changes: 28 additions & 0 deletions libraries/designsystem/src/main/res/drawable/ic_winner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--
~ Copyright (c) 2024 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.
-->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="16dp"
android:height="16dp"
android:viewportWidth="16"
android:viewportHeight="16">
<group>
<clip-path android:pathData="M0,0h16v16h-16z" />
<path
android:fillColor="@android:color/white"
android:pathData="M12.667,3.333H11.333V2.667C11.333,2.3 11.033,2 10.667,2H5.333C4.967,2 4.667,2.3 4.667,2.667V3.333H3.333C2.6,3.333 2,3.933 2,4.667V5.333C2,7.033 3.28,8.42 4.927,8.627C5.347,9.627 6.247,10.38 7.333,10.6V12.667H5.333C4.967,12.667 4.667,12.967 4.667,13.333C4.667,13.7 4.967,14 5.333,14H10.667C11.033,14 11.333,13.7 11.333,13.333C11.333,12.967 11.033,12.667 10.667,12.667H8.667V10.6C9.753,10.38 10.653,9.627 11.073,8.627C12.72,8.42 14,7.033 14,5.333V4.667C14,3.933 13.4,3.333 12.667,3.333ZM3.333,5.333V4.667H4.667V7.213C3.893,6.933 3.333,6.2 3.333,5.333ZM12.667,5.333C12.667,6.2 12.107,6.933 11.333,7.213V4.667H12.667V5.333Z" />
</group>
</vector>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 347da7b

Please sign in to comment.