Add Switch Page in mvp 90/207790/4
authorOskar Chodowicz <o.chodowicz@samsung.com>
Wed, 12 Jun 2019 14:10:32 +0000 (16:10 +0200)
committerOskar Chodowicz <o.chodowicz@samsung.com>
Tue, 25 Jun 2019 13:45:52 +0000 (15:45 +0200)
Change-Id: I7d78f0a1bd5f8cd7b6d881103fdf589e2b899821

src/model/AddSwitchPageModel.cpp [new file with mode: 0644]
src/model/AddSwitchPageModel.hpp [new file with mode: 0644]
src/model/SwitchesPageModel.cpp
src/model/SwitchesPageModel.hpp
src/presenter/AddSwitchPagePresenter.cpp [new file with mode: 0644]
src/presenter/AddSwitchPagePresenter.hpp [new file with mode: 0644]
src/presenter/SwitchesPagePresenter.cpp
src/utils/UniversalSwitchTypes.hpp

diff --git a/src/model/AddSwitchPageModel.cpp b/src/model/AddSwitchPageModel.cpp
new file mode 100644 (file)
index 0000000..a57e396
--- /dev/null
@@ -0,0 +1,75 @@
+#include "AddSwitchPageModel.hpp"
+
+#include "UniversalSwitchConstants.hpp"
+
+AddSwitchPageModel::AddSwitchPageModel()
+{
+       dBusClient_ = DBus::DBusClient(BUS, PATH, IFACE, DBus::ConnectionType::SESSION);
+       synchronizeProviders();
+       dBusClient_.addSignal<void()>(std::string{"switchesConfigurationChanged"}, [this]() {
+               this->switchesConfigurationChanged_ = std::chrono::system_clock::now();
+       });
+}
+
+void AddSwitchPageModel::synchronizeProviders()
+{
+       providers_.clear();
+       auto reply = dBusClient_.method<DBus::ValueOrError<std::vector<std::tuple<std::string, std::string, std::string>>>()>("getAllSwitchProviders").call();
+       if (reply) {
+               for (auto provider : std::get<0>(reply))
+                       providers_.push_back(InfoType{std::get<0>(provider), std::get<1>(provider), std::get<2>(provider)});
+       } else {
+               ERROR("Error on function getAllSwitchProviders call");
+               ERROR("%s", reply.getError().message.c_str());
+       }
+}
+
+bool AddSwitchPageModel::areAllSwitchesConfigured(const InfoType &switchProvider)
+{
+       auto configuredSwitches = getAllSwitchConfigurationItems();
+       auto isSwitchConfigured = [&](const InfoType &switchItem) {
+               for (auto &configured : configuredSwitches) {
+                       if (switchItem.id == configured.switchId)
+                               return true;
+               }
+               return false;
+       };
+       for (auto &aSwitch : getAllSwitchesByProvider(switchProvider)) {
+               if (!isSwitchConfigured(aSwitch))
+                       return false;
+       }
+       return true;
+}
+
+const std::vector<InfoType> &AddSwitchPageModel::getProviders()
+{
+       return providers_;
+}
+
+std::vector<InfoType> AddSwitchPageModel::getAllSwitchesByProvider(const InfoType &switchProvider)
+{
+       std::vector<InfoType> allSwitches;
+       auto reply = dBusClient_.method<DBus::ValueOrError<std::vector<std::tuple<std::string, std::string, std::string>>>(std::string)>("getAllSwitchesByProviderId").call(switchProvider.id);
+       if (reply) {
+               for (auto aSwitch : std::get<0>(reply))
+                       allSwitches.push_back(InfoType{std::get<0>(aSwitch), std::get<1>(aSwitch), std::get<2>(aSwitch)});
+       } else {
+               ERROR("Error on function getAllSwitchProviders call");
+               ERROR("%s", reply.getError().message.c_str());
+       }
+       return allSwitches;
+}
+
+std::vector<SwitchConfigurationItem> AddSwitchPageModel::getAllSwitchConfigurationItems()
+{
+       std::vector<SwitchConfigurationItem> configurationItems{};
+       auto reply = dBusClient_.method<DBus::ValueOrError<std::vector<std::tuple<std::string, std::string, std::string, ChangeType>>>()>("getAllSwitchConfigurationItems").call();
+       if (reply) {
+               for (auto item : std::get<0>(reply))
+                       configurationItems.push_back(SwitchConfigurationItem{std::get<0>(item), std::get<1>(item), std::get<2>(item)});
+       } else {
+               ERROR("Error on function getAllSwitchConfigurationItems call");
+               ERROR("%s", reply.getError().message.c_str());
+       }
+       return configurationItems;
+}
diff --git a/src/model/AddSwitchPageModel.hpp b/src/model/AddSwitchPageModel.hpp
new file mode 100644 (file)
index 0000000..216b2af
--- /dev/null
@@ -0,0 +1,28 @@
+#ifndef ADD_SWITCH_PAGE_MODEL_HPP
+#define ADD_SWITCH_PAGE_MODEL_HPP
+
+#include "DBus.hpp"
+#include "ObservableProperty.hpp"
+#include "UniversalSwitchTypes.hpp"
+#include "VConf.hpp"
+
+#include <chrono>
+
+class AddSwitchPageModel
+{
+       public:
+       AddSwitchPageModel();
+       const std::vector<InfoType> &getProviders();
+       bool areAllSwitchesConfigured(const InfoType &switchProvider);
+       ObservableProperty<std::chrono::system_clock::time_point> switchesConfigurationChanged_;
+       std::vector<InfoType> getAllSwitchesByProvider(const InfoType &switchProvider);
+
+       private:
+       void synchronizeProviders();
+       std::vector<SwitchConfigurationItem> getAllSwitchConfigurationItems();
+
+       DBus::DBusClient dBusClient_;
+       std::vector<InfoType> providers_;
+};
+
+#endif
\ No newline at end of file
index d16a1a8c9f8689c573686c9aea84d1633f10db50..35cee11d46737d76012eff8b0af19c47067a7891 100644 (file)
@@ -17,7 +17,7 @@ std::vector<SwitchConfigurationItem> SwitchesPageModel::getAllSwitchConfiguratio
        auto reply = dBusClient_.method<DBus::ValueOrError<std::vector<std::tuple<std::string, std::string, std::string, ChangeType>>>()>("getAllSwitchConfigurationItems").call();
        if (reply) {
                for (auto item : std::get<0>(reply))
-                       configurationItems.push_back(SwitchConfigurationItem{std::get<0>(item), std::get<1>(item), std::get<2>(item), std::get<3>(item)});
+                       configurationItems.push_back(SwitchConfigurationItem{std::get<0>(item), std::get<1>(item), std::get<2>(item)});
        } else {
                ERROR("Error on function getAllSwitchConfigurationItems call");
                ERROR("%s", reply.getError().message.c_str());
@@ -39,7 +39,7 @@ void SwitchesPageModel::synchronizeBindableActivities()
        auto reply = dBusClient_.method<DBus::ValueOrError<std::vector<std::tuple<std::string, std::string, std::string>>>()>("getBindableActivityTypes").call();
        if (reply) {
                for (auto activity : std::get<0>(reply))
-                       bindableActivities_.push_back(SwitchInfoType{std::get<0>(activity), std::get<1>(activity), std::get<2>(activity)});
+                       bindableActivities_.push_back(InfoType{std::get<0>(activity), std::get<1>(activity), std::get<2>(activity)});
        } else {
                ERROR("Error on function getBindableActivityTypes call");
                ERROR("%s", reply.getError().message.c_str());
index 5bb301888e50aac0de464a5dc7dc6c6771c25bc7..e758b7dd18de104b63d526f44cb284bc0ac850e3 100644 (file)
@@ -18,7 +18,7 @@ class SwitchesPageModel
 
        private:
        void synchronizeBindableActivities();
-       std::vector<SwitchInfoType> bindableActivities_;
+       std::vector<InfoType> bindableActivities_;
        DBus::DBusClient dBusClient_;
 };
 
diff --git a/src/presenter/AddSwitchPagePresenter.cpp b/src/presenter/AddSwitchPagePresenter.cpp
new file mode 100644 (file)
index 0000000..683055a
--- /dev/null
@@ -0,0 +1,24 @@
+#include "AddSwitchPagePresenter.hpp"
+
+AddSwitchPagePresenter::AddSwitchPagePresenter()
+{
+       setTitle("IDS_ACCS_UNIVERSAL_SWITCH_SETTINGS_GROUP_SWITCHES");
+       groups_.emplace_back("");
+       auto &items = groups_.back().items_;
+       for (auto provider : model_.getProviders()) {
+               items.push_back(std::make_unique<ListItem>(
+                       provider.name,
+                       provider.description,
+                       [this](auto item) {
+                               //Page for adding special switch should be added here
+                       }));
+       }
+       auto onConfigurationChange = [this](auto changeTime) {
+               auto providers = this->model_.getProviders();
+               auto size = providers.size();
+               for (auto i = 0u; i < size; ++i)
+                       this->groups_.back().items_[i]->enabled_ = !this->model_.areAllSwitchesConfigured(providers[i]);
+       };
+       model_.switchesConfigurationChanged_.attach(onConfigurationChange);
+       onConfigurationChange(std::chrono::system_clock::now());
+}
\ No newline at end of file
diff --git a/src/presenter/AddSwitchPagePresenter.hpp b/src/presenter/AddSwitchPagePresenter.hpp
new file mode 100644 (file)
index 0000000..b205c3b
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef ADD_SWITCH_PAGE_PRESENTER_HPP
+#define ADD_SWITCH_PAGE_PRESENTER_HPP
+
+#include "AddSwitchPageModel.hpp"
+#include "ListPresenter.hpp"
+
+class AddSwitchPagePresenter : public ListPresenter
+{
+       public:
+       AddSwitchPagePresenter();
+
+       private:
+       AddSwitchPageModel model_;
+};
+
+#endif
index 7ae757196368b961769e1cb721d2f5593fb2888d..46156e6e24fe8279092240c044ca025a3c496f61 100644 (file)
@@ -1,5 +1,7 @@
 #include "SwitchesPagePresenter.hpp"
 
+#include "AddSwitchPagePresenter.hpp"
+#include "AppContext.hpp"
 #include "UniversalSwitchConstants.hpp"
 
 SwitchesPagePresenter::SwitchesPagePresenter()
@@ -36,7 +38,7 @@ void SwitchesPagePresenter::addItemsToList()
                "IDS_ACCS_UNIVERSAL_SWITCH_SETTINGS_GROUP_SWITCHES_ADD_SWITCH",
                std::string{},
                [this](auto item) {
-                       //AddSwitchPage should be added here
+                       Singleton<AppContext>::instance().push(std::make_unique<AddSwitchPagePresenter>());
                },
                ListItem::WidgetType::icon,
                std::function<void(ListItem * item)>{},
index 6f803e2ba2a55ac2980422398447a9255e4a0fe2..ddde58b4ed37b126b042578e024ec7973c1482a2 100644 (file)
@@ -16,10 +16,9 @@ struct SwitchConfigurationItem
        std::string switchId;
        std::string userName;
        std::string activityType;
-       ChangeType changetype;
 };
 
-struct SwitchInfoType
+struct InfoType
 {
        std::string id;
        std::string name;