mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-04-14 19:56:32 +00:00
Fixes #1826
This commit is contained in:
parent
d4f466c9ab
commit
149e5b01a7
6 changed files with 44 additions and 70 deletions
|
|
@ -10,8 +10,8 @@ import com.habitrpg.android.habitica.models.social.Challenge
|
|||
import com.habitrpg.android.habitica.models.social.ChallengeMembership
|
||||
import com.habitrpg.android.habitica.ui.adapter.BaseRecyclerViewAdapter
|
||||
import com.habitrpg.android.habitica.ui.fragments.social.challenges.ChallengeFilterOptions
|
||||
import com.habitrpg.common.habitica.helpers.EmojiParser
|
||||
import com.habitrpg.android.habitica.ui.views.HabiticaIconsHelper
|
||||
import com.habitrpg.common.habitica.helpers.EmojiParser
|
||||
import io.reactivex.rxjava3.core.BackpressureStrategy
|
||||
import io.reactivex.rxjava3.core.Flowable
|
||||
import io.reactivex.rxjava3.subjects.PublishSubject
|
||||
|
|
@ -53,7 +53,7 @@ class ChallengesListViewAdapter(
|
|||
|
||||
var query = unfilteredData.where()
|
||||
|
||||
if (filterOptions.showByGroups != null && filterOptions.showByGroups.size > 0) {
|
||||
if (filterOptions.showByGroups.isNotEmpty()) {
|
||||
val groupIds = arrayOfNulls<String>(filterOptions.showByGroups.size)
|
||||
var index = 0
|
||||
for (group in filterOptions.showByGroups) {
|
||||
|
|
|
|||
|
|
@ -3,79 +3,47 @@ package com.habitrpg.android.habitica.ui.adapter.social.challenges
|
|||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.CheckBox
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.databinding.DialogChallengeFilterGroupItemBinding
|
||||
import com.habitrpg.android.habitica.models.social.Group
|
||||
|
||||
class ChallengesFilterRecyclerViewAdapter(entries: List<Group>) : RecyclerView.Adapter<ChallengesFilterRecyclerViewAdapter.ChallengeViewHolder>() {
|
||||
|
||||
private val entries: List<Group>
|
||||
private val holderList: MutableList<ChallengeViewHolder>
|
||||
val checkedEntries: List<Group>
|
||||
get() {
|
||||
val result = ArrayList<Group>()
|
||||
|
||||
for (h in holderList) {
|
||||
if (h.checkbox.isChecked) {
|
||||
h.group?.let {
|
||||
result.add(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
val checkedEntries: MutableList<Group> = mutableListOf()
|
||||
|
||||
init {
|
||||
this.entries = ArrayList(entries)
|
||||
this.holderList = ArrayList()
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChallengeViewHolder {
|
||||
val view = LayoutInflater.from(parent.context)
|
||||
.inflate(R.layout.dialog_challenge_filter_group_item, parent, false)
|
||||
|
||||
val challengeViewHolder = ChallengeViewHolder(view)
|
||||
holderList.add(challengeViewHolder)
|
||||
|
||||
return challengeViewHolder
|
||||
return ChallengeViewHolder(view)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ChallengeViewHolder, position: Int) {
|
||||
holder.bind(entries[position])
|
||||
holder.bind(entries[position], checkedEntries)
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
return entries.size
|
||||
}
|
||||
|
||||
fun deSelectAll() {
|
||||
for (h in holderList) {
|
||||
h.checkbox.isChecked = false
|
||||
}
|
||||
}
|
||||
|
||||
fun selectAll() {
|
||||
for (h in holderList) {
|
||||
h.checkbox.isChecked = true
|
||||
}
|
||||
}
|
||||
|
||||
fun selectAll(groupsToCheck: List<Group>) {
|
||||
for (h in holderList) {
|
||||
h.checkbox.isChecked = groupsToCheck.find { g -> h.group?.id == g.id } != null
|
||||
}
|
||||
}
|
||||
|
||||
class ChallengeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
internal val checkbox: CheckBox = itemView.findViewById(R.id.challenge_filter_group_checkbox)
|
||||
private val binding = DialogChallengeFilterGroupItemBinding.bind(itemView)
|
||||
|
||||
var group: Group? = null
|
||||
|
||||
fun bind(group: Group) {
|
||||
this.group = group
|
||||
|
||||
checkbox.text = group.name
|
||||
fun bind(group: Group, checkedEntries: MutableList<Group>) {
|
||||
binding.root.text = group.name
|
||||
binding.root.isChecked = checkedEntries.contains(group)
|
||||
binding.root.setOnCheckedChangeListener { _, isChecked ->
|
||||
if (isChecked && !checkedEntries.contains(group)) {
|
||||
checkedEntries.add(group)
|
||||
} else if (!isChecked && checkedEntries.contains(group)) {
|
||||
checkedEntries.remove(group)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ internal class ChallengeFilterDialogHolder private constructor(
|
|||
) {
|
||||
private val binding = DialogChallengeFilterBinding.bind(view)
|
||||
|
||||
private var filterGroups: List<Group>? = null
|
||||
private var filterGroups: List<Group> = emptyList()
|
||||
private var currentFilter: ChallengeFilterOptions? = null
|
||||
private var selectedGroupsCallback: ((ChallengeFilterOptions) -> Unit)? = null
|
||||
private var adapter: ChallengesFilterRecyclerViewAdapter? = null
|
||||
|
|
@ -23,6 +23,12 @@ internal class ChallengeFilterDialogHolder private constructor(
|
|||
init {
|
||||
binding.challengeFilterButtonAll.setOnClickListener { allClicked() }
|
||||
binding.challengeFilterButtonNone.setOnClickListener { noneClicked() }
|
||||
binding.challengeFilterOwned.setOnCheckedChangeListener { _, isChecked ->
|
||||
currentFilter?.showOwned = isChecked
|
||||
}
|
||||
binding.challengeFilterNotOwned.setOnCheckedChangeListener { _, isChecked ->
|
||||
currentFilter?.notOwned = isChecked
|
||||
}
|
||||
}
|
||||
|
||||
fun bind(
|
||||
|
|
@ -43,38 +49,41 @@ internal class ChallengeFilterDialogHolder private constructor(
|
|||
|
||||
private fun fillChallengeGroups() {
|
||||
binding.challengeFilterRecyclerView.layoutManager = LinearLayoutManager(context)
|
||||
adapter = filterGroups?.let { ChallengesFilterRecyclerViewAdapter(it) }
|
||||
adapter = ChallengesFilterRecyclerViewAdapter(filterGroups)
|
||||
if (currentFilter != null && currentFilter?.showByGroups != null) {
|
||||
adapter?.selectAll(currentFilter?.showByGroups ?: emptyList())
|
||||
adapter?.checkedEntries?.addAll(currentFilter?.showByGroups ?: emptyList())
|
||||
}
|
||||
|
||||
binding.challengeFilterRecyclerView.adapter = adapter
|
||||
}
|
||||
|
||||
private fun allClicked() {
|
||||
this.adapter?.selectAll()
|
||||
this.adapter?.checkedEntries?.clear()
|
||||
adapter?.checkedEntries?.addAll(filterGroups)
|
||||
}
|
||||
|
||||
private fun noneClicked() {
|
||||
this.adapter?.deSelectAll()
|
||||
this.adapter?.checkedEntries?.clear()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun showDialog(
|
||||
activity: Activity,
|
||||
filterGroups: List<Group>,
|
||||
currentFilter: ChallengeFilterOptions?,
|
||||
selectedGroupsCallback: ((ChallengeFilterOptions) -> Unit)?
|
||||
selectedGroupsCallback: ((ChallengeFilterOptions) -> Unit)
|
||||
) {
|
||||
val dialogLayout = activity.layoutInflater.inflate(R.layout.dialog_challenge_filter, null)
|
||||
|
||||
val challengeFilterDialogHolder = ChallengeFilterDialogHolder(dialogLayout, activity)
|
||||
val holder = ChallengeFilterDialogHolder(dialogLayout, activity)
|
||||
|
||||
val sheet = HabiticaBottomSheetDialog(activity)
|
||||
sheet.setContentView(dialogLayout)
|
||||
sheet.setOnDismissListener {
|
||||
selectedGroupsCallback(ChallengeFilterOptions(holder.adapter?.checkedEntries ?: emptyList(), holder.binding.challengeFilterOwned.isChecked, holder.binding.challengeFilterNotOwned.isChecked))
|
||||
}
|
||||
|
||||
challengeFilterDialogHolder.bind(filterGroups, currentFilter, selectedGroupsCallback)
|
||||
holder.bind(filterGroups, currentFilter, selectedGroupsCallback)
|
||||
sheet.show()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
package com.habitrpg.android.habitica.ui.fragments.social.challenges;
|
||||
|
||||
import com.habitrpg.android.habitica.models.social.Group;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ChallengeFilterOptions {
|
||||
public List<Group> showByGroups;
|
||||
public boolean showOwned;
|
||||
public boolean notOwned;
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package com.habitrpg.android.habitica.ui.fragments.social.challenges
|
||||
|
||||
import com.habitrpg.android.habitica.models.social.Group
|
||||
|
||||
data class ChallengeFilterOptions(var showByGroups: List<Group>,
|
||||
var showOwned: Boolean = false,
|
||||
var notOwned: Boolean = false
|
||||
)
|
||||
|
|
@ -1,2 +1,2 @@
|
|||
NAME=4.0.1
|
||||
CODE=4330
|
||||
CODE=4360
|
||||
Loading…
Reference in a new issue