Created
April 2, 2019 23:05
-
-
Save marcelpinto/76c68b12e47611fd8dcc3e0eca900519 to your computer and use it in GitHub Desktop.
Gist for ViewData best practice blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class MyViewData(val text: String) | |
class MyViewModel(application: Application): AndroidViewModel(application) { | |
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 | |
} | |
val text = getApplication<Application>().getString(R.string.my_view_distance, value, label) | |
_myViewData.value = MyViewData(text) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment