Skip to content

Commit

Permalink
Merge pull request #221 from Securrency-OSS/asim/form-builder/updated…
Browse files Browse the repository at this point in the history
…-request-body

Asim/form builder/updated request body
  • Loading branch information
divyanshub024 authored Oct 4, 2023
2 parents 2fa4795 + 0ee5d7d commit a327084
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 37 deletions.
2 changes: 1 addition & 1 deletion examples/mirai_gallery/assets/json/sign_in_screen.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"action": {
"actionType": "navigate",
"navigationStyle": "pushReplacement",
"assetPath": "assets/jsons/kyc_intro_screen.json"
"assetPath": "assets/json/user_profile_screen.json"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,35 @@ class MiraiNetworkRequestParser extends MiraiActionParser<MiraiNetworkRequest> {
response = e.response;
}

if (response != null) {
final expectedResult = model.results
.firstWhere((result) => response?.statusCode == result.statusCode);
if (response != null && response.statusCode != null) {
final expectedResult = model.results.firstWhere(
(result) => response?.statusCode == result.statusCode,
orElse: () => MiraiNetworkResult(
statusCode: response?.statusCode ?? 0,
action: {
"actionType": "showDialog",
"widget": {
"type": "alertDialog",
"title": {
"type": "text",
"data": "${response?.statusMessage} - ${response?.statusCode}",
"textAlign": "center",
"style": {"fontSize": 18}
},
"content": {
"type": "padding",
"padding": {"top": 8, "left": 12, "right": 12, "bottom": 12},
"child": {
"type": "text",
"data": "${response?.data}",
"textAlign": "center",
"style": {"fontSize": 12}
}
}
}
},
),
);

return Mirai.onCallFromJson(
expectedResult.action, context.mounted ? context : context);
Expand Down
78 changes: 45 additions & 33 deletions packages/mirai/lib/src/services/mirai_network_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MiraiNetworkService {
MiraiNetworkRequest request,
BuildContext context,
) async {
final body = await _getBody(context, request.body);
final body = await _updateBody(context, request.body);
return _dio.post(
request.url,
data: body,
Expand Down Expand Up @@ -81,52 +81,64 @@ class MiraiNetworkService {
);
}

static Future<dynamic> _getBody(
static Future<dynamic> _updateBody(
BuildContext context,
dynamic body,
) async {
if (body != null) {
if (body is Map) {
dynamic finalBody = {};

await Future.forEach(body.keys, (key) async {
final value = body[key];
if (value is Map && value.containsKey('actionType')) {
if (body is Map) {
for (dynamic mapEntry in body.entries) {
final key = mapEntry.key;
final value = mapEntry.value;
if (value is Map && value.containsKey('actionType')) {
try {
Log.d("Loading from an action callback");

final dynamic callbackValue = await Future<dynamic>.value(
Mirai.onCallFromJson(value as Map<String, dynamic>, context),
);
Log.d("Loaded value from the callback: $callbackValue");

finalBody[key] = callbackValue;
} else if (value is File) {
String fileName = value.path.split('/').last;
final multipart =
await MultipartFile.fromFile(value.path, filename: fileName);

FormData formData = FormData.fromMap({
key: multipart,
});
body.update(
key,
(existingValue) => callbackValue,
);

finalBody = formData;
} else {
finalBody[key] = value;
continue;
} catch (e) {
Log.e(e);
}
});

return finalBody;
} else if (body is List) {
List<dynamic> finalBody = [];
for (dynamic value in body) {
final result = await _getBody(context, value);
finalBody.add(result);
} else if (value is File) {
String fileName = value.path.split('/').last;
final multipart =
await MultipartFile.fromFile(value.path, filename: fileName);

FormData formData = FormData.fromMap({
key: multipart,
});

body = formData;
break;
}

if (mapEntry.value is Map) {
_updateBody(context.mounted ? context : context, mapEntry.value);
}

return finalBody;
} else {
return body;
if (mapEntry.value is List) {
for (Map<String, dynamic> listItem in mapEntry.value) {
_updateBody(context.mounted ? context : context, listItem);
}
}
}
} else if (body is List) {
List<dynamic> updatedList = [];
for (dynamic value in body) {
final updatedValue = await _updateBody(context, value);
updatedList.add(updatedValue);
}

body = updatedList;
}

return body;
}
}

0 comments on commit a327084

Please sign in to comment.