2023-04-16 18:19:06 +02:00
|
|
|
package com.denizk0461.studip.viewmodel
|
|
|
|
|
|
|
|
|
|
import android.app.Application
|
|
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
|
import com.denizk0461.studip.data.StwParser
|
2023-04-24 15:43:42 +02:00
|
|
|
import com.denizk0461.studip.model.*
|
2023-04-16 18:19:06 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* View model for [com.denizk0461.studip.fragment.CanteenFragment]
|
|
|
|
|
*
|
|
|
|
|
* @param app reference to the app
|
|
|
|
|
*/
|
2023-04-23 17:13:48 +02:00
|
|
|
class CanteenViewModel(app: Application) : AppViewModel(app) {
|
2023-04-16 18:19:06 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Instantiate a parser, should the user want to refresh the canteen offers
|
2023-04-16 18:19:06 +02:00
|
|
|
private val parser = StwParser()
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* Retrieves all Stud.IP events.
|
|
|
|
|
*
|
|
|
|
|
* @return all Stud.IP events exposed through a LiveData object
|
|
|
|
|
*/
|
2023-04-16 18:19:06 +02:00
|
|
|
val allOffers: LiveData<List<CanteenOffer>> = repo.allOffers
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* Retrieve the user's dietary preferences.
|
|
|
|
|
*
|
|
|
|
|
* @return dietary preferences
|
|
|
|
|
*/
|
2023-04-18 15:39:49 +02:00
|
|
|
fun getDietaryPrefs(): DietaryPrefObject = repo.getDietaryPrefsAsObj()
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* Retrieves all canteen offer date objects.
|
|
|
|
|
*
|
|
|
|
|
* @return a list of instances of canteen offer dates
|
|
|
|
|
*/
|
2023-04-20 21:35:09 +02:00
|
|
|
fun getDates(): List<OfferDate> = returnBlocking { repo.getDates() }
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Fetch the canteen offers asynchronously. Updates will be provided through a LiveData object.
|
|
|
|
|
fun fetchOffers(onRefreshUpdate: (status: Int) -> Unit, onFinish: () -> Unit) {
|
|
|
|
|
doAsync { parser.parse(onRefreshUpdate, onFinish) }
|
|
|
|
|
}
|
2023-04-18 10:25:45 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* Updates a given dietary preference to a new value.
|
|
|
|
|
*
|
|
|
|
|
* @param pref preference to be updated
|
|
|
|
|
* @param newValue value to set the preference to
|
|
|
|
|
*/
|
|
|
|
|
fun setPreference(pref: DietaryPreferences, newValue: Boolean) {
|
|
|
|
|
repo.setPreference(pref, newValue)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves a single user-specified dietary preference.
|
|
|
|
|
*
|
|
|
|
|
* @param pref the dietary preference that should be retrieved
|
|
|
|
|
* @return whether the preference needs to be met
|
|
|
|
|
*/
|
2023-04-18 10:25:45 +02:00
|
|
|
fun getPreference(pref: DietaryPreferences): Boolean = repo.getPreference(pref)
|
2023-04-23 22:06:29 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This value determines whether the user wants to have allergens marked.
|
|
|
|
|
*/
|
|
|
|
|
val preferenceAllergen: Boolean
|
2023-04-24 15:43:42 +02:00
|
|
|
get() = repo.getPreference(SettingsPreferences.ALLERGEN)
|
2023-04-16 18:19:06 +02:00
|
|
|
}
|