Add update of GenlistItem fields when text and description are changed
[profile/mobile/apps/native/accessibility-setting.git] / src / SwitchesPage.cpp
1 /*
2  * Copyright 2018 Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *  http://www.apache.org/licenses/LICENSE-2.0
9
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "SwitchesPage.hpp"
18
19 #include "Singleton.hpp"
20 #include "setting-accessibility.h"
21
22 #include <app.h>
23
24 SwitchesPage::SwitchesPage()
25         : context_(Singleton<AppContext>::instance())
26 {
27         auto naviframe = context_.navContext_.getNaviframe();
28         switches_ = Widget::make<Genlist>(naviframe);
29         switches_->setMode(ELM_LIST_COMPRESS);
30         switches_->setStyle("dialogue");
31
32         auto prevBtn = Widget::make<Button>(naviframe,
33                 [naviframe, this]() {
34                         naviframe->removeChild(removeSwitchesPageBtn_);
35                         naviframe->popBack();
36                 },
37                 std::string{},
38                 Button::BACK_BUTTON_ARROW_STYLE);
39
40         naviframe->pushBack("IDS_ACCS_UNIVERSAL_SWITCH_SETTINGS_GROUP_SWITCHES",
41                 switches_,
42                 [this]() {
43                         if (universal_switch_switches_back)
44                                 universal_switch_switches_back(universal_switch_switches_back_data);
45
46                         universal_switch_switches_back = nullptr;
47                         universal_switch_switches_back_data = nullptr;
48                 },
49                 prevBtn);
50
51         removeSwitchesPageBtn_ = Widget::make<Button>(naviframe,
52                 [this]() {
53                         removeSwitchPage_ = std::make_unique<RemoveSwitchPage>();
54                         removeSwitchPage_->attachCallback(switchRemovedCb, this);
55                 },
56                 "IDS_ACCS_DELETE_CAPS",
57                 "naviframe/title_right");
58         naviframe->setPartContent("title_right_btn", removeSwitchesPageBtn_);
59         updateSwitchesList();
60 }
61
62 void SwitchesPage::attachCallback(back_cb callback, void *data)
63 {
64         universal_switch_switches_back = callback;
65         universal_switch_switches_back_data = data;
66 }
67
68 void SwitchesPage::updateSwitchesList()
69 {
70         switches_->clear();
71         createSwitchesGroup();
72         switches_->appendItem({"type1",
73                 "IDS_ACCS_UNIVERSAL_SWITCH_SETTINGS_GROUP_SWITCHES_ADD_SWITCH",
74                 {},
75                 [this](auto item) {
76                         addSwitchPage_ = std::make_unique<AddSwitchPage>();
77                         addSwitchPage_->attachCallbackOnSwitchAttach(addSwitchCb, this);
78                 },
79                 ACCESSIBILITY_UNIVERSAL_SWITCH_PLUS_ICON});
80         removeSwitchesPageBtn_->disable(context_.config.configuration_items.size() == 0);
81 }
82
83 void SwitchesPage::createSwitchesGroup()
84 {
85         for (auto &it : context_.config.configuration_items) {
86                 auto activity_name = setting_accessibility_universal_switch_dbus_config_get_activity_name(
87                         &context_.config, it->activity_type);
88                 switches_->appendItem({"type1", it->user_name, activity_name, [=](auto item) {
89                                                                    this->updateSwitchPage_ = std::make_unique<UpdateSwitchPage>(it);
90                                                                    this->updateSwitchPage_->attachActionCallback(switchUpdatedCb, this);
91                                                            }});
92         }
93 }
94
95 void SwitchesPage::switchUpdatedCb(
96         void *data,
97         const std::string &switch_id,
98         const std::string &switch_action,
99         const std::string &switch_name)
100 {
101         RETURN_IF(!data, "Input parameter is NULL");
102         auto self = static_cast<SwitchesPage *>(data);
103
104         DEBUG("Switch updated, id: %s, name: %s, action: %s.", switch_id.c_str(), switch_name.c_str(), switch_action.c_str());
105         setting_accessibility_universal_switch_dbus_config_updateSwitchConfigurationItem(
106                 &self->context_.config, switch_id, switch_name, switch_action);
107         self->updateSwitchesList();
108 }
109
110 void SwitchesPage::switchRemovedCb(
111         void *data,
112         const std::string &switch_id,
113         const std::string &switch_action,
114         const std::string &switch_name)
115 {
116         RETURN_IF(!data, "Input parameter is NULL");
117         auto self = static_cast<SwitchesPage *>(data);
118
119         DEBUG("Switch removed, name: %s, id: %s, action: %s.", switch_name.c_str(), switch_id.c_str(), switch_action.c_str());
120         setting_accessibility_universal_switch_dbus_config_removeSwitchConfigurationItem(&self->context_.config, switch_id);
121         self->updateSwitchesList();
122 }
123
124 void SwitchesPage::addSwitchCb(
125         void *data,
126         const std::string &switch_id,
127         const std::string &switch_action,
128         const std::string &switch_name)
129 {
130         RETURN_IF(!data, "Input parameter is NULL");
131         auto self = static_cast<SwitchesPage *>(data);
132
133         DEBUG("Add switch, id: %s, name: %s, action: %s.", switch_id.c_str(), switch_name.c_str(), switch_action.c_str());
134         setting_accessibility_universal_switch_dbus_config_addSwitchConfigurationItem(
135                 &self->context_.config, switch_id, switch_name, switch_action);
136         self->updateSwitchesList();
137 }