diff --git a/src/model.h b/src/model.h index cb977ce..dc59a5a 100644 --- a/src/model.h +++ b/src/model.h @@ -175,6 +175,7 @@ struct Model { std::vector alerts; + std::vector favorite_locations; std::list location_history; bool display_daily_forecast{false}; diff --git a/src/state.h b/src/state.h index 4c3d854..6c412da 100644 --- a/src/state.h +++ b/src/state.h @@ -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() { @@ -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; static std::string get_application_state_path() { @@ -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