sockeqwe / mosby

A Model-View-Presenter / Model-View-Intent library for modern Android apps
http://hannesdorfmann.com/mosby/
Apache License 2.0
5.49k stars 841 forks source link

MVI: Help with intent() function and RxJava1 #216

Closed sevar83 closed 7 years ago

sevar83 commented 7 years ago

Hi, my project is still RxJava. I can't do the interop with the RxJava2 Observable parameter required by intent().

What I have:

private class RxInteropViewIntentBinder<V : MvpView, I>(val binder: (V) -> Observable<I>) : ViewIntentBinder<V, I> {
        override fun bind(v: V): io.reactivex.Observable<I> {
            return RxJavaInterop.toV2Observable(binder.invoke(v))
        }
    }

    override fun bindIntents() {

        val loadFeed: Observable<PartialStateChanges> = intent(RxInteropViewIntentBinder(TvFeedView::loadFeedIntent))
                .doOnNext { Timber.d("intent: load feed") }
                .flatMap {
                    feedInteractor    
// At this stage the Observable is ver.2 so I can't compose it with mine which are ver.1. 
// Somehow I need to convert it back to v.1 but I don't know how. RxJavaInterop has 
// a method named toV1Observable but it requires Producer. 
                 ... }
     }

Could someone help?

sevar83 commented 7 years ago

Well, now I made it compilable at least...

val loadFeed: Observable<PartialStateChanges> = RxJavaInterop.toV1Observable(intent(RxInteropViewIntentBinder(TvFeedView::loadFeedIntent))
                .toFlowable(BackpressureStrategy.DROP))

But don't know if it gonna work. I hope so. Otherwise I have to migrate the project to RxJava2.

sockeqwe commented 7 years ago

Isn't it much simpler to use RxJava Interop when creating the intent() method on View layer for example in your activity/fragment?

sevar83 commented 7 years ago

Not sure what do you mean exactly, but intent() is a method of MviBasePresenter and it returns v2 Observable. If I use Interop on the View layer, I should change my view interfaces to v2. Anyway, now it compiles, let's see if it works. crosses fingers

sockeqwe commented 7 years ago

yes, I meant that the View interface already returns a v2 Observable.

Anyway, converting my apps based on RxJava1 to RxJava2 was pretty straight forward. Basically it was a Replace in Path to replace

import rx

to

import io.reactivex

definitely an option to consider ...

sevar83 commented 7 years ago

I already tried with RxJava2 in the first place, but some libraries were RxJava1 only and I didn't know the existence of RxJavaInterop. I'm going to migrate as soon I stabilize it. Thanks.