Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. package com.stayhealthy.owl.features.main.menu.mealdiary.newentry
  2.  
  3. import androidx.lifecycle.MutableLiveData
  4. import androidx.lifecycle.ViewModel
  5. import androidx.lifecycle.viewModelScope
  6. import com.paulmarkcastillo.androidtoolbox.datetime.convertToUtcDateString
  7. import com.stayhealthy.owl.common.ui.mealdiarycomponents.CategoryCheckbox
  8. import com.stayhealthy.owl.common.ui.mealdiarycomponents.CategoryCheckboxListListener
  9. import com.stayhealthy.owl.common.ui.mealdiarycomponents.DiarySeekBar
  10. import com.stayhealthy.owl.common.ui.mealdiarycomponents.SeekBarsComponent
  11. import com.stayhealthy.owl.common.ui.mealdiarycomponents.Tag
  12. import com.stayhealthy.owl.common.ui.mealdiarycomponents.TagListListener
  13. import com.stayhealthy.owl.data.manager.IDataManager
  14. import com.stayhealthy.owl.data.manager.IDatabaseManager
  15. import com.stayhealthy.owl.data.model.GeoPoint
  16. import com.stayhealthy.owl.data.model.MealLog
  17. import com.stayhealthy.owl.data.model.SimpleLocation
  18. import com.stayhealthy.owl.features.main.menu.mealdiary.components.location.LocationFragment
  19. import com.stayhealthy.owl.network.INetworkManager
  20. import com.stayhealthy.owl.network.Resource
  21. import kotlinx.coroutines.launch
  22. import java.io.File
  23. import java.util.Date
  24. import kotlin.collections.ArrayList
  25.  
  26. class NewMealEntryViewModel(
  27. private val dataManager: IDataManager,
  28. private val networkManager: INetworkManager,
  29. private val databaseManager: IDatabaseManager
  30. ) : ViewModel(),
  31. TagListListener,
  32. LocationFragment.LocationListener,
  33. CategoryCheckboxListListener,
  34. SeekBarsComponent.CustomSeekBarProgressChangeListener {
  35.  
  36. companion object {
  37. const val GEOPOINT_FORMAT = "(%3.4f, %3.4f)"
  38. }
  39.  
  40. val mealDate = MutableLiveData<Date>()
  41. val description = MutableLiveData("")
  42. val newImage = MutableLiveData<File?>()
  43. val newEntryAction = MutableLiveData<NewEntryAction>()
  44. var addMealDiaryResponse = MutableLiveData<Resource<MealLog>>()
  45.  
  46. private val tagList = ArrayList<String>()
  47. private var location: SimpleLocation? = null
  48. private var categoryCheckboxList = ArrayList<CategoryCheckbox>()
  49. private var seekBarFruitsVegetablesProgress = 0
  50. private var seekBarProteinOthersProgress = 0
  51.  
  52.  
  53. fun getSeekBars(): ArrayList<DiarySeekBar> {
  54. return dataManager.getDiarySeekBarsList()
  55. }
  56.  
  57. fun getCategoryCheckboxes(): ArrayList<CategoryCheckbox> {
  58. return dataManager.getCategoryCheckboxList()
  59. }
  60.  
  61. override fun onLocationSelected(location: SimpleLocation?) {
  62. this.location = location
  63. }
  64.  
  65. fun onDateTimePickerToggled() {
  66. newEntryAction.value = NewEntryAction.DATE_TIME_PICKER
  67. }
  68.  
  69. fun onImagePickerToggled() {
  70. newEntryAction.value = NewEntryAction.IMAGE_PICKER
  71. }
  72.  
  73. fun onEntrySaved() {
  74. newEntryAction.value = NewEntryAction.SAVE
  75. }
  76.  
  77. override fun onTagListChanged(tagList: ArrayList<Tag>) {
  78. this.tagList.clear()
  79. this.tagList.addAll(tagList.map { it.tagMessage })
  80. }
  81.  
  82. override fun onCategoryCheckboxListChanged(categoryCheckboxList: ArrayList<CategoryCheckbox>) {
  83. this.categoryCheckboxList = categoryCheckboxList
  84. }
  85.  
  86. override fun onFruitsVegetablesSeekBarProgressChanged(progress: Int) {
  87. seekBarFruitsVegetablesProgress = progress
  88. }
  89.  
  90. override fun onProteinOthersSeekBarProgressChanged(progress: Int) {
  91. seekBarProteinOthersProgress = progress
  92. }
  93.  
  94. fun addMealDiary() {
  95. val category = categoryCheckboxList.find { it.isChecked }?.category
  96. val longLatString = location?.run {
  97. if (long != null && lat != null) GEOPOINT_FORMAT.format(long, lat)
  98. else null
  99. }
  100. val location = location?.name ?: ""
  101. val seekBarFruitsVegetableProgress = seekBarFruitsVegetablesProgress.toFloat()
  102. val description = description.value ?: ""
  103. val mealDate = mealDate.value?.convertToUtcDateString().toString()
  104.  
  105. val mealLog = MealLog().apply {
  106. this.location = location
  107. this.mealDate = mealDate
  108. this.category = category
  109. this.tags = tagList
  110. this.percentFruitVeg = seekBarFruitsVegetableProgress
  111. this.description = description
  112. this.longLat = longLatString
  113. }
  114.  
  115. viewModelScope.launch {
  116. when (val result = networkManager.addMealDiary(mealLog)) {
  117. is Resource.Success -> {
  118. result.data?.let { data ->
  119. databaseManager.insertMealLog(mealLog.apply {
  120. this.id = data.id
  121. this.location = data.location
  122. this.mealDate = data.mealDate
  123. this.category = data.category
  124. this.tags = data.tags
  125. this.percentFruitVeg = data.percentFruitVeg
  126. this.description = data.description
  127. this.longLat = this@NewMealEntryViewModel.location?.run {
  128. if (long != null && lat != null) GeoPoint(lat, long)
  129. else null
  130. }
  131. this.imageUrl = data.imageUrl
  132. this.thumbnailUrl = data.thumbnailUrl
  133. this.userId = data.userId
  134. this.created = data.created
  135. this.modified = data.modified
  136. })
  137. }
  138. addMealDiaryResponse.postValue(result)
  139. }
  140. is Resource.Error -> addMealDiaryResponse.postValue(result)
  141. }
  142. }
  143. }
  144. }
  145.  
  146. enum class NewEntryAction {
  147. DATE_TIME_PICKER,
  148. IMAGE_PICKER,
  149. SAVE
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement