Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
implement lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
HookedBehemoth committed Oct 16, 2019
1 parent e53b69a commit 25c1d7a
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 178 deletions.
5 changes: 2 additions & 3 deletions include/ui/MainLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -29,7 +28,7 @@ namespace ui {
std::vector<model::comic> 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;
};
}
30 changes: 6 additions & 24 deletions include/ui/plutonium/elm_DetailedMenu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,10 @@
#include <vector>
#include <chrono>
#include <functional>
#include "web.hpp"

namespace pu::ui::elm
{
class RichMenuItem
{
public:
RichMenuItem(String Name);
PU_SMART_CTOR(RichMenuItem)

void AddOnClick(std::function<void()> Callback, u64 Key = KEY_A);
s32 GetCallbackCount();
std::function<void()> 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<std::function<void()>> cbs;
std::vector<u64> cbipts;
};

class RichMenu : public Element
{
public:
Expand All @@ -64,11 +44,12 @@ namespace pu::ui::elm
void SetOnFocusColor(Color Color);
Color GetScrollbarColor();
void SetScrollbarColor(Color Color);
void SetCallback(std::function<void()> Callback);
void SetOnSelectionChanged(std::function<void()> 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);
Expand All @@ -92,8 +73,9 @@ namespace pu::ui::elm
bool icdown;
int basestatus;
std::chrono::time_point<std::chrono::steady_clock> basetime;
std::function<void()> onclick;
std::function<void()> onselch;
std::vector<RichMenuItem::Ref> itms;
std::vector<model::comic *> itms;
render::NativeFont basefont;
render::NativeFont richfont;
std::vector<render::NativeTexture> loadednames;
Expand Down
2 changes: 2 additions & 0 deletions source/ui/DetailLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions source/ui/ImageLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions source/ui/MainApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}

Expand Down
76 changes: 22 additions & 54 deletions source/ui/MainLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)<comics.size()) addComic(comics[i+this->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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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){
Expand Down Expand Up @@ -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();
}
}
Loading

0 comments on commit 25c1d7a

Please sign in to comment.