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
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) | |
} |
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
private fun combineLatestData( | |
onlineTimeResult: LiveData<Long>, | |
checkinsResult: LiveData<CheckinsResult> | |
): UserDataResult { | |
val onlineTime = onlineTimeResult.value | |
val checkins = checkinsResult.value | |
// Don't send a success until we have both results | |
if (onlineTime == null || checkins == null) { |
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
sharedLiveDataSource.loadDataForUser("1").observe(this, Observer { | |
// Show result on screen | |
}) |
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 SlowRandomNumberGenerator { | |
private val rnd = Random() | |
fun getNumber(): LiveData<Int> { | |
val result = MutableLiveData<Int>() | |
// Send a random number after a while | |
Executors.newSingleThreadExecutor().execute { | |
Thread.sleep(500) | |
result.postValue(rnd.nextInt(1000)) |
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
var lateinit randomNumber: LiveData<Int> | |
/** | |
* Called on button click. | |
*/ | |
fun onGetNumber() { | |
randomNumber = Transformations.map(numberGenerator.getNumber()) { | |
it | |
} | |
} |
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
viewmodel.randomNumber.observe(this, Observer { number -> | |
numberTv.text = resources.getString(R.string.random_text, number) | |
}) |