wip: create poll using livedata

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-06-20 10:48:53 +02:00 committed by Andy Scherzinger (Rebase PR Action)
parent af427f8300
commit b140cc8751
7 changed files with 116 additions and 67 deletions

View File

@ -1,53 +1,52 @@
package com.nextcloud.talk.polls.adapters
import android.annotation.SuppressLint
import android.text.Editable
import android.text.TextWatcher
import androidx.recyclerview.widget.RecyclerView
import com.nextcloud.talk.databinding.PollCreateOptionsItemBinding
import com.nextcloud.talk.utils.EmojiTextInputEditText
class PollCreateOptionViewHolder(
private val binding: PollCreateOptionsItemBinding
) : RecyclerView.ViewHolder(binding.root) {
lateinit var optionText: EmojiTextInputEditText
var textListener: TextWatcher? = null
@SuppressLint("SetTextI18n")
fun bind(
pollCreateOptionItem: PollCreateOptionItem,
clickListener: PollCreateOptionsItemClickListener,
position: Int
) {
// binding.root.setOnClickListener { }
textListener?.let {
binding.pollOptionText.removeTextChangedListener(it)
}
binding.pollOptionText.setText(pollCreateOptionItem.pollOption)
binding.pollOptionDelete.setOnClickListener {
clickListener.onDeleteClick(pollCreateOptionItem, position)
clickListener.onRemoveOptionsItemClick(pollCreateOptionItem, position)
}
// binding.pollOptionText.addTextChangedListener(object : TextWatcher {
// override fun afterTextChanged(s: Editable) {
// // unused atm
// }
//
// override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
// // unused atm
// }
//
// override fun onTextChanged(option: CharSequence, start: Int, before: Int, count: Int) {
// pollCreateOptionItem.pollOption = option.toString()
// }
// })
textListener = getTextWatcher(pollCreateOptionItem)
binding.pollOptionText.addTextChangedListener(textListener)
}
// fun onBind(item: SharedItem) {
// Log.d("","bbbb")
// }
//
// fun onLongClick(view: View?): Boolean {
// // moviesList.remove(getAdapterPosition())
// // notifyItemRemoved(getAdapterPosition())
//
// Log.d("", "dfdrg")
// return true
// }
//
// override fun onClick(v: View?) {
// Log.d("", "dfdrg")
// }
private fun getTextWatcher(pollCreateOptionItem: PollCreateOptionItem) =
object : TextWatcher {
override fun afterTextChanged(s: Editable) {
// unused atm
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
// unused atm
}
override fun onTextChanged(option: CharSequence, start: Int, before: Int, count: Int) {
pollCreateOptionItem.pollOption = option.toString()
}
}
}

View File

