From 3c12d7d461f33194ee46f28f392241c88a6cfcbf Mon Sep 17 00:00:00 2001 From: Matthias Meulien Date: Fri, 13 Oct 2023 22:34:30 +0200 Subject: [PATCH] Don't clear model in case of data refresh failure Refs: #57 --- NEWS.md | 3 +++ src/app.cc | 46 +++++++++++++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/NEWS.md b/NEWS.md index 4d8eb88..9c187cd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -18,6 +18,9 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Changed +- Don't clear model in case of data refresh failure + [#57](https://github.com/orontee/taranis/issues/57) + ### Removed ## [1.2.0] - 2023-10-09 diff --git a/src/app.cc b/src/app.cc index b7363a0..1726ae2 100644 --- a/src/app.cc +++ b/src/app.cc @@ -5,6 +5,7 @@ #include "events.h" #include "inkview.h" #include "l10n.h" +#include "model.h" #include "units.h" #include "util.h" @@ -286,27 +287,23 @@ void App::refresh_data(bool force) { ClearTimerByName(App::refresh_timer_name); - clear_model_weather_conditions(); - const auto units = Units{this->model}.to_string(); + bool failed = false; + Condition current_condition; + std::vector hourly_forecast; + std::vector daily_forecast; + std::vector alerts; this->service->set_location(this->model->location); try { this->service->fetch_data(currentLang(), units); - this->model->current_condition = this->service->get_current_condition(); - - this->model->hourly_forecast.clear(); - for (auto &forecast : this->service->get_hourly_forecast()) { - this->model->hourly_forecast.push_back(forecast); - } - - this->model->daily_forecast.clear(); - for (auto &forecast : this->service->get_daily_forecast()) { - this->model->daily_forecast.push_back(forecast); - } + current_condition = this->service->get_current_condition(); + hourly_forecast = this->service->get_hourly_forecast(); + daily_forecast = this->service->get_daily_forecast(); + alerts = this->service->get_alerts(); - this->model->alerts = this->service->get_alerts(); } catch (const ConnectionError &error) { + failed = true; BOOST_LOG_TRIVIAL(debug) << "Connection error while refreshing weather conditions " << error.what(); @@ -316,6 +313,7 @@ void App::refresh_data(bool force) { "connection."), App::error_dialog_delay); } catch (const RequestError &error) { + failed = true; BOOST_LOG_TRIVIAL(debug) << "Request error while refreshing weather conditions " << error.what(); Message( @@ -324,17 +322,31 @@ void App::refresh_data(bool force) { "connection."), App::error_dialog_delay); } catch (const ServiceError &error) { + failed = true; BOOST_LOG_TRIVIAL(debug) << "Service error while refreshing weather conditions " << error.what(); Message(ICON_WARNING, GetLangText("Service unavailable"), error.what(), App::error_dialog_delay); } + if (not failed or force) { + clear_model_weather_conditions(); + + // if model isn't cleared and a forced refresh has been required + // (user changes language or location), incoherent data will be + // shown! + } + if (not failed) { + this->model->current_condition = current_condition; + this->model->hourly_forecast = hourly_forecast; + this->model->daily_forecast = daily_forecast; + this->model->alerts = alerts; + + const auto event_handler = GetEventHandler(); + SendEvent(event_handler, EVT_CUSTOM, CustomEvent::model_updated, 0); + } SetHardTimer(App::refresh_timer_name, &taranis::App::refresh_data_maybe, App::refresh_period); - const auto event_handler = GetEventHandler(); - SendEvent(event_handler, EVT_CUSTOM, CustomEvent::model_updated, 0); - HideHourglass(); }