Skip to content

Commit

Permalink
Extend model and application state with favorite locations
Browse files Browse the repository at this point in the history
Refs: #40
  • Loading branch information
orontee committed Sep 27, 2023
1 parent dae709d commit 6115c2e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ struct Model {

std::vector<Alert> alerts;

std::vector<Location> favorite_locations;
std::list<Location> location_history;

bool display_daily_forecast{false};
Expand Down
79 changes: 61 additions & 18 deletions src/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,12 @@ class ApplicationState {
if (!input) {
return;
}
this->model->location_history.clear();

Json::Value root;
input >> root;
for (const auto &value : root["location_history"]) {
const auto town{value.get("town", "").asString()};
if (town.empty()) {
return;
}
const Location location{town, value.get("country", "").asString()};
this->model->location_history.push_back(location);
}

this->restore_location_history(root);
this->restore_favorite_locations(root);
}

void dump() {
Expand All @@ -46,20 +40,18 @@ class ApplicationState {
return;
}
Json::Value root;
auto &history_value = root["location_history"];
const auto &history = this->model->location_history;
int location_index = 0;
for (const auto &location : history) {
history_value[location_index]["town"] = location.town;
history_value[location_index]["country"] = location.country;

++location_index;
}
this->dump_location_history(root);
this->dump_favorite_locations(root);

output << root << std::endl;
output.close();
}

private:
static constexpr char LOCATION_HISTORY_KEY[]{"location_history"};
static constexpr char FAVORITE_LOCATIONS_KEY[]{"favorite_locations"};

std::shared_ptr<Model> model;

static std::string get_application_state_path() {
Expand All @@ -70,5 +62,56 @@ class ApplicationState {
}
return std::string{STATEPATH} + "/taranis.json";
}

void restore_location_history(const Json::Value &root) {
this->model->location_history.clear();

for (const auto &value : root[ApplicationState::LOCATION_HISTORY_KEY]) {
const auto town{value.get("town", "").asString()};
if (town.empty()) {
return;
}
const Location location{town, value.get("country", "").asString()};
this->model->location_history.push_back(location);
}
}

void dump_location_history(Json::Value &root) {
auto &history_value = root[ApplicationState::LOCATION_HISTORY_KEY];
const auto &history = this->model->location_history;
int location_index = 0;
for (const auto &location : history) {
history_value[location_index]["town"] = location.town;
history_value[location_index]["country"] = location.country;

++location_index;
}
}

void restore_favorite_locations(const Json::Value &root) {
this->model->favorite_locations.clear();

for (const auto &value : root[ApplicationState::FAVORITE_LOCATIONS_KEY]) {
const auto town{value.get("town", "").asString()};
if (town.empty()) {
return;
}
const Location location{town, value.get("country", "").asString()};
this->model->favorite_locations.push_back(location);
}
}

void dump_favorite_locations(Json::Value &root) {
auto &favorite_locations_value =
root[ApplicationState::FAVORITE_LOCATIONS_KEY];
const auto &favorites = this->model->favorite_locations;
int favorite_index = 0;
for (const auto &location : favorites) {
favorite_locations_value[favorite_index]["town"] = location.town;
favorite_locations_value[favorite_index]["country"] = location.country;

++favorite_index;
}
}
};
}
} // namespace taranis

0 comments on commit 6115c2e

Please sign in to comment.