Skip to content

Commit

Permalink
Don't clear model in case of data refresh failure
Browse files Browse the repository at this point in the history
Refs: #57
  • Loading branch information
orontee committed Oct 13, 2023
1 parent 8a0e757 commit 3c12d7d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 29 additions & 17 deletions src/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "events.h"
#include "inkview.h"
#include "l10n.h"
#include "model.h"
#include "units.h"
#include "util.h"

Expand Down Expand Up @@ -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<Condition> hourly_forecast;
std::vector<DailyCondition> daily_forecast;
std::vector<Alert> 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();
Expand All @@ -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(
Expand All @@ -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();
}

Expand Down

0 comments on commit 3c12d7d

Please sign in to comment.