ifTrue

fun <T, B : Boolean?> LiveData<B>.ifTrue(block: () -> LiveData<T>): LiveData<T?>

Executes and switchMaps to block if this has a true value. If this is false or null, it returns LiveData with value null. If this should not return null, ifTrueOrDefault can be used instead.

This can be used to replace:

myLiveData.switchMap {
if (it == true) {
myNewLiveData
} else {
EmptyLiveData()
}
}.distinctUntilChanged()

with a more concise syntax:

myLiveData.ifTrue { myNewLiveData }