0

I have a difficulties with MVP realization using Moxy library. I read Moxy's github pages and checked examples, but any solution doesn't helps me.

In MyFragment the callbacks of MyFragmentView methods not called, but in MyFragmentPresenter getViewState() returns not null.

I mean that

getViewState().showProgressDialog();
getViewState().setAccounts(accountsResponse);
getViewState().hideProgressDialog();

are called in MyPresenter, but in MyFragment

@Override
public void showProgressDialog() {
    // some code
}

@Override
public void hideProgressDialog() {
    // some code
}

@Override
public void setAccounts(AccountsResponse accounts) {
    // some code
}

doesn't called.

Please help, what's wrong?

My code below.

Gradle

compile 'com.arello-mobile:moxy:1.5.5'
compile 'com.arello-mobile:moxy-app-compat:1.5.5'
compile 'com.arello-mobile:moxy-android:1.5.5'
annotationProcessor 'com.arello-mobile:moxy-compiler:1.5.5'

MyFragmentView

@StateStrategyType(AddToEndSingleStrategy.class)
public interface MyFragmentView extends MvpView {

    void showProgressDialog();
    void hideProgressDialog();

    void showTextTestMessage(String s);
    void showCouldNotRetrieveAccounts();
}

MyFragmentPresenter

@PerFragment
@InjectViewState
public class MyFragmentPresenter extends MvpPresenter<MyFragmentView> {

private ApiService apiService;

@Inject
    public MyFragmentPresenter(ApiService apiService) {
        this.apiService = apiService;
    }

public void getAccounts() {
        getViewState().showProgressDialog();

        getCompositeDisposable().add(apiService.getAccounts()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(accountsResponse -> {
                    if (accountsResponse.err_code != 0) {
                        throw new LogicException(accountsResponse.message);
                    }

                    return accountsResponse;
                })
                .subscribe(
                        accountsResponse -> {
                           getViewState().setAccounts(accountsResponse);
                           getViewState().hideProgressDialog();
                        },
                        throwable -> {
                            getViewState().hideProgressDialog();
                            getViewState().showCouldNotRetrieveAccounts();
                        }
                )
        );
    }
}

MyFragment

public class MyFragment extends MvpAppCompatFragment implements MyFragmentView {

    @Inject
    @InjectPresenter
    public MyFragmentPresenter mPresenter;

    @ProvidePresenter
    public MyFragmentPresenter providePresenter() {
        return mPresenter;
    }

    @Override
    public void onActivityCreated (@Nullable Bundle savedInstanceState){
        ComponentManager.getInstance().getActivityComponent(getActivity()).inject((MyActivity) getActivity());
        super.onActivityCreated(savedInstanceState);

        mPresenter.getAccounts();
    }

    @Override
    public void showProgressDialog() {
        // some code
    }

    @Override
    public void hideProgressDialog() {
        // some code
    }

    @Override
    public void setAccounts(AccountsResponse accounts) {
        // some code
    }

    @Override
    public void showCouldNotRetrieveBankAccounts() {
        // some code
    }
}

**MyFragmentPagerAdapter **

public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {

    @Override
    public Fragment getItem (int position){
        switch (position) {
            case 0:
                //
            case 1:
                //
            case 2:
                return new MyFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

}
Roman
  • 1
  • 2
  • All looks right. Method `getViewState()` doesn't depends on any DI. It is initialized in base presenter constructor. Feel free to debug `MvpPresenter`. Also you can see generated code and check that `ViewState` was generated. – senneco Aug 29 '18 at 03:02
  • @senneco Thank you! I'll check and commented here after that. When i did debug i faced that getViewState in Activities returns the same View during switching screens, but for MyFragment it differs - getViewState returns always different Views. – Roman Aug 29 '18 at 16:44
  • @senneco >> _and check that ViewState was generated_ << What i should looking for? – Roman Aug 29 '18 at 16:49
  • _What i should looking for?_ just find realization of your view interface by AndroidStudio tooling. Android Studio should show you generated code as view implementation. – senneco Aug 30 '18 at 10:27
  • _When i did debug i faced that getViewState in Activities returns the same View during switching screens, but for MyFragment it differs - getViewState returns always different Views._ But you told that getViewState is null? – senneco Aug 30 '18 at 10:28
  • @senneco oh, no. I told that getViewState always returns not null value and i haven't NPE on getViewState.someMethod() in MyPresenter – Roman Aug 30 '18 at 10:58
  • well, everything looks right. You also can try to run github sample and than try to change the example to your code. Also you can publish all your project, then I will look at that. – senneco Aug 31 '18 at 11:16

1 Answers1

0

I think something went wrong with connecting dagger with moxy. Check the following issue on GitHub: https://github.com/Arello-Mobile/Moxy/issues/100

Guys solve the same problem there

Vova
  • 956
  • 8
  • 22