From 25c1d7af5007478fefba23d73db15f17651d9384 Mon Sep 17 00:00:00 2001 From: HookedBehemoth Date: Thu, 17 Oct 2019 01:05:45 +0200 Subject: [PATCH] implement lazy loading --- include/ui/MainLayout.hpp | 5 +- include/ui/plutonium/elm_DetailedMenu.hpp | 30 ++---- source/ui/DetailLayout.cpp | 2 + source/ui/ImageLayout.cpp | 2 + source/ui/MainApplication.cpp | 4 +- source/ui/MainLayout.cpp | 76 ++++--------- source/ui/plutonium/elm_DetailedMenu.cpp | 124 ++++++---------------- source/web.cpp | 4 +- 8 files changed, 69 insertions(+), 178 deletions(-) diff --git a/include/ui/MainLayout.hpp b/include/ui/MainLayout.hpp index 793cade..20563c9 100644 --- a/include/ui/MainLayout.hpp +++ b/include/ui/MainLayout.hpp @@ -16,9 +16,8 @@ namespace ui { private: void next(); void prev(); - void loadSection(); + void displayPage(); void openSelectedComic(); - void addComic(model::comic Comic); void showOptions(); bool search(); void onItemClick(); @@ -29,7 +28,7 @@ namespace ui { std::vector comics; std::string searchString; model::searchMode mode = model::searchMode::ALL; - int page = 1, maxPage = 1, section = 0, maxSection = 0; + int page = 1, maxPage = 1; bool popular = false; }; } \ No newline at end of file diff --git a/include/ui/plutonium/elm_DetailedMenu.hpp b/include/ui/plutonium/elm_DetailedMenu.hpp index e35dc5f..93c2057 100644 --- a/include/ui/plutonium/elm_DetailedMenu.hpp +++ b/include/ui/plutonium/elm_DetailedMenu.hpp @@ -17,30 +17,10 @@ #include #include #include +#include "web.hpp" namespace pu::ui::elm { - class RichMenuItem - { - public: - RichMenuItem(String Name); - PU_SMART_CTOR(RichMenuItem) - - void AddOnClick(std::function Callback, u64 Key = KEY_A); - s32 GetCallbackCount(); - std::function GetCallback(s32 Index); - u64 GetCallbackKey(s32 Index); - void SetIcon(std::string Icon); - void SetRichIcon(std::string Icon); - String name; - Color clr; - std::string icon; - std::string richname; - std::string richicon; - std::vector> cbs; - std::vector cbipts; - }; - class RichMenu : public Element { public: @@ -64,11 +44,12 @@ namespace pu::ui::elm void SetOnFocusColor(Color Color); Color GetScrollbarColor(); void SetScrollbarColor(Color Color); + void SetCallback(std::function Callback); void SetOnSelectionChanged(std::function Callback); - void AddItem(RichMenuItem::Ref &Item); + void AddItem(model::comic *); void ClearItems(); void SetCooldownEnabled(bool Cooldown); - RichMenuItem::Ref &GetSelectedItem(); + model::comic * GetSelectedItem(); s32 GetSelectedIndex(); void SetSelectedIndex(s32 Index); void OnRender(render::Renderer::Ref &Drawer, s32 X, s32 Y); @@ -92,8 +73,9 @@ namespace pu::ui::elm bool icdown; int basestatus; std::chrono::time_point basetime; + std::function onclick; std::function onselch; - std::vector itms; + std::vector itms; render::NativeFont basefont; render::NativeFont richfont; std::vector loadednames; diff --git a/source/ui/DetailLayout.cpp b/source/ui/DetailLayout.cpp index 0744bd8..8c202d5 100644 --- a/source/ui/DetailLayout.cpp +++ b/source/ui/DetailLayout.cpp @@ -19,6 +19,8 @@ namespace ui { this->logo->SetWidth(111); this->logo->SetHeight(48); this->title = TextBlock::New(600, 30, "loading...", 40); + this->title->SetHorizontalAlign(HorizontalAlign::Center); + this->title->SetVerticalAlign(VerticalAlign::Up); this->title->SetColor(theme.textColor); this->tagMenu = Menu::New(640, 130, 590, theme.textColor, 50, 10); this->tagMenu->SetColor(theme.tagBg); diff --git a/source/ui/ImageLayout.cpp b/source/ui/ImageLayout.cpp index 972d540..26ba5ae 100644 --- a/source/ui/ImageLayout.cpp +++ b/source/ui/ImageLayout.cpp @@ -12,6 +12,8 @@ namespace ui { this->image->SetHorizontalAlign(HorizontalAlign::Center); this->image->SetVerticalAlign(VerticalAlign::Center); this->pageInfo = TextBlock::New(5, 690, "?/?"); + this->pageInfo->SetHorizontalAlign(HorizontalAlign::Left); + this->pageInfo->SetVerticalAlign(VerticalAlign::Down); this->pageInfo->SetColor(theme.textColor); this->Add(this->image); this->Add(pageInfo); diff --git a/source/ui/MainApplication.cpp b/source/ui/MainApplication.cpp index 7b8e0e8..c38d478 100644 --- a/source/ui/MainApplication.cpp +++ b/source/ui/MainApplication.cpp @@ -23,7 +23,7 @@ namespace ui { void MainApplication::checkPermission() { pctlInitialize(); Result rc; - bool pcactive; + bool pcactive = false; rc = pctlIsRestrictionEnabled(&pcactive); if(R_SUCCEEDED(rc) && pcactive) { rc = pctlauthShow(true); @@ -35,7 +35,7 @@ namespace ui { } else { allow(); } - printf("rc: %d\npcactive: %b", rc, pcactive); + printf("rc: %d\npcactive: %s", rc, pcactive ? "true" : "false"); pctlExit(); } diff --git a/source/ui/MainLayout.cpp b/source/ui/MainLayout.cpp index f2e6f0d..780f87f 100644 --- a/source/ui/MainLayout.cpp +++ b/source/ui/MainLayout.cpp @@ -16,8 +16,9 @@ namespace ui { this->logo->SetHeight(48); this->topText = TextBlock::New(600, 30, "loading...", 40); this->topText->SetColor(theme.textColor); + this->topText->SetHorizontalAlign(HorizontalAlign::Center); this->comicMenu = RichMenu::New(40, 120, 1200, theme.hoverColor, 120, 5); - //this->comicMenu->AddItem(RichMenuItem::New("fuck")); + this->comicMenu->SetCallback(std::bind(&MainLayout::onItemClick, this)); this->Add(this->topBarRect); this->Add(this->logo); this->Add(this->topText); @@ -45,45 +46,32 @@ namespace ui { } } void MainLayout::next() { - PRINTF("INFO: switched from page %d and section %d to...\n", this->page, this->section); - if(this->section >= (this->maxSection-1)) { - if (this->page < this->maxPage) { - this->page++; - this->section = 0; - loadPage(); - } else { - return; - } + if (this->page < this->maxPage) { + this->page++; + PRINTF("INFO: loading page %d...\n", this->page); + loadPage(); } else { - section++; + PRINTF("INFO: already at the end"); + return; } - PRINTF("INFO: ...page %d and section %d\n", this->page, this->section); - loadSection(); } void MainLayout::prev() { - PRINTF("INFO: switched from page %d and section %d to...\n", this->page, this->section); - if(this->section == 0) { - if(this->page > 1) { - this->page--; - this->section = 4; - loadPage(); - } else { - return; - } + if(this->page > 1) { + PRINTF("INFO: loading page %d...\n", this->page); + this->page--; + loadPage(); } else { - section--; + PRINTF("INFO: already at the start"); + return; } - PRINTF("INFO: ...page %d and section %d\n", this->page, this->section); - loadSection(); } - void MainLayout::loadSection() { - PRINTF("DEBUG: %d/%d, %d/%d\n", section, maxSection, page, maxPage); + void MainLayout::displayPage() { + PRINTF("DEBUG: %d/%d\n", page, maxPage); comicMenu->ClearItems(); - if(comics.size()) { - for(short i=0; i<+5; i++) { - if((i+this->section*5)section*5]); - } - } //else comicMenu->AddItem(RichMenuItem::New("nothing found :/")); + for(model::comic comic: comics) { + PRINTF("INFO: adding comic %s\n", comic.id.c_str());; + this->comicMenu->AddItem(new model::comic(comic)); + } this->comicMenu->SetSelectedIndex(0); std::string topStr, tmpString; tmpString = searchString; @@ -109,13 +97,8 @@ namespace ui { topStr+=std::to_string(page); topStr+="/"; topStr+=std::to_string(maxPage); - topStr+=" section: "; - topStr+=std::to_string(section+1); - topStr+="/"; - topStr+=std::to_string(maxSection); topStr+=")"; this->topText->SetText(topStr); - this->topText->SetX(640-(this->topText->GetWidth()/2)); } void MainLayout::loadPage() { std::string url = web::FORMAT_API; @@ -151,20 +134,7 @@ namespace ui { this->maxPage = page.maxPages; this->comics = page.comics; - maxSection = comics.size()/5; - if(this->comics.size()%5) maxSection +=1; - PRINTF("INFO: loading section %d\n", this->section); - loadSection(); - } - void MainLayout::addComic(model::comic Comic) { - PRINTF("INFO: adding comic %s\n", Comic.id.c_str());; - RichMenuItem::Ref item = RichMenuItem::New(Comic.name); - item->clr = theme.textColor; - item->SetIcon(web::getPath(Comic, 1, true)); - item->richname = Comic.id; - if(Comic.language != model::CLang::UNKNOWN) item->SetRichIcon(fs::getFlagPath(Comic.language)); - item->AddOnClick(std::bind(&MainLayout::onItemClick, this)); - this->comicMenu->AddItem(item); + displayPage(); } void MainLayout::showOptions() { int opts = mainApp->CreateShowDialog("Category", "what category do you want to see?", {"all", "search", "popular-toggle", "tags (TODO)"}, true); @@ -174,7 +144,6 @@ namespace ui { } else if(opts == 1) { if(!search()) return; this->page = 1; - this->section = 0; this->mode = model::searchMode::SEARCH; } else if(opts == 2) { if(mode == 0){ @@ -214,14 +183,13 @@ namespace ui { else return false; } void MainLayout::onItemClick() { - comic = this->comics[this->comicMenu->GetSelectedIndex()+5*section]; + comic = this->comics[this->comicMenu->GetSelectedIndex()]; mainApp->LoadLayout(mainApp->detailLayout); mainApp->detailLayout->showComicDetail(); } void MainLayout::tagSearch() { this->mode = model::searchMode::TAG; this->page = 1; - this->section = 0; loadPage(); } } \ No newline at end of file diff --git a/source/ui/plutonium/elm_DetailedMenu.cpp b/source/ui/plutonium/elm_DetailedMenu.cpp index 1d8d56c..9b04836 100644 --- a/source/ui/plutonium/elm_DetailedMenu.cpp +++ b/source/ui/plutonium/elm_DetailedMenu.cpp @@ -1,59 +1,8 @@ #include "ui/plutonium/elm_DetailedMenu.hpp" +extern model::theme theme; namespace pu::ui::elm { - RichMenuItem::RichMenuItem(String Name) - { - this->clr = { 10, 10, 10, 255 }; - this->name = Name; - } - - void RichMenuItem::AddOnClick(std::function Callback, u64 Key) - { - this->cbs.push_back(Callback); - this->cbipts.push_back(Key); - } - - s32 RichMenuItem::GetCallbackCount() - { - return this->cbs.size(); - } - - std::function RichMenuItem::GetCallback(s32 Index) - { - if(this->cbs.empty()) return [&](){}; - return this->cbs[Index]; - } - - u64 RichMenuItem::GetCallbackKey(s32 Index) - { - return this->cbipts[Index]; - } - - void RichMenuItem::SetIcon(std::string Icon) - { - std::ifstream ifs(Icon); - if(ifs.good()) - { - this->icon = Icon; - } else { - this->richicon = "romfs:/shrek.png"; - } - ifs.close(); - } - - void RichMenuItem::SetRichIcon(std::string Icon) - { - std::ifstream ifs(Icon); - if(ifs.good()) - { - this->richicon = Icon; - } else { - this->richicon = "romfs:/shrek.png"; - } - ifs.close(); - } - RichMenu::RichMenu(s32 X, s32 Y, s32 Width, Color OptionColor, s32 ItemSize, s32 ItemsToShow) : Element::Element() { this->x = X; @@ -162,14 +111,19 @@ namespace pu::ui::elm this->scb = Color; } + void RichMenu::SetCallback(std::function Callback) + { + this->onclick = Callback; + } + void RichMenu::SetOnSelectionChanged(std::function Callback) { this->onselch = Callback; } - void RichMenu::AddItem(RichMenuItem::Ref &qItem) + void RichMenu::AddItem(model::comic * comic) { - this->itms.push_back(qItem); + this->itms.push_back(comic); } void RichMenu::ClearItems() @@ -186,7 +140,7 @@ namespace pu::ui::elm this->icdown = Cooldown; } - RichMenuItem::Ref &RichMenu::GetSelectedItem() + model::comic * RichMenu::GetSelectedItem() { return this->itms[this->isel]; } @@ -270,13 +224,13 @@ namespace pu::ui::elm else Drawer->RenderRectangleFill(this->clr, cx, cy, cw, ch); } else Drawer->RenderRectangleFill(this->clr, cx, cy, cw, ch); - RichMenuItem::Ref itm = this->itms[i]; + model::comic * itm = this->itms[i]; s32 xh = render::GetTextureHeight(curname); s32 tx = (cx + 25); s32 ty; - if(itm->richname.empty()) ty = ((ch - xh) / 2) + cy; + if(itm->id.empty()) ty = ((ch - xh) / 2) + cy; else ty = (ch/3) - (xh/2) + cy; - if(!itm->icon.empty()) + if(!itm->mediaId.empty()) { float factor = (float)render::GetTextureHeight(curicon)/(float)render::GetTextureWidth(curicon); s32 icw = (this->isize - 10); @@ -295,12 +249,12 @@ namespace pu::ui::elm } Drawer->RenderTexture(curicon, icx, icy, { -1, icw, ich, -1.0f }); } - if(!itm->richname.empty()) + if(!itm->id.empty()) { s32 rxh = render::GetTextureHeight(currichname); s32 rtx = tx; s32 rty = cy + (ch/3)*2 + ((ch/3) - rxh)/2; - if(!itm->richicon.empty()) + if(itm->language != model::CLang::UNKNOWN) { float rfactor = (float)render::GetTextureHeight(currichicon)/(float)render::GetTextureWidth(currichicon); s32 ricw = (ch/3); @@ -392,7 +346,7 @@ namespace pu::ui::elm if((this->selfact >= 255) && (this->pselfact <= 0)) { if(this->icdown) this->icdown = false; - else (this->itms[this->isel]->GetCallback(0))(); + else (this->onclick)(); this->dtouch = false; } } @@ -499,15 +453,11 @@ namespace pu::ui::elm } else { - s32 ipc = this->itms[this->isel]->GetCallbackCount(); - if(ipc > 0) for(s32 i = 0; i < ipc; i++) - { - if(Down & this->itms[this->isel]->GetCallbackKey(i)) + if(Down & KEY_A) { if(this->icdown) this->icdown = false; - else (this->itms[this->isel]->GetCallback(i))(); + else (this->onclick)(); } - } } } } @@ -528,34 +478,22 @@ namespace pu::ui::elm if((its + this->fisel) > this->itms.size()) its = this->itms.size() - this->fisel; for(s32 i = this->fisel; i < (its + this->fisel); i++) { + /* name */ auto strname = this->itms[i]->name; - auto tex = render::RenderText(this->basefont, strname, this->itms[i]->clr); + auto tex = render::RenderText(this->basefont, strname, theme.textColor); this->loadednames.push_back(tex); - if(!this->itms[i]->icon.empty()) - { - auto stricon = this->itms[i]->icon; - auto icontex = render::LoadImage(stricon); - this->loadedicons.push_back(icontex); - } - else this->loadedicons.push_back(NULL); - if(!this->itms[i]->richname.empty()) - { - auto rstrname = this->itms[i]->richname; - auto rtex = render::RenderText(this->richfont, rstrname, this->itms[i]->clr); - this->loadedrichnames.push_back(rtex); - if(!this->itms[i]->richicon.empty()) - { - auto rstricon = this->itms[i]->richicon; - auto ricontex = render::LoadImage(rstricon); - this->loadedrichicons.push_back(ricontex); - } - else this->loadedrichicons.push_back(NULL); - } - else - { - this->loadedrichnames.push_back(NULL); - this->loadedrichicons.push_back(NULL); - } + /* icon */ + auto stricon = web::getPath(*itms[i], 1, true); + auto icontex = render::LoadImage(stricon); + this->loadedicons.push_back(icontex); + /* rich name */ + auto rstrname = this->itms[i]->id; + auto rtex = render::RenderText(this->richfont, rstrname, theme.textColor); + this->loadedrichnames.push_back(rtex); + /* rich icon */ + auto rstricon = fs::getFlagPath(itms[i]->language); + auto ricontex = render::LoadImage(rstricon); + this->loadedrichicons.push_back(ricontex); } } } \ No newline at end of file diff --git a/source/web.cpp b/source/web.cpp index c3587e1..406e417 100644 --- a/source/web.cpp +++ b/source/web.cpp @@ -105,7 +105,7 @@ namespace web { if(d.HasMember("error")) return *page; page->maxPages = d["num_pages"].GetInt(); page->comics = getComicsFromVal(d["result"]); - PRINTF("INFO: returning page with %d comics\n", page->comics.size()); + PRINTF("INFO: returning page with %lx comics\n", page->comics.size()); return *page; } std::string getPath(model::comic comic, int page, bool thumb){ @@ -139,7 +139,7 @@ namespace web { PRINTF("INFO: progress=%g\n", progress); } void onCompleted(swurl::WebRequest * request) { - PRINTF("INFO: Download Completed with status code %d\n", request->response.statusCode); + PRINTF("INFO: Download Completed with status code %lx\n", request->response.statusCode); } void onError(swurl::WebRequest * request, std::string error) { PRINTF("ERROR: %s\n", error.c_str());