diff --git a/src/app.cc b/src/app.cc index 3719155..827e5e3 100644 --- a/src/app.cc +++ b/src/app.cc @@ -116,6 +116,10 @@ void App::load_config(bool force_data_refresh) { Config config; + const auto preserve_offline_from_config = + config.read_bool("preserve_offline", false); + this->model->preserve_offline = preserve_offline_from_config; + const auto api_key_from_config = config.read_string("api_key", ""); const auto is_api_key_obsolete = (not api_key_from_config.empty() and @@ -156,7 +160,8 @@ void App::load_config(bool force_data_refresh) { const auto event_handler = GetEventHandler(); if (is_data_obsolete) { - SendEvent(event_handler, EVT_CUSTOM, CustomEvent::refresh_data, 0); + SendEvent(event_handler, EVT_CUSTOM, CustomEvent::refresh_data, + CustomEventParam::triggered_by_configuration_change); } else if (is_display_daily_forecast_obsolete) { SendEvent(event_handler, EVT_CUSTOM, CustomEvent::model_daily_forecast_display_changed, 0); @@ -186,7 +191,10 @@ int App::handle_custom_event(int param_one, int param_two) { return 1; } } else if (param_one == CustomEvent::refresh_data) { - this->refresh_data(); + const bool force_refresh = (param_two == CustomEventParam::triggered_by_configuration_change or + param_two == CustomEventParam::triggered_by_user or + param_two == CustomEventParam::triggered_by_configuration_change); + this->refresh_data(force_refresh); return 1; } else if (param_one == CustomEvent::select_location_from_history) { const size_t history_index = param_two; @@ -252,9 +260,26 @@ void App::clear_model_weather_conditions() { this->model->refresh_date = 0; } -void App::refresh_data() { +bool App::must_skip_data_refresh() const { + const auto* const netinfo = NetInfo(); + if (netinfo == nullptr or not netinfo->connected) { + BOOST_LOG_TRIVIAL(debug) << "Device is offline"; + + if (this->model->preserve_offline) { + BOOST_LOG_TRIVIAL(info) + << "Won't refresh data since configured to preserve offline"; + return true; + } + } + return false; +} + +void App::refresh_data(bool force) { BOOST_LOG_TRIVIAL(debug) << "Refreshing data"; + if (not force and this->must_skip_data_refresh()) { + return; + } ShowHourglassForce(); ClearTimerByName(App::refresh_timer_name); @@ -372,7 +397,8 @@ void App::set_model_location(const Location &location) const { this->model->location = location; const auto event_handler = GetEventHandler(); - SendEvent(event_handler, EVT_CUSTOM, CustomEvent::refresh_data, 0); + SendEvent(event_handler, EVT_CUSTOM, CustomEvent::refresh_data, + CustomEventParam::triggered_by_model_change); } void App::update_configured_unit_system(UnitSystem unit_system) { @@ -416,7 +442,8 @@ void App::refresh_data_maybe() { return; } const auto event_handler = GetEventHandler(); - SendEvent(event_handler, EVT_CUSTOM, CustomEvent::refresh_data, 0); + SendEvent(event_handler, EVT_CUSTOM, CustomEvent::refresh_data, + CustomEventParam::triggered_by_timer); } void App::handle_about_dialog_button_clicked(int button_index) { diff --git a/src/app.h b/src/app.h index 3114db1..ac7289f 100644 --- a/src/app.h +++ b/src/app.h @@ -62,7 +62,9 @@ class App { void clear_model_weather_conditions(); - void refresh_data(); + bool must_skip_data_refresh() const; + + void refresh_data(bool force); void open_about_dialog(); diff --git a/src/events.h b/src/events.h index 3e76607..3381e75 100644 --- a/src/events.h +++ b/src/events.h @@ -25,6 +25,10 @@ enum CustomEvent { enum CustomEventParam { invalid_location, + triggered_by_configuration_change, + triggered_by_model_change, + triggered_by_timer, + triggered_by_user, }; std::string format_custom_event_param(int param); diff --git a/src/http.cc b/src/http.cc index 0c12a04..69b5f9e 100644 --- a/src/http.cc +++ b/src/http.cc @@ -1,6 +1,7 @@ #include "http.h" #include +#include #include #include "errors.h" @@ -90,7 +91,7 @@ std::unique_ptr HttpClient::prepare_curl() { } void HttpClient::ensure_network() const { - iv_netinfo *netinfo = NetInfo(); + const auto* netinfo = NetInfo(); if (netinfo == nullptr or not netinfo->connected) { BOOST_LOG_TRIVIAL(debug) << "Will try to establish connection"; diff --git a/src/model.h b/src/model.h index dfbab13..d640af1 100644 --- a/src/model.h +++ b/src/model.h @@ -202,5 +202,7 @@ struct Model { std::list location_history; bool display_daily_forecast{false}; + + bool preserve_offline{false}; }; } // namespace taranis diff --git a/src/ui.cc b/src/ui.cc index 7749c27..a266a36 100644 --- a/src/ui.cc +++ b/src/ui.cc @@ -194,7 +194,8 @@ void Ui::handle_menu_item_selected(int item_index) { const auto event_handler = GetEventHandler(); if (item_index == MENU_ITEM_REFRESH) { - SendEvent(event_handler, EVT_CUSTOM, CustomEvent::refresh_data, 0); + SendEvent(event_handler, EVT_CUSTOM, CustomEvent::refresh_data, + CustomEventParam::triggered_by_user); } else if (item_index == MENU_ITEM_TOGGLE_FAVORITE_LOCATION) { SendEvent(event_handler, EVT_CUSTOM, CustomEvent::toggle_current_location_favorite, 0);