Skip to content

Commit

Permalink
Bugfix: quarterHourly data not deleted when city is deleted
Browse files Browse the repository at this point in the history
Database: replace forecasts instead of calling delete and then add
  • Loading branch information
woheller69 committed Nov 24, 2023
1 parent 92efd04 commit 7a3d711
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ public void onLocationChanged(android.location.Location location) {
db.deleteWeekForecastsByCityId(getWidgetCityID(context));
db.deleteCurrentWeatherByCityId(getWidgetCityID(context));
db.deleteForecastsByCityId(getWidgetCityID(context));
db.deleteQuarterHourlyForecastsByCityId(getWidgetCityID(context));
pagerAdapter.loadCities();
viewPager2.setAdapter(pagerAdapter);
tabLayout.getTabAt(0).setText(city.getCityName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ public synchronized void deleteCityToWatch(CityToWatch cityToWatch) {
deleteCurrentWeatherByCityId(cityToWatch.getCityId());
deleteForecastsByCityId(cityToWatch.getCityId());
deleteWeekForecastsByCityId(cityToWatch.getCityId());
deleteQuarterHourlyForecastsByCityId(cityToWatch.getCityId());

//Now remove city from CITIES_TO_WATCH
SQLiteDatabase database = this.getWritableDatabase();
Expand Down Expand Up @@ -374,8 +375,10 @@ public synchronized boolean hasQuarterHourly(int cityId) {
}


public synchronized void addQuarterHourlyForecasts(List<QuarterHourlyForecast> quarterHourlyForecasts) {
public synchronized void replaceQuarterHourlyForecasts(List<QuarterHourlyForecast> quarterHourlyForecasts) {
SQLiteDatabase database = this.getWritableDatabase();
database.delete(TABLE_QUARTERHOURLYFORECAST, QUARTERHOURLYFORECAST_CITY_ID + " = ?",
new String[]{Integer.toString(quarterHourlyForecasts.get(0).getCity_id())});
for (QuarterHourlyForecast quarterHourlyForecast: quarterHourlyForecasts) {
ContentValues values = new ContentValues();
values.put(QUARTERHOURLYFORECAST_CITY_ID, quarterHourlyForecast.getCity_id());
Expand Down Expand Up @@ -442,8 +445,10 @@ public synchronized List<QuarterHourlyForecast> getQuarterHourlyForecastsByCityI
/**
* Methods for TABLE_FORECAST
*/
public synchronized void addForecasts(List<HourlyForecast> hourlyForecasts) {
public synchronized void replaceForecasts(List<HourlyForecast> hourlyForecasts) {
SQLiteDatabase database = this.getWritableDatabase();
database.delete(TABLE_HOURLY_FORECAST, FORECAST_CITY_ID + " = ?",
new String[]{Integer.toString(hourlyForecasts.get(0).getCity_id())});
for (HourlyForecast hourlyForecast: hourlyForecasts) {
ContentValues values = new ContentValues();
values.put(FORECAST_CITY_ID, hourlyForecast.getCity_id());
Expand Down Expand Up @@ -517,8 +522,10 @@ public synchronized List<HourlyForecast> getForecastsByCityId(int cityId) {
/**
* Methods for TABLE_WEEKFORECAST
*/
public synchronized void addWeekForecasts(List<WeekForecast> weekForecasts) {
public synchronized void replaceWeekForecasts(List<WeekForecast> weekForecasts) {
SQLiteDatabase database = this.getWritableDatabase();
database.delete(TABLE_WEEKFORECAST, WEEKFORECAST_CITY_ID + " = ?",
new String[]{Integer.toString(weekForecasts.get(0).getCity_id())});
for (WeekForecast weekForecast: weekForecasts) {
ContentValues values = new ContentValues();
values.put(WEEKFORECAST_CITY_ID, weekForecast.getCity_id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@ public void onBindViewHolder(ViewHolder viewHolder, final int position) {
holder.temperature.setText(StringFormatUtils.formatTemperature(context, next.getTemperature()));
}

} else if (viewHolder.getItemViewType() == DETAILS) { //not used at the moment
} else if (viewHolder.getItemViewType() == DETAILS) {

/* This viewHolder is not used at the moment in omWeather
DetailViewHolder holder = (DetailViewHolder) viewHolder;
Expand All @@ -324,7 +326,7 @@ public void onBindViewHolder(ViewHolder viewHolder, final int position) {
holder.rain60min.setText(R.string.error_no_rain60min_data);
}
holder.rain60minLegend.setText("( "+context.getResources().getString(R.string.units_mm_h)+String.format(Locale.getDefault(),": □ %.1f ▤ <%.1f ▦ <%.1f ■ >=%.1f )",0.0,0.5,2.5,2.5));

*/
} else if (viewHolder.getItemViewType() == WEEK) {

final WeekViewHolder holder = (WeekViewHolder) viewHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public void processSuccessScenario(String response, int cityId) {
JSONObject json = new JSONObject(response);

//Extract daily weather
dbHelper.deleteWeekForecastsByCityId(cityId);
List<WeekForecast> weekforecasts = new ArrayList<>();
weekforecasts = extractor.extractWeekForecast(json.getString("daily"));

Expand All @@ -99,6 +98,7 @@ public void processSuccessScenario(String response, int cityId) {
final String ERROR_MSG = context.getResources().getString(R.string.error_convert_to_json);
if (NavigationActivity.isVisible)
Toast.makeText(context, ERROR_MSG, Toast.LENGTH_LONG).show();
return;
} else {
weatherData.setCity_id(cityId);
weatherData.setRain60min(rain60min);
Expand All @@ -115,7 +115,6 @@ public void processSuccessScenario(String response, int cityId) {


//Extract hourly weather
dbHelper.deleteForecastsByCityId(cityId);
List<HourlyForecast> hourlyforecasts = new ArrayList<>();
hourlyforecasts = extractor.extractHourlyForecast(json.getString("hourly"));

Expand All @@ -129,17 +128,16 @@ public void processSuccessScenario(String response, int cityId) {
Toast.makeText(context, ERROR_MSG, Toast.LENGTH_LONG).show();
return;
}
dbHelper.addForecasts(hourlyforecasts);
dbHelper.replaceForecasts(hourlyforecasts);

SharedPreferences prefManager = PreferenceManager.getDefaultSharedPreferences(context);
if (prefManager.getBoolean("pref_weekIDs", false)){
weekforecasts = reanalyzeWeekIDs(weekforecasts, hourlyforecasts);
}
dbHelper.addWeekForecasts(weekforecasts);
dbHelper.replaceWeekForecasts(weekforecasts);


//Extract quarter-hourly weather
dbHelper.deleteQuarterHourlyForecastsByCityId(cityId);
if (json.has("minutely_15")){
List<QuarterHourlyForecast> quarterHourlyForecasts = new ArrayList<>();
quarterHourlyForecasts = extractor.extractQuarterHourlyForecast(json.getString("minutely_15"));
Expand All @@ -148,14 +146,13 @@ public void processSuccessScenario(String response, int cityId) {
for (QuarterHourlyForecast quarterHourlyForecast: quarterHourlyForecasts){
quarterHourlyForecast.setCity_id(cityId);
}

} else {
final String ERROR_MSG = context.getResources().getString(R.string.error_convert_to_json);
if (NavigationActivity.isVisible)
Toast.makeText(context, ERROR_MSG, Toast.LENGTH_LONG).show();
return;
}
dbHelper.addQuarterHourlyForecasts(quarterHourlyForecasts);
dbHelper.replaceQuarterHourlyForecasts(quarterHourlyForecasts);
}

possiblyUpdateWidgets(cityId, weatherData, weekforecasts, hourlyforecasts);
Expand Down

0 comments on commit 7a3d711

Please sign in to comment.