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 LiveEventViewModel { | |
... | |
private val liveData = MutableLiveData<String>() | |
val singleLiveEvent = liveData.toSingleEvent() | |
... | |
... { | |
liveData.value = "YES" | |
} | |
} |
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 <T> LiveData<T>.toSingleEvent(): LiveData<T> { | |
val result = LiveEvent<T>() | |
result.addSource(this) { | |
result.value = it | |
} | |
return result | |
} |
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 LiveEvent<T> : MediatorLiveData<T>() { | |
private val observers = ConcurrentHashMap<LifecycleOwner, MutableSet<ObserverWrapper<T>>>() | |
@MainThread | |
override fun observe(owner: LifecycleOwner, observer: Observer<T>) { | |
val wrapper = ObserverWrapper(observer) | |
val set = observers[owner] | |
set?.apply { | |
add(wrapper) |
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 initView() { | |
... | |
adapter.onCreateViewHolder = { viewHolder -> | |
// Pipe the View actions to the ViewModel | |
viewHolder.actionStream.subscribe(viewModel.actionStream::onNext).track() | |
} | |
... | |
} |
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
abstract class BaseViewHolder<in T : ViewData>(view: View) : RecyclerView.ViewHolder(view) { | |
val actionStream = PublishSubject.create<Action>() | |
} |
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 ExampleViewModel @Inject constructor( | |
... | |
) : BaseViewModel() { | |
init { | |
actionStream.filterTo(ClickedAction::class.java) | |
.throttleFirst(1, TimeUnit.SECONDS) // Avoid double click(Multi view click) less than one second | |
.subscribe(::clicked).track() | |
} | |
} |
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 ExampleViewModel @Inject constructor( | |
... | |
) : BaseViewModel() { | |
init { | |
val stream = actionStream | |
.compose(::firstLoggerMiddlewareFunction) | |
.compose(::secondLoggerMiddlewareFunction) | |
.share() | |
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 clicked() { | |
viewModel.actionStream.onNext(ClickedAction()) | |
} |
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 ExampleViewModel @Inject constructor( | |
... | |
) : BaseViewModel() { | |
init { | |
actionStream.filterTo(ClickedAction::class.java) | |
.subscribe(::clicked).track() | |
} | |
private fun clicked(clickedAction: clickedAction) { |
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
inline fun <T, reified E : T> Observable<in T>.filterTo( | |
@Suppress("UNUSED_PARAMETER") target: Class<E> | |
): Observable<out E> = this.filter { | |
when (it) { | |
is E -> true | |
else -> false | |
} | |
}.map { it as E } |