Skip to content

Commit

Permalink
Extend history items with favorite boolean
Browse files Browse the repository at this point in the history
Refs: #40
  • Loading branch information
orontee committed Sep 30, 2023
1 parent 4348f18 commit 415b0ad
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 111 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Added

- Extend history items with favorite boolean and make sure favorites
won't disappear from history
[#40](https://github.com/orontee/taranis/issues/40)

- Automatic refresh should not popup connection dialog
[#50](https://github.com/orontee/taranis/issues/50)

Expand Down
Binary file added icons/icon_favorite.bmp
Binary file not shown.
1 change: 1 addition & 0 deletions icons/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ icons = files(
'icon_11n_2x.bmp',
'icon_13n_2x.bmp',
'icon_50n_2x.bmp',
'icon_favorite.bmp',
'icon_menu.bmp',
'icon_warning.bmp'
)
6 changes: 6 additions & 0 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,17 @@ class App {
auto location = this->history->get_location(history_index);
if (location) {
this->update_config_location(*location);
} else {
DialogSynchro(ICON_WARNING, "Title", "Location not found!",
GetLangText("Ok"), nullptr, nullptr);
}
return 1;
} else if (param_one == CustomEvent::show_about_dialog) {
this->open_about_dialog();
return 1;
} else if (param_one == CustomEvent::toggle_current_location_favorite) {
this->history->toggle_current_location_favorite();
return 1;
}

// Events
Expand Down
1 change: 1 addition & 0 deletions src/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum CustomEvent {
refresh_data,
select_location_from_history,
show_about_dialog,
toggle_current_location_favorite,

// Events
model_daily_forecast_display_changed,
Expand Down
53 changes: 47 additions & 6 deletions src/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,64 @@ class LocationHistoryProxy {
}
std::optional<Location> result;
if (index == 0 and it != std::end(history)) {
result = *it;
result = it->location;
}
return result;
}

void update_history_maybe() {
auto &history = this->model->location_history;
auto &current_location = this->model->location;
auto found = std::find(history.begin(), history.end(), current_location);
auto &history = this->model->location_history;
const auto found = std::find_if(history.begin(), history.end(),
[current_location](const auto &item) {
return item.location == current_location;
});
const bool found_favorite = (found != history.end() and found->favorite);
if (found != history.end()) {
history.erase(found);
}
history.push_front(this->model->location);
if (history.size() == LocationHistoryProxy::max_size) {
auto last_not_favorite =
std::find_if(history.rbegin(), history.rend(),
[](const auto &item) { return not item.favorite; });
if (last_not_favorite != history.rend()) {
history.erase(std::prev(last_not_favorite.base()));
}
}
if (history.size() < LocationHistoryProxy::max_size) {
history.push_front(HistoryItem{this->model->location, found_favorite});
}
}

bool is_current_location_favorite() const {
auto &current_location = this->model->location;
auto &history = this->model->location_history;
const auto found = std::find_if(history.begin(), history.end(),
[current_location](const auto &item) {
return item.location == current_location;
});
return found != history.end() and found->favorite;
}

if (history.size() > LocationHistoryProxy::max_size) {
history.pop_back();
void toggle_current_location_favorite() {
auto &current_location = this->model->location;
auto &history = this->model->location_history;
auto found = std::find_if(history.begin(), history.end(),
[current_location](const auto &item) {
return item.location == current_location;
});
if (found == history.end()) {
return;
}
found->favorite = (not found->favorite);
}

bool can_add_favorite() {
auto &history = this->model->location_history;
const auto found =
std::find_if(history.begin(), history.end(),
[](const auto &item) { return not item.favorite; });
return found != history.end();
}

private:
Expand Down
47 changes: 15 additions & 32 deletions src/icons.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,69 +12,52 @@ extern const ibitmap icon_10n_2x;
extern const ibitmap icon_11n_2x;
extern const ibitmap icon_13n_2x;
extern const ibitmap icon_50n_2x;
extern const ibitmap icon_favorite;
extern const ibitmap icon_menu;
extern const ibitmap icon_warning;
;

namespace taranis {
class Icons {
public:
Icons()
: icon_01n{&::icon_01n_2x}, icon_02n{&::icon_02n_2x},
icon_03n{&::icon_03n_2x}, icon_04n{&::icon_04n_2x},
icon_09n{&::icon_09n_2x}, icon_10n{&::icon_10n_2x},
icon_11n{&::icon_11n_2x}, icon_13n{&::icon_13n_2x},
icon_50n{&::icon_50n_2x}, icon_menu{&::icon_menu},
icon_warning{&::icon_warning} {}

ibitmap *get(const std::string &name) {
if (name == "01d" or name == "01n") {
return const_cast<ibitmap *>(this->icon_01n);
return const_cast<ibitmap *>(&icon_01n_2x);
}
if (name == "02d" or name == "02n") {
return const_cast<ibitmap *>(this->icon_02n);
return const_cast<ibitmap *>(&icon_02n_2x);
}
if (name == "03d" or name == "03n") {
return const_cast<ibitmap *>(this->icon_03n);
return const_cast<ibitmap *>(&icon_03n_2x);
}
if (name == "04d" or name == "04n") {
return const_cast<ibitmap *>(this->icon_04n);
return const_cast<ibitmap *>(&icon_04n_2x);
}
if (name == "09d" or name == "09n") {
return const_cast<ibitmap *>(this->icon_09n);
return const_cast<ibitmap *>(&icon_09n_2x);
}
if (name == "10d" or name == "10n") {
return const_cast<ibitmap *>(this->icon_10n);
return const_cast<ibitmap *>(&icon_10n_2x);
}
if (name == "11d" or name == "11n") {
return const_cast<ibitmap *>(this->icon_11n);
return const_cast<ibitmap *>(&icon_11n_2x);
}
if (name == "13d" or name == "13n") {
return const_cast<ibitmap *>(this->icon_13n);
return const_cast<ibitmap *>(&icon_13n_2x);
}
if (name == "50d" or name == "50n") {
return const_cast<ibitmap *>(this->icon_50n);
return const_cast<ibitmap *>(&icon_50n_2x);
}
if (name == "favorite") {
return const_cast<ibitmap *>(&icon_favorite);
}
if (name == "menu") {
return const_cast<ibitmap *>(this->icon_menu);
return const_cast<ibitmap *>(&icon_menu);
}
if (name == "warning") {
return const_cast<ibitmap *>(this->icon_warning);
return const_cast<ibitmap *>(&icon_warning);
}
return nullptr;
}

private:
const ibitmap *const icon_01n;
const ibitmap *const icon_02n;
const ibitmap *const icon_03n;
const ibitmap *const icon_04n;
const ibitmap *const icon_09n;
const ibitmap *const icon_10n;
const ibitmap *const icon_11n;
const ibitmap *const icon_13n;
const ibitmap *const icon_50n;
const ibitmap *const icon_menu;
const ibitmap *const icon_warning;
};
} // namespace taranis
4 changes: 4 additions & 0 deletions src/l10n.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ inline void initialize_translations() {
AddTranslation("Units", "Unités");
AddTranslation("About…", "À propos…");
AddTranslation("Quit", "Quitter");
AddTranslation("Add to favorites", "Ajouter aux favoris");
AddTranslation("Remove from favorites", "Retirer des favoris");
AddTranslation("Empty", "Vide");

// statusbar.h
Expand Down Expand Up @@ -167,6 +169,8 @@ inline void initialize_translations() {
AddTranslation("Units", "Jednostki");
AddTranslation("About…", "O aplikacji");
AddTranslation("Quit", "Wyjście");
AddTranslation("Add to favorites", "Dodaj do ulubionych");
AddTranslation("Remove from favorites", "Usuń z ulubionych");
AddTranslation("Empty", "Pusty");

// statusbar.h
Expand Down
Loading

0 comments on commit 415b0ad

Please sign in to comment.