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 ListViewModel : ViewModel { | |
private val _navigateToDetails = SingleLiveEvent<Any>() | |
val navigateToDetails : LiveData<Any> | |
get() = _navigateToDetails | |
fun userClicksOnButton() { | |
_navigateToDetails.call() |
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
myViewModel.navigateToDetails.observe(this, Observer { | |
startActivity(DetailsActivity...) | |
}) |
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
/** | |
* Used as a wrapper for data that is exposed via a LiveData that represents an event. | |
*/ | |
open class Event<out T>(private val content: T) { | |
var hasBeenHandled = false | |
private set // Allow external read but not write | |
/** | |
* Returns the content and prevents its use again. |
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 ListViewModel : ViewModel { | |
private val _navigateToDetails = MutableLiveData<Event<String>>() | |
val navigateToDetails : LiveData<Event<String>> | |
get() = _navigateToDetails | |
fun userClicksOnButton(itemId: String) { | |
_navigateToDetails.value = Event(itemId) // Trigger the event by setting a new Event as a new value | |
} |
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
myViewModel.navigateToDetails.observe(this, Observer { | |
it.getContentIfNotHandled()?.let { // Only proceed if the event has never been handled | |
startActivity(DetailsActivity...) | |
} | |
}) |
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
/** | |
* An [Observer] for [Event]s, simplifying the pattern of checking if the [Event]'s content has | |
* already been handled. | |
* | |
* [onEventUnhandledContent] is *only* called if the [Event]'s contents has not been handled. | |
*/ | |
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> { | |
override fun onChanged(event: Event<T>?) { | |
event?.getContentIfNotHandled()?.let { value -> | |
onEventUnhandledContent(value) |
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 MainViewModel { | |
val viewModelResult = Transformations.map(repository.getDataForUser()) { data -> | |
convertDataToMainUIModel(data) | |
} | |
} |
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 MainViewModel { | |
val repositoryResult = Transformations.switchMap(userManager.user) { user -> | |
repository.getDataForUser(user) | |
} | |
} |
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
val liveData1: LiveData<Int> = ... | |
val liveData2: LiveData<Int> = ... | |
val result = MediatorLiveData<Int>() | |
result.addSource(liveData1) { value -> | |
result.setValue(value) | |
} | |
result.addSource(liveData2) { value -> | |
result.setValue(value) |
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
fun blogpostBoilerplateExample(newUser: String): LiveData<UserDataResult> { | |
val liveData1 = userOnlineDataSource.getOnlineTime(newUser) | |
val liveData2 = userCheckinsDataSource.getCheckins(newUser) | |
val result = MediatorLiveData<UserDataResult>() | |
result.addSource(liveData1) { value -> | |
result.value = combineLatestData(liveData1, liveData2) | |
} |