Skip to content

Instantly share code, notes, and snippets.

@BapNesS
Created February 11, 2020 14:27
Show Gist options
  • Save BapNesS/4e23073e553e2016e029df50d9c43829 to your computer and use it in GitHub Desktop.
Save BapNesS/4e23073e553e2016e029df50d9c43829 to your computer and use it in GitHub Desktop.
Easy way to Toast with ViewModel, LiveData & a bit of abstraction. 2/4
/**
* Base for other ViewModel
*/
abstract class BaseViewModel: ViewModel() {
// Mutable/LiveData of String resource reference Event
private val _message = MutableLiveData<Event<Int>>()
val message : LiveData<Event<Int>>
get() = _message
// Post in background thread
fun postMessage(@StringRes message: Int) {
_message.postValue(Event(message))
}
// Post in main thread
fun setMessage(@StringRes message: Int) {
_message.value = Event(message)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment