android - 如何在Android中为MVVM Clean架构创建用例

标签 android mvvm clean-architecture

存储库类

 public Observable<Weather> requestWeather (String location,String unit,String appId){

return weatherAPI.requestWeather(location, unit,appId);

 }

GetUseCase
public interface GetUseCase {

    void  execute(String location,String unit,String appId);
}

GetUseCaseImpl
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);
    }

}

WeatherView已声明
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);

}

WeatherViewModel
public class WeatherViewModel extends ViewModel {

    private MutableLiveData<Weather> mutableWeatherLiveData;

    private WeatherRepositorty weatherRepositorty;

    public void init(String location,String unit,String appID) {

    }

}

这是我的代码,我正在尝试使用mvvm clean体系结构编写代码,但是我无法在GetUseCase和GetUseCaseImpl中添加代码,以便可以在Viewmodel类中获取MutableLiveDta成功和失败状态,以便可以在MainActvitiy中使用它,请建议我调用以及如何在View Model中获取数据。

最佳答案

更改用例

public interface GetUseCase {
    void  execute(String location,String unit,String appId);
}


public interface GetUseCase {
    Observable<Weather>  execute(String location,String unit,String appId);
}

GetUseCaseImpl至
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)

    }
}

View 模型
 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/

相关文章:

java - 二进制 XML 文件行 # : Error inflating class <unknown> using an ArrayAdapter and AsyncTask

android - float 操作按钮不会引发 OnClick 事件

java - 将 dagger2 与 Retrofit 和 MVVM 结合使用

wpf - 如何在 XAML 中使元素引用 StaticResource Storyboard(而不是 Storyboard 引用元素)

android - Dagger 在单独的 gradle 模块中

android - 使用 RxJava 和 Retrofit 实现 Room

android - 当用户在水平回收器 View 上滑动时停止垂直移动

java - 阅读从 GMail 发送的邮件

silverlight - Caliburn Micro 中保护方法的强制重新评估

java - java对象中更新字段的正确方法(设计模式)