Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asim/form builder/updated request body #221

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}