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
/** | |
* Sets the value to the result of a function that is called when both `LiveData`s have data | |
* or when they receive updates after that. | |
*/ | |
fun <T, A, B> LiveData<A>.combineAndCompute(other: LiveData<B>, onChange: (A, B) -> T): MediatorLiveData<T> { | |
var source1emitted = false | |
var source2emitted = false | |
val result = MediatorLiveData<T>() |
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
/** | |
* Notifies the event LiveData of a new request for a random number. | |
*/ | |
fun onGetNumber() { | |
newNumberEvent.value = Event(Unit) | |
} |
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
private val newNumberEvent = MutableLiveData<Event<Any>>() | |
val randomNumber: LiveData<Int> = Transformations.switchMap(newNumberEvent) { | |
numberGenerator.getNumber() | |
} |
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
viewmodel.randomNumber.observe(this, Observer { number -> | |
numberTv.text = resources.getString(R.string.random_text, number) | |
}) |
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
var lateinit randomNumber: LiveData<Int> | |
/** | |
* Called on button click. | |
*/ | |
fun onGetNumber() { | |
randomNumber = Transformations.map(numberGenerator.getNumber()) { | |
it | |
} | |
} |
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
val randomNumber = MediatorLiveData<Int>() | |
/** | |
* *Don't do this.* | |
* | |
* Called when the user clicks on a button | |
* | |
* This function adds a new source to the result but it doesn't remove the previous ones. | |
*/ | |
fun onGetNumber() { |
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
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 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
sharedLiveDataSource.loadDataForUser("2").observe(this, Observer { | |
// Show result on screen | |
}) |
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
sharedLiveDataSource.loadDataForUser("1").observe(this, Observer { | |
// Show result on screen | |
}) |