Skip to content

Commit

Permalink
Configuration to preserve device being offline
Browse files Browse the repository at this point in the history
Refs: #57
  • Loading branch information
orontee committed Oct 11, 2023
1 parent b27ca88 commit 2fe8d19
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 17 deletions.
60 changes: 47 additions & 13 deletions src/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace taranis {

bool config_already_loaded{false};

int App::process_event(int event_type, int param_one, int param_two) {
BOOST_LOG_TRIVIAL(debug) << "Processing event of type "
<< format_event_type(event_type);
Expand Down Expand Up @@ -82,10 +84,7 @@ void App::setup() {
current_location.country = "FR";
current_location.state = "Ile-de-France";
}

BOOST_LOG_TRIVIAL(info)
<< "First load of configuration asked to trigger a data refresh";
this->load_config(true);
this->load_config();
}

void App::show() {
Expand All @@ -111,11 +110,20 @@ void App::initialize_language() {
this->language = currentLang();
initialize_translations();
}
void App::load_config(bool force_data_refresh) {
BOOST_LOG_TRIVIAL(debug) << "Loading config";

void App::load_config() {
if (not config_already_loaded) {
BOOST_LOG_TRIVIAL(debug) << "Loading configuration";
} else {
BOOST_LOG_TRIVIAL(debug) << "Rereading configuration";
}

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
Expand Down Expand Up @@ -148,19 +156,22 @@ void App::load_config(bool force_data_refresh) {
initialize_translations();
}

const bool is_data_obsolete = force_data_refresh or is_api_key_obsolete or
is_unit_system_obsolete or is_language_obsolete;
const bool is_data_obsolete =
(not config_already_loaded and not this->model->preserve_offline) or
is_api_key_obsolete or is_unit_system_obsolete or is_language_obsolete;
// temperatures, wind speed and weather description are computed
// by the backend thus unit system or language change implies that
// data are obsolete

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);
}
config_already_loaded = true;
}

int App::handle_custom_event(int param_one, int param_two) {
Expand All @@ -186,7 +197,11 @@ 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;
Expand Down Expand Up @@ -252,9 +267,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);
Expand Down Expand Up @@ -372,7 +404,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) {
Expand Down Expand Up @@ -416,7 +449,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) {
Expand Down
6 changes: 4 additions & 2 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ class App {

void initialize_language();

void load_config(bool force_data_refresh = false);
void load_config();

int handle_custom_event(int param_one, int param_two);

void clear_model_weather_conditions();

void refresh_data();
bool must_skip_data_refresh() const;

void refresh_data(bool force);

void open_about_dialog();

Expand Down
4 changes: 4 additions & 0 deletions src/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/http.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "http.h"

#include <boost/log/trivial.hpp>
#include <memory>
#include <sstream>

#include "errors.h"
Expand Down Expand Up @@ -90,7 +91,7 @@ std::unique_ptr<CURL, void (*)(CURL *)> 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";

Expand Down
2 changes: 2 additions & 0 deletions src/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,7 @@ struct Model {
std::list<HistoryItem> location_history;

bool display_daily_forecast{false};

bool preserve_offline{false};
};
} // namespace taranis
3 changes: 2 additions & 1 deletion src/ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2fe8d19

Please sign in to comment.