0

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

    }

}

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 .

1 Answer 1

3

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

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()
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

I am getting java.lang.IllegalArgumentException: Unable to create call adapter for io.reactivex.Observable in RepositoryClass please suggest me
could you please help to convert given code in mvvm clean architecure?
Ok wait i am giving git repo this small only one get API i am struggling from 3 days to create mvvm clean architecture
Okay.. Let me check give access to droiddevgeeks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.