From e03cf47b6774bfcb636650885e78d3e95186e727 Mon Sep 17 00:00:00 2001 From: StackZ <47382115+SuperSaiyajinStackZ@users.noreply.github.com> Date: Fri, 19 Jun 2020 12:56:12 +0200 Subject: [PATCH] Add Searching in UniStore v2! --- include/utils/store.hpp | 2 ++ romfs/lang/en/app.json | 4 +++- source/screens/unistore_v2.cpp | 37 ++++++++++++++++++++++++++++++++-- source/utils/store.cpp | 16 +++++++++++++++ 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/include/utils/store.hpp b/include/utils/store.hpp index 68a9e64..976faf3 100644 --- a/include/utils/store.hpp +++ b/include/utils/store.hpp @@ -61,6 +61,8 @@ public: int returnJSONIndex(const int index); int getSize(); bool getAscending() { return this->ascending; } + int searchForEntries(const std::string searchResult); + void reset() { this->sortedStore = this->unsortedStore; } const int getSortType() { if (this->sorttype == SortType::TITLE) return 0; diff --git a/romfs/lang/en/app.json b/romfs/lang/en/app.json index ac43273..4721b47 100644 --- a/romfs/lang/en/app.json +++ b/romfs/lang/en/app.json @@ -193,5 +193,7 @@ "ASCENDING": "Ascending", "TITLE_BTN": "Title", "AUTHOR_BTN": "Author", - "LAST_UPDATED_BTN": "Last updated" + "LAST_UPDATED_BTN": "Last updated", + "ENTER_SEARCH": "Enter what you like to search.", + "NO_RESULTS_FOUND": "No results found!" } diff --git a/source/screens/unistore_v2.cpp b/source/screens/unistore_v2.cpp index 28b2d76..47950b2 100644 --- a/source/screens/unistore_v2.cpp +++ b/source/screens/unistore_v2.cpp @@ -26,6 +26,7 @@ #include "download.hpp" #include "json.hpp" +#include "keyboard.hpp" #include "scriptHelper.hpp" #include "unistore_v2.hpp" @@ -334,7 +335,7 @@ void UniStoreV2::Draw(void) const { this->DrawGrid(); - Gui::DrawStringCentered(0, 218, 0.6f, this->returnTextColor(), std::to_string(this->storePage + 1) + " | " + std::to_string(1 + (this->storeJson.at("storeContent").size() / STORE_ENTRIES))); + Gui::DrawStringCentered(0, 218, 0.6f, this->returnTextColor(), std::to_string(this->storePage + 1) + " | " + std::to_string(1 + (this->sortedStore->getSize() / STORE_ENTRIES))); if (fadealpha > 0) Gui::Draw_Rect(0, 0, 400, 240, C2D_Color32(fadecolor, fadecolor, fadecolor, fadealpha)); this->DrawBaseBottom(); @@ -351,7 +352,7 @@ void UniStoreV2::Draw(void) const { } this->DrawList(); - Gui::DrawStringCentered(0, 218, 0.6f, this->returnTextColor(), std::to_string(this->storePageList + 1) + " | " + std::to_string(1 + (this->storeJson.at("storeContent").size() / STORE_ENTRIES_LIST))); + Gui::DrawStringCentered(0, 218, 0.6f, this->returnTextColor(), std::to_string(this->storePageList + 1) + " | " + std::to_string(1 + (this->sortedStore->getSize() / STORE_ENTRIES_LIST))); if (fadealpha > 0) Gui::Draw_Rect(0, 0, 400, 240, C2D_Color32(fadecolor, fadecolor, fadecolor, fadealpha)); this->DrawBaseBottom(); @@ -461,6 +462,22 @@ void UniStoreV2::Logic(u32 hDown, u32 hHeld, touchPosition touch) { } } + if (hDown & KEY_X) { + std::string temp = Input::getStringLong(Lang::get("ENTER_SEARCH")); + if (temp != "") { + this->selectedBox = 0; + this->storePage = 0; + int amount = this->sortedStore->searchForEntries(temp); + if (amount == 0) Msg::DisplayWarnMsg(Lang::get("NO_RESULTS_FOUND")); + } + } + + if (hDown & KEY_Y) { + this->selectedBox = 0; + this->storePage = 0; + this->sortedStore->reset(); + } + if (hDown & KEY_L) { if (this->storePage > 0) { this->selectedBox = 0; @@ -548,6 +565,22 @@ void UniStoreV2::Logic(u32 hDown, u32 hHeld, touchPosition touch) { } } + if (hDown & KEY_X) { + std::string temp = Input::getStringLong(Lang::get("ENTER_SEARCH")); + if (temp != "") { + this->selectedBoxList = 0; + this->storePageList = 0; + int amount = this->sortedStore->searchForEntries(temp); + if (amount == 0) Msg::DisplayWarnMsg(Lang::get("NO_RESULTS_FOUND")); + } + } + + if (hDown & KEY_Y) { + this->selectedBoxList = 0; + this->storePageList = 0; + this->sortedStore->reset(); + } + } else if (this->mode == 2) { if (hDown & KEY_TOUCH) { if (this->objects.size() > 0) { diff --git a/source/utils/store.cpp b/source/utils/store.cpp index 8283d57..9942bdb 100644 --- a/source/utils/store.cpp +++ b/source/utils/store.cpp @@ -82,6 +82,22 @@ UniStoreV2Struct Store::getData(const int index) { return temp; } +int Store::searchForEntries(const std::string searchResult) { + std::vector temp; + + for (int i = 0; i < (int)this->sortedStore.size(); i++) { + if (this->sortedStore[i].title.find(searchResult) != std::string::npos) { + temp.push_back({this->sortedStore[i]}); + } + } + + if (temp.size() != 0) { + this->sortedStore = temp; + } + + return (int)temp.size(); +} + // Title. bool compareTitleDescending(const UniStoreV2Struct& a, const UniStoreV2Struct& b) { int result = a.title.compare(b.title);