Last active
July 31, 2020 05:27
-
-
Save ShinichiroFunatsu/165d0026760deef4a4d669a660dea6ec to your computer and use it in GitHub Desktop.
LiveData Event Mediator Beta
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
interface EventMediator<T> { | |
interface Input<T> { | |
fun bind(input: LiveData<T>) | |
} | |
interface Output<T> { | |
val onAction: LiveData<T> | |
} | |
val input: Input<T> | |
val output: Output<T> | |
companion object { | |
fun <T> create() = object : EventMediator<T> { | |
override val output = object : Output<T> { | |
override val onAction: MediatorLiveData<T> = MediatorLiveData() | |
} | |
override val input = object : Input<T> { | |
override fun bind(input: LiveData<T>) { | |
output.onAction.addSource(input) { | |
output.onAction.value = it | |
} | |
} | |
} | |
} | |
} | |
} |
This is better than custom something.
val listItemClickEventInput = MediatorLiveData<FooListItem>()
val listItemClickEventOutput: LiveData<FooListItem>
get() = listItemClickEventInput
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
適当につくったけど、これでは単にMediatorLiveDataの方が良さそうに見える