Implemented chips for selecting dietary preferences

This commit is contained in:
denizk0461
2023-04-18 10:25:45 +02:00
parent 56cdc4c5ee
commit c385da4287
11 changed files with 299 additions and 24 deletions
@@ -4,11 +4,14 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.children
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.denizk0461.studip.adapter.CanteenOfferPageAdapter
import com.denizk0461.studip.databinding.FragmentCanteenBinding
import com.denizk0461.studip.model.DietaryPreferences
import com.denizk0461.studip.viewmodel.CanteenViewModel
import com.google.android.material.chip.Chip
import com.google.android.material.tabs.TabLayoutMediator
class CanteenFragment : Fragment() {
@@ -32,6 +35,45 @@ class CanteenFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
/* Since this removes the view before immediately adding it back, this method causes a
* flicker. Note for the future: implement a more efficient / better-looking method.
*
* TODO order the chips alphabetically
*/
for (chip in binding.chipsPreference.children) {
val nChip = chip as Chip
nChip.setOnCheckedChangeListener { buttonView, _ ->
val index = binding.chipsPreference.indexOfChild(buttonView)
binding.chipsPreference.removeView(buttonView)
binding.chipsPreference.addView(buttonView, index)
}
}
val chipMap = mapOf<Chip, DietaryPreferences>(
binding.chipPrefFair to DietaryPreferences.FAIR,
binding.chipPrefFish to DietaryPreferences.FISH,
binding.chipPrefPoultry to DietaryPreferences.POULTRY,
binding.chipPrefLamb to DietaryPreferences.LAMB,
binding.chipPrefVital to DietaryPreferences.VITAL,
binding.chipPrefBeef to DietaryPreferences.BEEF,
binding.chipPrefPork to DietaryPreferences.PORK,
binding.chipPrefVegetarian to DietaryPreferences.VEGETARIAN,
binding.chipPrefVegan to DietaryPreferences.VEGAN,
binding.chipPrefGame to DietaryPreferences.GAME,
)
chipMap.forEach { chip, pref ->
chip.isChecked = getPreference(pref)
chip.setOnCheckedChangeListener { _, newValue ->
setPreference(pref, newValue)
}
}
binding.chipPrefFair
binding.chipPrefFair.setOnCheckedChangeListener { _, newValue ->
setPreference(DietaryPreferences.FAIR, newValue)
}
viewPagerAdapter = CanteenOfferPageAdapter(listOf(), 0)
binding.viewPager.adapter = viewPagerAdapter
@@ -49,9 +91,11 @@ class CanteenFragment : Fragment() {
}
binding.swipeRefreshLayout.setOnRefreshListener {
viewModel.fetchOffers {
viewModel.fetchOffers(onRefreshUpdate = { status ->
}, onFinish = {
// binding.swipeRefreshLayout.isRefreshing = false
}
})
}
}
@@ -67,4 +111,12 @@ class CanteenFragment : Fragment() {
super.onDestroyView()
_binding = null
}
private fun setPreference(pref: DietaryPreferences, newValue: Boolean) {
viewModel.setPreference(pref, newValue)
// notifyDataSetChanged?
}
private fun getPreference(pref: DietaryPreferences): Boolean =
viewModel.getPreference(pref)
}