From b35a40b787f5d8f64f38c0541f0e2195b0c91a30 Mon Sep 17 00:00:00 2001 From: BttrDrgn <49764143+BttrDrgn@users.noreply.github.com> Date: Wed, 4 May 2022 03:06:28 -0500 Subject: [PATCH] filtering and performance enhancements --- src/app/api/api.cpp | 15 ++++++ src/app/api/api.hpp | 2 + src/app/menus/menus.cpp | 104 ++++++++++++++++++++++++++-------------- src/app/menus/menus.hpp | 4 +- 4 files changed, 87 insertions(+), 38 deletions(-) diff --git a/src/app/api/api.cpp b/src/app/api/api.cpp index f16c435..29da718 100644 --- a/src/app/api/api.cpp +++ b/src/app/api/api.cpp @@ -187,8 +187,23 @@ void api::get_station(const std::string& id) }).detach(); } +void api::filter_place(const std::string& key) +{ + api::filtered_place.clear(); + + for (auto place : api::place) + { + //Look for city, country, or id with this string + if (place.city.find(key) != std::string::npos || place.country.find(key) != std::string::npos || place.id.find(key) != std::string::npos) + { + api::filtered_place.emplace_back(place); + } + } +} + nl::json api::places; std::vector api::place; +std::vector api::filtered_place; bool api::places_done = true; nl::json api::details; diff --git a/src/app/api/api.hpp b/src/app/api/api.hpp index d24dea3..48d2a48 100644 --- a/src/app/api/api.hpp +++ b/src/app/api/api.hpp @@ -27,11 +27,13 @@ class api { public: static void get_places(); + static void filter_place(const std::string& key); static void get_details(const std::string& id); static void get_station(const std::string& id); static nl::json places; static std::vector place; + static std::vector filtered_place; static bool places_done; static nl::json details; diff --git a/src/app/menus/menus.cpp b/src/app/menus/menus.cpp index c1c7a4b..8cc3b20 100644 --- a/src/app/menus/menus.cpp +++ b/src/app/menus/menus.cpp @@ -109,56 +109,77 @@ void menus::places() { if (ImGui::BeginMenu("Places")) { - - if (ImGui::BeginCombo("Country", &menus::current_country[0])) + if (std::strlen(menus::search_buffer) == 0) { - if (api::places_done) - { - if (ImGui::Button("Reset")) - { - menus::current_country = "N/A"; - ImGui::CloseCurrentPopup(); - } - - ImGui::NewLine(); + menus::filtering = false; + } - for (auto c : api::all_countries) - { - if (ImGui::Button(&c[0])) - { - menus::current_country = c; - ImGui::CloseCurrentPopup(); - } - } - } - ImGui::EndCombo(); + if (ImGui::Button("Reset Filter")) + { + menus::filtering = false; + memset(menus::search_buffer, 0, sizeof(menus::search_buffer)); } + if (ImGui::InputText("Search", menus::search_buffer, sizeof(search_buffer))) + { + menus::filtering = true; + api::filter_place(std::string(menus::search_buffer)); + } if (ImGui::BeginMenu("Locations")) { if (api::places_done) { - for (auto place : api::place) + if (!menus::filtering) { - std::string country = place.country; - if (std::strcmp(&menus::current_country[0], "N/A")) + if (api::place.empty()) + { + ImGui::Text("Places are empty!\nYou might need to refresh\n\nPlease click Actions -> Refresh Places."); + } + else { - if (!std::strcmp(&place.country[0], &menus::current_country[0])) + if (!menus::show_all_stations) { - if (ImGui::Button(&logger::va("%s", &place.city[0])[0])) + ImGui::Text("There are %i places, hidden by default for performance.\nClick the button to show all stations", api::place.size()); + if (ImGui::Button("Show All")) { - api::get_details(place.id); - ImGui::CloseCurrentPopup(); + menus::show_all_stations = true; + } + } + else if (menus::show_all_stations) + { + if (ImGui::Button("Hide All")) + { + menus::show_all_stations = false; + } + + ImGui::NewLine(); + + for (auto place : api::place) + { + if (ImGui::Button(&logger::va("[%s] %s", &place.country[0], &place.city[0])[0])) + { + api::get_details(place.id); + ImGui::CloseCurrentPopup(); + } } } } - else + } + else if (menus::filtering) + { + if (api::filtered_place.empty()) + { + ImGui::Text("Places are empty!\nYou might need to refresh\n\nPlease click Actions -> Refresh Places."); + } { - if (ImGui::Button(&logger::va("[%s] %s", &country[0], &place.city[0])[0])) + for (auto place : api::filtered_place) { - api::get_details(place.id); - ImGui::CloseCurrentPopup(); + if (ImGui::Button(&logger::va("[%s] %s", &place.country[0], &place.city[0])[0])) + { + api::get_details(place.id); + ImGui::CloseCurrentPopup(); + } } } } @@ -173,11 +194,18 @@ void menus::stations() { if (ImGui::BeginMenu("Stations")) { - for (station_t station : api::station) + if (api::station.empty()) { - if (ImGui::Button(&logger::va("%s", &station.title[0])[0])) + ImGui::Text("Stations are empty!\nYou might need to select a place."); + } + else + { + for (station_t station : api::station) { - audio::play(station.id); + if (ImGui::Button(&logger::va("%s", &station.title[0])[0])) + { + audio::play(station.id); + } } } ImGui::EndMenu(); @@ -218,7 +246,9 @@ void menus::enumerate_snow() } } -std::string menus::current_country = "N/A"; std::vector menus::snow; std::int32_t menus::max_points = 255; -bool menus::show_snow = false; \ No newline at end of file +bool menus::show_all_stations = false; +bool menus::show_snow = false; +bool menus::filtering = false; +char menus::search_buffer[64]; \ No newline at end of file diff --git a/src/app/menus/menus.hpp b/src/app/menus/menus.hpp index dfc005c..be4f568 100644 --- a/src/app/menus/menus.hpp +++ b/src/app/menus/menus.hpp @@ -15,17 +15,19 @@ class menus static void init(); static void update(); - static std::string current_country; static std::vector snow; static std::int32_t max_points; + static bool show_all_stations; static bool show_snow; + static char search_buffer[64]; private: static void main_menu_bar(); static void actions(); static void places(); static void stations(); + static bool filtering; static void render_snow(); static void enumerate_snow();