Skip to content

Instantly share code, notes, and snippets.

@marcelpinto
Created April 2, 2019 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelpinto/d7410b27325e0569020492ea564aac19 to your computer and use it in GitHub Desktop.
Save marcelpinto/d7410b27325e0569020492ea564aac19 to your computer and use it in GitHub Desktop.
Gist for ViewData best practice blog - Good way
data class MyViewData(val value: Int, val textId: Int, val labelId: Int)
class MyViewModel: ViewModel() {
private val _myViewData = MutableLiveData<MyViewData>()
val myViewData: LiveData<MyViewData> = _myViewData
fun updateViewData() {
val value = getDistance()
val distanceUnit = userPref.getDistanceUnit()
val label = when (distanceUnit) {
KM -> R.string.km_label
MILES -> R.string.miles_label
}
_myViewData.value = MyViewData(distance, R.string.my_view_distance, label)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment