From 8aa1a4aa963b9ba28b9e520dd680d8a1c4ac7efd Mon Sep 17 00:00:00 2001 From: Aasim Khan Date: Wed, 27 Sep 2023 16:08:37 +0400 Subject: [PATCH 1/4] Refactored update body json in `mirai_network_service` for deep into every obj of the json --- .../assets/json/sign_in_screen.json | 2 +- .../src/services/mirai_network_service.dart | 111 ++++++++++++------ 2 files changed, 79 insertions(+), 34 deletions(-) diff --git a/examples/mirai_gallery/assets/json/sign_in_screen.json b/examples/mirai_gallery/assets/json/sign_in_screen.json index 652ed9c2..7a5d565f 100644 --- a/examples/mirai_gallery/assets/json/sign_in_screen.json +++ b/examples/mirai_gallery/assets/json/sign_in_screen.json @@ -121,7 +121,7 @@ "action": { "actionType": "navigate", "navigationStyle": "pushReplacement", - "assetPath": "assets/jsons/kyc_intro_screen.json" + "assetPath": "assets/json/user_profile_screen.json" } }, { diff --git a/packages/mirai/lib/src/services/mirai_network_service.dart b/packages/mirai/lib/src/services/mirai_network_service.dart index 89ff9a27..1c44e789 100644 --- a/packages/mirai/lib/src/services/mirai_network_service.dart +++ b/packages/mirai/lib/src/services/mirai_network_service.dart @@ -45,10 +45,10 @@ class MiraiNetworkService { MiraiNetworkRequest request, BuildContext context, ) async { - final body = await _getBody(context, request.body); + await _updateBody(context, request.body); return _dio.post( request.url, - data: body, + data: request.body, queryParameters: request.queryParameters, options: Options( contentType: request.contentType, @@ -81,52 +81,97 @@ class MiraiNetworkService { ); } - static Future _getBody( + static Future _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')) { + 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.value( Mirai.onCallFromJson(value as Map, 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 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, + }); + + return formData; } - return finalBody; - } else { - return body; + if (mapEntry.value is Map) { + _updateBody(context.mounted ? context : context, mapEntry.value); + } + + if (mapEntry.value is List) { + for (Map listItem in mapEntry.value) { + _updateBody(context.mounted ? context : context, listItem); + } + } } + + // dynamic finalBody = {}; + + // await Future.forEach(body.keys, (key) async { + // final value = body[key]; + // if (value is Map && value.containsKey('actionType')) { + // Log.d("Loading from an action callback"); + + // final dynamic callbackValue = await Future.value( + // Mirai.onCallFromJson(value as Map, 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, + // }); + + // finalBody = formData; + // } else { + // finalBody[key] = value; + // } + // }); + + // return finalBody; + } + + // else if (body is List) { + // // List finalBody = []; + // // for (dynamic value in body) { + // // final result = await _getBody(context, value); + // // finalBody.add(result); + // // } + + // // return finalBody; + // } + + else { + return body; } } } From 86e5c3982d1442bc6395356e034777f8439b414c Mon Sep 17 00:00:00 2001 From: Aasim Khan Date: Wed, 27 Sep 2023 16:59:57 +0400 Subject: [PATCH 2/4] Updated body from json in `mirai_network_service` to handle list of body items --- .../src/services/mirai_network_service.dart | 59 ++++--------------- 1 file changed, 13 insertions(+), 46 deletions(-) diff --git a/packages/mirai/lib/src/services/mirai_network_service.dart b/packages/mirai/lib/src/services/mirai_network_service.dart index 1c44e789..786edea9 100644 --- a/packages/mirai/lib/src/services/mirai_network_service.dart +++ b/packages/mirai/lib/src/services/mirai_network_service.dart @@ -45,10 +45,10 @@ class MiraiNetworkService { MiraiNetworkRequest request, BuildContext context, ) async { - await _updateBody(context, request.body); + final body = await _updateBody(context, request.body); return _dio.post( request.url, - data: request.body, + data: body, queryParameters: request.queryParameters, options: Options( contentType: request.contentType, @@ -85,7 +85,7 @@ class MiraiNetworkService { BuildContext context, dynamic body, ) async { - if (body != null) { + if (body is Map) { for (dynamic mapEntry in body.entries) { final key = mapEntry.key; final value = mapEntry.value; @@ -115,7 +115,8 @@ class MiraiNetworkService { key: multipart, }); - return formData; + body = formData; + break; } if (mapEntry.value is Map) { @@ -128,50 +129,16 @@ class MiraiNetworkService { } } } + } else if (body is List) { + List updatedList = []; + for (dynamic value in body) { + final updatedValue = await _updateBody(context, value); + updatedList.add(updatedValue); + } - // dynamic finalBody = {}; - - // await Future.forEach(body.keys, (key) async { - // final value = body[key]; - // if (value is Map && value.containsKey('actionType')) { - // Log.d("Loading from an action callback"); - - // final dynamic callbackValue = await Future.value( - // Mirai.onCallFromJson(value as Map, 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, - // }); - - // finalBody = formData; - // } else { - // finalBody[key] = value; - // } - // }); - - // return finalBody; + body = updatedList; } - // else if (body is List) { - // // List finalBody = []; - // // for (dynamic value in body) { - // // final result = await _getBody(context, value); - // // finalBody.add(result); - // // } - - // // return finalBody; - // } - - else { - return body; - } + return body; } } From 1db557d81a20822d55659764f71faec56ad3c4b1 Mon Sep 17 00:00:00 2001 From: Aasim Khan Date: Thu, 28 Sep 2023 11:25:55 +0400 Subject: [PATCH 3/4] Added default case for status-code not provided --- .../mirai_network_request_parser.dart | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request_parser.dart b/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request_parser.dart index c5da410f..5894f294 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request_parser.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request_parser.dart @@ -26,9 +26,35 @@ class MiraiNetworkRequestParser extends MiraiActionParser { 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, + "align": "center", + "style": {"fontSize": 18} + }, + "content": { + "type": "padding", + "padding": {"top": 8, "left": 12, "right": 12, "bottom": 8}, + "child": { + "type": "text", + "data": response?.data, + "align": "center", + "style": {"fontSize": 12} + } + } + } + }, + ), + ); return Mirai.onCallFromJson( expectedResult.action, context.mounted ? context : context); From 0ee5d7d10736f19782be471ffb23d8546043a558 Mon Sep 17 00:00:00 2001 From: Aasim Khan Date: Mon, 2 Oct 2023 12:34:19 +0400 Subject: [PATCH 4/4] Updated response message and title in network_request_parser --- .../mirai_network_request_parser.dart | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request_parser.dart b/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request_parser.dart index 5894f294..fc9eba7c 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request_parser.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request_parser.dart @@ -37,17 +37,17 @@ class MiraiNetworkRequestParser extends MiraiActionParser { "type": "alertDialog", "title": { "type": "text", - "data": response?.statusMessage, - "align": "center", + "data": "${response?.statusMessage} - ${response?.statusCode}", + "textAlign": "center", "style": {"fontSize": 18} }, "content": { "type": "padding", - "padding": {"top": 8, "left": 12, "right": 12, "bottom": 8}, + "padding": {"top": 8, "left": 12, "right": 12, "bottom": 12}, "child": { "type": "text", - "data": response?.data, - "align": "center", + "data": "${response?.data}", + "textAlign": "center", "style": {"fontSize": 12} } }