Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

How to use sendToView ? #57

Closed
AruLNadhaN opened this issue Jan 9, 2017 · 19 comments
Closed

How to use sendToView ? #57

AruLNadhaN opened this issue Jan 9, 2017 · 19 comments
Labels

Comments

@AruLNadhaN
Copy link

AruLNadhaN commented Jan 9, 2017

I'm trying to call a method in my View through sendToView. But it is not working ?
I have tried using getView. It returns NPE because i'm calling the getProductsByCat after the Presenter onAttach

public void getProductsByCat(String Category) {
//        final BrowseView view = getView();
        this.sendToView(BrowseView::showProgress);

        rxHelper.manageSubscription(
                RxFirebaseDatabase.observeValueEvent(mRef,
                        dataSnapshot -> {
                            List<Product> mProd = new ArrayList<>();
                            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                                Timber.d("Count >>" + postSnapshot.getChildrenCount());
                                Product Prod = Product.create(postSnapshot);
                                mProd.add(Prod);
                            }
                            return mProd;
                        })
                        .subscribe(mProd -> {
                            Timber.d(mProd.get(0).productName());
                            sendToView(view -> {
                                view.hideProgress();
                                view.showText("Hello");
                                view.updateItems(mProd);
                            });
                        })
        );
    }
@StefMa
Copy link
Contributor

StefMa commented Jan 9, 2017

Hey,
what are you trying?
sendToView wait's until the view got attached.
You can use it in your onCreate (for example):
sendToView(view -> view.showProgress());

getView() can cause a NPE when your view isn't attached yet to the TiPresenter.

i'm calling the getProductsByCat after the Presenter onAttach

When do you call this method?

@StefMa StefMa added the question label Jan 9, 2017
@AruLNadhaN
Copy link
Author

I'm trying to load Items into my Recyclerview when the loading is completed in my Presenter. How can i Call updateItems from my Presenter ?

The onViewReady is called in the Oncreate after setContentView

public class BrowseActivity extends BaseActivity implements BrowseView, BrowseAdapter.ClickListener{

    @Inject
    DataManager mDataManager;
    @Inject
    BrowsePresenter mBrowsePresenter;

    BrowseAdapter mBrowseAdapter;
    @BindView(R.id.bmb)
    BoomMenuButton bmb;

 private final TiActivityPlugin<BrowsePresenter, BrowseView> mPresenterPlugin = new TiActivityPlugin<BrowsePresenter, BrowseView>(
            () -> new BrowsePresenter(mDataManager));

    public BrowseActivity() {
        addPlugin(mPresenterPlugin);
    }

    @Override
    protected void onViewReady(Bundle savedInstanceState, Intent intent) {
        super.onViewReady(savedInstanceState, intent);
        setupViews();
        assert bmb != null;
        bmb.setButtonEnum(ButtonEnum.TextInsideCircle);
        bmb.setPiecePlaceEnum(PiecePlaceEnum.DOT_9_1);
        bmb.setButtonPlaceEnum(ButtonPlaceEnum.SC_9_2);
        TextInsideCircleButton.Builder builder = new TextInsideCircleButton.Builder()
                        .normalImageRes(R.drawable.cat_beverages)
                        .normalText("Beverages")
                        .normalColorRes(R.color.orange)
                        .listener(new OnBMClickListener() {
                                @Override
                            public void onBoomButtonClick(int index) {
                                mBrowsePresenter.getProductsByCat(MYConstants.CAT_BEVERAGES);
                            }
                        });
                bmb.addBuilder(builder);
            }
}

 @Override
    public void updateItems(List<Product> items) {
        mBrowseAdapter.setNewData(items);
        mBrowseAdapter.notifyDataSetChanged();
        Timber.d("Adapter Count" + items.get(0).productName() + mBrowseAdapter.getData().size());
    }

@passsy
Copy link
Contributor

passsy commented Jan 9, 2017

Can you confirm onNext is called?

                    .subscribe(mProd -> {
                            Timber.d(mProd.get(0).productName());
                            sendToView(view -> {
                                view.hideProgress();
                                view.showText("Hello");
                                view.updateItems(mProd);
                            });
                        })

Either you are never calling getProductsByCat(category) or this Observable completes before it emits an item.

@AruLNadhaN
Copy link
Author

I can confirm that the onNext is called. I have also logged all the items from mProd. It is working as expected!

@passsy
Copy link
Contributor

passsy commented Jan 9, 2017

And your Activity is in foreground? Then the sendToView lambda should be called.

@AruLNadhaN
Copy link
Author

