Add update of GenlistItem fields when text and description are changed
[profile/mobile/apps/native/accessibility-setting.git] / src / UpdateSwitchPage.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 "UpdateSwitchPage.hpp"
18
19 #include "AccessibilitySettingLog.hpp"
20 #include "Singleton.hpp"
21 #include "Toolbar.hpp"
22 #include "setting-accessibility.h"
23
24 #include <app.h>
25
26 #define GRP_SET_VALUE_UPDATE_ACCESSORIES_SWITCH "set-value-update-accessories-switch"
27 #define PRT_SET_VALUE_ENTRY "set-value-entry"
28 #define PRT_SET_VALUE_DESC "set-value-desc"
29 #define EDJ_SET_VALUE "edje/accessibility-settings-set-value.edj"
30 #define PRT_SET_VALUE_LIST "set-value-list"
31
32 UpdateSwitchPage::UpdateSwitchPage(UniversalSwitchConfigurationItem *config_item)
33         : context_(Singleton<AppContext>::instance())
34 {
35         config_item_to_update_.activity_type = config_item->activity_type;
36         config_item_to_update_.switch_id = config_item->switch_id;
37         config_item_to_update_.user_name = config_item->user_name;
38
39         if (config_item->provider_id == ACCESSIBILITY_UNIVERSAL_SWITCH_ACCESSORIES_SWITCH_PROVIDER) {
40                 createUpdateAccessoriesSwitchPage();
41         } else {
42                 actionPage_ = std::make_unique<ActionPage>();
43                 actionPage_->attachCallback(onActionUpdate, this);
44         }
45 }
46
47 void UpdateSwitchPage::attachActionCallback(UniversalSwitchCb cb, void *cbData)
48 {
49         switch_updated_cb = cb;
50         switch_updated_cb_data = cbData;
51 }
52
53 void UpdateSwitchPage::onActionUpdate(const std::string &action, void *user_data)
54 {
55         auto self = static_cast<UpdateSwitchPage *>(user_data);
56         if (self->switch_updated_cb) {
57                 self->switch_updated_cb(self->switch_updated_cb_data,
58                         self->config_item_to_update_.switch_id,
59                         action,
60                         self->config_item_to_update_.user_name);
61         }
62 }
63
64 void UpdateSwitchPage::createUpdateAccessoriesSwitchPage()
65 {
66         auto naviframe = context_.navContext_.getNaviframe();
67         layout_ = Widget::make<Layout>(
68                 naviframe, "edje/accessibility-settings-set-value.edj", GRP_SET_VALUE_UPDATE_ACCESSORIES_SWITCH);
69         layout_->setPartText(PRT_SET_VALUE_DESC, "IDS_ACCS_UNIVERSAL_SWITCH_NAME");
70
71         auto tabbar = Widget::make<Toolbar>(naviframe);
72         auto backCb = [naviframe, tabbar]() {
73                 naviframe->popBack();
74                 naviframe->removeChild(tabbar);
75         };
76
77         tabbar->addItem("IDS_ACCS_UNIVERSAL_SWITCH_CANCEL", backCb);
78         tabbar->addItem("IDS_ACCS_UNIVERSAL_SWITCH_SAVE", [this, backCb]() {
79                 if (switch_updated_cb) {
80                         switch_updated_cb(switch_updated_cb_data,
81                                 config_item_to_update_.switch_id,
82                                 config_item_to_update_.activity_type,
83                                 entry_->getEntryText());
84                 }
85                 backCb();
86         });
87
88         entry_ = Widget::make<Entry>(layout_);
89         entry_->setEntryText(config_item_to_update_.user_name);
90         entry_->moveCursorToEnd();
91         layout_->setPartContent(PRT_SET_VALUE_ENTRY, entry_);
92
93         updateAccessoriesActionGenlist_ = createGenlist(layout_, {});
94         updateAccessoriesActionGenlist_->appendItem({"type1",
95                 "IDS_ACCS_UNIVERSAL_SWITCH_ACTION",
96                 setting_accessibility_universal_switch_dbus_config_get_activity_name(
97                         &context_.config, config_item_to_update_.activity_type),
98                 [this](auto item) { this->updateSwitchAction(); }});
99
100         auto naviItem = naviframe->pushBack({}, layout_, {}, nullptr, "tabbar/notitle");
101         naviItem.setPartContent("tabbar", tabbar);
102 }
103
104 Genlist *UpdateSwitchPage::createGenlist(Widget *parent, std::string title)
105 {
106         auto genlist = Widget::make<Genlist>(parent);
107         genlist->setMode(ELM_LIST_COMPRESS);
108         genlist->setStyle("dialogue");
109
110         if (!title.empty())
111                 genlist->appendItem({"group_index", std::move(title)});
112
113         parent->setPartContent(PRT_SET_VALUE_LIST, genlist);
114
115         return genlist;
116 }
117
118 void UpdateSwitchPage::updateSwitchAction()
119 {
120         actionPage_ = std::make_unique<ActionPage>();
121         actionPage_->attachCallback(updateSwitchChooseAction, this);
122 }
123
124 void UpdateSwitchPage::updateSwitchChooseAction(const std::string &action, void *user_data)
125 {
126         auto self = static_cast<UpdateSwitchPage *>(user_data);
127
128         self->config_item_to_update_.activity_type = action;
129         self->updateAccessoriesActionGenlist_->clear();
130
131         self->updateAccessoriesActionGenlist_->appendItem({"type1",
132                 "IDS_ACCS_UNIVERSAL_SWITCH_ACTION",
133                 setting_accessibility_universal_switch_dbus_config_get_activity_name(
134                         &self->context_.config, self->config_item_to_update_.activity_type),
135                 [self](auto item) { self->updateSwitchAction(); }});
136
137         self->context_.navContext_.getNaviframe()->popBack();
138 }