我正在开发一个示例Android应用程序,我正在尝试实现一个演示者类,因为我遵循MVP模式。我的演示者实现在
之下public class WeatherForecastPresenter extends AsyncTask<Void, Void, WeatherForecast> {
private double latitude;
private double longitude;
private String address;
// class that makes sync OkHttp call
private WeatherForecastService weatherForecastService;
// interface that has callback methods
private WeatherForecastView weatherForecastView;
public WeatherForecastPresenter(WeatherForecastView weatherForecastView, double latitude, double longitude, String address) {
this.latitude = latitude;
this.longitude = longitude;
this.address = address;
this.weatherForecastView = weatherForecastView;
weatherForecastService = new WeatherForecastService();
}
@Override
protected void onPreExecute() {
weatherForecastView.toggleRefresh();
}
@Override
protected WeatherForecast doInBackground(Void... voids) {
// gets weather forecast data of given location
return weatherForecastService.getCurrentWeather(latitude, longitude);
}
@Override
protected void onPostExecute(WeatherForecast weatherForecast) {
weatherForecastView.toggleRefresh();
if (weatherForecast != null) {
weatherForecastView.updateUi(weatherForecast, address);
} else {
weatherForecastView.displayErrorDialog();
}
}
}
我正在寻找实现演示者类的最佳实践,我相信将AsyncTask
移动到单独的类并以更通用的方式实现它将是一种更好的方法,但我找不到合适的解决方案。
如果你能帮助我,我将不胜感激。