简体   繁体   English

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

[英]How to create UseCase for MVVM Clean architecture in android

RepositoryClass存储库类

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

}

WeatherViewStated WeatherViewStated

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) {

    }

}

This is my code i am trying to write code using mvvm clean architecture but i am unable to add code in GetUseCase and GetUseCaseImpl so that i can get MutableLiveDta success and failure state in viewmodel class so that i can use it in MainActvitiy please suggest me how to call and how to get data in View Model .这是我的代码,我正在尝试使用 mvvm clean 架构编写代码,但我无法在 GetUseCase 和 GetUseCaseImpl 中添加代码,以便我可以在 viewmodel 类中获得 MutableLiveDta 成功和失败状态,以便我可以在 MainActvitiy 中使用它,请建议我如何调用以及如何在 View Model 中获取数据。

Change UseCase更改用例

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

To

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

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

    }
}

ViewModel视图模型

 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()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM