Skip to content

Instantly share code, notes, and snippets.

@makorowy
Last active November 22, 2020 14:45
Show Gist options
  • Save makorowy/c9dcf945d2951974ba9a44c037a4eb12 to your computer and use it in GitHub Desktop.
Save makorowy/c9dcf945d2951974ba9a44c037a4eb12 to your computer and use it in GitHub Desktop.
Example of common ViewModel implementation
class SettingsViewModel(
settingsRepository: SettingsRepository
) : ViewModel() {
private val _firstName = MutableLiveData<String>()
val firstName: LiveData<String> = _firstName
private val _lastName = MutableLiveData<String>()
val lastName: LiveData<String> = _lastName
private val _notificationEnabled = MutableLiveData<Boolean>()
val notificationEnabled: LiveData<Boolean> = _notificationEnabled
init {
val settings = settingsRepository.settings
_firstName.postValue(settings.firstName)
_lastName.postValue(settings.lastName)
_notificationEnabled.postValue(settings.notificationsChecked)
}
fun notificationsSwitchChanged(enabled: Boolean) {
/* logic */
}
fun firstNameChanged(name: String) {
/* logic */
}
fun lastNameChanged(name: String) {
/* logic */
}
fun notificationsSettingsButtonClicked() {
/* logic */
}
fun helpButtonClicked() {
/* logic */
}
fun logOutButtonClicked() {
/* logic */
}
fun advancedSettingsButtonClicked() {
/* logic */
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment