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 ff2354c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
37 changes: 32 additions & 5 deletions src/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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 ff2354c

Please sign in to comment.