1
\$\begingroup\$

I am working on a simple android app to learn MVP. I am using synchronous OkHttp with AsyncTask for retrieving data from a REST API.

Presenter

public class CurrentWeatherPresenterImpl implements CurrentWeatherContract.CurrentWeatherPresenter, LocationCallback {

    private CurrentWeatherContract.CurrentWeatherView currentWeatherView;
    private CurrentWeatherContract.Navigator navigator;
    private WeatherForecastTask weatherForecastTask;
    private LocationService locationService;

    public CurrentWeatherPresenterImpl(CurrentWeatherContract.CurrentWeatherView currentWeatherView, CurrentWeatherContract.Navigator navigator) {
        this.currentWeatherView = currentWeatherView;
        this.navigator = navigator;
        locationService = new LocationService(currentWeatherView.getContext(), this);
    }

    @Override
    public void connectToLocationService() {
        locationService.connect();
    }

    @Override
    public void disconnectFromLocationService() {
        locationService.disconnect();
    }

    @Override
    public void getLastLocation() {
        locationService.getLastLocation();
    }

    @Override
    public void cancelCurrentWeatherTask() {
        if (weatherForecastTask != null && weatherForecastTask.getStatus() == WeatherForecastTask.TASK_STATUS_RUNNING) {
            weatherForecastTask.cancel(true);
        }
    }

    @Override
    public void openHourlyWeatherActivity() {
        navigator.openHourlyWeatherActivity();
    }

    @Override
    public void openDailyWeatherActivity() {
        navigator.openDailyWeatherActivity();
    }

    @Override
    public void onLocationReceived(double latitude, double longitude) {
        Double[] locationParams = {latitude, longitude};
        weatherForecastTask = new WeatherForecastTask(currentWeatherView);
        weatherForecastTask.execute(locationParams);
    }
}

AsynTask

public class WeatherForecastTask extends AsyncTask<Double, Void, WeatherForecastResponse> {

    public static final Status TASK_STATUS_RUNNING = AsyncTask.Status.RUNNING;

    private CurrentWeatherContract.CurrentWeatherView currentWeatherView;
    private WeatherForecastService weatherForecastService;
    private AddressFinder addressFinder;

    public WeatherForecastTask(CurrentWeatherContract.CurrentWeatherView currentWeatherView) {
        this.currentWeatherView = currentWeatherView;
        weatherForecastService = new WeatherForecastService();
        addressFinder = new AddressFinder(currentWeatherView.getContext());
    }

    @Override
    protected void onPreExecute() {
        currentWeatherView.toggleRefresh();
    }

    @Override
    protected WeatherForecastResponse doInBackground(Double... locationParams) {
        double latitude, longitude;
        latitude = locationParams[0];
        longitude = locationParams[1];

        WeatherForecastResponse weatherForecastResponse = new WeatherForecastResponse();

        Forecast forecast = null;

        if (NetworkUtils.isNetworkAvailable(currentWeatherView.getContext())) {

            forecast = weatherForecastService.getWeatherForecastData(latitude, longitude);

            if (forecast != null) {
                weatherForecastResponse.setMessage(WeatherForecastResponse.SUCCESS_MSG);

                String address = addressFinder.getCompleteAddress(latitude, longitude);
                forecast.getCurrrentWeather().setAddress(address);
            } else {
                weatherForecastResponse.setMessage(WeatherForecastResponse.SERVICE_UNAVAILABLE_MSG);
            }

        } else {
            weatherForecastResponse.setMessage(WeatherForecastResponse.NO_NETWORK_CONNECTION_MSG);
        }

        weatherForecastResponse.setForecast(forecast);

        return weatherForecastResponse;
    }

    @Override
    protected void onPostExecute(WeatherForecastResponse weatherForecastResponse) {
        currentWeatherView.toggleRefresh();

        if (weatherForecastResponse.getMessage().equals(WeatherForecastResponse.SUCCESS_MSG)) {
            currentWeatherView.displayCurrentWeatherData(weatherForecastResponse.getForecast().getCurrrentWeather());
        } else if (weatherForecastResponse.getMessage().equals(WeatherForecastResponse.SERVICE_UNAVAILABLE_MSG)) {
            currentWeatherView.displayEmptyScreenWithMsg(R.string.service_error_txt);
        } else if (weatherForecastResponse.getMessage().equals(WeatherForecastResponse.NO_NETWORK_CONNECTION_MSG)) {
            currentWeatherView.displayEmptyScreenWithMsg(R.string.network_error_txt);
        }
    }

}

I have been reviewing a lot of examples of MVP but I feel kinda straitened now. My problem is, I do not feel good about this implementation. It makes me feel like I violate the Single Responsibility Principle and missing some points of the MVP approach. I would be appreciated if someone can suggest a better approach for how I can use AsyncTask more efficiently.

I do not want to use Retrofit, Dagger, RxJava, Otto etc. since first i want to learn some concepts from scratch.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.