marcinmoskala
02/10/2018, 5:38 AMsubscriptions += repository.getQuests()
.applySchedulers()
.smartSubscribe(
onSuccess = view::showList and this::updateUpTrackingList,
onError = view::handleError,
onFinish = view::hideLoader
)
Which is, as you can guess, RxJava usage. On success I use both show list on view instance (this is in presenter) and I update tracking list:
private fun updateUpTrackingList(quests: List<Quest>) {
AppDatabase.addAppsToInstall(*quests.map { it.pkg }.toTypedArray())
}
and
is obviously commonly used function in my projects:
infix fun <T> ((T) -> Unit).and(f: (T) -> Unit): (T) -> Unit = { this(it); f(it) }
I had to add buffering, but I wanted to keep nice descriptive form of functions passing. So I made following function:
private var previousList: List<Quest>? = null
private fun ifChanged(onListChanged: (List<Quest>) -> Unit) = fun(list: List<Quest>) {
if(previousList != list) {
previousList = list
onListChanged(list)
}
}
And I use it this way:
subscriptions += repository.getQuests()
.applySchedulers()
.smartSubscribe(
onSuccess = ifChanged(view::showList and this::updateUpTrackingList),
onError = view::handleError,
onFinish = view::hideLoader
)
For me it looks great but I am not sure if it is not too complex for other people. What do you think? @mg6maciej? Functional programming passionates? Everyone?
PS. If I would have function delegate then I could even extract this buffering as a common patter (my proposition for this functionality is waiting on KEEP)dave08
02/11/2018, 3:21 AMmarcinmoskala
02/12/2018, 5:14 AMdave08
02/12/2018, 7:43 AMmarcinmoskala
02/12/2018, 2:35 PMdave08
02/12/2018, 2:36 PMdave08
02/12/2018, 2:46 PMmarcinmoskala
02/12/2018, 3:29 PM