-
Notifications
You must be signed in to change notification settings - Fork 35
/
PollQuestionViewHolder.kt
179 lines (165 loc) · 8.52 KB
/
PollQuestionViewHolder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package live.hms.roomkit.ui.polls
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import live.hms.roomkit.R
import live.hms.roomkit.databinding.LayoutPollQuestionCreationItemBinding
import live.hms.roomkit.databinding.LayoutPollQuizItemShortAnswerBinding
import live.hms.roomkit.databinding.LayoutPollQuizOptionsItemMultiChoiceBinding
import live.hms.roomkit.util.setOnSingleClickListener
private var count : Long = 0
sealed class QuestionUi(open val index : Long, open val viewType : Int, open val requiredToAnswer : Boolean){
// Makes the creator UI show up first.
object QuestionCreator : QuestionUi(0, 0, false)
// Actual questions that might be asked.
data class MultiChoiceQuestion(val withTitle: String, val options: List<String>, val correctOptionIndex: List<Int>?, override val index : Long,
override val requiredToAnswer: Boolean) : QuestionUi(index, 1, requiredToAnswer)
data class SingleChoiceQuestion(val withTitle: String,
val options: List<String>,
val correctOptionIndex: Int?,
override val index : Long,
override val requiredToAnswer: Boolean) : QuestionUi(index, 2, requiredToAnswer)
data class LongAnswer(val text : String, override val index: Long,
override val requiredToAnswer: Boolean) : QuestionUi(index, 3, requiredToAnswer)
data class ShortAnswer(val text : String, override val index: Long,
override val requiredToAnswer: Boolean) : QuestionUi(index, 4, requiredToAnswer)
}
class PollQuestionViewHolder<T : ViewBinding>(val binding: T,
val saveInfo : (questionUi: QuestionUi) -> Unit
) : RecyclerView.ViewHolder(binding.root) {
private val TAG = "PollQuestionViewHolder"
fun bind(questionUi: QuestionUi) {
when(questionUi) {
is QuestionUi.QuestionCreator -> bind(QuestionUi.QuestionCreator)
is QuestionUi.LongAnswer -> bind(questionUi)
is QuestionUi.MultiChoiceQuestion -> bind(questionUi)
is QuestionUi.ShortAnswer -> bind(questionUi)
is QuestionUi.SingleChoiceQuestion -> bind(questionUi)
}
}
private fun bind(questionUi: QuestionUi.QuestionCreator) {
with(binding as LayoutPollQuestionCreationItemBinding) {
val requiredToAnswer = !notRequiredToAnswer.isChecked
val optionsAdapter = OptionsListAdapter()
optionsListView.adapter = optionsAdapter
optionsListView.layoutManager = LinearLayoutManager(binding.root.context)
questionTypeSpinner.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
// Reset options whenever a question type is selected
optionsAdapter.submitList(emptyList())
// If short/long answer hide the options else show them
val multiOptionVisibility = if(position > 1) View.GONE else View.VISIBLE
addAnOptionTextView.visibility = multiOptionVisibility
optionsListView.visibility = multiOptionVisibility
optionsHeading.visibility = multiOptionVisibility
optionsHeading.text = if(position == 0 ) {
binding.root.context.getString(R.string.single_choice_text)
} else if(position == 1 ){
binding.root.context.getString(R.string.multi_choice_text)
} else {
""
}
// Only the UI might need to be toggled
Log.d(TAG,"Toggle UI")
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
addAnOptionTextView.setOnSingleClickListener {
val showCheckBox = questionTypeSpinner.selectedItemPosition == 1
optionsAdapter.submitList(optionsAdapter.currentList.plus(Option("", showCheckBox)))
saveButton.isEnabled = true
}
deleteOptionTrashButton.setOnSingleClickListener {
// Delete the last option when delete is clicked.
optionsAdapter.submitList(optionsAdapter.currentList.dropLast(1))
saveButton.isEnabled = optionsAdapter.currentList.size > 1
}
saveButton.isEnabled = false
saveButton.setOnClickListener {
saveButton.isEnabled = false
val title = askAQuestionEditText.text.toString()
val newQuestionUi = when(questionTypeSpinner.selectedItemPosition){
// single, multi, short, long
0,1 -> {
val items = (optionsListView.adapter as OptionsListAdapter).currentList.map { it.text }
if(questionTypeSpinner.selectedItemPosition == 0) {
val selectedIndex = optionsAdapter.currentList.indexOfFirst { it.isChecked }
val selected = if(selectedIndex == -1)
null
else
selectedIndex
QuestionUi.SingleChoiceQuestion(
title,
items,
selected,
count++,
requiredToAnswer
)
}
else {
val selectedIndices = mutableListOf<Int>()
optionsAdapter.currentList.forEachIndexed { index, option ->
if(option.isChecked)
selectedIndices.add(index)
}
QuestionUi.MultiChoiceQuestion(
title,
items,
selectedIndices,
count++,
requiredToAnswer
)
}
}
2 -> QuestionUi.ShortAnswer(title, count++, requiredToAnswer)
3 -> QuestionUi.LongAnswer(title, count++, requiredToAnswer)
else -> null
}
// Reset the UI
optionsAdapter.submitList(emptyList())
askAQuestionEditText.setText("")
// Save the info
saveInfo(newQuestionUi!!)
}
}
}
private fun bind(questionUi: QuestionUi.MultiChoiceQuestion) {
with(binding as LayoutPollQuizOptionsItemMultiChoiceBinding){
questionTitle.text = questionUi.withTitle
val adapter = ArrayAdapter<String>(binding.root.context, android.R.layout.simple_list_item_1)
options.layoutManager = LinearLayoutManager(binding.root.context)
adapter.addAll(questionUi.options)
}
}
private fun bind(questionUi: QuestionUi.SingleChoiceQuestion) {
with(binding as LayoutPollQuizOptionsItemMultiChoiceBinding){
questionTitle.text = questionUi.withTitle
val adapter = ArrayAdapter<String>(binding.root.context, android.R.layout.simple_list_item_1)
options.layoutManager = LinearLayoutManager(binding.root.context)
adapter.addAll(questionUi.options)
}
}
private fun bind(questionUi: QuestionUi.ShortAnswer) {
with(binding as LayoutPollQuizItemShortAnswerBinding) {
questionNumberHeading.text = "QUESTION ${questionUi.index} : Short Answer"
questionHeading.text = questionUi.text
}
}
private fun bind(questionUi: QuestionUi.LongAnswer) {
with(binding as LayoutPollQuizItemShortAnswerBinding) {
questionNumberHeading.text = "QUESTION ${questionUi.index} : Long Answer"
questionHeading.text = questionUi.text
}
}
}