Created
February 11, 2020 14:27
-
-
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
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
/** | |
* 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