java—我有我的存储库和视图模型类,在这些类中我操作了来自改型api的数据,这是正确的方法吗?

whitzsjs  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(239)

我知道如何从微调器获取数据,然后将数据从api传递到存储库,但是在我的例子中,如何在mvvm中正确地从存储库获取数据到viewmodel呢?在存储库中我有mutablelivedata,我想在视图模型中获取数据。我已经用我自己的方式做了这件事,但它是否正确有效?我只是想看看。
存储库类

public class DogsRepository {
public MutableLiveData<List<DogBreedModel>> mutableDogsBreedList = new MutableLiveData<>();
public MutableLiveData<Boolean> mutableDogLoadError = new MutableLiveData<>();
public MutableLiveData<Boolean> mutableLoading = new MutableLiveData<>();
private MutableLiveData<DogBreedModel> dogLiveData;
private DogsApiService dogsApiService;

public Single<List<DogBreedModel>> getApi() {
    dogsApiService = new DogsApiService();
    return dogsApiService.getDogs();
}

/在这里我得到了三个可变变量,要么是可变的,要么是活的/
视图模型类

public class ListViewModel extends AndroidViewModel {
DogsRepository dogsRepository = new DogsRepository();
private CompositeDisposable disposable = new CompositeDisposable();
public ListViewModel(@NonNull Application application) {
    super(application);
}
public MutableLiveData<List<DogBreedModel>> listMutableLiveData = dogsRepository.mutableDogsBreedList;
public MutableLiveData<Boolean> mutableLoading = dogsRepository.mutableLoading;
public MutableLiveData<Boolean> mutableDogLoadError = dogsRepository.mutableDogLoadError;

public void refresh() {
    fetchFromRemote();
}
private void fetchFromRemote() {
    mutableLoading.setValue(true);
    disposable.add(
            dogsRepository.getApi()
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableSingleObserver<List<DogBreedModel>>() {
                        @Override
                        public void onSuccess(List<DogBreedModel> dogBreedModels) {
                            listMutableLiveData.setValue(dogBreedModels);
                            mutableDogLoadError.setValue(false);
                            mutableLoading.setValue(false);
                        }
                        @Override
                        public void onError(Throwable e) {
                            mutableDogLoadError.setValue(true);
                            mutableLoading.setValue(false);
                            e.printStackTrace();
                        }
                    })
    );
}

@Override
protected void onCleared() {
    super.onCleared();
    disposable.clear();
}

然后通过可变类型方法将存储库可变数据引入viewmodel。这条路对吗?并保存以这种方式获取api。
apiservice类

public class DogsApiService {
private static final String BASE_URL = "https://raw.githubusercontent.com/";
private DogsApi api;

public DogsApiService() {
    api = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
            .create(DogsApi.class);
}

public Single<List<DogBreedModel>> getDogs(){
    return api.getDogs();
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题