[英]Android MVP: Best Practice
我正在研究一個示例Android應用程序,並且由於遵循MVP模式,因此我試圖實現Presenter類。 我的主持人實施如下
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();
}
}
}
我正在尋找實現presenter類的最佳實踐,我相信將AsyncTask
移到單獨的類並以更通用的方式實現將是更好的方法,但我找不到合適的解決方案。 如果您能幫助我,我將不勝感激。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.