Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AND-319] Improve poll creation process(compose). #5634

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

### ⬆️ Improved
- Autofocus the input fields in the poll creation screen. [#5629](https://github.com/GetStream/stream-chat-android/pull/5629)
- 🚨Breaking change: Change the `AttachmentPickerPollCreation` event to hold a prepared `PollConfig` to simplify the poll creation handling. [#5634](https://github.com/GetStream/stream-chat-android/pull/5634)

### ✅ Added

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@ import io.getstream.chat.android.compose.viewmodel.messages.MessageComposerViewM
import io.getstream.chat.android.compose.viewmodel.messages.MessageListViewModel
import io.getstream.chat.android.compose.viewmodel.messages.MessagesViewModelFactory
import io.getstream.chat.android.models.Channel
import io.getstream.chat.android.models.PollConfig
import io.getstream.chat.android.models.ReactionSortingByFirstReactionAt
import io.getstream.chat.android.models.ReactionSortingByLastReactionAt
import io.getstream.chat.android.models.VotingVisibility
import io.getstream.chat.android.ui.common.state.messages.MessageMode
import io.getstream.chat.android.ui.common.state.messages.Reply
import io.getstream.chat.android.ui.common.state.messages.list.DeletedMessageVisibility
Expand Down Expand Up @@ -279,25 +277,7 @@ class MessagesActivity : BaseConnectedActivity() {
onTabClick = { _, tab -> isFullScreenContent = tab.isFullContent },
onAttachmentPickerAction = { action ->
if (action is AttachmentPickerPollCreation) {
composerViewModel.createPoll(
pollConfig = PollConfig(
name = action.question,
options = action.options.filter { it.title.isNotEmpty() }.map { it.title },
description = action.question,
allowUserSuggestedOptions = action.switches.any { it.key == "allowUserSuggestedOptions" && it.enabled },
votingVisibility = if (action.switches.any { it.key == "votingVisibility" && it.enabled }) {
VotingVisibility.ANONYMOUS
} else {
VotingVisibility.PUBLIC
},
maxVotesAllowed = if (action.switches.any { it.key == "maxVotesAllowed" && it.enabled }) {
action.switches.first { it.key == "maxVotesAllowed" }.pollSwitchInput?.value.toString()
.toInt()
} else {
1
},
),
)
composerViewModel.createPoll(action.pollConfig)
}
},
onDismiss = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1556,16 +1556,12 @@ public final class io/getstream/chat/android/compose/ui/messages/attachments/fac

public final class io/getstream/chat/android/compose/ui/messages/attachments/factory/AttachmentPickerPollCreation : io/getstream/chat/android/compose/ui/messages/attachments/factory/AttachmentPickerAction {
public static final field $stable I
public fun <init> (Ljava/lang/String;Ljava/util/List;Ljava/util/List;)V
public final fun component1 ()Ljava/lang/String;
public final fun component2 ()Ljava/util/List;
public final fun component3 ()Ljava/util/List;
public final fun copy (Ljava/lang/String;Ljava/util/List;Ljava/util/List;)Lio/getstream/chat/android/compose/ui/messages/attachments/factory/AttachmentPickerPollCreation;
public static synthetic fun copy$default (Lio/getstream/chat/android/compose/ui/messages/attachments/factory/AttachmentPickerPollCreation;Ljava/lang/String;Ljava/util/List;Ljava/util/List;ILjava/lang/Object;)Lio/getstream/chat/android/compose/ui/messages/attachments/factory/AttachmentPickerPollCreation;
public fun <init> (Lio/getstream/chat/android/models/PollConfig;)V
public final fun component1 ()Lio/getstream/chat/android/models/PollConfig;
public final fun copy (Lio/getstream/chat/android/models/PollConfig;)Lio/getstream/chat/android/compose/ui/messages/attachments/factory/AttachmentPickerPollCreation;
public static synthetic fun copy$default (Lio/getstream/chat/android/compose/ui/messages/attachments/factory/AttachmentPickerPollCreation;Lio/getstream/chat/android/models/PollConfig;ILjava/lang/Object;)Lio/getstream/chat/android/compose/ui/messages/attachments/factory/AttachmentPickerPollCreation;
public fun equals (Ljava/lang/Object;)Z
public final fun getOptions ()Ljava/util/List;
public final fun getQuestion ()Ljava/lang/String;
public final fun getSwitches ()Ljava/util/List;
public final fun getPollConfig ()Lio/getstream/chat/android/models/PollConfig;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ import io.getstream.chat.android.compose.viewmodel.messages.MessagesViewModelFac
import io.getstream.chat.android.models.Channel
import io.getstream.chat.android.models.LinkPreview
import io.getstream.chat.android.models.Message
import io.getstream.chat.android.models.PollConfig
import io.getstream.chat.android.models.ReactionSorting
import io.getstream.chat.android.models.ReactionSortingByFirstReactionAt
import io.getstream.chat.android.models.User
import io.getstream.chat.android.models.VotingVisibility
import io.getstream.chat.android.ui.common.state.messages.Delete
import io.getstream.chat.android.ui.common.state.messages.Edit
import io.getstream.chat.android.ui.common.state.messages.Flag
Expand Down Expand Up @@ -703,26 +701,7 @@ public fun BoxScope.AttachmentsPickerMenu(
},
onAttachmentPickerAction = { action ->
if (action is AttachmentPickerPollCreation) {
composerViewModel.createPoll(
pollConfig = PollConfig(
name = action.question,
options = action.options.filter { it.title.isNotEmpty() }.map { it.title },
allowUserSuggestedOptions = action.switches.any { it.key == "allowUserSuggestedOptions" && it.enabled },
allowAnswers = action.switches.any { it.key == "allowAnswers" && it.enabled },
votingVisibility = if (action.switches.any { it.key == "votingVisibility" && it.enabled }) {
VotingVisibility.ANONYMOUS
} else {
VotingVisibility.PUBLIC
},
maxVotesAllowed = if (action.switches.any { it.key == "maxVotesAllowed" && it.enabled }) {
action.switches.first { it.key == "maxVotesAllowed" }.pollSwitchInput?.value.toString()
.toInt()
} else {
1
},
enforceUniqueVote = action.switches.none { it.key == "maxVotesAllowed" && it.enabled },
),
)
composerViewModel.createPoll(action.pollConfig)
}
},
onTabClick = { _, tab -> isFullScreenContent = tab.isFullContent },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
package io.getstream.chat.android.compose.ui.messages.attachments.factory

import androidx.compose.runtime.Stable
import io.getstream.chat.android.compose.ui.messages.attachments.poll.PollOptionItem
import io.getstream.chat.android.compose.ui.messages.attachments.poll.PollSwitchItem
import io.getstream.chat.android.models.PollConfig

/**
* An user action event that occurs inside the attachment picker screen.
Expand All @@ -34,10 +33,8 @@ public data object AttachmentPickerBack : AttachmentPickerAction

/**
* An user action that indicates a poll creation event.
*
* @param pollConfig The [PollConfig] object holding the data required to create the new poll.
*/
@Stable
public data class AttachmentPickerPollCreation(
public val question: String,
public val options: List<PollOptionItem>,
public val switches: List<PollSwitchItem>,
) : AttachmentPickerAction
public data class AttachmentPickerPollCreation(public val pollConfig: PollConfig) : AttachmentPickerAction
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ import io.getstream.chat.android.compose.ui.messages.attachments.poll.PollOption
import io.getstream.chat.android.compose.ui.messages.attachments.poll.PollOptionList
import io.getstream.chat.android.compose.ui.messages.attachments.poll.PollQuestionInput
import io.getstream.chat.android.compose.ui.messages.attachments.poll.PollSwitchItem
import io.getstream.chat.android.compose.ui.messages.attachments.poll.PollSwitchItemKeys
import io.getstream.chat.android.compose.ui.messages.attachments.poll.PollSwitchList
import io.getstream.chat.android.compose.ui.messages.attachments.poll.pollConfigFrom
import io.getstream.chat.android.compose.ui.theme.ChatTheme
import io.getstream.chat.android.models.Channel
import io.getstream.chat.android.models.ChannelCapabilities
Expand Down Expand Up @@ -149,9 +151,11 @@ public class AttachmentsPickerPollTabFactory : AttachmentsPickerTabFactory {
onPollCreateClicked = {
onAttachmentPickerAction.invoke(
AttachmentPickerPollCreation(
question = question,
options = optionItemList,
switches = switchItemList,
pollConfigFrom(
pollQuestion = question,
pollOptions = optionItemList,
pollSwitches = switchItemList,
),
),
)
onAttachmentPickerAction.invoke(AttachmentPickerBack)
Expand Down Expand Up @@ -236,7 +240,7 @@ private fun updateMaxVotesAllowedSwitch(
): List<PollSwitchItem> =
switchItemList.map {
when (it.key) {
"maxVotesAllowed" -> it.copy(
PollSwitchItemKeys.MAX_VOTES_ALLOWED -> it.copy(
pollSwitchInput = it.pollSwitchInput?.copy(
maxValue = optionItemList.count { item -> item.title.isNotBlank() },
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
*
* 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.getstream.chat.android.compose.ui.messages.attachments.poll

import io.getstream.chat.android.compose.ui.messages.attachments.factory.AttachmentPickerPollCreation
import io.getstream.chat.android.models.PollConfig
import io.getstream.chat.android.models.VotingVisibility

/**
* Builds a [PollConfig] from the provided [AttachmentPickerPollCreation] data.
*
* @param pollQuestion The question of the poll.
* @param pollOptions The list of poll options.
* @param pollSwitches The list of poll switches.
*/
internal fun pollConfigFrom(
pollQuestion: String,
pollOptions: List<PollOptionItem>,
pollSwitches: List<PollSwitchItem>,
): PollConfig {
val options = pollOptions
.filter { it.title.isNotEmpty() }
.map { it.title }
val allowUserSuggestedOptions = pollSwitches.any {
it.key == PollSwitchItemKeys.ALLOW_USER_SUGGESTED_OPTIONS && it.enabled
}
val allowAnswers = pollSwitches.any {
it.key == PollSwitchItemKeys.ALLOW_ANSWERS && it.enabled
}
val anonymousPoll = pollSwitches.any {
it.key == PollSwitchItemKeys.VOTING_VISIBILITY && it.enabled
}
val votingVisibility = if (anonymousPoll) {
VotingVisibility.ANONYMOUS
} else {
VotingVisibility.PUBLIC
}
val maxVotesEnabled = pollSwitches.any {
it.key == PollSwitchItemKeys.MAX_VOTES_ALLOWED && it.enabled
}
val maxVotesAllowed = if (maxVotesEnabled) {
pollSwitches.first { it.key == PollSwitchItemKeys.MAX_VOTES_ALLOWED }.pollSwitchInput?.value.toString().toInt()
} else {
1
}
val enforceUniqueVotes = pollSwitches.none {
it.key == PollSwitchItemKeys.MAX_VOTES_ALLOWED && it.enabled
}
return PollConfig(
name = pollQuestion,
options = options,
allowUserSuggestedOptions = allowUserSuggestedOptions,
allowAnswers = allowAnswers,
votingVisibility = votingVisibility,
maxVotesAllowed = maxVotesAllowed,
enforceUniqueVote = enforceUniqueVotes,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,29 @@ public data class PollSwitchInput(
public val maxValue: Any? = null,
public val keyboardType: KeyboardType = KeyboardType.Text,
)

/**
* Holds the internal keys identifying the different poll switch items.
*/
internal object PollSwitchItemKeys {

/**
* The key that identifies the maximum votes allowed switch.
*/
internal const val MAX_VOTES_ALLOWED = "maxVotesAllowed"

/**
* The key that identifies the voting visibility switch.
*/
internal const val VOTING_VISIBILITY = "votingVisibility"

/**
* The key that identifies the allow user suggested options switch.
*/
internal const val ALLOW_USER_SUGGESTED_OPTIONS = "allowUserSuggestedOptions"

/**
* The key that identifies the allow answers switch.
*/
internal const val ALLOW_ANSWERS = "allowAnswers"
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.ui.text.input.KeyboardType
import io.getstream.chat.android.compose.R
import io.getstream.chat.android.compose.ui.messages.attachments.poll.PollSwitchInput
import io.getstream.chat.android.compose.ui.messages.attachments.poll.PollSwitchItem
import io.getstream.chat.android.compose.ui.messages.attachments.poll.PollSwitchItemKeys

/**
* An interface that allows the creation of poll switch items for the creation screen.
Expand Down Expand Up @@ -67,22 +68,22 @@ public class DefaultPollSwitchItemFactory(
maxValue = 2,
keyboardType = KeyboardType.Decimal,
),
key = "maxVotesAllowed",
key = PollSwitchItemKeys.MAX_VOTES_ALLOWED,
enabled = false,
),
PollSwitchItem(
title = context.getString(R.string.stream_compose_poll_option_switch_anonymous_poll),
key = "votingVisibility",
key = PollSwitchItemKeys.VOTING_VISIBILITY,
enabled = false,
),
PollSwitchItem(
title = context.getString(R.string.stream_compose_poll_option_switch_suggest_option),
key = "allowUserSuggestedOptions",
key = PollSwitchItemKeys.ALLOW_USER_SUGGESTED_OPTIONS,
enabled = false,
),
PollSwitchItem(
title = context.getString(R.string.stream_compose_poll_option_switch_add_comment),
key = "allowAnswers",
key = PollSwitchItemKeys.ALLOW_ANSWERS,
enabled = false,
),
)
Expand Down
Loading
Loading