Skip to content

Commit

Permalink
feat: enabled weak_from_this for all views
Browse files Browse the repository at this point in the history
  • Loading branch information
cEvolve05 committed Aug 8, 2024
1 parent b2d1340 commit 434d242
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Controller/Core/BasicView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ EVENTO_UI_START

class ViewManager;

class BasicView {
class BasicView : public std::enable_shared_from_this<BasicView> {
friend class ViewManager;

public:
Expand Down
10 changes: 7 additions & 3 deletions src/Controller/Core/ViewManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ ViewManager::ViewManager(slint::ComponentHandle<UiEntryName> uiEntry)
return self.isViewShow(target);
});
self->on_clean_push([this](ViewName newView) {
if (viewStack.size() != 0 && newView == viewStack.top()) {
return;
}
cleanStack();
pushView(newView);
});
Expand All @@ -31,7 +34,7 @@ ViewManager::~ViewManager() {
}
};

void ViewManager::attach(ViewName name, std::unique_ptr<BasicView> object) {
void ViewManager::attach(ViewName name, std::shared_ptr<BasicView> object) {
views.emplace(name, std::move(object));
}

Expand Down Expand Up @@ -67,7 +70,7 @@ void ViewManager::exit() {
}

std::string ViewManager::getViewName(ViewName target) {
static std::unordered_map<ViewName, std::string> mapper{
static const std::unordered_map<ViewName, std::string> mapper{
{ViewName::DiscoveryPage, "DiscoveryPage"},
{ViewName::SearchPage, "SearchPage"},
{ViewName::HistoryPage, "HistoryPage"},
Expand All @@ -82,6 +85,7 @@ std::string ViewManager::getViewName(ViewName target) {
}

bool ViewManager::isOverlay(ViewName target) {
static const std::set<ViewName> overlayList{ViewName::MenuOverlay, ViewName::LoginOverlay};
return overlayList.find(target) != overlayList.end();
}

Expand Down Expand Up @@ -210,7 +214,7 @@ void ViewManager::StylishLog::viewVisibilityChanged(std::string actionName, std:
void ViewManager::call(Action& action) {
std::for_each(views.begin(),
views.end(),
[&action](const std::pair<const ViewName, std::unique_ptr<BasicView>>& view) {
[&action](const std::pair<const ViewName, std::shared_ptr<BasicView>>& view) {
action(*view.second);
});
}
Expand Down
8 changes: 3 additions & 5 deletions src/Controller/Core/ViewManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Controller/Core/BasicView.h>
#include <Controller/Core/GlobalAgent.hpp>
#include <Controller/Core/UiBase.h>
#include <memory>
#include <set>
#include <stack>
#include <string>
Expand All @@ -11,7 +12,7 @@ EVENTO_UI_START

class ViewManager : public GlobalAgent<::ViewManager> {
slint::ComponentHandle<UiEntryName> uiEntry;
std::unordered_map<ViewName, std::unique_ptr<BasicView>> views;
std::unordered_map<ViewName, std::shared_ptr<BasicView>> views;
bool inEventLoop = false;
std::stack<ViewName> viewStack;
std::set<ViewName> visibleViews;
Expand All @@ -22,7 +23,7 @@ class ViewManager : public GlobalAgent<::ViewManager> {
~ViewManager();

// register view to manager to auto-call on* functions
void attach(ViewName name, std::unique_ptr<BasicView> object);
void attach(ViewName name, std::shared_ptr<BasicView> object);

void show();
void run();
Expand All @@ -42,9 +43,6 @@ class ViewManager : public GlobalAgent<::ViewManager> {
void cleanStack();

private:
static inline const std::set<ViewName> overlayList{ViewName::MenuOverlay,
ViewName::LoginOverlay};

void showView(ViewName target);
void hideView(ViewName target);
bool isViewShow(ViewName target);
Expand Down
18 changes: 9 additions & 9 deletions src/Controller/UiBridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ EVENTO_UI_START

UiBridge::UiBridge(slint::ComponentHandle<UiEntryName>& uiEntry)
: manager(uiEntry) {
manager.attach(ViewName::DiscoveryPage, std::make_unique<DiscoveryPage>(uiEntry));
manager.attach(ViewName::SearchPage, std::make_unique<SearchPage>(uiEntry));
manager.attach(ViewName::HistoryPage, std::make_unique<HistoryPage>(uiEntry));
manager.attach(ViewName::MyEventPage, std::make_unique<MyEventPage>(uiEntry));
manager.attach(ViewName::DetailPage, std::make_unique<DetailPage>(uiEntry));
manager.attach(ViewName::AboutPage, std::make_unique<AboutPage>(uiEntry));
manager.attach(ViewName::SettingPage, std::make_unique<SettingPage>(uiEntry));
manager.attach(ViewName::LoginOverlay, std::make_unique<LoginOverlay>(uiEntry));
manager.attach(ViewName::MenuOverlay, std::make_unique<MenuOverlay>(uiEntry));
manager.attach(ViewName::DiscoveryPage, std::make_shared<DiscoveryPage>(uiEntry));
manager.attach(ViewName::SearchPage, std::make_shared<SearchPage>(uiEntry));
manager.attach(ViewName::HistoryPage, std::make_shared<HistoryPage>(uiEntry));
manager.attach(ViewName::MyEventPage, std::make_shared<MyEventPage>(uiEntry));
manager.attach(ViewName::DetailPage, std::make_shared<DetailPage>(uiEntry));
manager.attach(ViewName::AboutPage, std::make_shared<AboutPage>(uiEntry));
manager.attach(ViewName::SettingPage, std::make_shared<SettingPage>(uiEntry));
manager.attach(ViewName::LoginOverlay, std::make_shared<LoginOverlay>(uiEntry));
manager.attach(ViewName::MenuOverlay, std::make_shared<MenuOverlay>(uiEntry));

manager.pushView(ViewName::DiscoveryPage);
manager.pushView(ViewName::LoginOverlay);
Expand Down

0 comments on commit 434d242

Please sign in to comment.