1717package io.getstream.chat.android.ui.feature.messages.composer.attachment.picker.poll
1818
1919import android.os.Bundle
20+ import android.text.InputFilter
2021import android.view.LayoutInflater
2122import android.view.MenuItem
2223import android.view.View
@@ -30,6 +31,7 @@ import androidx.core.widget.addTextChangedListener
3031import androidx.fragment.app.viewModels
3132import androidx.lifecycle.lifecycleScope
3233import io.getstream.chat.android.models.PollConfig
34+ import io.getstream.chat.android.ui.ChatUI
3335import io.getstream.chat.android.ui.R
3436import io.getstream.chat.android.ui.common.utils.PollsConstants
3537import io.getstream.chat.android.ui.databinding.StreamUiFragmentCreatePollBinding
@@ -40,15 +42,28 @@ import kotlinx.coroutines.launch
4042
4143/* *
4244 * Represent the bottom sheet dialog that allows users to pick attachments.
45+ *
46+ * Use [newInstance] to create an instance with optional [PollsConfig].
4347 */
4448public class CreatePollDialogFragment : AppCompatDialogFragment () {
4549
4650 private var _binding : StreamUiFragmentCreatePollBinding ? = null
4751 private val binding get() = _binding !!
4852 private var createPollDialogListener: CreatePollDialogListener ? = null
4953 private val createPollViewModel: CreatePollViewModel by viewModels()
54+ private val pollsConfig: PollsConfig by lazy {
55+ if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .TIRAMISU ) {
56+ arguments?.getParcelable(ARG_POLLS_CONFIG , PollsConfig ::class .java)
57+ } else {
58+ @Suppress(" DEPRECATION" )
59+ arguments?.getParcelable(ARG_POLLS_CONFIG )
60+ } ? : ChatUI .pollsConfig
61+ }
5062 private val optionsAdapter: OptionsAdapter by lazy {
51- OptionsAdapter { id, text -> createPollViewModel.onOptionTextChanged(id, text) }
63+ OptionsAdapter (
64+ optionTextLimit = pollsConfig.optionTextLimit,
65+ onOptionChange = { id, text -> createPollViewModel.onOptionTextChanged(id, text) },
66+ )
5267 }
5368 private lateinit var sendMenuItem: MenuItem
5469
@@ -76,11 +91,56 @@ public class CreatePollDialogFragment : AppCompatDialogFragment() {
7691 setupDialog()
7792 }
7893
94+ /* *
95+ * Configures the visibility and default values of poll features based on [pollsConfig].
96+ */
97+ private fun configurePollFeatures () {
98+ // Configure multiple votes feature
99+ createPollViewModel.setAllowMultipleVotes(pollsConfig.multipleVotes.defaultValue)
100+ binding.multipleAnswersLabel.isVisible = pollsConfig.multipleVotes.configurable
101+ binding.multipleAnswersSwitch.isVisible = pollsConfig.multipleVotes.configurable
102+ if (pollsConfig.multipleVotes.configurable) {
103+ binding.multipleAnswersSwitch.isChecked = pollsConfig.multipleVotes.defaultValue
104+ binding.multipleAnswersCount.isVisible = pollsConfig.multipleVotes.defaultValue
105+ }
106+
107+ // Configure anonymous poll feature
108+ createPollViewModel.setAnnonymousPoll(pollsConfig.anonymousPoll.defaultValue)
109+ binding.anonymousPollLabel.isVisible = pollsConfig.anonymousPoll.configurable
110+ binding.anonymousPollSwitch.isVisible = pollsConfig.anonymousPoll.configurable
111+ if (pollsConfig.anonymousPoll.configurable) {
112+ binding.anonymousPollSwitch.isChecked = pollsConfig.anonymousPoll.defaultValue
113+ }
114+
115+ // Configure suggest an option feature
116+ createPollViewModel.setSuggestAnOption(pollsConfig.suggestAnOption.defaultValue)
117+ binding.suggestAnOptionLabel.isVisible = pollsConfig.suggestAnOption.configurable
118+ binding.suggestAnOptionSwitch.isVisible = pollsConfig.suggestAnOption.configurable
119+ if (pollsConfig.suggestAnOption.configurable) {
120+ binding.suggestAnOptionSwitch.isChecked = pollsConfig.suggestAnOption.defaultValue
121+ }
122+
123+ // Configure add a comment feature
124+ createPollViewModel.setAllowAnswers(pollsConfig.addComments.defaultValue)
125+ binding.addACommentLabel.isVisible = pollsConfig.addComments.configurable
126+ binding.addACommentLabelSwitch.isVisible = pollsConfig.addComments.configurable
127+ if (pollsConfig.addComments.configurable) {
128+ binding.addACommentLabelSwitch.isChecked = pollsConfig.addComments.defaultValue
129+ }
130+ }
131+
79132 /* *
80133 * Initializes the dialog.
81134 */
82135 private fun setupDialog () {
83136 setupToolbar(binding.toolbar)
137+ pollsConfig.questionTextLimit?.takeIf { it > 0 }?.let { limit ->
138+ binding.question.filters = arrayOf(InputFilter .LengthFilter (limit))
139+ }
140+
141+ // Configure poll feature visibility and default values based on pollsConfig
142+ configurePollFeatures()
143+
84144 binding.multipleAnswersSwitch.setOnCheckedChangeListener { _, isChecked ->
85145 binding.multipleAnswersCount.isVisible = isChecked
86146 createPollViewModel.setAllowMultipleVotes(isChecked)
@@ -97,6 +157,9 @@ public class CreatePollDialogFragment : AppCompatDialogFragment() {
97157 binding.suggestAnOptionSwitch.setOnCheckedChangeListener { _, isChecked ->
98158 createPollViewModel.setSuggestAnOption(isChecked)
99159 }
160+ binding.addACommentLabelSwitch.setOnCheckedChangeListener { _, isChecked ->
161+ createPollViewModel.setAllowAnswers(isChecked)
162+ }
100163 binding.addOption.setOnClickListener {
101164 createPollViewModel.createOption()
102165 }
@@ -158,15 +221,25 @@ public class CreatePollDialogFragment : AppCompatDialogFragment() {
158221
159222 public companion object {
160223 public const val TAG : String = " create_poll_dialog_fragment"
224+ private const val ARG_POLLS_CONFIG : String = " arg_polls_config"
161225
162226 /* *
163227 * Creates a new instance of [CreatePollDialogFragment].
164228 *
229+ * @param createPollDialogListener The listener for poll creation events.
230+ * @param pollsConfig Optional configuration for poll features. Defaults to [ChatUI.pollsConfig].
165231 * @return A new instance of [CreatePollDialogFragment].
166232 */
167- public fun newInstance (createPollDialogListener : CreatePollDialogListener ): CreatePollDialogFragment {
168- return CreatePollDialogFragment ()
169- .setCreatePollDialogListener(createPollDialogListener)
233+ @JvmOverloads
234+ public fun newInstance (
235+ createPollDialogListener : CreatePollDialogListener ,
236+ pollsConfig : PollsConfig ? = null,
237+ ): CreatePollDialogFragment {
238+ return CreatePollDialogFragment ().apply {
239+ arguments = Bundle ().apply {
240+ pollsConfig?.let { config -> putParcelable(ARG_POLLS_CONFIG , config as android.os.Parcelable ) }
241+ }
242+ }.setCreatePollDialogListener(createPollDialogListener)
170243 }
171244 }
172245
0 commit comments