2023-04-16 18:19:06 +02:00
|
|
|
package com.denizk0461.studip.fragment
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
2023-04-18 15:39:49 +02:00
|
|
|
import android.util.Log
|
2023-04-16 18:19:06 +02:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.view.ViewGroup
|
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
|
import androidx.fragment.app.viewModels
|
2023-04-18 15:39:49 +02:00
|
|
|
import androidx.lifecycle.LiveData
|
2023-04-16 21:41:46 +02:00
|
|
|
import com.denizk0461.studip.adapter.CanteenOfferPageAdapter
|
2023-04-16 18:19:06 +02:00
|
|
|
import com.denizk0461.studip.databinding.FragmentCanteenBinding
|
2023-04-18 15:39:49 +02:00
|
|
|
import com.denizk0461.studip.model.CanteenOffer
|
2023-04-20 08:17:23 +02:00
|
|
|
import com.denizk0461.studip.model.CanteenOfferGroup
|
|
|
|
|
import com.denizk0461.studip.model.CanteenOfferGroupElement
|
2023-04-18 10:25:45 +02:00
|
|
|
import com.denizk0461.studip.model.DietaryPreferences
|
2023-04-16 18:19:06 +02:00
|
|
|
import com.denizk0461.studip.viewmodel.CanteenViewModel
|
2023-04-16 21:41:46 +02:00
|
|
|
import com.google.android.material.tabs.TabLayoutMediator
|
2023-04-16 18:19:06 +02:00
|
|
|
|
|
|
|
|
class CanteenFragment : Fragment() {
|
|
|
|
|
|
|
|
|
|
private var _binding: FragmentCanteenBinding? = null
|
|
|
|
|
|
|
|
|
|
// This property is only valid between onCreateView and
|
|
|
|
|
// onDestroyView.
|
|
|
|
|
private val binding get() = _binding!!
|
|
|
|
|
|
2023-04-16 21:41:46 +02:00
|
|
|
private var dates = listOf<String>()
|
|
|
|
|
|
2023-04-18 15:39:49 +02:00
|
|
|
private lateinit var liveData: LiveData<List<CanteenOffer>>
|
|
|
|
|
|
2023-04-16 21:41:46 +02:00
|
|
|
private lateinit var viewPagerAdapter: CanteenOfferPageAdapter
|
2023-04-16 18:19:06 +02:00
|
|
|
private val viewModel: CanteenViewModel by viewModels()
|
|
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
|
|
|
|
_binding = FragmentCanteenBinding.inflate(inflater, container, false)
|
|
|
|
|
return binding.root
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
2023-04-18 15:39:49 +02:00
|
|
|
val chipMap = mapOf(
|
2023-04-18 10:25:45 +02:00
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
|
2023-04-18 15:39:49 +02:00
|
|
|
chipMap.forEach { (chip, pref) ->
|
2023-04-18 10:25:45 +02:00
|
|
|
chip.isChecked = getPreference(pref)
|
2023-04-18 15:39:49 +02:00
|
|
|
chip.setOnCheckedChangeListener { buttonView, newValue ->
|
2023-04-18 10:25:45 +02:00
|
|
|
setPreference(pref, newValue)
|
2023-04-18 15:39:49 +02:00
|
|
|
Log.d("eek!4", "object as : ${viewModel.getDietaryPrefs().deconstruct()}")
|
|
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
*/
|
|
|
|
|
val index = binding.chipsPreference.indexOfChild(buttonView)
|
|
|
|
|
binding.chipsPreference.removeView(buttonView)
|
|
|
|
|
binding.chipsPreference.addView(buttonView, index)
|
2023-04-18 10:25:45 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 16:41:01 +02:00
|
|
|
viewPagerAdapter = CanteenOfferPageAdapter(listOf(), 0, getPrefRegex())
|
2023-04-16 21:41:46 +02:00
|
|
|
binding.viewPager.adapter = viewPagerAdapter
|
|
|
|
|
|
|
|
|
|
createTabLayoutMediator()
|
|
|
|
|
|
2023-04-18 16:41:01 +02:00
|
|
|
liveData = viewModel.allOffers
|
2023-04-18 15:39:49 +02:00
|
|
|
liveData.observe(viewLifecycleOwner) { offers ->
|
2023-04-16 21:41:46 +02:00
|
|
|
|
2023-04-20 08:17:23 +02:00
|
|
|
val groupedElements = offers.groupElements().distinct()
|
|
|
|
|
|
|
|
|
|
dates = groupedElements.map { it.date }.distinct()
|
2023-04-16 21:41:46 +02:00
|
|
|
createTabLayoutMediator()
|
|
|
|
|
|
2023-04-20 08:17:23 +02:00
|
|
|
viewPagerAdapter.setNewItems(groupedElements, dates.size)
|
2023-04-16 21:41:46 +02:00
|
|
|
|
2023-04-16 18:19:06 +02:00
|
|
|
// TODO this must be changed. if an exception is raised, then this will not fire
|
|
|
|
|
binding.swipeRefreshLayout.isRefreshing = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
binding.swipeRefreshLayout.setOnRefreshListener {
|
2023-04-18 10:25:45 +02:00
|
|
|
viewModel.fetchOffers(onRefreshUpdate = { status ->
|
2023-04-18 15:39:49 +02:00
|
|
|
// TODO refresh updates
|
2023-04-18 10:25:45 +02:00
|
|
|
}, onFinish = {
|
2023-04-16 18:19:06 +02:00
|
|
|
// binding.swipeRefreshLayout.isRefreshing = false
|
2023-04-18 10:25:45 +02:00
|
|
|
})
|
2023-04-16 18:19:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 21:41:46 +02:00
|
|
|
private fun createTabLayoutMediator() {
|
|
|
|
|
if (dates.isNotEmpty()) {
|
|
|
|
|
TabLayoutMediator(binding.dayTabLayout, binding.viewPager) { tab, position ->
|
|
|
|
|
tab.text = dates[position]
|
|
|
|
|
}.attach()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 18:19:06 +02:00
|
|
|
override fun onDestroyView() {
|
|
|
|
|
super.onDestroyView()
|
|
|
|
|
_binding = null
|
|
|
|
|
}
|
2023-04-18 10:25:45 +02:00
|
|
|
|
|
|
|
|
private fun setPreference(pref: DietaryPreferences, newValue: Boolean) {
|
|
|
|
|
viewModel.setPreference(pref, newValue)
|
2023-04-18 16:41:01 +02:00
|
|
|
viewPagerAdapter.refreshView(getPrefRegex())
|
2023-04-18 10:25:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getPreference(pref: DietaryPreferences): Boolean =
|
|
|
|
|
viewModel.getPreference(pref)
|
2023-04-18 15:39:49 +02:00
|
|
|
|
2023-04-18 16:41:01 +02:00
|
|
|
private fun getPrefRegex(): Regex =
|
2023-04-18 21:33:24 +02:00
|
|
|
// Regex(viewModel.getDietaryPrefs().deconstruct().replace('t', '.'))
|
|
|
|
|
Regex(viewModel.getDietaryPrefs().deconstruct().replace('f', '.'))
|
2023-04-20 08:17:23 +02:00
|
|
|
|
|
|
|
|
private fun List<CanteenOffer>.groupElements(): List<CanteenOfferGroup> {
|
|
|
|
|
|
|
|
|
|
val prefsRegex = getPrefRegex()
|
|
|
|
|
|
|
|
|
|
val filteredForPreferences = if (prefsRegex.toString() == "ffffffffff") {
|
|
|
|
|
// show all elements and skip filtering
|
|
|
|
|
this
|
|
|
|
|
} else {
|
|
|
|
|
this.filter {
|
|
|
|
|
// Log.d("eek!5", "${it.title}: - ${prefsRegex} vs ${it.dietaryPreferences}")
|
|
|
|
|
prefsRegex.matches(it.dietaryPreferences)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 10:05:24 +02:00
|
|
|
// val a = filteredForPreferences.map {
|
|
|
|
|
// CanteenOfferGroupElement(
|
|
|
|
|
// it.title,
|
|
|
|
|
// it.price,
|
|
|
|
|
// it.dietaryPreferences,
|
|
|
|
|
// )
|
|
|
|
|
// }
|
2023-04-20 08:17:23 +02:00
|
|
|
val b = filteredForPreferences.map { offer ->
|
|
|
|
|
CanteenOfferGroup(
|
|
|
|
|
offer.date,
|
|
|
|
|
offer.dateId,
|
|
|
|
|
offer.category,
|
|
|
|
|
offer.canteen,
|
|
|
|
|
filteredForPreferences.filter {
|
|
|
|
|
it.category == offer.category && it.date == offer.date && it.canteen == offer.canteen
|
|
|
|
|
}.map {
|
|
|
|
|
CanteenOfferGroupElement(
|
|
|
|
|
it.title,
|
|
|
|
|
it.price,
|
|
|
|
|
it.dietaryPreferences,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return b
|
|
|
|
|
}
|
2023-04-16 18:19:06 +02:00
|
|
|
}
|