Yeah. The Activity is in foreground. I even add a log statement in sendToView. It is also not executed :(

@passsy
Copy link
Contributor

passsy commented Jan 9, 2017

When you say getView() is null then the view is not attached. Either onAttachView(view) isn't called or onDetachView() is called already.
Add a TiLogger and post the output

TiLog.setLogger(TiLog.LOGCAT);

@AruLNadhaN
Copy link
Author

AruLNadhaN commented Jan 9, 2017

Instead of calling getProductsByCat(category) from Activity. I moved it to onAttachView & used getView() instead of sendToView. It works perfectly.
But Calling from Activity doesn't triggers getView or sendToView

This Log is called while the App is in foreground
01-09 22:55:15.176 31529-31529/? V/ThirtyInch: BrowsePresenter:TiPresenter@3ae18bb: deprecated onWakeUp()

These Logs are called when i Press home & comeback

01-09 22:58:28.056 31529-31529/? V/ThirtyInch: BrowsePresenter:TiPresenter@3ae18bb: deprecated onSleep()
01-09 22:58:28.056 31529-31529/? V/ThirtyInch: BrowsePresenter:TiPresenter@3ae18bb: onDetachView()
01-09 22:58:32.945 31529-31529/? V/ThirtyInch: TiActivityPlugin:TiActivity@1a4f10db:BrowseActivity@2f2f1778: binding the cached view to Presenter me.arulnad.activities.BrowseActivity@2f2f1778
01-09 22:58:32.945 31529-31529/? V/ThirtyInch: BrowsePresenter:TiPresenter@3ae18bb: onAttachView(TiView)
01-09 22:58:32.946 31529-31529/? V/ThirtyInch: BrowsePresenter:TiPresenter@3ae18bb: deprecated onWakeUp()

@AruLNadhaN
Copy link
Author

If i use manageViewSubscription instead of manageSubscription . I get the below exception!

java.lang.IllegalStateException: view subscriptions can't be handled when there is no view at net.grandcentrix.thirtyinch.rx.RxTiPresenterSubscriptionHandler.manageViewSubscription(RxTiPresenterSubscriptionHandler.java:80) at me.arulnadhan.grocerycorecommon.ui.browse.BrowsePresenter.getProductsByCat(BrowsePresenter.java:59)

@StefMa
Copy link
Contributor

StefMa commented Jan 10, 2017

Can you please show your presenter code, when you call your getProductsByCat()❓❗️

Normally you have to use manageViewSubscription because you will destroy the subscription after your view got detached. That is the right behaviour.
It seems, however why, that the View don't get attached to the Presenter 🤔

@StefMa
Copy link
Contributor

StefMa commented Jan 10, 2017

Instead of calling getProductsByCat(category) from Activity. I moved it to onAttachView & used getView() instead of sendToView. It works perfectly.
But Calling from Activity doesn't triggers getView or sendToView

So, wait what?!
getProductsByCat() is business logic and have nothing todo inside your Activity.
So they right way is to have it inside the Presenter!

@AruLNadhaN
Copy link
Author

AruLNadhaN commented Jan 10, 2017

When a Button is clicked on my Activity, I should change the products according to the category. So i should pass the Category ID to the Presenter in my Activity.
So i can't call getProductsByCat() from my Presenter!

Please take a look at my gist https://gist.github.com/AruLNadhaN/dc023c5854dab5000b73e800ae96fc70#file-browseactivity-java-L34

@StefMa
Copy link
Contributor

StefMa commented Jan 10, 2017

I'm I wrong or do you use two different Presenters? ;)
First presenter
Second One

You attach the First one to your Activity but call the getProductsByCat() to the second one. And the second one don't get attached to the Activity... 🤔 Right?

@StefMa
Copy link
Contributor

StefMa commented Jan 10, 2017

I've created a short test and everything worked like expected.
-> https://gist.github.com/anonymous/111e6c4c50548503f46868731fb08d9f

@passsy
Copy link
Contributor

passsy commented Jan 10, 2017

You inject one Presenter and create another one in your Activity. Don't inject the Presenter and only use the one created from the plugin.

@passsy passsy closed this as completed Jan 10, 2017
@AruLNadhaN
Copy link
Author

Solved it! The problem is that i'm injecting another instance of presenter in my activity.

@AruLNadhaN
Copy link
Author

@StefMa @passsy I recently changed my view code from Activity to Fragment. After the change, the String values are not retained. They always return NULL.

The string Email returns the value here correctly. But when it is called from the Fragment. It always returns NULL

@passsy
Copy link
Contributor

passsy commented Jan 14, 2017

Please ask those questions on Stackoverflow where you can get quick help from a bigger audience for your beginner questions.
Open issues here when you find bugs in the library, not bugs in your code.

@AruLNadhaN
Copy link
Author

sorry. I thought that the values are not retained due to the library. Because it was working fine. When i was calling the presenter from the Activity.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants