Edited canteen views to have direct database access in the CanteenPageFragment.kt, allowing for filtering the individual days through the database; filtering now works (with changes animated!!) through updating a LiveData and observing it in the page fragment

This commit is contained in:
denizk0461
2023-04-28 22:44:23 +02:00
parent 509a0434c5
commit 8fb41d6520
11 changed files with 320 additions and 193 deletions
@@ -0,0 +1,33 @@
package com.denizk0461.studip.viewmodel
import android.app.Application
import androidx.lifecycle.LiveData
import com.denizk0461.studip.model.CanteenOffer
import com.denizk0461.studip.model.DietaryPreferences
import com.denizk0461.studip.model.SettingsPreferences
class CanteenPageViewModel(app: Application) : AppViewModel(app) {
/**
* Retrieves all canteen offers that match given day.
*
* @return all canteen offers matching the given day exposed through a LiveData object
*/
fun getOffersByDay(day: Int): LiveData<List<CanteenOffer>> =
repo.getOffersByDay(day)
val dietaryPreferencesUpdate: LiveData<Int> = repo.dietaryPreferencesUpdate
/**
* Retrieve the user's dietary preferences.
*
* @return dietary preferences
*/
fun getDietaryPrefs(): DietaryPreferences.Object = repo.getDietaryPrefsAsObject()
/**
* This value determines whether the user wants to have allergens marked.
*/
val preferenceAllergen: Boolean
get() = repo.getBooleanPreference(SettingsPreferences.ALLERGEN, defaultValue = true)
}
@@ -22,12 +22,7 @@ class CanteenViewModel(app: Application) : AppViewModel(app) {
*/
val allOffers: LiveData<List<CanteenOffer>> = repo.allOffers
/**
* Retrieve the user's dietary preferences.
*
* @return dietary preferences
*/
fun getDietaryPrefs(): DietaryPreferences.Object = repo.getDietaryPrefsAsObject()
/**
* Retrieves all canteen offer date objects.
@@ -54,6 +49,17 @@ class CanteenViewModel(app: Application) : AppViewModel(app) {
doAsync { parser.parse(canteen, onRefreshUpdate, onFinish) }
}
fun registerDietaryPreferencesUpdate() {
repo.dietaryPreferencesUpdate.postValue(repo.dietaryPreferencesUpdate.value?.plus(1))
}
/**
* Retrieves the amount of dates represented in the offers stored locally. Can be observed.
*
* @return date count as LiveData
*/
fun getDateCount(): LiveData<Int> = repo.getDateCount()
/**
* Updates a given dietary preference to a new value.
*