Skip to content

Commit 7bfe815

Browse files
bbrto21swift-kim
authored andcommitted
Refactor platform view channel (#118)
* Use template to extract value from argmuments map Signed-off-by: Boram Bae <[email protected]> * Cleanup platform_view_channel Signed-off-by: Boram Bae <[email protected]> * Fix a typo Signed-off-by: Boram Bae <[email protected]>
1 parent 3213d86 commit 7bfe815

File tree

1 file changed

+68
-98
lines changed

1 file changed

+68
-98
lines changed

shell/platform/tizen/channels/platform_view_channel.cc

+68-98
Original file line numberDiff line numberDiff line change
@@ -14,66 +14,20 @@
1414

1515
static constexpr char kChannelName[] = "flutter/platform_views";
1616

17-
std::string ExtractStringFromMap(const flutter::EncodableValue& arguments,
18-
const char* key) {
19-
if (std::holds_alternative<flutter::EncodableMap>(arguments)) {
20-
flutter::EncodableMap values = std::get<flutter::EncodableMap>(arguments);
21-
flutter::EncodableValue value = values[flutter::EncodableValue(key)];
22-
if (std::holds_alternative<std::string>(value)) {
23-
return std::get<std::string>(value);
24-
}
25-
}
26-
return std::string();
27-
}
28-
29-
int ExtractIntFromMap(const flutter::EncodableValue& arguments,
30-
const char* key) {
31-
if (std::holds_alternative<flutter::EncodableMap>(arguments)) {
32-
flutter::EncodableMap values = std::get<flutter::EncodableMap>(arguments);
33-
flutter::EncodableValue value = values[flutter::EncodableValue(key)];
34-
if (std::holds_alternative<int>(value)) {
35-
return std::get<int>(value);
36-
}
37-
}
38-
return -1;
39-
}
40-
41-
double ExtractDoubleFromMap(const flutter::EncodableValue& arguments,
42-
const char* key) {
43-
if (std::holds_alternative<flutter::EncodableMap>(arguments)) {
44-
flutter::EncodableMap values = std::get<flutter::EncodableMap>(arguments);
45-
flutter::EncodableValue value = values[flutter::EncodableValue(key)];
46-
if (std::holds_alternative<double>(value)) {
47-
return std::get<double>(value);
48-
}
49-
}
50-
return -1;
51-
}
52-
53-
flutter::EncodableMap ExtractMapFromMap(
54-
const flutter::EncodableValue& arguments,
55-
const char* key) {
56-
if (std::holds_alternative<flutter::EncodableMap>(arguments)) {
57-
flutter::EncodableMap values = std::get<flutter::EncodableMap>(arguments);
58-
flutter::EncodableValue value = values[flutter::EncodableValue(key)];
59-
if (std::holds_alternative<flutter::EncodableMap>(value)) {
60-
return std::get<flutter::EncodableMap>(value);
61-
}
62-
}
63-
return flutter::EncodableMap();
64-
}
65-
66-
flutter::EncodableList ExtractListFromMap(
67-
const flutter::EncodableValue& arguments,
68-
const char* key) {
69-
if (std::holds_alternative<flutter::EncodableMap>(arguments)) {
70-
flutter::EncodableMap values = std::get<flutter::EncodableMap>(arguments);
71-
flutter::EncodableValue value = values[flutter::EncodableValue(key)];
72-
if (std::holds_alternative<flutter::EncodableList>(value)) {
73-
return std::get<flutter::EncodableList>(value);
17+
template <typename T>
18+
bool GetValueFromEncodableMap(const flutter::EncodableValue& arguments,
19+
std::string key,
20+
T* out) {
21+
if (auto pmap = std::get_if<flutter::EncodableMap>(&arguments)) {
22+
auto iter = pmap->find(flutter::EncodableValue(key));
23+
if (iter != pmap->end() && !iter->second.IsNull()) {
24+
if (auto pval = std::get_if<T>(&iter->second)) {
25+
*out = *pval;
26+
return true;
27+
}
7428
}
7529
}
76-
return flutter::EncodableList();
30+
return false;
7731
}
7832

7933
PlatformViewChannel::PlatformViewChannel(flutter::BinaryMessenger* messenger,
@@ -96,14 +50,14 @@ PlatformViewChannel::~PlatformViewChannel() {
9650

9751
void PlatformViewChannel::Dispose() {
9852
// Clean-up view_instances_
99-
for (auto const& [viewId, viewInstance] : view_instances_) {
100-
delete viewInstance;
53+
for (auto const& [view_id, view_instance] : view_instances_) {
54+
delete view_instance;
10155
}
10256
view_instances_.clear();
10357

10458
// Clean-up view_factories_
105-
for (auto const& [viewType, viewFactory] : view_factories_) {
106-
viewFactory->Dispose();
59+
for (auto const& [view_type, view_factory] : view_factories_) {
60+
view_factory->Dispose();
10761
}
10862
view_factories_.clear();
10963
}
@@ -152,83 +106,99 @@ void PlatformViewChannel::HandleMethodCall(
152106
const auto method = call.method_name();
153107
const auto& arguments = *call.arguments();
154108

155-
FT_LOGI("PlatformViewChannel method: %s", method.c_str());
156109
if (method == "create") {
157-
std::string viewType = ExtractStringFromMap(arguments, "viewType");
158-
int viewId = ExtractIntFromMap(arguments, "id");
159-
double width = ExtractDoubleFromMap(arguments, "width");
160-
double height = ExtractDoubleFromMap(arguments, "height");
110+
std::string view_type;
111+
int view_id = -1;
112+
double width = 0.0, height = 0.0;
113+
if (!GetValueFromEncodableMap(arguments, "viewType", &view_type) ||
114+
!GetValueFromEncodableMap(arguments, "id", &view_id) ||
115+
!GetValueFromEncodableMap(arguments, "width", &width) ||
116+
!GetValueFromEncodableMap(arguments, "height", &height)) {
117+
result->Error("Invalid arguments");
118+
return;
119+
}
161120

162121
FT_LOGI(
163122
"PlatformViewChannel create viewType: %s id: %d width: %f height: %f ",
164-
viewType.c_str(), viewId, width, height);
123+
view_type.c_str(), view_id, width, height);
165124

166125
flutter::EncodableMap values = std::get<flutter::EncodableMap>(arguments);
167126
flutter::EncodableValue value = values[flutter::EncodableValue("params")];
168-
ByteMessage byteMessage;
127+
ByteMessage byte_message;
169128
if (std::holds_alternative<ByteMessage>(value)) {
170-
byteMessage = std::get<ByteMessage>(value);
129+
byte_message = std::get<ByteMessage>(value);
171130
}
172-
auto it = view_factories_.find(viewType);
131+
auto it = view_factories_.find(view_type);
173132
if (it != view_factories_.end()) {
174-
auto focuesdView = view_instances_.find(CurrentFocusedViewId());
175-
if (focuesdView != view_instances_.end()) {
176-
focuesdView->second->SetFocus(false);
133+
auto focused_view = view_instances_.find(CurrentFocusedViewId());
134+
if (focused_view != view_instances_.end()) {
135+
focused_view->second->SetFocus(false);
177136
}
178137

179-
auto viewInstance =
180-
it->second->Create(viewId, width, height, byteMessage);
181-
if (viewInstance) {
138+
auto view_instance =
139+
it->second->Create(view_id, width, height, byte_message);
140+
if (view_instance) {
182141
view_instances_.insert(
183-
std::pair<int, PlatformView*>(viewId, viewInstance));
142+
std::pair<int, PlatformView*>(view_id, view_instance));
184143

185144
if (engine_ && engine_->text_input_channel) {
186145
Ecore_IMF_Context* context =
187146
engine_->text_input_channel->GetImfContext();
188-
viewInstance->SetSoftwareKeyboardContext(context);
147+
view_instance->SetSoftwareKeyboardContext(context);
189148
}
190-
result->Success(flutter::EncodableValue(viewInstance->GetTextureId()));
149+
result->Success(flutter::EncodableValue(view_instance->GetTextureId()));
191150
} else {
192151
result->Error("Can't create a webview instance!!");
193152
}
194153
} else {
195-
FT_LOGE("can't find view type = %s", viewType.c_str());
154+
FT_LOGE("can't find view type = %s", view_type.c_str());
196155
result->Error("Can't find view type");
197156
}
198157
} else if (method == "clearFocus") {
199-
int viewId = -1;
158+
int view_id = -1;
200159
if (std::holds_alternative<int>(arguments)) {
201-
viewId = std::get<int>(arguments);
160+
view_id = std::get<int>(arguments);
202161
};
203-
auto it = view_instances_.find(viewId);
204-
if (viewId >= 0 && it != view_instances_.end()) {
162+
auto it = view_instances_.find(view_id);
163+
if (view_id >= 0 && it != view_instances_.end()) {
205164
it->second->SetFocus(false);
206165
it->second->ClearFocus();
207166
result->Success();
208167
} else {
209168
result->Error("Can't find view id");
210169
}
211170
} else {
212-
int viewId = ExtractIntFromMap(arguments, "id");
213-
auto it = view_instances_.find(viewId);
214-
if (viewId >= 0 && it != view_instances_.end()) {
171+
int view_id = -1;
172+
if (!GetValueFromEncodableMap(arguments, "id", &view_id)) {
173+
result->Error("Invalid arguments");
174+
return;
175+
}
176+
177+
auto it = view_instances_.find(view_id);
178+
if (view_id >= 0 && it != view_instances_.end()) {
215179
if (method == "dispose") {
216180
it->second->Dispose();
217181
result->Success();
218182
} else if (method == "resize") {
219-
double width = ExtractDoubleFromMap(arguments, "width");
220-
double height = ExtractDoubleFromMap(arguments, "height");
183+
double width = 0.0, height = 0.0;
184+
if (!GetValueFromEncodableMap(arguments, "width", &width) ||
185+
!GetValueFromEncodableMap(arguments, "height", &height)) {
186+
result->Error("Invalid arguments");
187+
return;
188+
}
221189
it->second->Resize(width, height);
222190
result->Success();
223191
} else if (method == "touch") {
224-
int type, button;
225-
double x, y, dx, dy;
192+
int type = 0, button = 0;
193+
double x = 0.0, y = 0.0, dx = 0.0, dy = 0.0;
226194

227-
flutter::EncodableList event = ExtractListFromMap(arguments, "event");
228-
if (event.size() != 6) {
195+
flutter::EncodableList event;
196+
if (!GetValueFromEncodableMap(arguments, "event", &event) ||
197+
event.size() != 6) {
229198
result->Error("Invalid Arguments");
230199
return;
231200
}
201+
232202
type = std::get<int>(event[0]);
233203
button = std::get<int>(event[1]);
234204
x = std::get<double>(event[2]);
@@ -239,14 +209,14 @@ void PlatformViewChannel::HandleMethodCall(
239209
it->second->Touch(type, button, x, y, dx, dy);
240210

241211
if (!it->second->IsFocused()) {
242-
auto focuesdView = view_instances_.find(CurrentFocusedViewId());
243-
if (focuesdView != view_instances_.end()) {
244-
focuesdView->second->SetFocus(false);
212+
auto focused_view = view_instances_.find(CurrentFocusedViewId());
213+
if (focused_view != view_instances_.end()) {
214+
focused_view->second->SetFocus(false);
245215
}
246216

247217
it->second->SetFocus(true);
248218
if (channel_ != nullptr) {
249-
auto id = std::make_unique<flutter::EncodableValue>(viewId);
219+
auto id = std::make_unique<flutter::EncodableValue>(view_id);
250220
channel_->InvokeMethod("viewFocused", std::move(id));
251221
}
252222
}

0 commit comments

Comments
 (0)