注入presenter子类来查看android

时间:2016-09-30 23:24:38

标签: android dependency-injection mvp

我正在使用MVP模式创建一个Android应用程序。对于依赖注入,我使用匕首2.我有一个抽象片段,实现"视图"界面,使其成为Model-View-Presenter中的视图。

public abstract class MediaDetailFragment extends Fragment implements DetailsMvpView {
private static final String RESOURCE = "resource";
private static final String POSTER_SIZE = "w342/";


@Inject DetailPresenter mDetailPresenter;
@Inject CreditPresenter mCreditPresenter;
@BindView(R.id.media_image_flipper) ControllableFlipper mImageFlipper;
@BindView(R.id.keyword_recyclerview) RecyclerView mKeywordRecyclerView;
@BindView(R.id.title_textview) TextView mTitleTextView;
@BindView(R.id.overview_content_textview) TextView mOverviewTextView;
@BindView(R.id.cast_recyclerview) CreditRecyclerView mCastRecyclerView;
@BindView(R.id.crew_recyclerview) CreditRecyclerView mCrewRecyclerView;
@BindView(R.id.empty_view) TextView mRecyclerviewEmpty;
@BindView(R.id.button_share) ImageButton mShare;
@BindView(R.id.button_trailers) ImageButton mTrailers;
@BindView(R.id.button_reviews) ImageButton mReviews;
@BindBool(R.bool.isTablet) boolean mIsTablet;

@OnClick(R.id.button_share) public void shareMedia(View view) {
    startActivity(new Intent(Intent.ACTION_SEND)
            .putExtra(Intent.EXTRA_TEXT, getActivity().getString(R.string.base_youtube_url)
                    + POSTER_SIZE + mMedia.posterPath() + "\n\n"
                    + mMedia.title() + "\n\n" + mMedia.overview())
            .setType("text/plain"));
}

@OnClick(R.id.button_trailers) public void requestTrailers(View view) {
    mDetailPresenter.loadMovies(mMedia.id());
}

@OnClick(R.id.button_reviews) public void requestReviews(View view) {
    if (NetworkUtil.isNetworkConnected(getContext())) {
        ((FragmentHandler) getActivity()).onReviewsRequested(mMedia);
    } else {
        ViewUtil.displayNoNetworkSnackbar(getActivity());
    }
}

private Media mMedia;
private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
private OnGlobalLayoutListener mListener;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mMedia = getArguments().getParcelable(RESOURCE);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.media_details_fragment, container, false);

    ((BaseActivity) getActivity()).activityComponent().inject(this);
    ButterKnife.bind(this, rootView);

    getActivity().getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);

    mTitleTextView.setText(mMedia.title());
    mOverviewTextView.setText(mMedia.overview());

    mKeywordRecyclerView.setAdapter(new KeywordAdapter(getContext()));
    mCastRecyclerView.setAdapter(R.layout.item_credit_normal, mRecyclerviewEmpty);
    mCrewRecyclerView.setAdapter(R.layout.item_credit_normal, mRecyclerviewEmpty);

    mListener = () -> rootView.post(() -> {
        if (mIsTablet) {
            mImageFlipper.getLayoutParams().height =
                    ViewUtil.setHeightForAspectRatio(rootView.getWidth(), ViewUtil.STANDARD);
        } else {
            mImageFlipper.getLayoutParams().height =
                    ViewUtil.setHeightForAspectRatio(mDisplayMetrics.widthPixels, ViewUtil.STANDARD);
        }
        rootView.getViewTreeObserver().removeOnGlobalLayoutListener(mListener);
    });
    rootView.getViewTreeObserver().addOnGlobalLayoutListener(mListener);

    return rootView;
}

@Override
public void onStart() {
    super.onStart();

    mDetailPresenter.attachView(this);
    mDetailPresenter.loadImages(mMedia.id());
    mDetailPresenter.loadKeywords(mMedia.id());

    mCreditPresenter.attachCastView(mCastRecyclerView);
    mCreditPresenter.attachCrewView(mCrewRecyclerView);
    mCreditPresenter.loadCredits(mMedia.id());
}

@Override
public void onStop() {
    mDetailPresenter.detachView();
    mCreditPresenter.detachCastView();
    mCreditPresenter.detachCrewView();
    super.onStop();
}

@Override
public void showImages(String image) {
    mImageFlipper.addImagePath(image);
}

@Override
public void showKeywords(List<String> keywords) {
    ((KeywordAdapter) mKeywordRecyclerView.getAdapter()).setKeywords(keywords);
}

@Override
public void showVideos(List<Video> videos) {
    TrailerDialogFragment.newInstance(videos).show(getFragmentManager(), null);
}

@Override
public void showError() {
    if (!NetworkUtil.isNetworkConnected(getContext())) {
        ViewUtil.displayNoNetworkSnackbar(getActivity());
    }
}
}

根据具体情况,我将使用此抽象视图的两个子类之一。正如你所看到的,我注入了一个&#34; DetailsPresenter&#34;进入视野。 DetailsPresenter也是一个抽象类,有两个子类(一个用于视图的每个子类)。

但是,如果视图是MovieDetailFragment,它应该有一个MovieDetailPresenter,如果它是ShowDetailFragment,它应该有一个ShowDetailPresenter。

我的问题是:我应该在哪里实现逻辑以检查它是什么类型的视图并提供正确的DetailsPresenter?我应该在Dagger模块中执行此操作(可能在返回演示者的方法中)吗?我应该在片段本身吗?

这是我的模块:

@Module
public class ConfigPersistentModule {

    @Provides
    DetailPresenter provideDetailPresenter(DataManager dataManager) {
        return new MovieDetailPresenter(dataManager);
    }
}

请帮帮我。

1 个答案:

答案 0 :(得分:1)

您必须在子类(MovieDetailFragment&amp; ShowDetailFragment)中注入您的演示者,并且在您的模块中,您需要提供TWO提供如下模块:

@Module
public class ConfigPersistentModule {

@Provides
MovieDetailFragment provideDetailPresenter(DataManager dataManager) {
    return new MovieDetailPresenter(dataManager);
}

@Provides
ShowDetailFragment provideDetailPresenter(DataManager dataManager) {
    return new ShowDetailFragment(dataManager);
}
}

然后在您的子课程中注入如下:

ShowDetailFragment中的

@Inject ShowDetailFragment mCreditPresenter;
在MovieDetailFragment中

@Inject MovieDetailFragment mCreditPresenter;

现在在你的父类(MediaDetailFragment)中放置了一个名为“getPresenter”的抽象函数,子函数必须覆盖它,如下所示:

public abstract class MediaDetailFragment extends Fragment implements DetailsMvpView {
   private mDetailPresenter;

   public abstract DetailPresenter getPresenter();

   public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      this.mDetailPresenter = getPresenter();
      ...
      ...
   }
   ...
   ...
}

现在每个实现MediaDetailFragment的类都必须告诉父亲他的演示者和父亲使用该演示者。所以在子类(MovieDetailFragment&amp; ShowDetailFragment)中写道:

@Override
public DetailPresenter getPresenter()
{
  return mCreditPresenter;
}