我正在开发一个示例 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
到一个单独的类并以更通用的方式实现它会是一种更好的方法,但我找不到合适的解决方案。如果您能帮助我,我将不胜感激。