Archived
Made some improvements to the dietary preference handling, but CanteenOfferPageAdapter.kt still needs some work for the filtering to work properly; it should only filter out elements if they meet NO preferences!
This commit is contained in:
@@ -6,8 +6,9 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
|||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.denizk0461.studip.databinding.ItemScrollablePageBinding
|
import com.denizk0461.studip.databinding.ItemScrollablePageBinding
|
||||||
import com.denizk0461.studip.model.CanteenOffer
|
import com.denizk0461.studip.model.CanteenOffer
|
||||||
|
import com.denizk0461.studip.model.DietaryPrefObject
|
||||||
|
|
||||||
class CanteenOfferPageAdapter(private var offers: List<CanteenOffer>, private var daysCovered: Int) : RecyclerView.Adapter<CanteenOfferPageAdapter.CanteenOfferPageViewHolder>() {
|
class CanteenOfferPageAdapter(private var offers: List<CanteenOffer>, private var daysCovered: Int, private var prefs: DietaryPrefObject) : RecyclerView.Adapter<CanteenOfferPageAdapter.CanteenOfferPageViewHolder>() {
|
||||||
|
|
||||||
class CanteenOfferPageViewHolder(val binding: ItemScrollablePageBinding) : RecyclerView.ViewHolder(binding.root)
|
class CanteenOfferPageViewHolder(val binding: ItemScrollablePageBinding) : RecyclerView.ViewHolder(binding.root)
|
||||||
|
|
||||||
@@ -28,7 +29,8 @@ class CanteenOfferPageAdapter(private var offers: List<CanteenOffer>, private va
|
|||||||
|
|
||||||
layoutManager = LinearLayoutManager(holder.binding.root.context, LinearLayoutManager.VERTICAL, false)
|
layoutManager = LinearLayoutManager(holder.binding.root.context, LinearLayoutManager.VERTICAL, false)
|
||||||
|
|
||||||
val filtered = offers.filter { it.dateId == position }
|
// TODO second filter needs to be worked on; it shouldn't filter out something if two options are selected and only one is met
|
||||||
|
val filtered = offers.filter { it.dateId == position }.filter { it.dietaryPreferences == prefs.deconstruct() }
|
||||||
adapter = CanteenOfferItemAdapter(filtered) // TODO check if empty
|
adapter = CanteenOfferItemAdapter(filtered) // TODO check if empty
|
||||||
scheduleLayoutAnimation()
|
scheduleLayoutAnimation()
|
||||||
}
|
}
|
||||||
@@ -39,4 +41,9 @@ class CanteenOfferPageAdapter(private var offers: List<CanteenOffer>, private va
|
|||||||
offers = items
|
offers = items
|
||||||
notifyDataSetChanged()
|
notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun refreshView(prefs: DietaryPrefObject) {
|
||||||
|
this.prefs = prefs
|
||||||
|
notifyDataSetChanged()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -76,22 +76,28 @@ class StwParser {
|
|||||||
val prefs =
|
val prefs =
|
||||||
element.getElementsByClass("field field-name-field-food-types")[0]
|
element.getElementsByClass("field field-name-field-food-types")[0]
|
||||||
|
|
||||||
|
val prefString = DietaryPrefObject(
|
||||||
|
isFair = prefs.isDietaryPreferenceMet(PREFERENCE_FAIR),
|
||||||
|
isFish = prefs.isDietaryPreferenceMet(PREFERENCE_FISH),
|
||||||
|
isPoultry = prefs.isDietaryPreferenceMet(PREFERENCE_POULTRY),
|
||||||
|
isLamb = prefs.isDietaryPreferenceMet(PREFERENCE_LAMB),
|
||||||
|
isVital = prefs.isDietaryPreferenceMet(PREFERENCE_VITAL),
|
||||||
|
isBeef = prefs.isDietaryPreferenceMet(PREFERENCE_BEEF),
|
||||||
|
isPork = prefs.isDietaryPreferenceMet(PREFERENCE_PORK),
|
||||||
|
isVegan = prefs.isDietaryPreferenceMet(PREFERENCE_VEGAN),
|
||||||
|
isVegetarian = prefs.isDietaryPreferenceMet(PREFERENCE_VEGETARIAN),
|
||||||
|
isGame = prefs.isDietaryPreferenceMet(PREFERENCE_GAME),
|
||||||
|
).deconstruct()
|
||||||
|
|
||||||
|
// Log.d("eek!4", "prefString = $prefString")
|
||||||
|
|
||||||
repo.insert(
|
repo.insert(
|
||||||
OfferItem(
|
OfferItem(
|
||||||
itemId,
|
itemId,
|
||||||
categoryId,
|
categoryId,
|
||||||
title = tableRows[1].getFilteredText(),
|
title = tableRows[1].getFilteredText(),
|
||||||
price = tableRows.getTextOrEmpty(2),
|
price = tableRows.getTextOrEmpty(2),
|
||||||
isFair = prefs.isDietaryPreferenceMet(PREFERENCE_FAIR),
|
prefString,
|
||||||
isFish = prefs.isDietaryPreferenceMet(PREFERENCE_FISH),
|
|
||||||
isPoultry = prefs.isDietaryPreferenceMet(PREFERENCE_POULTRY),
|
|
||||||
isLamb = prefs.isDietaryPreferenceMet(PREFERENCE_LAMB),
|
|
||||||
isVital = prefs.isDietaryPreferenceMet(PREFERENCE_VITAL),
|
|
||||||
isBeef = prefs.isDietaryPreferenceMet(PREFERENCE_BEEF),
|
|
||||||
isPork = prefs.isDietaryPreferenceMet(PREFERENCE_PORK),
|
|
||||||
isVegan = prefs.isDietaryPreferenceMet(PREFERENCE_VEGAN),
|
|
||||||
isVegetarian = prefs.isDietaryPreferenceMet(PREFERENCE_VEGETARIAN),
|
|
||||||
isGame = prefs.isDietaryPreferenceMet(PREFERENCE_GAME),
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
itemId += 1
|
itemId += 1
|
||||||
|
|||||||
@@ -46,14 +46,21 @@ interface EventDAO {
|
|||||||
"JOIN offer_category ON offer_item.categoryId = offer_category.id " +
|
"JOIN offer_category ON offer_item.categoryId = offer_category.id " +
|
||||||
"JOIN offer_canteen ON offer_category.canteenId = offer_canteen.id " +
|
"JOIN offer_canteen ON offer_category.canteenId = offer_canteen.id " +
|
||||||
"JOIN offer_date ON offer_category.dateId = offer_date.id " +
|
"JOIN offer_date ON offer_category.dateId = offer_date.id " +
|
||||||
"WHERE isFair = :isFair AND isFish = :isFish AND isPoultry = :isPoultry " +
|
"WHERE dietary_preferences = :prefs "
|
||||||
"AND isLamb = :isLamb AND isVital = :isVital AND isBeef = :isBeef " +
|
// "WHERE isFair = :isFair " +
|
||||||
"AND isPork = :isPork AND isVegan = :isVegan AND isVegetarian = :isVegetarian " +
|
// "OR isFish = :isFish " +
|
||||||
"AND isGame = :isGame "
|
// "OR isPoultry = :isPoultry " +
|
||||||
|
// "OR isLamb = :isLamb " +
|
||||||
|
// "OR isVital = :isVital " +
|
||||||
|
// "OR isBeef = :isBeef " +
|
||||||
|
// "OR isPork = :isPork " +
|
||||||
|
// "OR isVegan = :isVegan " +
|
||||||
|
// "OR isVegetarian = :isVegetarian " +
|
||||||
|
// "OR isGame = :isGame "
|
||||||
)
|
)
|
||||||
fun getOffersByPreference(
|
fun getOffersByPreference(prefs: String
|
||||||
isFair: Boolean, isFish: Boolean, isPoultry: Boolean, isLamb: Boolean, isVital: Boolean,
|
// isFair: Boolean, isFish: Boolean, isPoultry: Boolean, isLamb: Boolean, isVital: Boolean,
|
||||||
isBeef: Boolean, isPork: Boolean, isVegan: Boolean, isVegetarian: Boolean, isGame: Boolean
|
// isBeef: Boolean, isPork: Boolean, isVegan: Boolean, isVegetarian: Boolean, isGame: Boolean
|
||||||
): LiveData<List<CanteenOffer>>
|
): LiveData<List<CanteenOffer>>
|
||||||
|
|
||||||
@get:Query("SELECT id FROM offer_date")
|
@get:Query("SELECT id FROM offer_date")
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import com.denizk0461.studip.model.*
|
|||||||
OfferCategory::class,
|
OfferCategory::class,
|
||||||
OfferItem::class
|
OfferItem::class
|
||||||
],
|
],
|
||||||
version = 8
|
version = 10,
|
||||||
)
|
)
|
||||||
abstract class EventDatabase : RoomDatabase() {
|
abstract class EventDatabase : RoomDatabase() {
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.denizk0461.studip.db
|
package com.denizk0461.studip.db
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
|
import android.util.Log
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import com.denizk0461.studip.model.*
|
import com.denizk0461.studip.model.*
|
||||||
@@ -12,6 +13,8 @@ class EventRepository(private val app: Application) {
|
|||||||
|
|
||||||
val allEvents: LiveData<List<StudIPEvent>> = dao.allEvents
|
val allEvents: LiveData<List<StudIPEvent>> = dao.allEvents
|
||||||
|
|
||||||
|
private val dietaryPrefString = "prefs_obj"
|
||||||
|
|
||||||
fun insertEvent(event: StudIPEvent) { dao.insertEvent(event) }
|
fun insertEvent(event: StudIPEvent) { dao.insertEvent(event) }
|
||||||
fun insertEvents(events: List<StudIPEvent>) { dao.insertEvents(events) }
|
fun insertEvents(events: List<StudIPEvent>) { dao.insertEvents(events) }
|
||||||
fun nukeEvents() { dao.nukeEvents() }
|
fun nukeEvents() { dao.nukeEvents() }
|
||||||
@@ -19,63 +22,100 @@ class EventRepository(private val app: Application) {
|
|||||||
val allOffers: LiveData<List<CanteenOffer>> = dao.allOffers
|
val allOffers: LiveData<List<CanteenOffer>> = dao.allOffers
|
||||||
|
|
||||||
// is this better than List.filter?
|
// is this better than List.filter?
|
||||||
fun getOffersByPreference(prefs: DietaryPrefObject = getDietaryPrefs()): LiveData<List<CanteenOffer>> {
|
fun getOffersByPreference(prefs: String = getDietaryPrefs()): LiveData<List<CanteenOffer>> {
|
||||||
return if (isPreferenceSet()) {
|
return if (prefs != "ffffffffff") {//isPreferenceSet()) {
|
||||||
dao.getOffersByPreference(
|
dao.getOffersByPreference(
|
||||||
isFair = prefs.isFair,
|
prefs
|
||||||
isFish = prefs.isFish,
|
// isFair = prefs.isFair,
|
||||||
isPoultry = prefs.isPoultry,
|
// isFish = prefs.isFish,
|
||||||
isLamb = prefs.isLamb,
|
// isPoultry = prefs.isPoultry,
|
||||||
isVital = prefs.isVital,
|
// isLamb = prefs.isLamb,
|
||||||
isBeef = prefs.isBeef,
|
// isVital = prefs.isVital,
|
||||||
isPork = prefs.isPork,
|
// isBeef = prefs.isBeef,
|
||||||
isVegan = prefs.isVegan,
|
// isPork = prefs.isPork,
|
||||||
isVegetarian = prefs.isVegetarian,
|
// isVegan = prefs.isVegan,
|
||||||
isGame = prefs.isGame,
|
// isVegetarian = prefs.isVegetarian,
|
||||||
|
// isGame = prefs.isGame,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
dao.allOffers
|
dao.allOffers
|
||||||
}
|
}
|
||||||
|
// return dao.allOffers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun getDietaryPrefs(): DietaryPrefObject {
|
fun getDietaryPrefs(): String {
|
||||||
return DietaryPrefObject(
|
return prefs.getString(dietaryPrefString, "ffffffffff") ?: "ffffffffff"
|
||||||
isFair = prefs.getBoolean(DietaryPreferences.FAIR.value, false),
|
// return DietaryPrefObject(
|
||||||
isFish = prefs.getBoolean(DietaryPreferences.FISH.value, false),
|
// isFair = prefs.getBoolean(DietaryPreferences.FAIR.value, false),
|
||||||
isPoultry = prefs.getBoolean(DietaryPreferences.POULTRY.value, false),
|
// isFish = prefs.getBoolean(DietaryPreferences.FISH.value, false),
|
||||||
isLamb = prefs.getBoolean(DietaryPreferences.LAMB.value, false),
|
// isPoultry = prefs.getBoolean(DietaryPreferences.POULTRY.value, false),
|
||||||
isVital = prefs.getBoolean(DietaryPreferences.VITAL.value, false),
|
// isLamb = prefs.getBoolean(DietaryPreferences.LAMB.value, false),
|
||||||
isBeef = prefs.getBoolean(DietaryPreferences.BEEF.value, false),
|
// isVital = prefs.getBoolean(DietaryPreferences.VITAL.value, false),
|
||||||
isPork = prefs.getBoolean(DietaryPreferences.PORK.value, false),
|
// isBeef = prefs.getBoolean(DietaryPreferences.BEEF.value, false),
|
||||||
isVegan = prefs.getBoolean(DietaryPreferences.VEGAN.value, false),
|
// isPork = prefs.getBoolean(DietaryPreferences.PORK.value, false),
|
||||||
isVegetarian = prefs.getBoolean(DietaryPreferences.VEGETARIAN.value, false),
|
// isVegan = prefs.getBoolean(DietaryPreferences.VEGAN.value, false),
|
||||||
isGame = prefs.getBoolean(DietaryPreferences.GAME.value, false),
|
// isVegetarian = prefs.getBoolean(DietaryPreferences.VEGETARIAN.value, false),
|
||||||
)
|
// isGame = prefs.getBoolean(DietaryPreferences.GAME.value, false),
|
||||||
|
// )
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isPreferenceSet(): Boolean {
|
fun getDietaryPrefsAsObj(): DietaryPrefObject = DietaryPrefObject.construct(getDietaryPrefs())
|
||||||
getDietaryPrefs().also { p ->
|
|
||||||
if (p.isFair) return true
|
// private fun isPreferenceSet(): Boolean {
|
||||||
if (p.isFish) return true
|
// getDietaryPrefs().also { p ->
|
||||||
if (p.isPoultry) return true
|
// if (p.isFair) return true
|
||||||
if (p.isLamb) return true
|
// if (p.isFish) return true
|
||||||
if (p.isVital) return true
|
// if (p.isPoultry) return true
|
||||||
if (p.isBeef) return true
|
// if (p.isLamb) return true
|
||||||
if (p.isPork) return true
|
// if (p.isVital) return true
|
||||||
if (p.isVegan) return true
|
// if (p.isBeef) return true
|
||||||
if (p.isVegetarian) return true
|
// if (p.isPork) return true
|
||||||
if (p.isGame) return true
|
// if (p.isVegan) return true
|
||||||
}
|
// if (p.isVegetarian) return true
|
||||||
return false
|
// if (p.isGame) return true
|
||||||
}
|
// }
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
|
||||||
fun setPreference(pref: DietaryPreferences, newValue: Boolean) {
|
fun setPreference(pref: DietaryPreferences, newValue: Boolean) {
|
||||||
prefs.edit().putBoolean(pref.value, newValue).apply()
|
// prefs.edit().putBoolean(pref.value, newValue).apply()
|
||||||
|
val oldString = getDietaryPrefs().toMutableList()
|
||||||
|
when (pref) {
|
||||||
|
DietaryPreferences.FAIR -> oldString[0] = newValue.toChar()
|
||||||
|
DietaryPreferences.FISH -> oldString[1] = newValue.toChar()
|
||||||
|
DietaryPreferences.POULTRY -> oldString[2] = newValue.toChar()
|
||||||
|
DietaryPreferences.LAMB -> oldString[3] = newValue.toChar()
|
||||||
|
DietaryPreferences.VITAL -> oldString[4] = newValue.toChar()
|
||||||
|
DietaryPreferences.BEEF -> oldString[5] = newValue.toChar()
|
||||||
|
DietaryPreferences.PORK -> oldString[6] = newValue.toChar()
|
||||||
|
DietaryPreferences.VEGAN -> oldString[7] = newValue.toChar()
|
||||||
|
DietaryPreferences.VEGETARIAN -> oldString[8] = newValue.toChar()
|
||||||
|
DietaryPreferences.GAME -> oldString[9] = newValue.toChar()
|
||||||
|
}
|
||||||
|
val newString = oldString.joinToString("")
|
||||||
|
Log.d("eek!4", newString)
|
||||||
|
prefs.edit().putString(dietaryPrefString, newString).apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getPreference(pref: DietaryPreferences): Boolean =
|
private fun Boolean.toChar() = if (this) 't' else 'f'
|
||||||
prefs.getBoolean(pref.value, false)
|
|
||||||
|
fun getPreference(pref: DietaryPreferences): Boolean {
|
||||||
|
val prefString = getDietaryPrefs().toList()
|
||||||
|
return when (pref) {
|
||||||
|
DietaryPreferences.FAIR -> prefString[0] == 't'
|
||||||
|
DietaryPreferences.FISH -> prefString[1] == 't'
|
||||||
|
DietaryPreferences.POULTRY -> prefString[2] == 't'
|
||||||
|
DietaryPreferences.LAMB -> prefString[3] == 't'
|
||||||
|
DietaryPreferences.VITAL -> prefString[4] == 't'
|
||||||
|
DietaryPreferences.BEEF -> prefString[5] == 't'
|
||||||
|
DietaryPreferences.PORK -> prefString[6] == 't'
|
||||||
|
DietaryPreferences.VEGAN -> prefString[7] == 't'
|
||||||
|
DietaryPreferences.VEGETARIAN -> prefString[8] == 't'
|
||||||
|
DietaryPreferences.GAME -> prefString[9] == 't'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// prefs.getBoolean(pref.value, false)
|
||||||
|
|
||||||
// fun insertOffers(offers: List<CanteenOffer>) { dao.insertOffers(offers) }
|
// fun insertOffers(offers: List<CanteenOffer>) { dao.insertOffers(offers) }
|
||||||
fun nukeOffers() {
|
fun nukeOffers() {
|
||||||
|
|||||||
@@ -1,14 +1,18 @@
|
|||||||
package com.denizk0461.studip.fragment
|
package com.denizk0461.studip.fragment
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.core.view.children
|
import androidx.core.view.children
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.fragment.app.viewModels
|
import androidx.fragment.app.viewModels
|
||||||
|
import androidx.lifecycle.LiveData
|
||||||
|
import androidx.lifecycle.MutableLiveData
|
||||||
import com.denizk0461.studip.adapter.CanteenOfferPageAdapter
|
import com.denizk0461.studip.adapter.CanteenOfferPageAdapter
|
||||||
import com.denizk0461.studip.databinding.FragmentCanteenBinding
|
import com.denizk0461.studip.databinding.FragmentCanteenBinding
|
||||||
|
import com.denizk0461.studip.model.CanteenOffer
|
||||||
import com.denizk0461.studip.model.DietaryPreferences
|
import com.denizk0461.studip.model.DietaryPreferences
|
||||||
import com.denizk0461.studip.viewmodel.CanteenViewModel
|
import com.denizk0461.studip.viewmodel.CanteenViewModel
|
||||||
import com.google.android.material.chip.Chip
|
import com.google.android.material.chip.Chip
|
||||||
@@ -24,6 +28,8 @@ class CanteenFragment : Fragment() {
|
|||||||
|
|
||||||
private var dates = listOf<String>()
|
private var dates = listOf<String>()
|
||||||
|
|
||||||
|
private lateinit var liveData: LiveData<List<CanteenOffer>>
|
||||||
|
|
||||||
private lateinit var viewPagerAdapter: CanteenOfferPageAdapter
|
private lateinit var viewPagerAdapter: CanteenOfferPageAdapter
|
||||||
private val viewModel: CanteenViewModel by viewModels()
|
private val viewModel: CanteenViewModel by viewModels()
|
||||||
|
|
||||||
@@ -35,21 +41,7 @@ class CanteenFragment : Fragment() {
|
|||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
|
||||||
/* Since this removes the view before immediately adding it back, this method causes a
|
val chipMap = mapOf(
|
||||||
* 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.chipPrefFair to DietaryPreferences.FAIR,
|
||||||
binding.chipPrefFish to DietaryPreferences.FISH,
|
binding.chipPrefFish to DietaryPreferences.FISH,
|
||||||
binding.chipPrefPoultry to DietaryPreferences.POULTRY,
|
binding.chipPrefPoultry to DietaryPreferences.POULTRY,
|
||||||
@@ -62,28 +54,57 @@ class CanteenFragment : Fragment() {
|
|||||||
binding.chipPrefGame to DietaryPreferences.GAME,
|
binding.chipPrefGame to DietaryPreferences.GAME,
|
||||||
)
|
)
|
||||||
|
|
||||||
chipMap.forEach { chip, pref ->
|
chipMap.forEach { (chip, pref) ->
|
||||||
chip.isChecked = getPreference(pref)
|
chip.isChecked = getPreference(pref)
|
||||||
chip.setOnCheckedChangeListener { _, newValue ->
|
chip.setOnCheckedChangeListener { buttonView, newValue ->
|
||||||
setPreference(pref, newValue)
|
setPreference(pref, newValue)
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.chipPrefFair
|
// binding.chipPrefFair
|
||||||
binding.chipPrefFair.setOnCheckedChangeListener { _, newValue ->
|
// binding.chipPrefFair.setOnCheckedChangeListener { _, newValue ->
|
||||||
setPreference(DietaryPreferences.FAIR, newValue)
|
// setPreference(DietaryPreferences.FAIR, newValue)
|
||||||
}
|
// }
|
||||||
|
|
||||||
viewPagerAdapter = CanteenOfferPageAdapter(listOf(), 0)
|
viewPagerAdapter = CanteenOfferPageAdapter(listOf(), 0, viewModel.getDietaryPrefs())
|
||||||
binding.viewPager.adapter = viewPagerAdapter
|
binding.viewPager.adapter = viewPagerAdapter
|
||||||
|
|
||||||
createTabLayoutMediator()
|
createTabLayoutMediator()
|
||||||
|
|
||||||
viewModel.allOffers.observe(viewLifecycleOwner) { offers ->
|
liveData = viewModel.getOffersByPreference()
|
||||||
|
liveData.observe(viewLifecycleOwner) { offers ->
|
||||||
|
|
||||||
dates = offers.map { it.date }.distinct()
|
dates = offers.map { it.date }.distinct()
|
||||||
createTabLayoutMediator()
|
createTabLayoutMediator()
|
||||||
|
|
||||||
|
// val newOffers = mutableListOf<CanteenOffer>()
|
||||||
|
|
||||||
|
// val prefs = viewModel.getDietaryPrefs()
|
||||||
|
// if (prefs.isFair) { newOffers.addAll(offers.filter { it.isFair }) }
|
||||||
|
// if (prefs.isFish) { newOffers.addAll(offers.filter { it.isFish }) }
|
||||||
|
// if (prefs.isPoultry) { newOffers.addAll(offers.filter { it.isPoultry }) }
|
||||||
|
// if (prefs.isLamb) { newOffers.addAll(offers.filter { it.isLamb }) }
|
||||||
|
// if (prefs.isVital) { newOffers.addAll(offers.filter { it.isVital }) }
|
||||||
|
// if (prefs.isBeef) { newOffers.addAll(offers.filter { it.isBeef }) }
|
||||||
|
// if (prefs.isPork) { newOffers.addAll(offers.filter { it.isPork }) }
|
||||||
|
// if (prefs.isVegetarian) { newOffers.addAll(offers.filter { it.isVegetarian }) }
|
||||||
|
// if (prefs.isVegan) { newOffers.addAll(offers.filter { it.isVegan }) }
|
||||||
|
// if (prefs.isGame) { newOffers.addAll(offers.filter { it.isGame }) }
|
||||||
|
// newOffers.sortBy { it ->
|
||||||
|
// Log.d("eek!4", "title: ${it.title} - id: ${it.itemId}")
|
||||||
|
// it.itemId
|
||||||
|
// }
|
||||||
|
|
||||||
viewPagerAdapter.setNewItems(offers, dates.size)
|
viewPagerAdapter.setNewItems(offers, dates.size)
|
||||||
|
|
||||||
// TODO this must be changed. if an exception is raised, then this will not fire
|
// TODO this must be changed. if an exception is raised, then this will not fire
|
||||||
@@ -92,7 +113,7 @@ class CanteenFragment : Fragment() {
|
|||||||
|
|
||||||
binding.swipeRefreshLayout.setOnRefreshListener {
|
binding.swipeRefreshLayout.setOnRefreshListener {
|
||||||
viewModel.fetchOffers(onRefreshUpdate = { status ->
|
viewModel.fetchOffers(onRefreshUpdate = { status ->
|
||||||
|
// TODO refresh updates
|
||||||
}, onFinish = {
|
}, onFinish = {
|
||||||
// binding.swipeRefreshLayout.isRefreshing = false
|
// binding.swipeRefreshLayout.isRefreshing = false
|
||||||
})
|
})
|
||||||
@@ -114,9 +135,17 @@ class CanteenFragment : Fragment() {
|
|||||||
|
|
||||||
private fun setPreference(pref: DietaryPreferences, newValue: Boolean) {
|
private fun setPreference(pref: DietaryPreferences, newValue: Boolean) {
|
||||||
viewModel.setPreference(pref, newValue)
|
viewModel.setPreference(pref, newValue)
|
||||||
|
viewPagerAdapter.refreshView(viewModel.getDietaryPrefs())
|
||||||
|
// Log.d("eek!4", "this")
|
||||||
|
// liveData.forceRefresh()
|
||||||
|
// viewModel.forceRefresh()
|
||||||
// notifyDataSetChanged?
|
// notifyDataSetChanged?
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getPreference(pref: DietaryPreferences): Boolean =
|
private fun getPreference(pref: DietaryPreferences): Boolean =
|
||||||
viewModel.getPreference(pref)
|
viewModel.getPreference(pref)
|
||||||
|
|
||||||
|
private fun MutableLiveData<List<CanteenOffer>>.forceRefresh() {
|
||||||
|
this.value = this.value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
package com.denizk0461.studip.model
|
package com.denizk0461.studip.model
|
||||||
|
|
||||||
|
import androidx.room.ColumnInfo
|
||||||
|
|
||||||
//@Entity(tableName = "offers")
|
//@Entity(tableName = "offers")
|
||||||
data class CanteenOffer(
|
data class CanteenOffer(
|
||||||
// @PrimaryKey val id: Int,
|
// @PrimaryKey val id: Int,
|
||||||
val id: Int,
|
val itemId: Int,
|
||||||
val date: String,
|
val date: String,
|
||||||
val dateId: Int,
|
val dateId: Int,
|
||||||
val category: String,
|
val category: String,
|
||||||
@@ -12,14 +14,5 @@ data class CanteenOffer(
|
|||||||
val canteenId: Int,
|
val canteenId: Int,
|
||||||
val title: String,
|
val title: String,
|
||||||
val price: String, // price for students
|
val price: String, // price for students
|
||||||
val isFair: Boolean, // artgerechte Tierhaltung
|
@ColumnInfo(name = "dietary_preferences") val dietaryPreferences: String,
|
||||||
val isFish: Boolean, // Fisch
|
|
||||||
val isPoultry: Boolean, // Geflügel
|
|
||||||
val isLamb: Boolean, // Lamm
|
|
||||||
val isVital: Boolean, // mensaVital
|
|
||||||
val isBeef: Boolean, // Rindfleisch
|
|
||||||
val isPork: Boolean, // Schweinefleisch
|
|
||||||
val isVegan: Boolean, // plant-based (vegan)
|
|
||||||
val isVegetarian: Boolean, // vegetarisch
|
|
||||||
val isGame: Boolean, // Wild
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -11,4 +11,38 @@ data class DietaryPrefObject(
|
|||||||
val isVegan: Boolean,
|
val isVegan: Boolean,
|
||||||
val isVegetarian: Boolean,
|
val isVegetarian: Boolean,
|
||||||
val isGame: Boolean,
|
val isGame: Boolean,
|
||||||
)
|
) {
|
||||||
|
companion object {
|
||||||
|
/* assume that the values must be parsed as follows:
|
||||||
|
* "xxxxxxxxxx"
|
||||||
|
* t = true, f = false
|
||||||
|
*/
|
||||||
|
fun construct(values: String) = DietaryPrefObject(
|
||||||
|
isFair = values[0] == 't',
|
||||||
|
isFish = values[1] == 't',
|
||||||
|
isPoultry = values[2] == 't',
|
||||||
|
isLamb = values[3] == 't',
|
||||||
|
isVital = values[4] == 't',
|
||||||
|
isBeef = values[5] == 't',
|
||||||
|
isPork = values[6] == 't',
|
||||||
|
isVegan = values[7] == 't',
|
||||||
|
isVegetarian = values[8] == 't',
|
||||||
|
isGame = values[9] == 't',
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun deconstruct() = String(
|
||||||
|
charArrayOf(
|
||||||
|
if (this.isFair) 't' else 'f',
|
||||||
|
if (this.isFish) 't' else 'f',
|
||||||
|
if (this.isPoultry) 't' else 'f',
|
||||||
|
if (this.isLamb) 't' else 'f',
|
||||||
|
if (this.isVital) 't' else 'f',
|
||||||
|
if (this.isBeef) 't' else 'f',
|
||||||
|
if (this.isPork) 't' else 'f',
|
||||||
|
if (this.isVegan) 't' else 'f',
|
||||||
|
if (this.isVegetarian) 't' else 'f',
|
||||||
|
if (this.isGame) 't' else 'f',
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.denizk0461.studip.model
|
package com.denizk0461.studip.model
|
||||||
|
|
||||||
|
import androidx.room.ColumnInfo
|
||||||
import androidx.room.Entity
|
import androidx.room.Entity
|
||||||
import androidx.room.ForeignKey
|
import androidx.room.ForeignKey
|
||||||
import androidx.room.PrimaryKey
|
import androidx.room.PrimaryKey
|
||||||
@@ -16,18 +17,19 @@ import androidx.room.PrimaryKey
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
data class OfferItem(
|
data class OfferItem(
|
||||||
@PrimaryKey val id: Int,
|
@PrimaryKey val itemId: Int,
|
||||||
val categoryId: Int,
|
val categoryId: Int,
|
||||||
val title: String,
|
val title: String,
|
||||||
val price: String,
|
val price: String,
|
||||||
val isFair: Boolean,
|
@ColumnInfo(name = "dietary_preferences") val dietaryPreferences: String,
|
||||||
val isFish: Boolean,
|
// val isFair: Boolean,
|
||||||
val isPoultry: Boolean,
|
// val isFish: Boolean,
|
||||||
val isLamb: Boolean,
|
// val isPoultry: Boolean,
|
||||||
val isVital: Boolean,
|
// val isLamb: Boolean,
|
||||||
val isBeef: Boolean,
|
// val isVital: Boolean,
|
||||||
val isPork: Boolean,
|
// val isBeef: Boolean,
|
||||||
val isVegan: Boolean,
|
// val isPork: Boolean,
|
||||||
val isVegetarian: Boolean,
|
// val isVegan: Boolean,
|
||||||
val isGame: Boolean,
|
// val isVegetarian: Boolean,
|
||||||
|
// val isGame: Boolean,
|
||||||
)
|
)
|
||||||
@@ -2,18 +2,32 @@ package com.denizk0461.studip.viewmodel
|
|||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
|
import androidx.lifecycle.MutableLiveData
|
||||||
import com.denizk0461.studip.data.StwParser
|
import com.denizk0461.studip.data.StwParser
|
||||||
import com.denizk0461.studip.model.CanteenOffer
|
import com.denizk0461.studip.model.CanteenOffer
|
||||||
|
import com.denizk0461.studip.model.DietaryPrefObject
|
||||||
import com.denizk0461.studip.model.DietaryPreferences
|
import com.denizk0461.studip.model.DietaryPreferences
|
||||||
|
|
||||||
class CanteenViewModel(app: Application) : TemplateViewModel(app) {
|
class CanteenViewModel(app: Application) : TemplateViewModel(app) {
|
||||||
|
|
||||||
private val parser = StwParser()
|
private val parser = StwParser()
|
||||||
|
|
||||||
|
// bad pattern i know
|
||||||
|
private val mutableOffers = MutableLiveData<List<CanteenOffer>>()
|
||||||
|
|
||||||
val allOffers: LiveData<List<CanteenOffer>> = repo.allOffers
|
val allOffers: LiveData<List<CanteenOffer>> = repo.allOffers
|
||||||
|
|
||||||
fun getOffersByPreference(): LiveData<List<CanteenOffer>> =
|
fun getOffersByPreference(): LiveData<List<CanteenOffer>> {
|
||||||
repo.getOffersByPreference()
|
val of = repo.getOffersByPreference()
|
||||||
|
mutableOffers.postValue(of.value)
|
||||||
|
return of
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getDietaryPrefs(): DietaryPrefObject = repo.getDietaryPrefsAsObj()
|
||||||
|
|
||||||
|
fun forceRefresh() {
|
||||||
|
mutableOffers.value = mutableOffers.value
|
||||||
|
}
|
||||||
|
|
||||||
// fun insertOffers(offers: List<CanteenOffer>) { doAsync { repo.insertOffers(offers) }}
|
// fun insertOffers(offers: List<CanteenOffer>) { doAsync { repo.insertOffers(offers) }}
|
||||||
fun nukeOffers() { doAsync { repo.nukeOffers() }}
|
fun nukeOffers() { doAsync { repo.nukeOffers() }}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<vector android:height="24dp" android:tint="#000000"
|
||||||
|
android:viewportHeight="24" android:viewportWidth="24"
|
||||||
|
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<path android:fillColor="@android:color/white" android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
|
||||||
|
</vector>
|
||||||
@@ -61,7 +61,9 @@
|
|||||||
android:id="@+id/chip_pref_fish"
|
android:id="@+id/chip_pref_fish"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="@string/pref_fish"/>
|
android:text="@string/pref_fish"
|
||||||
|
app:chipIcon="@drawable/cross"
|
||||||
|
app:checkedIcon="@drawable/flatware"/>
|
||||||
|
|
||||||
<com.google.android.material.chip.Chip
|
<com.google.android.material.chip.Chip
|
||||||
style="@style/Widget.Material3.Chip.Filter"
|
style="@style/Widget.Material3.Chip.Filter"
|
||||||
|
|||||||
Reference in New Issue
Block a user