mirror of
https://github.com/nextcloud/talk-android
synced 2025-07-14 08:15:04 +01:00
wip: create poll using livedata
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
parent
af427f8300
commit
b140cc8751
@ -1,53 +1,52 @@
|
|||||||
package com.nextcloud.talk.polls.adapters
|
package com.nextcloud.talk.polls.adapters
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
|
import android.text.Editable
|
||||||
|
import android.text.TextWatcher
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.nextcloud.talk.databinding.PollCreateOptionsItemBinding
|
import com.nextcloud.talk.databinding.PollCreateOptionsItemBinding
|
||||||
|
import com.nextcloud.talk.utils.EmojiTextInputEditText
|
||||||
|
|
||||||
class PollCreateOptionViewHolder(
|
class PollCreateOptionViewHolder(
|
||||||
private val binding: PollCreateOptionsItemBinding
|
private val binding: PollCreateOptionsItemBinding
|
||||||
) : RecyclerView.ViewHolder(binding.root) {
|
) : RecyclerView.ViewHolder(binding.root) {
|
||||||
|
|
||||||
|
lateinit var optionText: EmojiTextInputEditText
|
||||||
|
var textListener: TextWatcher? = null
|
||||||
|
|
||||||
@SuppressLint("SetTextI18n")
|
@SuppressLint("SetTextI18n")
|
||||||
fun bind(
|
fun bind(
|
||||||
pollCreateOptionItem: PollCreateOptionItem,
|
pollCreateOptionItem: PollCreateOptionItem,
|
||||||
clickListener: PollCreateOptionsItemClickListener,
|
clickListener: PollCreateOptionsItemClickListener,
|
||||||
position: Int
|
position: Int
|
||||||
) {
|
) {
|
||||||
// binding.root.setOnClickListener { }
|
|
||||||
|
textListener?.let {
|
||||||
|
binding.pollOptionText.removeTextChangedListener(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
binding.pollOptionText.setText(pollCreateOptionItem.pollOption)
|
||||||
|
|
||||||
binding.pollOptionDelete.setOnClickListener {
|
binding.pollOptionDelete.setOnClickListener {
|
||||||
clickListener.onDeleteClick(pollCreateOptionItem, position)
|
clickListener.onRemoveOptionsItemClick(pollCreateOptionItem, position)
|
||||||
}
|
}
|
||||||
|
|
||||||
// binding.pollOptionText.addTextChangedListener(object : TextWatcher {
|
textListener = getTextWatcher(pollCreateOptionItem)
|
||||||
// override fun afterTextChanged(s: Editable) {
|
binding.pollOptionText.addTextChangedListener(textListener)
|
||||||
// // 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()
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fun onBind(item: SharedItem) {
|
private fun getTextWatcher(pollCreateOptionItem: PollCreateOptionItem) =
|
||||||
// Log.d("","bbbb")
|
object : TextWatcher {
|
||||||
// }
|
override fun afterTextChanged(s: Editable) {
|
||||||
//
|
// unused atm
|
||||||
// fun onLongClick(view: View?): Boolean {
|
}
|
||||||
// // moviesList.remove(getAdapterPosition())
|
|
||||||
// // notifyItemRemoved(getAdapterPosition())
|
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
|
||||||
//
|
// unused atm
|
||||||
// Log.d("", "dfdrg")
|
}
|
||||||
// return true
|
|
||||||
// }
|
override fun onTextChanged(option: CharSequence, start: Int, before: Int, count: Int) {
|
||||||
//
|
pollCreateOptionItem.pollOption = option.toString()
|
||||||
// override fun onClick(v: View?) {
|
}
|
||||||
// Log.d("", "dfdrg")
|
}
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ class PollCreateOptionsAdapter(
|
|||||||
private val clickListener: PollCreateOptionsItemClickListener
|
private val clickListener: PollCreateOptionsItemClickListener
|
||||||
) : RecyclerView.Adapter<PollCreateOptionViewHolder>() {
|
) : 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 {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PollCreateOptionViewHolder {
|
||||||
val itemBinding = PollCreateOptionsItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
val itemBinding = PollCreateOptionsItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
||||||
@ -18,10 +18,16 @@ class PollCreateOptionsAdapter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: PollCreateOptionViewHolder, position: Int) {
|
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 {
|
override fun getItemCount(): Int {
|
||||||
return list.size
|
return list.size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun updateOptionsList(optionsList: ArrayList<PollCreateOptionItem>) {
|
||||||
|
list = optionsList
|
||||||
|
notifyDataSetChanged()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
package com.nextcloud.talk.polls.adapters
|
package com.nextcloud.talk.polls.adapters
|
||||||
|
|
||||||
interface PollCreateOptionsItemClickListener {
|
interface PollCreateOptionsItemClickListener {
|
||||||
fun onDeleteClick(pollCreateOptionItem: PollCreateOptionItem, position: Int)
|
fun onRemoveOptionsItemClick(pollCreateOptionItem: PollCreateOptionItem, position: Int)
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import android.view.View
|
|||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
|
import androidx.lifecycle.Observer
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import autodagger.AutoInjector
|
import autodagger.AutoInjector
|
||||||
@ -39,6 +40,7 @@ class PollCreateDialogFragment(
|
|||||||
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
|
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
|
||||||
|
|
||||||
viewModel = ViewModelProvider(this, viewModelFactory)[PollCreateViewModel::class.java]
|
viewModel = ViewModelProvider(this, viewModelFactory)[PollCreateViewModel::class.java]
|
||||||
|
viewModel.options.observe(this, optionsObserver)
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("InflateParams")
|
@SuppressLint("InflateParams")
|
||||||
@ -59,21 +61,19 @@ class PollCreateDialogFragment(
|
|||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
|
||||||
|
binding.pollCreateOptionsList.layoutManager = LinearLayoutManager(context)
|
||||||
|
|
||||||
adapter = PollCreateOptionsAdapter(this)
|
adapter = PollCreateOptionsAdapter(this)
|
||||||
binding?.pollCreateOptionsList?.adapter = adapter
|
binding.pollCreateOptionsList.adapter = adapter
|
||||||
binding?.pollCreateOptionsList?.layoutManager = LinearLayoutManager(context)
|
|
||||||
|
|
||||||
viewModel.initialize(roomToken)
|
viewModel.initialize(roomToken)
|
||||||
|
|
||||||
for (i in 1..3) {
|
binding.pollAddOptionsItem.setOnClickListener {
|
||||||
val item = PollCreateOptionItem("a")
|
viewModel.addOption()
|
||||||
adapter?.list?.add(item)
|
// viewModel.options?.value?.let { it1 -> adapter?.notifyItemInserted(it1.size) }
|
||||||
}
|
|
||||||
|
|
||||||
binding.pollAddOption.setOnClickListener {
|
// viewModel.options?.value?.let { it1 -> adapter?.notifyItemChanged(it1.size) }
|
||||||
val item = PollCreateOptionItem("a")
|
// viewModel.options?.value?.let { it1 -> adapter?.notifyItemRangeInserted(it1.size, 1) }
|
||||||
adapter?.list?.add(item)
|
|
||||||
adapter?.notifyDataSetChanged()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.pollDismiss.setOnClickListener {
|
binding.pollDismiss.setOnClickListener {
|
||||||
@ -93,25 +93,16 @@ class PollCreateDialogFragment(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onTextChanged(question: CharSequence, start: Int, before: Int, count: Int) {
|
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()
|
viewModel.question = question.toString()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// binding.option1.addTextChangedListener(object : TextWatcher {
|
// viewModel.question.observe { it -> binding.pollCreateQuestion.text = it }
|
||||||
// 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())
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
|
|
||||||
binding.pollPrivatePollCheckbox.setOnClickListener {
|
binding.pollPrivatePollCheckbox.setOnClickListener {
|
||||||
|
// FIXME
|
||||||
viewModel.multipleAnswer = binding.pollMultipleAnswersCheckbox.isChecked
|
viewModel.multipleAnswer = binding.pollMultipleAnswersCheckbox.isChecked
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,13 +123,21 @@ class PollCreateDialogFragment(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
viewModel.initialize(roomToken)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDeleteClick(pollCreateOptionItem: PollCreateOptionItem, position: Int) {
|
override fun onRemoveOptionsItemClick(pollCreateOptionItem: PollCreateOptionItem, position: Int) {
|
||||||
adapter?.list?.remove(pollCreateOptionItem)
|
viewModel.removeOption(pollCreateOptionItem)
|
||||||
adapter?.notifyItemRemoved(position)
|
// 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,6 +3,7 @@ package com.nextcloud.talk.polls.viewmodels
|
|||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
|
import com.nextcloud.talk.polls.adapters.PollCreateOptionItem
|
||||||
import com.nextcloud.talk.polls.model.Poll
|
import com.nextcloud.talk.polls.model.Poll
|
||||||
import com.nextcloud.talk.polls.repositories.PollRepository
|
import com.nextcloud.talk.polls.repositories.PollRepository
|
||||||
import io.reactivex.Observer
|
import io.reactivex.Observer
|
||||||
@ -15,8 +16,22 @@ class PollCreateViewModel @Inject constructor(private val repository: PollReposi
|
|||||||
|
|
||||||
private lateinit var roomToken: String
|
private lateinit var roomToken: String
|
||||||
|
|
||||||
lateinit var question: String
|
// TODO remove testing items
|
||||||
lateinit var options: List<String>
|
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 privatePoll: Boolean = false
|
||||||
var multipleAnswer: Boolean = false
|
var multipleAnswer: Boolean = false
|
||||||
|
|
||||||
@ -41,6 +56,23 @@ class PollCreateViewModel @Inject constructor(private val repository: PollReposi
|
|||||||
disposable?.dispose()
|
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() {
|
fun createPoll() {
|
||||||
var maxVotes = 1
|
var maxVotes = 1
|
||||||
if (multipleAnswer) {
|
if (multipleAnswer) {
|
||||||
@ -52,13 +84,26 @@ class PollCreateViewModel @Inject constructor(private val repository: PollReposi
|
|||||||
resultMode = 1
|
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 }
|
?.doOnSubscribe { disposable = it }
|
||||||
?.subscribeOn(Schedulers.io())
|
?.subscribeOn(Schedulers.io())
|
||||||
?.observeOn(AndroidSchedulers.mainThread())
|
?.observeOn(AndroidSchedulers.mainThread())
|
||||||
?.subscribe(PollObserver())
|
?.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> {
|
inner class PollObserver : Observer<Poll> {
|
||||||
|
|
||||||
lateinit var poll: Poll
|
lateinit var poll: Poll
|
||||||
|
@ -68,7 +68,7 @@
|
|||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<com.google.android.material.button.MaterialButton
|
<com.google.android.material.button.MaterialButton
|
||||||
android:id="@+id/poll_add_option"
|
android:id="@+id/poll_add_options_item"
|
||||||
style="@style/OutlinedButton"
|
style="@style/OutlinedButton"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
android:orientation="horizontal"
|
android:orientation="horizontal"
|
||||||
tools:background="@color/white">
|
tools:background="@color/white">
|
||||||
|
|
||||||
<com.nextcloud.talk.utils.EmojiTextInputEditText
|
<EditText
|
||||||
android:id="@+id/poll_option_text"
|
android:id="@+id/poll_option_text"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
Loading…
Reference in New Issue
Block a user