@ -9,7 +9,7 @@ class PollCreateOptionsAdapter(
private val clickListener: PollCreateOptionsItemClickListener
) : RecyclerView.Adapter<PollCreateOptionViewHolder>() {
internal var list: MutableList<PollCreateOptionItem> = ArrayList<PollCreateOptionItem>()
internal var list: ArrayList<PollCreateOptionItem> = ArrayList<PollCreateOptionItem>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PollCreateOptionViewHolder {
val itemBinding = PollCreateOptionsItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
@ -18,10 +18,16 @@ class PollCreateOptionsAdapter(
}
override fun onBindViewHolder(holder: PollCreateOptionViewHolder, position: Int) {
holder.bind(list[position], clickListener, position)
val currentItem = list[position]
holder.bind(currentItem, clickListener, position)
}
override fun getItemCount(): Int {
return list.size
}
fun updateOptionsList(optionsList: ArrayList<PollCreateOptionItem>) {
list = optionsList
notifyDataSetChanged()
}
}

View File

@ -1,5 +1,5 @@
package com.nextcloud.talk.polls.adapters
interface PollCreateOptionsItemClickListener {
fun onDeleteClick(pollCreateOptionItem: PollCreateOptionItem, position: Int)
fun onRemoveOptionsItemClick(pollCreateOptionItem: PollCreateOptionItem, position: Int)
}

View File

@ -10,6 +10,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import autodagger.AutoInjector
@ -39,6 +40,7 @@ class PollCreateDialogFragment(
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
viewModel = ViewModelProvider(this, viewModelFactory)[PollCreateViewModel::class.java]
viewModel.options.observe(this, optionsObserver)
}
@SuppressLint("InflateParams")
@ -59,21 +61,19 @@ class PollCreateDialogFragment(
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.pollCreateOptionsList.layoutManager = LinearLayoutManager(context)
adapter = PollCreateOptionsAdapter(this)
binding?.pollCreateOptionsList?.adapter = adapter
binding?.pollCreateOptionsList?.layoutManager = LinearLayoutManager(context)
binding.pollCreateOptionsList.adapter = adapter
viewModel.initialize(roomToken)
for (i in 1..3) {
val item = PollCreateOptionItem("a")
adapter?.list?.add(item)
}
binding.pollAddOptionsItem.setOnClickListener {
viewModel.addOption()
// viewModel.options?.value?.let { it1 -> adapter?.notifyItemInserted(it1.size) }
binding.pollAddOption.setOnClickListener {
val item = PollCreateOptionItem("a")
adapter?.list?.add(item)
adapter?.notifyDataSetChanged()
// viewModel.options?.value?.let { it1 -> adapter?.notifyItemChanged(it1.size) }
// viewModel.options?.value?.let { it1 -> adapter?.notifyItemRangeInserted(it1.size, 1) }
}
binding.pollDismiss.setOnClickListener {
@ -93,25 +93,16 @@ class PollCreateDialogFragment(
}
override fun onTextChanged(question: CharSequence, start: Int, before: Int, count: Int) {
// TODO make question a livedata
// if(question != viewmodel.question.value) viewModel.setQuestion(question)
viewModel.question = question.toString()
}
})
// binding.option1.addTextChangedListener(object : TextWatcher {
// override fun afterTextChanged(s: Editable) {
// // unused atm
// }
//
// override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
// // unused atm
// }
//
// override fun onTextChanged(option: CharSequence, start: Int, before: Int, count: Int) {
// viewModel.options = listOf(option.toString())
// }
// })
// viewModel.question.observe { it -> binding.pollCreateQuestion.text = it }
binding.pollPrivatePollCheckbox.setOnClickListener {
// FIXME
viewModel.multipleAnswer = binding.pollMultipleAnswersCheckbox.isChecked
}
@ -132,13 +123,21 @@ class PollCreateDialogFragment(
}
}
}
viewModel.initialize(roomToken)
}
override fun onDeleteClick(pollCreateOptionItem: PollCreateOptionItem, position: Int) {
adapter?.list?.remove(pollCreateOptionItem)
adapter?.notifyItemRemoved(position)
override fun onRemoveOptionsItemClick(pollCreateOptionItem: PollCreateOptionItem, position: Int) {
viewModel.removeOption(pollCreateOptionItem)
// adapter?.notifyItemRemoved(position)
// adapter?.notifyItemChanged(position)
// adapter?.notifyItemRangeRemoved(position, 1)
}
var optionsObserver: Observer<ArrayList<PollCreateOptionItem>> =
object : Observer<ArrayList<PollCreateOptionItem>> {
override fun onChanged(options: ArrayList<PollCreateOptionItem>) {
adapter?.updateOptionsList(options)
}
}
/**

View File

@ -3,6 +3,7 @@ package com.nextcloud.talk.polls.viewmodels
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.nextcloud.talk.polls.adapters.PollCreateOptionItem
import com.nextcloud.talk.polls.model.Poll
import com.nextcloud.talk.polls.repositories.PollRepository
import io.reactivex.Observer
@ -15,8 +16,22 @@ class PollCreateViewModel @Inject constructor(private val repository: PollReposi
private lateinit var roomToken: String
lateinit var question: String
lateinit var options: List<String>
// TODO remove testing items
var defaultOptions: MutableList<PollCreateOptionItem> = mutableListOf<PollCreateOptionItem>().apply {
for (i in 1..3) {
val item = PollCreateOptionItem("item " + i)
this.add(item)
}
}
private var _options: MutableLiveData<ArrayList<PollCreateOptionItem>> =
MutableLiveData<ArrayList<PollCreateOptionItem>>(defaultOptions)
val options: LiveData<ArrayList<PollCreateOptionItem>>
get() = _options
var question: String = ""
// lateinit var options: List<String>
var privatePoll: Boolean = false
var multipleAnswer: Boolean = false
@ -41,6 +56,23 @@ class PollCreateViewModel @Inject constructor(private val repository: PollReposi
disposable?.dispose()
}
private fun <T> MutableLiveData<T>.notifyObserver() {
this.value = this.value
}
fun addOption() {
val item = PollCreateOptionItem("")
val currentOptions: ArrayList<PollCreateOptionItem> = _options.value ?: ArrayList()
currentOptions.add(item)
_options.value = currentOptions
}
fun removeOption(item: PollCreateOptionItem) {
val currentOptions: ArrayList<PollCreateOptionItem> = _options.value ?: ArrayList()
currentOptions.remove(item)
_options.value = currentOptions
}
fun createPoll() {
var maxVotes = 1
if (multipleAnswer) {
@ -52,13 +84,26 @@ class PollCreateViewModel @Inject constructor(private val repository: PollReposi
resultMode = 1
}
repository.createPoll(roomToken, question, options, resultMode, maxVotes)
val items = _options.value!!
repository.createPoll(roomToken, question, items.map { it.pollOption }, resultMode, maxVotes)
?.doOnSubscribe { disposable = it }
?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(PollObserver())
}
private fun map(
list: ArrayList<PollCreateOptionItem>,
): List<String> {
val resultList: ArrayList<String>? = ArrayList()
list.forEach {
resultList?.add(it.pollOption)
}
return resultList?.toList()!!
}
inner class PollObserver : Observer<Poll> {
lateinit var poll: Poll

View File

@ -68,7 +68,7 @@
</LinearLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/poll_add_option"
android:id="@+id/poll_add_options_item"
style="@style/OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

View File

@ -6,7 +6,7 @@
android:orientation="horizontal"
tools:background="@color/white">
<com.nextcloud.talk.utils.EmojiTextInputEditText
<EditText
android:id="@+id/poll_option_text"
android:layout_width="0dp"
android:layout_height="wrap_content"