带有Dagger 2的Android Java ViewModel

时间:2018-07-04 08:49:21

标签: android mvvm viewmodel dagger-2 android-architecture-lifecycle

我正在尝试将Viewmodel与Dagger 2配合使用。
但是,在更改方向时,将重新创建ViewModel。我在SwipeRefreshLayout上得到NullPointerException featuredViewModel.getLoading.observe()
我的失败在哪里?

Dagger 2片段模块(贡献AndroidInjector)

    @Module(includes = {ApplicationModule.class, PicassoModule.class})
public class FeaturedProjectsModule {

    @Provides
    FeaturedViewModel provideMainViewModel(FeaturedProjectsFragment fragment, ViewModelProvider.Factory factory) {
        return ViewModelProviders.of(fragment, factory).get(FeaturedViewModel.class);
    }

    @Provides
    ViewModelProvider.Factory featuredProjectsViewModelProvider() {
        return new ViewModelFactory<>(new FeaturedViewModel());
    }

    @Provides
    FeaturedProjectsAdapter provideFeaturedProjectsAdapter(Picasso picasso) {
        return new FeaturedProjectsAdapter(picasso);
    }
}

ViewModel片段

   public class FeaturedViewModel extends ViewModel {

    private CompositeDisposable disposable;

    private final MutableLiveData<ProjectResponse> featuredProjects = new MutableLiveData<>();
    private final MutableLiveData<Boolean> loading = new MutableLiveData<>();
    private int pages = 1;
    private int page = 1;

    LiveData<ProjectResponse> getFeaturedProjects() {
        return featuredProjects;
    }

    LiveData<Boolean> getLoading() {
        return loading;
    }

    FeaturedViewModel() {
        disposable = new CompositeDisposable();
    }

    public void loadFeaturedProjects(PrefUtils prefUtils, RestRepository restRepository) {
        if(page <= pages) {
            disposable.add(restRepository.getFeaturedProjects(prefUtils.getSavedAuthData().getAccessToken(), page).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSingleObserver<ProjectResponse>() {
                        @Override
                        public void onSuccess(ProjectResponse value) {
                            page++;
                            pages = value.getMeta().getTotalPages();

                            featuredProjects.setValue(value);
                            loading.setValue(false);
                        }

                        @Override
                        public void onError(Throwable e) {
                            loading.setValue(false);
                        }
                    }));
        }
    }

    public void resetPage() {
        page = 1;
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (disposable != null) {
            disposable.clear();
            disposable = null;
        }
    }
}

片段

    public class FeaturedProjectsFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView) RecyclerView recyclerView;

    @Inject PrefUtils prefUtils;
    @Inject RestRepository restRepository;
    @Inject FeaturedProjectsAdapter adapter;
    @Inject FeaturedViewModel featuredViewModel;

    @Override
    protected int layoutRes() {
        return R.layout.fragment_featured_projects;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        swipeRefreshLayout.setColorSchemeColors(getBaseActivity().getResources().getColor(R.color.colorPrimaryText));
        swipeRefreshLayout.setOnRefreshListener(this);
        setupRecyclerView();

        featuredViewModel.getLoading().observe(getBaseActivity(), loading -> {
            Log.d(Constants.TAG, "REFRESH");
            swipeRefreshLayout.setRefreshing(loading == null ? false : loading);
        });

        featuredViewModel.getFeaturedProjects().observe(getBaseActivity(), response -> {
            if (response != null) {
                if(response.getMeta().getCurrentPage() == 1)
                    adapter.clearData();
                adapter.addAllData(response.getRecords());
            }
        });

        loadFeaturedProjects();
    }

    private void loadFeaturedProjects() {
        featuredViewModel.loadFeaturedProjects(prefUtils, restRepository);
    }

    @Override
    public void onRefresh() {
        featuredViewModel.resetPage();
        loadFeaturedProjects();
    }

    private void setupRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(!recyclerView.canScrollVertically(1)) {
                    loadFeaturedProjects();
                }
            }
        });
        recyclerView.setAdapter(adapter);
    }
}

编辑: 我正在那样注射

@Module
public abstract class MainFragmentBindingModule {


    @ContributesAndroidInjector(modules = {ApplicationModule.class, UtilsModule.class,
            RestServiceModule.class, PicassoModule.class, FeaturedProjectsModule.class})
    abstract FeaturedProjectsFragment provideFeaturedProjectsFragmentFactory();

}

谢谢..

0 个答案:

没有答案