From 9f1e9dd2af4de64386b900ed9576036066a8fb00 Mon Sep 17 00:00:00 2001 From: Lukasz Wlazly Date: Thu, 13 Jun 2019 14:42:54 +0200 Subject: [PATCH] Add RemoveSwitchesPagePresenter Change-Id: I37a14f33496b488b1a1a3d13ccad64f69dd47c0b --- src/model/RemoveSwitchesModel.cpp | 23 +++++++++ src/model/RemoveSwitchesModel.hpp | 19 +++++++ src/presenter/Action.cpp | 7 ++- src/presenter/AppContext.cpp | 39 +++++++++++++-- src/presenter/AppContext.hpp | 13 ++++- src/presenter/ListPresenter.cpp | 7 ++- src/presenter/ListPresenter.hpp | 2 +- src/presenter/ModalPresenter.cpp | 21 ++++++++ src/presenter/ModalPresenter.hpp | 23 +++++++++ src/presenter/RemoveSwitchesModalPresenter.cpp | 19 +++++++ src/presenter/RemoveSwitchesModalPresenter.hpp | 19 +++++++ src/presenter/RemoveSwitchesPagePresenter.cpp | 68 ++++++++++++++++++++++++++ src/presenter/RemoveSwitchesPagePresenter.hpp | 24 +++++++++ src/presenter/SwitchesPagePresenter.cpp | 3 +- src/view/ModalView.cpp | 38 ++++++++++++++ src/view/ModalView.hpp | 16 ++++++ src/view/NavigationContext.cpp | 19 ++++++- src/view/NavigationContext.hpp | 5 ++ 18 files changed, 350 insertions(+), 15 deletions(-) create mode 100644 src/model/RemoveSwitchesModel.cpp create mode 100644 src/model/RemoveSwitchesModel.hpp create mode 100644 src/presenter/ModalPresenter.cpp create mode 100644 src/presenter/ModalPresenter.hpp create mode 100644 src/presenter/RemoveSwitchesModalPresenter.cpp create mode 100644 src/presenter/RemoveSwitchesModalPresenter.hpp create mode 100644 src/presenter/RemoveSwitchesPagePresenter.cpp create mode 100644 src/presenter/RemoveSwitchesPagePresenter.hpp create mode 100644 src/view/ModalView.cpp create mode 100644 src/view/ModalView.hpp diff --git a/src/model/RemoveSwitchesModel.cpp b/src/model/RemoveSwitchesModel.cpp new file mode 100644 index 0000000..cf587d0 --- /dev/null +++ b/src/model/RemoveSwitchesModel.cpp @@ -0,0 +1,23 @@ +#include "RemoveSwitchesModel.hpp" + +#include "UniversalSwitchConstants.hpp" + +RemoveSwitchesModel::RemoveSwitchesModel() +{ + isUniversalSwitchActiveHandle_ = Singleton::instance().registerAndGet(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE, false, [this](auto val) { + this->isUniversalSwitchActive_ = val; + }); + + isUniversalSwitchActive_.attach([](auto val) { + Singleton::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE, val); + }); +} + +void RemoveSwitchesModel::removeSwitches(std::vector &switchesIds) +{ + auto dBusClient_ = DBus::DBusClient(BUS, PATH, IFACE, DBus::ConnectionType::SESSION); + for (auto &s : switchesIds) { + DEBUG("removing: %s", s.c_str()); + dBusClient_.method("removeSwitchConfigurationItem").call(s); + } +} \ No newline at end of file diff --git a/src/model/RemoveSwitchesModel.hpp b/src/model/RemoveSwitchesModel.hpp new file mode 100644 index 0000000..a29e0f1 --- /dev/null +++ b/src/model/RemoveSwitchesModel.hpp @@ -0,0 +1,19 @@ +#ifndef REMOVE_SWITCHES_MODEL_HPP +#define REMOVE_SWITCHES_MODEL_HPP + +#include "DBus.hpp" +#include "ObservableProperty.hpp" +#include "VConf.hpp" + +class RemoveSwitchesModel +{ + public: + RemoveSwitchesModel(); + ObservableProperty isUniversalSwitchActive_; + void removeSwitches(std::vector &switchesIds); + + private: + VConfInterface::CallbackHandle isUniversalSwitchActiveHandle_; +}; + +#endif \ No newline at end of file diff --git a/src/presenter/Action.cpp b/src/presenter/Action.cpp index 6721b18..9044f92 100644 --- a/src/presenter/Action.cpp +++ b/src/presenter/Action.cpp @@ -7,8 +7,7 @@ Action::Action(std::string actionId, std::string title, std::function presenter) { presentersStack_.push_back(std::move(presenter)); - toBroadcast_ = presentersStack_.back().get(); + presenterToBroadcast_ = presentersStack_.back().get(); notify(); } void AppContext::pop() { - toBroadcast_ = nullptr; + presenterToBroadcast_ = nullptr; notify(); presentersStack_.pop_back(); @@ -41,9 +41,40 @@ bool AppContext::empty() return presentersStack_.empty(); } +void AppContext::pushModal(std::unique_ptr presenter) +{ + modalPresentersStack_.push_back(std::move(presenter)); + modalPresenterToBroadcast_ = modalPresentersStack_.back().get(); + notifyAboutModal(); +} + +void AppContext::popModal() +{ + modalPresenterToBroadcast_ = nullptr; + notifyAboutModal(); + modalPresentersStack_.pop_back(); +} + +ModalPresenter *AppContext::modalsBack() +{ + return modalPresentersStack_.back().get(); +} + +bool AppContext::modalsEmpty() +{ + return modalPresentersStack_.empty(); +} + void AppContext::notify() { - for (auto &c : this->onChangeCallbacks_) + for (auto &c : this->Observable::onChangeCallbacks_) + if (c) + c(presenterToBroadcast_); +} + +void AppContext::notifyAboutModal() +{ + for (auto &c : this->Observable::onChangeCallbacks_) if (c) - c(toBroadcast_); + c(modalPresenterToBroadcast_); } diff --git a/src/presenter/AppContext.hpp b/src/presenter/AppContext.hpp index a30baa9..60fdc4c 100644 --- a/src/presenter/AppContext.hpp +++ b/src/presenter/AppContext.hpp @@ -1,6 +1,7 @@ #ifndef APP_CONTEXT_HPP #define APP_CONTEXT_HPP +#include "ModalPresenter.hpp" #include "Naviframe.hpp" #include "Observable.hpp" #include "Presenter.hpp" @@ -11,7 +12,7 @@ #include #include -class AppContext : public Observable +class AppContext : public Observable, public Observable { public: AppContext(); @@ -20,6 +21,12 @@ class AppContext : public Observable Presenter *back(); bool empty(); void notify(); + void notifyAboutModal(); + + void pushModal(std::unique_ptr presenter); + void popModal(); + ModalPresenter *modalsBack(); + bool modalsEmpty(); // TODO: should be private UniversalSwitchDbusConfig config; @@ -27,7 +34,9 @@ class AppContext : public Observable private: std::vector> presentersStack_; - Presenter *toBroadcast_ = nullptr; + std::vector> modalPresentersStack_; + Presenter *presenterToBroadcast_ = nullptr; + ModalPresenter *modalPresenterToBroadcast_ = nullptr; }; #endif \ No newline at end of file diff --git a/src/presenter/ListPresenter.cpp b/src/presenter/ListPresenter.cpp index 8d96e36..4b51eae 100644 --- a/src/presenter/ListPresenter.cpp +++ b/src/presenter/ListPresenter.cpp @@ -15,9 +15,12 @@ Action *ListPresenter::getAction(const std::string &actionId) const return it->second.get(); } -void ListPresenter::addAction(std::unique_ptr action) +Action *ListPresenter::addAction(std::unique_ptr action) { - actions_.insert_or_assign(action->actionId_, std::move(action)); + auto ret = actions_.insert_or_assign(action->actionId_, std::move(action)); + ASSERT(ret.second, "Action cannot be added"); + + return ret.first->second.get(); } void ListPresenter::setOnListUpdateCallback(std::function callback) diff --git a/src/presenter/ListPresenter.hpp b/src/presenter/ListPresenter.hpp index ddbe529..23737c7 100644 --- a/src/presenter/ListPresenter.hpp +++ b/src/presenter/ListPresenter.hpp @@ -13,7 +13,7 @@ class ListPresenter : public Presenter, public Observable<> public: const std::vector &getListGroups() const; Action *getAction(const std::string &actionId) const; - void addAction(std::unique_ptr action); + Action *addAction(std::unique_ptr action); void setOnListUpdateCallback(std::function callback); protected: diff --git a/src/presenter/ModalPresenter.cpp b/src/presenter/ModalPresenter.cpp new file mode 100644 index 0000000..5a14dc8 --- /dev/null +++ b/src/presenter/ModalPresenter.cpp @@ -0,0 +1,21 @@ +#include "ModalPresenter.hpp" + +std::function ModalPresenter::getDoneCb() +{ + return doneCb_; +} + +std::function ModalPresenter::getCancelCb() +{ + return cancelCb_; +} + +std::string ModalPresenter::getText() +{ + return text_; +} + +std::string ModalPresenter::getDoneText() +{ + return doneText_; +} \ No newline at end of file diff --git a/src/presenter/ModalPresenter.hpp b/src/presenter/ModalPresenter.hpp new file mode 100644 index 0000000..5cd77df --- /dev/null +++ b/src/presenter/ModalPresenter.hpp @@ -0,0 +1,23 @@ +#ifndef MODAL_PRESENTER_HPP +#define MODAL_PRESENTER_HPP + +#include "Presenter.hpp" + +class ModalPresenter : public Presenter +{ + public: + std::function getDoneCb(); + std::function getCancelCb(); + std::string getText(); + std::string getDoneText(); + + protected: + using Presenter::Presenter; + + std::function doneCb_; + std::function cancelCb_; + std::string text_; + std::string doneText_; +}; + +#endif \ No newline at end of file diff --git a/src/presenter/RemoveSwitchesModalPresenter.cpp b/src/presenter/RemoveSwitchesModalPresenter.cpp new file mode 100644 index 0000000..35979ad --- /dev/null +++ b/src/presenter/RemoveSwitchesModalPresenter.cpp @@ -0,0 +1,19 @@ +#include "RemoveSwitchesModalPresenter.hpp" + +RemoveSwitchesModalPresenter::RemoveSwitchesModalPresenter(std::vector switches, std::vector switchesToRemove) + : switches_(std::move(switches)), switchesToRemove_(std::move(switchesToRemove)) +{ + setTitle("IDS_ACCS_UNIVERSAL_SWITCH_DELETE_SWITCHES"); + + if (switchesToRemove_.size() == switches_.size() && model_.isUniversalSwitchActive_.value()) { + text_ = "IDS_ACCS_UNIVERSAL_SWITCH_DELETE_ALL_SWITCHES_DESC"; + } else { + text_ = std::to_string(switchesToRemove_.size()) + " " + "IDS_ACCS_UNIVERSAL_SWITCH_DELETE_SWITCHES_DESC"; + } + + doneText_ = "IDS_ACCS_DELETE"; + + doneCb_ = [this]() { + model_.removeSwitches(switchesToRemove_); + }; +} \ No newline at end of file diff --git a/src/presenter/RemoveSwitchesModalPresenter.hpp b/src/presenter/RemoveSwitchesModalPresenter.hpp new file mode 100644 index 0000000..758239e --- /dev/null +++ b/src/presenter/RemoveSwitchesModalPresenter.hpp @@ -0,0 +1,19 @@ +#ifndef REMOVE_SWITCHES_MODAL_PRESENTER_HPP +#define REMOVE_SWITCHES_MODAL_PRESENTER_HPP + +#include "ModalPresenter.hpp" +#include "RemoveSwitchesModel.hpp" +#include "UniversalSwitchTypes.hpp" + +class RemoveSwitchesModalPresenter : public ModalPresenter +{ + public: + RemoveSwitchesModalPresenter(std::vector switches, std::vector switchesToRemove); + + private: + RemoveSwitchesModel model_; + std::vector switches_; + std::vector switchesToRemove_; +}; + +#endif \ No newline at end of file diff --git a/src/presenter/RemoveSwitchesPagePresenter.cpp b/src/presenter/RemoveSwitchesPagePresenter.cpp new file mode 100644 index 0000000..4d1eca1 --- /dev/null +++ b/src/presenter/RemoveSwitchesPagePresenter.cpp @@ -0,0 +1,68 @@ +#include "RemoveSwitchesPagePresenter.hpp" + +#include "AppContext.hpp" +#include "RemoveSwitchesModalPresenter.hpp" + +RemoveSwitchesPagePresenter::RemoveSwitchesPagePresenter() +{ + setTitle("IDS_ACCS_UNIVERSAL_SWITCH_SETTINGS_GROUP_SWITCHES"); + groups_.emplace_back(""); + + deleteAction_ = addAction(std::make_unique("titleRightAction", "IDS_ACCS_DELETE_CAPS", [this](auto action) { + std::vector toRemove; + for (auto i = 0u; i < groups_[0].items_.size(); ++i) { + if (groups_[0].items_[i]->widgetState_.value()) + toRemove.push_back(switches_[i].switchId); + } + + Singleton::instance().pushModal(std::make_unique(switches_, toRemove)); + }, + true)); + + selectAllAction_ = addAction(std::make_unique("selectAll", std::string{}, [this](auto action) { + if (!changeAllSelectedItems_) { + changeAllSelectedItems_ = true; + return; + } + + for (auto &it : groups_[0].items_) { + it->widgetState_ = action->state_.value(); + } + selectedNo_ = action->state_.value() ? groups_[0].items_.size() : 0; + }, + true)); + + addItemsToList(); + model_.switchesConfigurationChanged_.attach([this](auto timeStamp) { + groups_.back().items_.clear(); + this->addItemsToList(); + if (this->onListUpdate_) + this->onListUpdate_(); + }); +} + +void RemoveSwitchesPagePresenter::addItemsToList() +{ + auto &items = groups_.back().items_; + switches_ = model_.getAllSwitchConfigurationItems(); + for (auto oneSwitch : switches_) { + items.push_back(std::make_unique( + oneSwitch.userName, + model_.getActivityName(oneSwitch.activityType), + [this](auto item) { + if (item->widgetState_.value()) + ++selectedNo_; + else + --selectedNo_; + + if (selectedNo_ == groups_[0].items_.size() && !selectAllAction_->state_.value()) { + changeAllSelectedItems_ = false; + selectAllAction_->state_ = true; + } else if (selectedNo_ < groups_[0].items_.size() && selectAllAction_->state_.value()) { + changeAllSelectedItems_ = false; + selectAllAction_->state_ = false; + } + }, + ListItem::WidgetType::check)); + } +} diff --git a/src/presenter/RemoveSwitchesPagePresenter.hpp b/src/presenter/RemoveSwitchesPagePresenter.hpp new file mode 100644 index 0000000..9e6f524 --- /dev/null +++ b/src/presenter/RemoveSwitchesPagePresenter.hpp @@ -0,0 +1,24 @@ +#ifndef REMOVE_SWITCHES_PAGE_PRESENTER_HPP +#define REMOVE_SWITCHES_PAGE_PRESENTER_HPP + +#include "Action.hpp" +#include "ListPresenter.hpp" +#include "SwitchesPageModel.hpp" + +class RemoveSwitchesPagePresenter : public ListPresenter +{ + public: + RemoveSwitchesPagePresenter(); + + private: + SwitchesPageModel model_; + void addItemsToList(); + + Action *deleteAction_ = nullptr; + Action *selectAllAction_ = nullptr; + std::vector switches_; + size_t selectedNo_ = 0; + bool changeAllSelectedItems_ = true; +}; + +#endif \ No newline at end of file diff --git a/src/presenter/SwitchesPagePresenter.cpp b/src/presenter/SwitchesPagePresenter.cpp index 46156e6..22a082c 100644 --- a/src/presenter/SwitchesPagePresenter.cpp +++ b/src/presenter/SwitchesPagePresenter.cpp @@ -2,6 +2,7 @@ #include "AddSwitchPagePresenter.hpp" #include "AppContext.hpp" +#include "RemoveSwitchesPagePresenter.hpp" #include "UniversalSwitchConstants.hpp" SwitchesPagePresenter::SwitchesPagePresenter() @@ -10,7 +11,7 @@ SwitchesPagePresenter::SwitchesPagePresenter() groups_.emplace_back(""); addAction(std::make_unique("titleRightAction", "IDS_ACCS_DELETE_CAPS", [this](auto action) { - //DeleteSwitchPage should be added here + Singleton::instance().push(std::make_unique()); }, true)); addItemsToList(); diff --git a/src/view/ModalView.cpp b/src/view/ModalView.cpp new file mode 100644 index 0000000..3a435a8 --- /dev/null +++ b/src/view/ModalView.cpp @@ -0,0 +1,38 @@ +#include "ModalView.hpp" + +#include "Button.hpp" +#include "NavigationContext.hpp" +#include "Popup.hpp" + +ModalView::ModalView(const NavigationContext &context, ModalPresenter *presenter) + : View(context), presenter_(presenter) +{ + auto naviframe = context.getNaviframe(); + auto popup = Widget::make(naviframe, presenter_->getTitle()); + popup->setOrientation(ELM_POPUP_ORIENT_CENTER); + + popup->setText(presenter_->getText()); + + auto removeCb = [p = popup, nf = naviframe, this]() { + auto c = presenter_->getCancelCb(); + if (c) + c(); + nf->removeChild(p); + }; + popup->setEextEventCallback(EEXT_CALLBACK_BACK, removeCb); + popup->setEvasSmartCallback("dismissed", removeCb); + popup->setEvasSmartCallback("block,clicked", removeCb); + + auto cancelBtn = Widget::make