作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
存储库类
public Observable<Weather> requestWeather (String location,String unit,String appId){
return weatherAPI.requestWeather(location, unit,appId);
}
public interface GetUseCase {
void execute(String location,String unit,String appId);
}
public class GetUseCaseImpl implements GetUseCase {
private WeatherRepositorty weatherRepositorty;
private CompositeDisposable disposable = new CompositeDisposable();
private MutableLiveData<WeatherViewStated> mutableLiveData = new MutableLiveData<>();
public GetUseCaseImpl() {
weatherRepositorty = WeatherRepositorty.getInstance();
}
@Override
public void execute(String location, String unit, String appId) {
disposable.add(weatherRepositorty.requestWeather(location, unit, appId).subscribeOn(Schedulers.io()).doOnSubscribe((newsList) -> onLoading())
.subscribe(this::onSuccess,
this::onError));
}
private void onSuccess(Weather weather) {
WeatherViewStated.SUCCESS_STATE.setData(weather);
mutableLiveData.postValue(WeatherViewStated.SUCCESS_STATE);
}
private void onError(Throwable error) {
WeatherViewStated.ERROR_STATE.setError(error);
mutableLiveData.postValue(WeatherViewStated.ERROR_STATE);
}
private void onLoading() {
mutableLiveData.postValue(WeatherViewStated.LOADING_STATE);
}
}
public class WeatherViewStated extends WeatherViewState<Weather> {
private WeatherViewStated(Weather data, int currentState, Throwable error) {
this.data = data;
this.error = error;
this.currentState = currentState;
}
public static WeatherViewStated ERROR_STATE = new WeatherViewStated(null, WeatherViewState.State.FAILED.value, new Throwable());
public static WeatherViewStated LOADING_STATE = new WeatherViewStated(null, State.LOADING.value, null);
public static WeatherViewStated SUCCESS_STATE = new WeatherViewStated(new Weather(), State.SUCCESS.value, null);
}
public class WeatherViewModel extends ViewModel {
private MutableLiveData<Weather> mutableWeatherLiveData;
private WeatherRepositorty weatherRepositorty;
public void init(String location,String unit,String appID) {
}
}
最佳答案
更改用例
public interface GetUseCase {
void execute(String location,String unit,String appId);
}
public interface GetUseCase {
Observable<Weather> execute(String location,String unit,String appId);
}
public class GetUseCaseImpl implements GetUseCase {
private WeatherRepositorty weatherRepositorty;
public GetUseCaseImpl() {
weatherRepositorty = WeatherRepositorty.getInstance();
}
@Override
public Observable<Weather> execute(String location, String unit, String appId) {
return weatherRepositorty.requestWeather(location, unit, appId)
}
}
public class WeatherViewModel extends ViewModel {
private CompositeDisposable disposable = new CompositeDisposable();
private MutableLiveData<WeatherViewStated> mutableLiveData = new
MutableLiveData<>();
private GetUseCaseImpl useCaseImpl = new GetUseCaseImpl()
public void init(String location,String unit,String appID) {
disposable.add(useCaseImpl.execute(location, unit, appId).subscribeOn(Schedulers.io()).doOnSubscribe((newsList) -> onLoading())
.subscribe(this::onSuccess,
this::onError));
}
private void onSuccess(Weather weather) {
WeatherViewStated.SUCCESS_STATE.setData(weather);
mutableLiveData.postValue(WeatherViewStated.SUCCESS_STATE);
}
private void onError(Throwable error) {
WeatherViewStated.ERROR_STATE.setError(error);
mutableLiveData.postValue(WeatherViewStated.ERROR_STATE);
}
private void onLoading() {
mutableLiveData.postValue(WeatherViewStated.LOADING_STATE);
}
override fun onCleared() {
disposable.clear()
}
}
关于android - 如何在Android中为MVVM Clean架构创建用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60220564/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!