Как создать UseCase для MVVM Clean архитектуры в android - PullRequest
0 голосов
/ 14 февраля 2020

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

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, но я Невозможно добавить код в GetUseCase и GetUseCaseImpl, чтобы я мог получить состояние успеха и неудачи MutableLiveDta в классе viewmodel, чтобы я мог использовать его в MainActvitiy. Пожалуйста, предложите мне, как вызвать и как получить данные в View Model.

1 Ответ

3 голосов
/ 14 февраля 2020

Изменить UseCase

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)

    }
}

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()
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...