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();
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);