Last active
November 22, 2020 14:45
-
-
Save makorowy/c9dcf945d2951974ba9a44c037a4eb12 to your computer and use it in GitHub Desktop.
Example of common ViewModel implementation
This file contains 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
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