Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.stayhealthy.owl.features.main.menu.mealdiary.newentry
- import androidx.lifecycle.MutableLiveData
- import androidx.lifecycle.ViewModel
- import androidx.lifecycle.viewModelScope
- import com.paulmarkcastillo.androidtoolbox.datetime.convertToUtcDateString
- import com.stayhealthy.owl.common.ui.mealdiarycomponents.CategoryCheckbox
- import com.stayhealthy.owl.common.ui.mealdiarycomponents.CategoryCheckboxListListener
- import com.stayhealthy.owl.common.ui.mealdiarycomponents.DiarySeekBar
- import com.stayhealthy.owl.common.ui.mealdiarycomponents.SeekBarsComponent
- import com.stayhealthy.owl.common.ui.mealdiarycomponents.Tag
- import com.stayhealthy.owl.common.ui.mealdiarycomponents.TagListListener
- import com.stayhealthy.owl.data.manager.IDataManager
- import com.stayhealthy.owl.data.manager.IDatabaseManager
- import com.stayhealthy.owl.data.model.GeoPoint
- import com.stayhealthy.owl.data.model.MealLog
- import com.stayhealthy.owl.data.model.SimpleLocation
- import com.stayhealthy.owl.features.main.menu.mealdiary.components.location.LocationFragment
- import com.stayhealthy.owl.network.INetworkManager
- import com.stayhealthy.owl.network.Resource
- import kotlinx.coroutines.launch
- import java.io.File
- import java.util.Date
- import kotlin.collections.ArrayList
- class NewMealEntryViewModel(
- private val dataManager: IDataManager,
- private val networkManager: INetworkManager,
- private val databaseManager: IDatabaseManager
- ) : ViewModel(),
- TagListListener,
- LocationFragment.LocationListener,
- CategoryCheckboxListListener,
- SeekBarsComponent.CustomSeekBarProgressChangeListener {
- companion object {
- const val GEOPOINT_FORMAT = "(%3.4f, %3.4f)"
- }
- val mealDate = MutableLiveData<Date>()
- val description = MutableLiveData("")
- val newImage = MutableLiveData<File?>()
- val newEntryAction = MutableLiveData<NewEntryAction>()
- var addMealDiaryResponse = MutableLiveData<Resource<MealLog>>()
- private val tagList = ArrayList<String>()
- private var location: SimpleLocation? = null
- private var categoryCheckboxList = ArrayList<CategoryCheckbox>()
- private var seekBarFruitsVegetablesProgress = 0
- private var seekBarProteinOthersProgress = 0
- fun getSeekBars(): ArrayList<DiarySeekBar> {
- return dataManager.getDiarySeekBarsList()
- }
- fun getCategoryCheckboxes(): ArrayList<CategoryCheckbox> {
- return dataManager.getCategoryCheckboxList()
- }
- override fun onLocationSelected(location: SimpleLocation?) {
- this.location = location
- }
- fun onDateTimePickerToggled() {
- newEntryAction.value = NewEntryAction.DATE_TIME_PICKER
- }
- fun onImagePickerToggled() {
- newEntryAction.value = NewEntryAction.IMAGE_PICKER
- }
- fun onEntrySaved() {
- newEntryAction.value = NewEntryAction.SAVE
- }
- override fun onTagListChanged(tagList: ArrayList<Tag>) {
- this.tagList.clear()
- this.tagList.addAll(tagList.map { it.tagMessage })
- }
- override fun onCategoryCheckboxListChanged(categoryCheckboxList: ArrayList<CategoryCheckbox>) {
- this.categoryCheckboxList = categoryCheckboxList
- }
- override fun onFruitsVegetablesSeekBarProgressChanged(progress: Int) {
- seekBarFruitsVegetablesProgress = progress
- }
- override fun onProteinOthersSeekBarProgressChanged(progress: Int) {
- seekBarProteinOthersProgress = progress
- }
- fun addMealDiary() {
- val category = categoryCheckboxList.find { it.isChecked }?.category
- val longLatString = location?.run {
- if (long != null && lat != null) GEOPOINT_FORMAT.format(long, lat)
- else null
- }
- val location = location?.name ?: ""
- val seekBarFruitsVegetableProgress = seekBarFruitsVegetablesProgress.toFloat()
- val description = description.value ?: ""
- val mealDate = mealDate.value?.convertToUtcDateString().toString()
- val mealLog = MealLog().apply {
- this.location = location
- this.mealDate = mealDate
- this.category = category
- this.tags = tagList
- this.percentFruitVeg = seekBarFruitsVegetableProgress
- this.description = description
- this.longLat = longLatString
- }
- viewModelScope.launch {
- when (val result = networkManager.addMealDiary(mealLog)) {
- is Resource.Success -> {
- result.data?.let { data ->
- databaseManager.insertMealLog(mealLog.apply {
- this.id = data.id
- this.location = data.location
- this.mealDate = data.mealDate
- this.category = data.category
- this.tags = data.tags
- this.percentFruitVeg = data.percentFruitVeg
- this.description = data.description
- this.longLat = this@NewMealEntryViewModel.location?.run {
- if (long != null && lat != null) GeoPoint(lat, long)
- else null
- }
- this.imageUrl = data.imageUrl
- this.thumbnailUrl = data.thumbnailUrl
- this.userId = data.userId
- this.created = data.created
- this.modified = data.modified
- })
- }
- addMealDiaryResponse.postValue(result)
- }
- is Resource.Error -> addMealDiaryResponse.postValue(result)
- }
- }
- }
- }
- enum class NewEntryAction {
- DATE_TIME_PICKER,
- IMAGE_PICKER,
- SAVE
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement