AddPollDialog.kt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Copyright 2019 Tusky Contributors
  2. *
  3. * This file is a part of Tusky.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it under the terms of the
  6. * GNU General Public License as published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  10. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. * Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with Tusky; if not,
  14. * see <http://www.gnu.org/licenses>. */
  15. @file:JvmName("AddPollDialog")
  16. package com.keylesspalace.tusky.components.compose.dialog
  17. import android.content.Context
  18. import android.view.LayoutInflater
  19. import android.view.WindowManager
  20. import android.widget.ArrayAdapter
  21. import androidx.appcompat.app.AlertDialog
  22. import com.keylesspalace.tusky.R
  23. import com.keylesspalace.tusky.databinding.DialogAddPollBinding
  24. import com.keylesspalace.tusky.entity.NewPoll
  25. fun showAddPollDialog(
  26. context: Context,
  27. poll: NewPoll?,
  28. maxOptionCount: Int,
  29. maxOptionLength: Int,
  30. minDuration: Int,
  31. maxDuration: Int,
  32. onUpdatePoll: (NewPoll) -> Unit
  33. ) {
  34. val binding = DialogAddPollBinding.inflate(LayoutInflater.from(context))
  35. val dialog = AlertDialog.Builder(context)
  36. .setIcon(R.drawable.ic_poll_24dp)
  37. .setTitle(R.string.create_poll_title)
  38. .setView(binding.root)
  39. .setNegativeButton(android.R.string.cancel, null)
  40. .setPositiveButton(android.R.string.ok, null)
  41. .create()
  42. val adapter = AddPollOptionsAdapter(
  43. options = poll?.options?.toMutableList() ?: mutableListOf("", ""),
  44. maxOptionLength = maxOptionLength,
  45. onOptionRemoved = { valid ->
  46. binding.addChoiceButton.isEnabled = true
  47. dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = valid
  48. },
  49. onOptionChanged = { valid ->
  50. dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = valid
  51. }
  52. )
  53. binding.pollChoices.adapter = adapter
  54. var durations = context.resources.getIntArray(R.array.poll_duration_values).toList()
  55. val durationLabels = context.resources.getStringArray(R.array.poll_duration_names).filterIndexed { index, _ -> durations[index] in minDuration..maxDuration }
  56. binding.pollDurationSpinner.adapter = ArrayAdapter(context, android.R.layout.simple_spinner_item, durationLabels).apply {
  57. setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item)
  58. }
  59. durations = durations.filter { it in minDuration..maxDuration }
  60. binding.addChoiceButton.setOnClickListener {
  61. if (adapter.itemCount < maxOptionCount) {
  62. adapter.addChoice()
  63. }
  64. if (adapter.itemCount >= maxOptionCount) {
  65. it.isEnabled = false
  66. }
  67. }
  68. val pollDurationId = durations.indexOfLast {
  69. it <= (poll?.expiresIn ?: 0)
  70. }
  71. binding.pollDurationSpinner.setSelection(pollDurationId)
  72. binding.multipleChoicesCheckBox.isChecked = poll?.multiple ?: false
  73. dialog.setOnShowListener {
  74. val button = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
  75. button.setOnClickListener {
  76. val selectedPollDurationId = binding.pollDurationSpinner.selectedItemPosition
  77. onUpdatePoll(
  78. NewPoll(
  79. options = adapter.pollOptions,
  80. expiresIn = durations[selectedPollDurationId],
  81. multiple = binding.multipleChoicesCheckBox.isChecked
  82. )
  83. )
  84. dialog.dismiss()
  85. }
  86. }
  87. dialog.show()
  88. // make the dialog focusable so the keyboard does not stay behind it
  89. dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
  90. }