From: gu.martinm@gmail.com Date: Wed, 24 Sep 2014 17:40:51 +0000 (+0200) Subject: WeatherInformation: conditions after AsyncTask X-Git-Tag: weatherinformation-1.0~118 X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=10e626d93e7b182d3de334d33adbc46243e03731;p=AndroidWeatherInformation WeatherInformation: conditions after AsyncTask Before updating UI with data coming from AsyncTask through BroadcastReceiver we must verify the data are the same as the ones that triggered the AsyncTask I am trying to avoid this case: 1. Conditions trigger AsyncTask 1 2. Screen rotation (or any other thing that creates new instance of our Fragment) 3. Conditions trigger AsyncTask 2 4. AsyncTask 1 ends with success. Updates UI. 5. AsyncTask 2 ends with error. Show error message. <-------- PROBLEM!!!! If before updating UI we check the current values, we may avoid the error created by AsyncTask 2. --- diff --git a/src/de/example/exampletdd/fragment/current/CurrentFragment.java b/src/de/example/exampletdd/fragment/current/CurrentFragment.java index 68444c2..9b829f8 100644 --- a/src/de/example/exampletdd/fragment/current/CurrentFragment.java +++ b/src/de/example/exampletdd/fragment/current/CurrentFragment.java @@ -92,26 +92,34 @@ public class CurrentFragment extends Fragment { public void onResume() { super.onResume(); - + this.mReceiver = new BroadcastReceiver() { @Override public void onReceive(final Context context, final Intent intent) { final String action = intent.getAction(); if (action.equals("de.example.exampletdd.UPDATECURRENT")) { - final Current current = (Current) intent.getSerializableExtra("current"); + final Current currentRemote = (Current) intent.getSerializableExtra("current"); - if (current != null) { - CurrentFragment.this.updateUI(current); + if (currentRemote != null) { + // 1. Check conditions. They must be the same as the ones that triggered the AsyncTask. + final DatabaseQueries query = new DatabaseQueries(CurrentFragment.this.getActivity().getApplicationContext()); + final WeatherLocation weatherLocation = query.queryDataBase(); final WeatherInformationApplication application = - (WeatherInformationApplication) getActivity().getApplication(); - application.setCurrent(current); + (WeatherInformationApplication) CurrentFragment.this.getActivity().getApplication(); + final Current current = application.getCurrent(); + + if (current == null || !CurrentFragment.this.isDataFresh(weatherLocation.getLastCurrentUIUpdate())) { + // 2. Update UI. + CurrentFragment.this.updateUI(currentRemote); + + // 3. Update Data. + application.setCurrent(currentRemote); + weatherLocation.setLastCurrentUIUpdate(new Date()); + query.updateDataBase(weatherLocation); + } - final DatabaseQueries query = new DatabaseQueries(CurrentFragment.this.getActivity().getApplicationContext()); - final WeatherLocation weatherLocation = query.queryDataBase(); - weatherLocation.setLastCurrentUIUpdate(new Date()); - query.updateDataBase(weatherLocation); } else { // Empty UI and show error message CurrentFragment.this.clearUI(); diff --git a/src/de/example/exampletdd/fragment/overview/OverviewFragment.java b/src/de/example/exampletdd/fragment/overview/OverviewFragment.java index c5e5e53..907791d 100644 --- a/src/de/example/exampletdd/fragment/overview/OverviewFragment.java +++ b/src/de/example/exampletdd/fragment/overview/OverviewFragment.java @@ -92,22 +92,30 @@ public class OverviewFragment extends ListFragment { public void onReceive(final Context context, final Intent intent) { final String action = intent.getAction(); if (action.equals("de.example.exampletdd.UPDATEFORECAST")) { - final Forecast forecast = (Forecast) intent.getSerializableExtra("forecast"); + final Forecast forecastRemote = (Forecast) intent.getSerializableExtra("forecast"); - if (forecast != null) { - OverviewFragment.this.updateUI(forecast); + if (forecastRemote != null) { - final WeatherInformationApplication application = - (WeatherInformationApplication) getActivity().getApplication(); - application.setForecast(forecast); - - final DatabaseQueries query = new DatabaseQueries(OverviewFragment.this.getActivity().getApplicationContext()); + // 1. Check conditions. They must be the same as the ones that triggered the AsyncTask. + final DatabaseQueries query = new DatabaseQueries(OverviewFragment.this.getActivity().getApplicationContext()); final WeatherLocation weatherLocation = query.queryDataBase(); - weatherLocation.setLastForecastUIUpdate(new Date()); - query.updateDataBase(weatherLocation); + final WeatherInformationApplication application = + (WeatherInformationApplication) OverviewFragment.this.getActivity().getApplication(); + final Forecast forecast = application.getForecast(); + + if (forecast == null || !OverviewFragment.this.isDataFresh(weatherLocation.getLastForecastUIUpdate())) { + // 2. Update UI. + OverviewFragment.this.updateUI(forecastRemote); + + // 3. Update Data. + application.setForecast(forecastRemote); + weatherLocation.setLastForecastUIUpdate(new Date()); + query.updateDataBase(weatherLocation); + + // 4. Show list. + OverviewFragment.this.setListShownNoAnimation(true); + } - // Show list. - OverviewFragment.this.setListShownNoAnimation(true); } else { // Empty list and show error message (see setEmptyText in onCreate) OverviewFragment.this.setListAdapter(null);