使用Mvvm方法处理分页库中的错误

时间:2019-05-30 23:33:32

标签: android mvvm error-handling android-paging-library

我刚开始使用分页库并且代码工作正常,但是我不知道如何处理进度条和Toast等错误消息,所以我的代码在那里 这是回购

public class SearchRepository extends PageKeyedDataSource<Integer, Doc> {
public static final int PAGE_SIZE = 5;
private static final int FIRST_PAGE = 1;
private static final String token = "1234";
private static final String TAG = "www";

public SearchRepository() {
}

@Override
public void loadInitial(@NonNull final LoadInitialParams<Integer> params, @NonNull final LoadInitialCallback<Integer, Doc> callback) {
    Log.e(TAG, "loadInitial: ");
    APIService apiService = RetroClass2.getAPIService();
    apiService.SeachPro(token, "name", "ascending", PAGE_SIZE, FIRST_PAGE, "")
            .enqueue(new Callback<Products>() {
                @Override
                public void onResponse(Call<Products> call, Response<Products> response) {
                    if (response.body() != null) {

                        callback.onResult(response.body().getResult().getDocs(), null, FIRST_PAGE + 1);
                    }
                }

                @Override
                public void onFailure(Call<Products> call, Throwable t) {

                }
            });
}

@Override
public void loadBefore(@NonNull final LoadParams<Integer> params, @NonNull final LoadCallback<Integer, Doc> callback) {
    Log.e(TAG, "loadBefore: ");
    APIService apiService = RetroClass2.getAPIService();
    apiService.SeachPro(token, "name", "ascending", PAGE_SIZE, params.key, "")
            .enqueue(new Callback<Products>() {
                @Override
                public void onResponse(Call<Products> call, Response<Products> response) {
                    if (response.body() != null) {
                        Integer key = (params.key > 1) ? params.key - 1 : null;
                        callback.onResult(response.body().getResult().getDocs(), key);

                    }
                }

                @Override
                public void onFailure(Call<Products> call, Throwable t) {
                }
            });
}

@Override
public void loadAfter(@NonNull final LoadParams<Integer> params, @NonNull final LoadCallback<Integer, Doc> callback) {
    Log.e(TAG, "loadAfter: ");
    APIService apiService = RetroClass2.getAPIService();
    apiService.SeachPro(token, "name", "ascending", PAGE_SIZE, params.key, "")
            .enqueue(new Callback<Products>() {
                @Override
                public void onResponse(Call<Products> call, Response<Products> response) {
                    if (response.body() != null) {

                        Integer key = response.body().getResult().getDocs().size() != 0 ? params.key + 1 : null;
                        callback.onResult(response.body().getResult().getDocs(), key);
                    }
                }

                @Override
                public void onFailure(Call<Products> call, Throwable t) {
                }
            });
}

这是我的工厂保留的数据

public class SearchRepoFactory extends DataSource.Factory {
private MutableLiveData<PageKeyedDataSource<Integer, Doc>> listMutableLiveData = new MutableLiveData<>();


@Override
public DataSource create() {
    SearchRepository searchRepository = new SearchRepository();
    listMutableLiveData.postValue(searchRepository);
    return searchRepository;
}

public MutableLiveData<PageKeyedDataSource<Integer, Doc>> getListMutableLiveData() {
    return listMutableLiveData;
   }
}

这是我的视图模型

public class SearchViewModel extends ViewModel {
public LiveData<PagedList<Doc>> listLiveData;
private LiveData<PageKeyedDataSource<Integer, Doc>> liveData;
private static final String TAG = "yyy";

public SearchViewModel() {

    SearchRepoFactory factory = new SearchRepoFactory();
    liveData = factory.getListMutableLiveData();

    PagedList.Config config = (new PagedList.Config.Builder()).setEnablePlaceholders(false).setPageSize(SearchRepository.PAGE_SIZE).build();

    listLiveData = (new LivePagedListBuilder(factory, config)).build();
    }
}

我的适配器工作正常,所以我将跳过适配器

和此活动

  recyclerView = findViewById(R.id.rv1);
    progressBar = findViewById(R.id.probar);
    final SearchAdapter adapter = new SearchAdapter(this);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    SearchViewModel searchViewModel = new SearchViewModel(this);

    searchViewModel.listLiveData.observe(this, new Observer<PagedList<Doc>>() {
        @Override
        public void onChanged(@Nullable PagedList<Doc> docs) {
            adapter.submitList(docs);
        }
    });
    recyclerView.setAdapter(adapter);
我会再说一遍,当没有错误时代码可以正常工作,但是我想处理错误,例如没有连接Toast和隐藏进度条,并且我不希望用户成为public static,我在SearchRepository中获得服务器响应,并且到UI的路很长,而且我知道我的班级命名并不完美:),那么有什么想法吗?

0 个答案:

没有答案