diff --git a/examples/mirai_gallery/assets/json/example_light_theme.json b/examples/mirai_gallery/assets/json/example_light_theme.json index 5cb79b8e..48a2ca08 100644 --- a/examples/mirai_gallery/assets/json/example_light_theme.json +++ b/examples/mirai_gallery/assets/json/example_light_theme.json @@ -1,7 +1,6 @@ { "brightness": "light", "disabledColor": "#60FEF7FF", - "fontFamily": "Handjet", "colorScheme": { "brightness": "light", "primary": "#6750a4", diff --git a/examples/mirai_gallery/assets/json/sign_in_screen.json b/examples/mirai_gallery/assets/json/sign_in_screen.json new file mode 100644 index 00000000..7a5d565f --- /dev/null +++ b/examples/mirai_gallery/assets/json/sign_in_screen.json @@ -0,0 +1,186 @@ +{ + "type": "scaffold", + "resizeToAvoidBottomInset": false, + "appBar": { + "type": "appBar" + }, + "body": { + "type": "padding", + "padding": { + "top": 24, + "bottom": 40, + "left": 24, + "right": 24 + }, + "child": { + "type": "form", + "child": { + "type": "column", + "crossAxisAlignment": "start", + "children": [ + { + "type": "text", + "data": "Welcome Back!", + "style": { + "fontSize": 32, + "fontWeight": "w700" + } + }, + { + "type": "sizedBox", + "height": 40 + }, + { + "id": "email", + "type": "textFormField", + "keyboardType": "emailAddress", + "textInputAction": "next", + "maxLines": 1, + "decoration": { + "hintText": "Email Address" + } + }, + { + "type": "sizedBox", + "height": 16 + }, + { + "id": "password", + "type": "textFormField", + "keyboardType": "visiblePassword", + "textInputAction": "done", + "maxLines": 1, + "obscureText": true, + "decoration": { + "hintText": "Password", + "suffixIcon": { + "type": "icon", + "iconType": "cupertino", + "icon": "eye", + "size": 20 + } + } + }, + { + "type": "sizedBox", + "height": 8 + }, + { + "type": "row", + "mainAxisAlignment": "end", + "children": [ + { + "type": "textButton", + "onPressed": {}, + "child": { + "type": "text", + "data": "Forgot Password?", + "style": { + "fontSize": 14, + "fontWeight": "w500", + "color": "#80FFFFFF" + } + } + } + ] + }, + { + "type": "expanded", + "child": { + "type": "sizedBox", + "height": 24 + } + }, + { + "type": "elevatedButton", + "child": { + "type": "text", + "data": "Sign In" + }, + "onPressed": { + "actionType": "networkRequest", + "url": "https://demo-identity.securrency.com/connect/token", + "method": "post", + "contentType": "application/x-www-form-urlencoded", + "body": { + "username": { + "actionType": "getFormDataValue", + "id": "email" + }, + "password": { + "actionType": "getFormDataValue", + "id": "password" + }, + "client_id": "mobile", + "scope": "openid email roles profile identity.public identity.admin entity securrency.finance securrency.exchange securrency.ens", + "grant_type": "mobile" + }, + "results": [ + { + "statusCode": 200, + "action": { + "actionType": "navigate", + "navigationStyle": "pushReplacement", + "assetPath": "assets/json/user_profile_screen.json" + } + }, + { + "statusCode": 400, + "action": { + "actionType": "showDialog", + "widget": { + "type": "alertDialog", + "titlePadding": { + "top": 8, + "left": 12, + "right": 12 + }, + "contentPadding": { + "top": 8, + "left": 12, + "right": 12 + }, + "title": { + "type": "text", + "data": "Failed to SignIn", + "align": "center", + "style": { + "fontSize": 21 + } + }, + "content": { + "type": "text", + "data": "Invalid username or password.", + "align": "center", + "style": { + "fontSize": 14 + } + }, + "actions": [ + { + "type": "textButton", + "child": { + "type": "text", + "data": "OK" + }, + "onPressed": { + "actionType": "navigate", + "navigationStyle": "pop" + } + }, + { + "type": "sizedBox", + "width": 12 + } + ] + } + } + } + ] + } + } + ] + } + } + } +} \ No newline at end of file diff --git a/examples/mirai_gallery/assets/json/user_profile_screen.json b/examples/mirai_gallery/assets/json/user_profile_screen.json new file mode 100644 index 00000000..851e713f --- /dev/null +++ b/examples/mirai_gallery/assets/json/user_profile_screen.json @@ -0,0 +1,43 @@ +{ + "type": "scaffold", + "resizeToAvoidBottomInset": false, + "appBar": { + "type": "appBar", + "title": { + "type": "text", + "data": "Welcome!" + } + }, + "body": { + "type": "column", + "crossAxisAlignment": "start", + "children": [ + { + "type": "expanded", + "child": { + "type": "padding", + "padding": { + "top": 24, + "left": 24, + "right": 24, + "bottom": 24 + }, + "child": { + "type": "column", + "children": [ + { + "type": "text", + "data": "You will see your profile details here.", + "textAlign": "center", + "style": { + "fontSize": 16, + "fontWeight": "w400" + } + } + ] + } + } + } + ] + } +} \ No newline at end of file diff --git a/examples/mirai_gallery/lib/app/example/example_screen.freezed.dart b/examples/mirai_gallery/lib/app/example/example_screen.freezed.dart index 7a3de3b6..d3ec0faf 100644 --- a/examples/mirai_gallery/lib/app/example/example_screen.freezed.dart +++ b/examples/mirai_gallery/lib/app/example/example_screen.freezed.dart @@ -62,22 +62,22 @@ class _$ExampleScreenCopyWithImpl<$Res, $Val extends ExampleScreen> } /// @nodoc -abstract class _$$_ExampleScreenCopyWith<$Res> +abstract class _$$ExampleScreenImplCopyWith<$Res> implements $ExampleScreenCopyWith<$Res> { - factory _$$_ExampleScreenCopyWith( - _$_ExampleScreen value, $Res Function(_$_ExampleScreen) then) = - __$$_ExampleScreenCopyWithImpl<$Res>; + factory _$$ExampleScreenImplCopyWith( + _$ExampleScreenImpl value, $Res Function(_$ExampleScreenImpl) then) = + __$$ExampleScreenImplCopyWithImpl<$Res>; @override @useResult $Res call({String assetPath}); } /// @nodoc -class __$$_ExampleScreenCopyWithImpl<$Res> - extends _$ExampleScreenCopyWithImpl<$Res, _$_ExampleScreen> - implements _$$_ExampleScreenCopyWith<$Res> { - __$$_ExampleScreenCopyWithImpl( - _$_ExampleScreen _value, $Res Function(_$_ExampleScreen) _then) +class __$$ExampleScreenImplCopyWithImpl<$Res> + extends _$ExampleScreenCopyWithImpl<$Res, _$ExampleScreenImpl> + implements _$$ExampleScreenImplCopyWith<$Res> { + __$$ExampleScreenImplCopyWithImpl( + _$ExampleScreenImpl _value, $Res Function(_$ExampleScreenImpl) _then) : super(_value, _then); @pragma('vm:prefer-inline') @@ -85,7 +85,7 @@ class __$$_ExampleScreenCopyWithImpl<$Res> $Res call({ Object? assetPath = null, }) { - return _then(_$_ExampleScreen( + return _then(_$ExampleScreenImpl( assetPath: null == assetPath ? _value.assetPath : assetPath // ignore: cast_nullable_to_non_nullable @@ -96,11 +96,11 @@ class __$$_ExampleScreenCopyWithImpl<$Res> /// @nodoc @JsonSerializable() -class _$_ExampleScreen implements _ExampleScreen { - const _$_ExampleScreen({required this.assetPath}); +class _$ExampleScreenImpl implements _ExampleScreen { + const _$ExampleScreenImpl({required this.assetPath}); - factory _$_ExampleScreen.fromJson(Map json) => - _$$_ExampleScreenFromJson(json); + factory _$ExampleScreenImpl.fromJson(Map json) => + _$$ExampleScreenImplFromJson(json); @override final String assetPath; @@ -114,7 +114,7 @@ class _$_ExampleScreen implements _ExampleScreen { bool operator ==(dynamic other) { return identical(this, other) || (other.runtimeType == runtimeType && - other is _$_ExampleScreen && + other is _$ExampleScreenImpl && (identical(other.assetPath, assetPath) || other.assetPath == assetPath)); } @@ -126,12 +126,12 @@ class _$_ExampleScreen implements _ExampleScreen { @JsonKey(ignore: true) @override @pragma('vm:prefer-inline') - _$$_ExampleScreenCopyWith<_$_ExampleScreen> get copyWith => - __$$_ExampleScreenCopyWithImpl<_$_ExampleScreen>(this, _$identity); + _$$ExampleScreenImplCopyWith<_$ExampleScreenImpl> get copyWith => + __$$ExampleScreenImplCopyWithImpl<_$ExampleScreenImpl>(this, _$identity); @override Map toJson() { - return _$$_ExampleScreenToJson( + return _$$ExampleScreenImplToJson( this, ); } @@ -139,15 +139,15 @@ class _$_ExampleScreen implements _ExampleScreen { abstract class _ExampleScreen implements ExampleScreen { const factory _ExampleScreen({required final String assetPath}) = - _$_ExampleScreen; + _$ExampleScreenImpl; factory _ExampleScreen.fromJson(Map json) = - _$_ExampleScreen.fromJson; + _$ExampleScreenImpl.fromJson; @override String get assetPath; @override @JsonKey(ignore: true) - _$$_ExampleScreenCopyWith<_$_ExampleScreen> get copyWith => + _$$ExampleScreenImplCopyWith<_$ExampleScreenImpl> get copyWith => throw _privateConstructorUsedError; } diff --git a/examples/mirai_gallery/lib/app/example/example_screen.g.dart b/examples/mirai_gallery/lib/app/example/example_screen.g.dart index e05d6bc5..49fa1de9 100644 --- a/examples/mirai_gallery/lib/app/example/example_screen.g.dart +++ b/examples/mirai_gallery/lib/app/example/example_screen.g.dart @@ -6,12 +6,12 @@ part of 'example_screen.dart'; // JsonSerializableGenerator // ************************************************************************** -_$_ExampleScreen _$$_ExampleScreenFromJson(Map json) => - _$_ExampleScreen( +_$ExampleScreenImpl _$$ExampleScreenImplFromJson(Map json) => + _$ExampleScreenImpl( assetPath: json['assetPath'] as String, ); -Map _$$_ExampleScreenToJson(_$_ExampleScreen instance) => +Map _$$ExampleScreenImplToJson(_$ExampleScreenImpl instance) => { 'assetPath': instance.assetPath, }; diff --git a/examples/mirai_gallery/lib/main.dart b/examples/mirai_gallery/lib/main.dart index 7d6046ac..1afdb79c 100644 --- a/examples/mirai_gallery/lib/main.dart +++ b/examples/mirai_gallery/lib/main.dart @@ -1,3 +1,7 @@ +import 'dart:io'; + +import 'package:dio/dio.dart'; +import 'package:dio/io.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mirai/mirai.dart'; @@ -8,11 +12,18 @@ import 'package:mirai_gallery/app_theme/app_theme_cubit.dart'; import 'package:mirai_webview/mirai_webview.dart'; void main() async { + Dio dio = Dio(); + (dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () => + HttpClient() + ..badCertificateCallback = + (X509Certificate cert, String host, int port) => true; + await Mirai.initialize( parsers: const [ ExampleScreenParser(), MiraiWebViewParser(), ], + dio: dio, ); runApp(const MyApp()); diff --git a/examples/mirai_gallery/pubspec.lock b/examples/mirai_gallery/pubspec.lock index 373174eb..b81420b0 100644 --- a/examples/mirai_gallery/pubspec.lock +++ b/examples/mirai_gallery/pubspec.lock @@ -77,10 +77,10 @@ packages: dependency: transitive description: name: build_resolvers - sha256: d912852cce27c9e80a93603db721c267716894462e7033165178b91138587972 + sha256: "64e12b0521812d1684b1917bc80945625391cb9bdd4312536b1d69dcb6133ed8" url: "https://pub.dev" source: hosted - version: "2.3.2" + version: "2.4.1" build_runner: dependency: "direct dev" description: @@ -93,10 +93,10 @@ packages: dependency: transitive description: name: build_runner_core - sha256: "6d6ee4276b1c5f34f21fdf39425202712d2be82019983d52f351c94aafbc2c41" + sha256: c9e32d21dd6626b5c163d48b037ce906bbe428bc23ab77bcd77bb21e593b6185 url: "https://pub.dev" source: hosted - version: "7.2.10" + version: "7.2.11" built_collection: dependency: transitive description: @@ -109,10 +109,10 @@ packages: dependency: transitive description: name: built_value - sha256: ff627b645b28fb8bdb69e645f910c2458fd6b65f6585c3a53e0626024897dedf + sha256: a8de5955205b4d1dbbbc267daddf2178bd737e4bab8987c04a500478c9651e74 url: "https://pub.dev" source: hosted - version: "8.6.2" + version: "8.6.3" characters: dependency: transitive description: @@ -141,10 +141,10 @@ packages: dependency: transitive description: name: code_builder - sha256: "315a598c7fbe77f22de1c9da7cfd6fd21816312f16ffa124453b4fc679e540f1" + sha256: "1be9be30396d7e4c0db42c35ea6ccd7cc6a1e19916b5dc64d6ac216b5544d677" url: "https://pub.dev" source: hosted - version: "4.6.0" + version: "4.7.0" collection: dependency: transitive description: @@ -181,18 +181,18 @@ packages: dependency: transitive description: name: dart_style - sha256: "1efa911ca7086affd35f463ca2fc1799584fb6aa89883cf0af8e3664d6a02d55" + sha256: abd7625e16f51f554ea244d090292945ec4d4be7bfbaf2ec8cccea568919d334 url: "https://pub.dev" source: hosted - version: "2.3.2" + version: "2.3.3" dio: - dependency: transitive + dependency: "direct main" description: name: dio - sha256: ce75a1b40947fea0a0e16ce73337122a86762e38b982e1ccb909daa3b9bc4197 + sha256: "417e2a6f9d83ab396ec38ff4ea5da6c254da71e4db765ad737a42af6930140b7" url: "https://pub.dev" source: hosted - version: "5.3.2" + version: "5.3.3" fake_async: dependency: transitive description: @@ -247,10 +247,10 @@ packages: dependency: "direct dev" description: name: freezed - sha256: "83462cfc33dc9680533a7f3a4a6ab60aa94f287db5f4ee6511248c22833c497f" + sha256: "21bf2825311de65501d22e563e3d7605dff57fb5e6da982db785ae5372ff018a" url: "https://pub.dev" source: hosted - version: "2.4.2" + version: "2.4.5" freezed_annotation: dependency: "direct main" description: @@ -617,18 +617,18 @@ packages: dependency: transitive description: name: webview_flutter - sha256: "82f6787d5df55907aa01e49bd9644f4ed1cc82af7a8257dd9947815959d2e755" + sha256: c1ab9b81090705c6069197d9fdc1625e587b52b8d70cdde2339d177ad0dbb98e url: "https://pub.dev" source: hosted - version: "4.2.4" + version: "4.4.1" webview_flutter_android: dependency: transitive description: name: webview_flutter_android - sha256: "9427774649fd3c8b7ff53523051395d13aed2ca355822b822e6493d79f5fc05a" + sha256: b0cd33dd7d3dd8e5f664e11a19e17ba12c352647269921a3b568406b001f1dff url: "https://pub.dev" source: hosted - version: "3.10.0" + version: "3.12.0" webview_flutter_platform_interface: dependency: transitive description: @@ -641,10 +641,10 @@ packages: dependency: transitive description: name: webview_flutter_wkwebview - sha256: d2f7241849582da80b79acb03bb936422412ce5c0c79fb5f6a1de5421a5aecc4 + sha256: b4b42295b3aa91ed22ba6d3dd7de56efbb8f3ab3d6e41d8b1615d13537c7801d url: "https://pub.dev" source: hosted - version: "3.7.4" + version: "3.9.2" yaml: dependency: transitive description: diff --git a/examples/mirai_gallery/pubspec.yaml b/examples/mirai_gallery/pubspec.yaml index 3569c1f3..68cd1544 100644 --- a/examples/mirai_gallery/pubspec.yaml +++ b/examples/mirai_gallery/pubspec.yaml @@ -39,6 +39,7 @@ dependencies: flutter_bloc: ^8.1.3 mirai: ^0.5.1 mirai_webview: ^0.0.2 + dio: dev_dependencies: flutter_test: diff --git a/packages/mirai/lib/mirai.dart b/packages/mirai/lib/mirai.dart index 0849b5d7..6aac5829 100644 --- a/packages/mirai/lib/mirai.dart +++ b/packages/mirai/lib/mirai.dart @@ -2,7 +2,7 @@ library mirai; export 'package:mirai/src/action_parsers/action_parsers.dart'; export 'package:mirai/src/framework/framework.dart'; -export 'package:mirai/src/network/network.dart'; export 'package:mirai/src/parsers/parsers.dart'; export 'package:mirai/src/utils/utils.dart'; export 'package:mirai_framework/mirai_framework.dart'; +export 'package:mirai/src/services/services.dart'; diff --git a/packages/mirai/lib/src/action_parsers/action_parsers.dart b/packages/mirai/lib/src/action_parsers/action_parsers.dart index 817f1d80..95d53e88 100644 --- a/packages/mirai/lib/src/action_parsers/action_parsers.dart +++ b/packages/mirai/lib/src/action_parsers/action_parsers.dart @@ -1,5 +1,6 @@ -export 'package:mirai/src/action_parsers/mirai_dialog_action/mirai_dialog_action.dart'; export 'package:mirai/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action_parser.dart'; -export 'package:mirai/src/action_parsers/mirai_navigate_action/mirai_navigate_action_parser.dart'; -export 'package:mirai/src/action_parsers/mirai_none_action/mirai_none_action_parser.dart'; -export 'package:mirai/src/action_parsers/mirai_request_action/mirai_request_action_parser.dart'; +export 'package:mirai/src/action_parsers/mirai_navigate/mirai_navigate_action_parser.dart'; +export 'package:mirai/src/action_parsers/mirai_dialog_action/mirai_dialog_action_parser.dart'; +export 'package:mirai/src/action_parsers/mirai_form_data/mirai_form_data_parser.dart'; +export 'package:mirai/src/action_parsers/mirai_network_request/mirai_network_request_parser.dart'; +export 'package:mirai/src/action_parsers/mirai_network_result/mirai_network_result.dart'; diff --git a/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action.dart b/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action.dart index 76de74dd..aca00f8c 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action.dart @@ -1,8 +1,6 @@ import 'package:flutter/material.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; -import 'package:mirai/src/network/mirai_request.dart'; - -export 'mirai_dialog_action_parser.dart'; +import 'package:mirai/src/action_parsers/mirai_network_request/mirai_network_request.dart'; part 'mirai_dialog_action.freezed.dart'; part 'mirai_dialog_action.g.dart'; @@ -11,7 +9,7 @@ part 'mirai_dialog_action.g.dart'; class MiraiDialogAction with _$MiraiDialogAction { const factory MiraiDialogAction({ Map? widget, - MiraiRequest? request, + MiraiNetworkRequest? request, String? assetPath, @Default(true) bool barrierDismissible, String? barrierColor, diff --git a/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action.freezed.dart b/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action.freezed.dart index 007de1e1..9d4d42a6 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action.freezed.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action.freezed.dart @@ -21,7 +21,7 @@ MiraiDialogAction _$MiraiDialogActionFromJson(Map json) { /// @nodoc mixin _$MiraiDialogAction { Map? get widget => throw _privateConstructorUsedError; - MiraiRequest? get request => throw _privateConstructorUsedError; + MiraiNetworkRequest? get request => throw _privateConstructorUsedError; String? get assetPath => throw _privateConstructorUsedError; bool get barrierDismissible => throw _privateConstructorUsedError; String? get barrierColor => throw _privateConstructorUsedError; @@ -44,7 +44,7 @@ abstract class $MiraiDialogActionCopyWith<$Res> { @useResult $Res call( {Map? widget, - MiraiRequest? request, + MiraiNetworkRequest? request, String? assetPath, bool barrierDismissible, String? barrierColor, @@ -52,7 +52,7 @@ abstract class $MiraiDialogActionCopyWith<$Res> { bool useSafeArea, TraversalEdgeBehavior? traversalEdgeBehavior}); - $MiraiRequestCopyWith<$Res>? get request; + $MiraiNetworkRequestCopyWith<$Res>? get request; } /// @nodoc @@ -85,7 +85,7 @@ class _$MiraiDialogActionCopyWithImpl<$Res, $Val extends MiraiDialogAction> request: freezed == request ? _value.request : request // ignore: cast_nullable_to_non_nullable - as MiraiRequest?, + as MiraiNetworkRequest?, assetPath: freezed == assetPath ? _value.assetPath : assetPath // ignore: cast_nullable_to_non_nullable @@ -115,12 +115,12 @@ class _$MiraiDialogActionCopyWithImpl<$Res, $Val extends MiraiDialogAction> @override @pragma('vm:prefer-inline') - $MiraiRequestCopyWith<$Res>? get request { + $MiraiNetworkRequestCopyWith<$Res>? get request { if (_value.request == null) { return null; } - return $MiraiRequestCopyWith<$Res>(_value.request!, (value) { + return $MiraiNetworkRequestCopyWith<$Res>(_value.request!, (value) { return _then(_value.copyWith(request: value) as $Val); }); } @@ -136,7 +136,7 @@ abstract class _$$_MiraiDialogActionCopyWith<$Res> @useResult $Res call( {Map? widget, - MiraiRequest? request, + MiraiNetworkRequest? request, String? assetPath, bool barrierDismissible, String? barrierColor, @@ -145,7 +145,7 @@ abstract class _$$_MiraiDialogActionCopyWith<$Res> TraversalEdgeBehavior? traversalEdgeBehavior}); @override - $MiraiRequestCopyWith<$Res>? get request; + $MiraiNetworkRequestCopyWith<$Res>? get request; } /// @nodoc @@ -176,7 +176,7 @@ class __$$_MiraiDialogActionCopyWithImpl<$Res> request: freezed == request ? _value.request : request // ignore: cast_nullable_to_non_nullable - as MiraiRequest?, + as MiraiNetworkRequest?, assetPath: freezed == assetPath ? _value.assetPath : assetPath // ignore: cast_nullable_to_non_nullable @@ -233,7 +233,7 @@ class _$_MiraiDialogAction implements _MiraiDialogAction { } @override - final MiraiRequest? request; + final MiraiNetworkRequest? request; @override final String? assetPath; @override @@ -306,7 +306,7 @@ class _$_MiraiDialogAction implements _MiraiDialogAction { abstract class _MiraiDialogAction implements MiraiDialogAction { const factory _MiraiDialogAction( {final Map? widget, - final MiraiRequest? request, + final MiraiNetworkRequest? request, final String? assetPath, final bool barrierDismissible, final String? barrierColor, @@ -321,7 +321,7 @@ abstract class _MiraiDialogAction implements MiraiDialogAction { @override Map? get widget; @override - MiraiRequest? get request; + MiraiNetworkRequest? get request; @override String? get assetPath; @override diff --git a/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action.g.dart b/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action.g.dart index 45969cde..dd37fefc 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action.g.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action.g.dart @@ -11,7 +11,8 @@ _$_MiraiDialogAction _$$_MiraiDialogActionFromJson(Map json) => widget: json['widget'] as Map?, request: json['request'] == null ? null - : MiraiRequest.fromJson(json['request'] as Map), + : MiraiNetworkRequest.fromJson( + json['request'] as Map), assetPath: json['assetPath'] as String?, barrierDismissible: json['barrierDismissible'] as bool? ?? true, barrierColor: json['barrierColor'] as String?, diff --git a/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action_parser.dart b/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action_parser.dart index 2e2cad10..9f782394 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action_parser.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_dialog_action/mirai_dialog_action_parser.dart @@ -7,6 +7,8 @@ import 'package:mirai/src/utils/action_type.dart'; import 'package:mirai/src/utils/color_utils.dart'; import 'package:mirai_framework/mirai_framework.dart'; +export 'mirai_dialog_action.dart'; + class MiraiDialogActionParser extends MiraiActionParser { const MiraiDialogActionParser(); @@ -35,7 +37,7 @@ class MiraiDialogActionParser extends MiraiActionParser { return _showDialog( context, model, - Mirai.fromNetwork(model.request!), + Mirai.fromNetwork(model.request!, context), ); } } diff --git a/packages/mirai/lib/src/action_parsers/mirai_form_data/mirai_form_data.dart b/packages/mirai/lib/src/action_parsers/mirai_form_data/mirai_form_data.dart new file mode 100644 index 00000000..2335d151 --- /dev/null +++ b/packages/mirai/lib/src/action_parsers/mirai_form_data/mirai_form_data.dart @@ -0,0 +1,14 @@ +import 'package:freezed_annotation/freezed_annotation.dart'; + +part 'mirai_form_data.freezed.dart'; +part 'mirai_form_data.g.dart'; + +@freezed +class MiraiFormData with _$MiraiFormData { + const factory MiraiFormData({ + required String id, + }) = _MiraiFormData; + + factory MiraiFormData.fromJson(Map json) => + _$MiraiFormDataFromJson(json); +} diff --git a/packages/mirai/lib/src/action_parsers/mirai_form_data/mirai_form_data.freezed.dart b/packages/mirai/lib/src/action_parsers/mirai_form_data/mirai_form_data.freezed.dart new file mode 100644 index 00000000..ea0bbc75 --- /dev/null +++ b/packages/mirai/lib/src/action_parsers/mirai_form_data/mirai_form_data.freezed.dart @@ -0,0 +1,151 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: type=lint +// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark + +part of 'mirai_form_data.dart'; + +// ************************************************************************** +// FreezedGenerator +// ************************************************************************** + +T _$identity(T value) => value; + +final _privateConstructorUsedError = UnsupportedError( + 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods'); + +MiraiFormData _$MiraiFormDataFromJson(Map json) { + return _MiraiFormData.fromJson(json); +} + +/// @nodoc +mixin _$MiraiFormData { + String get id => throw _privateConstructorUsedError; + + Map toJson() => throw _privateConstructorUsedError; + @JsonKey(ignore: true) + $MiraiFormDataCopyWith get copyWith => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $MiraiFormDataCopyWith<$Res> { + factory $MiraiFormDataCopyWith( + MiraiFormData value, $Res Function(MiraiFormData) then) = + _$MiraiFormDataCopyWithImpl<$Res, MiraiFormData>; + @useResult + $Res call({String id}); +} + +/// @nodoc +class _$MiraiFormDataCopyWithImpl<$Res, $Val extends MiraiFormData> + implements $MiraiFormDataCopyWith<$Res> { + _$MiraiFormDataCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? id = null, + }) { + return _then(_value.copyWith( + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as String, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$_MiraiFormDataCopyWith<$Res> + implements $MiraiFormDataCopyWith<$Res> { + factory _$$_MiraiFormDataCopyWith( + _$_MiraiFormData value, $Res Function(_$_MiraiFormData) then) = + __$$_MiraiFormDataCopyWithImpl<$Res>; + @override + @useResult + $Res call({String id}); +} + +/// @nodoc +class __$$_MiraiFormDataCopyWithImpl<$Res> + extends _$MiraiFormDataCopyWithImpl<$Res, _$_MiraiFormData> + implements _$$_MiraiFormDataCopyWith<$Res> { + __$$_MiraiFormDataCopyWithImpl( + _$_MiraiFormData _value, $Res Function(_$_MiraiFormData) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? id = null, + }) { + return _then(_$_MiraiFormData( + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as String, + )); + } +} + +/// @nodoc +@JsonSerializable() +class _$_MiraiFormData implements _MiraiFormData { + const _$_MiraiFormData({required this.id}); + + factory _$_MiraiFormData.fromJson(Map json) => + _$$_MiraiFormDataFromJson(json); + + @override + final String id; + + @override + String toString() { + return 'MiraiFormData(id: $id)'; + } + + @override + bool operator ==(dynamic other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$_MiraiFormData && + (identical(other.id, id) || other.id == id)); + } + + @JsonKey(ignore: true) + @override + int get hashCode => Object.hash(runtimeType, id); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$_MiraiFormDataCopyWith<_$_MiraiFormData> get copyWith => + __$$_MiraiFormDataCopyWithImpl<_$_MiraiFormData>(this, _$identity); + + @override + Map toJson() { + return _$$_MiraiFormDataToJson( + this, + ); + } +} + +abstract class _MiraiFormData implements MiraiFormData { + const factory _MiraiFormData({required final String id}) = _$_MiraiFormData; + + factory _MiraiFormData.fromJson(Map json) = + _$_MiraiFormData.fromJson; + + @override + String get id; + @override + @JsonKey(ignore: true) + _$$_MiraiFormDataCopyWith<_$_MiraiFormData> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/packages/mirai/lib/src/action_parsers/mirai_form_data/mirai_form_data.g.dart b/packages/mirai/lib/src/action_parsers/mirai_form_data/mirai_form_data.g.dart new file mode 100644 index 00000000..7252433b --- /dev/null +++ b/packages/mirai/lib/src/action_parsers/mirai_form_data/mirai_form_data.g.dart @@ -0,0 +1,17 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'mirai_form_data.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$_MiraiFormData _$$_MiraiFormDataFromJson(Map json) => + _$_MiraiFormData( + id: json['id'] as String, + ); + +Map _$$_MiraiFormDataToJson(_$_MiraiFormData instance) => + { + 'id': instance.id, + }; diff --git a/packages/mirai/lib/src/action_parsers/mirai_form_data/mirai_form_data_parser.dart b/packages/mirai/lib/src/action_parsers/mirai_form_data/mirai_form_data_parser.dart new file mode 100644 index 00000000..1928866a --- /dev/null +++ b/packages/mirai/lib/src/action_parsers/mirai_form_data/mirai_form_data_parser.dart @@ -0,0 +1,25 @@ +import 'package:flutter/widgets.dart'; +import 'package:mirai/src/action_parsers/mirai_form_data/mirai_form_data.dart'; +import 'package:mirai/src/parsers/mirai_form/mirai_form_parser.dart'; +import 'package:mirai/src/utils/log.dart'; +import 'package:mirai_framework/mirai_framework.dart'; + +export 'mirai_form_data.dart'; + +class MiraiFormDataActionParser extends MiraiActionParser { + const MiraiFormDataActionParser(); + + @override + String get actionType => 'getFormDataValue'; + + @override + MiraiFormData getModel(Map json) => + MiraiFormData.fromJson(json); + + @override + String onCall(BuildContext context, MiraiFormData model) { + final MiraiFormScope formScope = MiraiFormScope.of(context); + Log.d("value ${model.id}: ${formScope.formData[model.id]}"); + return formScope.formData[model.id].toString(); + } +} diff --git a/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action.dart b/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action.dart index 376e7791..e9e9d8fa 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action.dart @@ -1,5 +1,5 @@ import 'package:freezed_annotation/freezed_annotation.dart'; -import 'package:mirai/src/network/mirai_request.dart'; +import 'package:mirai/src/action_parsers/mirai_network_request/mirai_network_request.dart'; import 'package:mirai/src/parsers/mirai_border/mirai_border.dart'; import 'package:mirai/src/parsers/mirai_box_constraints/mirai_box_constraints.dart'; @@ -10,7 +10,7 @@ part 'mirai_modal_bottom_sheet_action.g.dart'; class MiraiModalBottomSheetAction with _$MiraiModalBottomSheetAction { const factory MiraiModalBottomSheetAction({ Map? widget, - MiraiRequest? request, + MiraiNetworkRequest? request, String? assetPath, String? backgroundColor, String? barrierLabel, diff --git a/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action.freezed.dart b/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action.freezed.dart index 596c2f49..8c9eabf1 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action.freezed.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action.freezed.dart @@ -22,7 +22,7 @@ MiraiModalBottomSheetAction _$MiraiModalBottomSheetActionFromJson( /// @nodoc mixin _$MiraiModalBottomSheetAction { Map? get widget => throw _privateConstructorUsedError; - MiraiRequest? get request => throw _privateConstructorUsedError; + MiraiNetworkRequest? get request => throw _privateConstructorUsedError; String? get assetPath => throw _privateConstructorUsedError; String? get backgroundColor => throw _privateConstructorUsedError; String? get barrierLabel => throw _privateConstructorUsedError; @@ -53,7 +53,7 @@ abstract class $MiraiModalBottomSheetActionCopyWith<$Res> { @useResult $Res call( {Map? widget, - MiraiRequest? request, + MiraiNetworkRequest? request, String? assetPath, String? backgroundColor, String? barrierLabel, @@ -68,7 +68,7 @@ abstract class $MiraiModalBottomSheetActionCopyWith<$Res> { bool? showDragHandle, bool useSafeArea}); - $MiraiRequestCopyWith<$Res>? get request; + $MiraiNetworkRequestCopyWith<$Res>? get request; $MiraiBorderCopyWith<$Res>? get shape; $MiraiBoxConstraintsCopyWith<$Res>? get constraints; } @@ -111,7 +111,7 @@ class _$MiraiModalBottomSheetActionCopyWithImpl<$Res, request: freezed == request ? _value.request : request // ignore: cast_nullable_to_non_nullable - as MiraiRequest?, + as MiraiNetworkRequest?, assetPath: freezed == assetPath ? _value.assetPath : assetPath // ignore: cast_nullable_to_non_nullable @@ -169,12 +169,12 @@ class _$MiraiModalBottomSheetActionCopyWithImpl<$Res, @override @pragma('vm:prefer-inline') - $MiraiRequestCopyWith<$Res>? get request { + $MiraiNetworkRequestCopyWith<$Res>? get request { if (_value.request == null) { return null; } - return $MiraiRequestCopyWith<$Res>(_value.request!, (value) { + return $MiraiNetworkRequestCopyWith<$Res>(_value.request!, (value) { return _then(_value.copyWith(request: value) as $Val); }); } @@ -215,7 +215,7 @@ abstract class _$$_MiraiModalBottomSheetActionCopyWith<$Res> @useResult $Res call( {Map? widget, - MiraiRequest? request, + MiraiNetworkRequest? request, String? assetPath, String? backgroundColor, String? barrierLabel, @@ -231,7 +231,7 @@ abstract class _$$_MiraiModalBottomSheetActionCopyWith<$Res> bool useSafeArea}); @override - $MiraiRequestCopyWith<$Res>? get request; + $MiraiNetworkRequestCopyWith<$Res>? get request; @override $MiraiBorderCopyWith<$Res>? get shape; @override @@ -275,7 +275,7 @@ class __$$_MiraiModalBottomSheetActionCopyWithImpl<$Res> request: freezed == request ? _value.request : request // ignore: cast_nullable_to_non_nullable - as MiraiRequest?, + as MiraiNetworkRequest?, assetPath: freezed == assetPath ? _value.assetPath : assetPath // ignore: cast_nullable_to_non_nullable @@ -367,7 +367,7 @@ class _$_MiraiModalBottomSheetAction implements _MiraiModalBottomSheetAction { } @override - final MiraiRequest? request; + final MiraiNetworkRequest? request; @override final String? assetPath; @override @@ -478,7 +478,7 @@ abstract class _MiraiModalBottomSheetAction implements MiraiModalBottomSheetAction { const factory _MiraiModalBottomSheetAction( {final Map? widget, - final MiraiRequest? request, + final MiraiNetworkRequest? request, final String? assetPath, final String? backgroundColor, final String? barrierLabel, @@ -499,7 +499,7 @@ abstract class _MiraiModalBottomSheetAction @override Map? get widget; @override - MiraiRequest? get request; + MiraiNetworkRequest? get request; @override String? get assetPath; @override diff --git a/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action.g.dart b/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action.g.dart index 6c797ab2..c45a44b9 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action.g.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action.g.dart @@ -12,7 +12,8 @@ _$_MiraiModalBottomSheetAction _$$_MiraiModalBottomSheetActionFromJson( widget: json['widget'] as Map?, request: json['request'] == null ? null - : MiraiRequest.fromJson(json['request'] as Map), + : MiraiNetworkRequest.fromJson( + json['request'] as Map), assetPath: json['assetPath'] as String?, backgroundColor: json['backgroundColor'] as String?, barrierLabel: json['barrierLabel'] as String?, diff --git a/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action_parser.dart b/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action_parser.dart index 84cc69af..f060a4ab 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action_parser.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_modal_bottom_sheet_action/mirai_modal_bottom_sheet_action_parser.dart @@ -40,7 +40,7 @@ class MiraiModalBottomSheetActionParser return _showModalBottomSheet( context, model, - Mirai.fromNetwork(model.request!), + Mirai.fromNetwork(model.request!, context), ); } } diff --git a/packages/mirai/lib/src/action_parsers/mirai_navigate_action/mirai_navigate_action.dart b/packages/mirai/lib/src/action_parsers/mirai_navigate/mirai_navigate_action.dart similarity index 85% rename from packages/mirai/lib/src/action_parsers/mirai_navigate_action/mirai_navigate_action.dart rename to packages/mirai/lib/src/action_parsers/mirai_navigate/mirai_navigate_action.dart index 9298d3f1..a926430d 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_navigate_action/mirai_navigate_action.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_navigate/mirai_navigate_action.dart @@ -1,5 +1,5 @@ import 'package:freezed_annotation/freezed_annotation.dart'; -import 'package:mirai/src/network/mirai_request.dart'; +import 'package:mirai/src/action_parsers/mirai_network_request/mirai_network_request.dart'; part 'mirai_navigate_action.freezed.dart'; part 'mirai_navigate_action.g.dart'; @@ -20,7 +20,7 @@ class MiraiNavigateAction with _$MiraiNavigateAction { const MiraiNavigateAction._(); factory MiraiNavigateAction({ - MiraiRequest? request, + MiraiNetworkRequest? request, Map? widgetJson, String? assetPath, String? routeName, diff --git a/packages/mirai/lib/src/action_parsers/mirai_navigate_action/mirai_navigate_action.freezed.dart b/packages/mirai/lib/src/action_parsers/mirai_navigate/mirai_navigate_action.freezed.dart similarity index 95% rename from packages/mirai/lib/src/action_parsers/mirai_navigate_action/mirai_navigate_action.freezed.dart rename to packages/mirai/lib/src/action_parsers/mirai_navigate/mirai_navigate_action.freezed.dart index 1f77a153..08512c91 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_navigate_action/mirai_navigate_action.freezed.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_navigate/mirai_navigate_action.freezed.dart @@ -20,7 +20,7 @@ MiraiNavigateAction _$MiraiNavigateActionFromJson(Map json) { /// @nodoc mixin _$MiraiNavigateAction { - MiraiRequest? get request => throw _privateConstructorUsedError; + MiraiNetworkRequest? get request => throw _privateConstructorUsedError; Map? get widgetJson => throw _privateConstructorUsedError; String? get assetPath => throw _privateConstructorUsedError; String? get routeName => throw _privateConstructorUsedError; @@ -41,7 +41,7 @@ abstract class $MiraiNavigateActionCopyWith<$Res> { _$MiraiNavigateActionCopyWithImpl<$Res, MiraiNavigateAction>; @useResult $Res call( - {MiraiRequest? request, + {MiraiNetworkRequest? request, Map? widgetJson, String? assetPath, String? routeName, @@ -49,7 +49,7 @@ abstract class $MiraiNavigateActionCopyWith<$Res> { Map? result, Map? arguments}); - $MiraiRequestCopyWith<$Res>? get request; + $MiraiNetworkRequestCopyWith<$Res>? get request; } /// @nodoc @@ -77,7 +77,7 @@ class _$MiraiNavigateActionCopyWithImpl<$Res, $Val extends MiraiNavigateAction> request: freezed == request ? _value.request : request // ignore: cast_nullable_to_non_nullable - as MiraiRequest?, + as MiraiNetworkRequest?, widgetJson: freezed == widgetJson ? _value.widgetJson : widgetJson // ignore: cast_nullable_to_non_nullable @@ -107,12 +107,12 @@ class _$MiraiNavigateActionCopyWithImpl<$Res, $Val extends MiraiNavigateAction> @override @pragma('vm:prefer-inline') - $MiraiRequestCopyWith<$Res>? get request { + $MiraiNetworkRequestCopyWith<$Res>? get request { if (_value.request == null) { return null; } - return $MiraiRequestCopyWith<$Res>(_value.request!, (value) { + return $MiraiNetworkRequestCopyWith<$Res>(_value.request!, (value) { return _then(_value.copyWith(request: value) as $Val); }); } @@ -127,7 +127,7 @@ abstract class _$$_MiraiNavigateActionCopyWith<$Res> @override @useResult $Res call( - {MiraiRequest? request, + {MiraiNetworkRequest? request, Map? widgetJson, String? assetPath, String? routeName, @@ -136,7 +136,7 @@ abstract class _$$_MiraiNavigateActionCopyWith<$Res> Map? arguments}); @override - $MiraiRequestCopyWith<$Res>? get request; + $MiraiNetworkRequestCopyWith<$Res>? get request; } /// @nodoc @@ -162,7 +162,7 @@ class __$$_MiraiNavigateActionCopyWithImpl<$Res> request: freezed == request ? _value.request : request // ignore: cast_nullable_to_non_nullable - as MiraiRequest?, + as MiraiNetworkRequest?, widgetJson: freezed == widgetJson ? _value._widgetJson : widgetJson // ignore: cast_nullable_to_non_nullable @@ -211,7 +211,7 @@ class _$_MiraiNavigateAction extends _MiraiNavigateAction { _$$_MiraiNavigateActionFromJson(json); @override - final MiraiRequest? request; + final MiraiNetworkRequest? request; final Map? _widgetJson; @override Map? get widgetJson { @@ -301,7 +301,7 @@ class _$_MiraiNavigateAction extends _MiraiNavigateAction { abstract class _MiraiNavigateAction extends MiraiNavigateAction { factory _MiraiNavigateAction( - {final MiraiRequest? request, + {final MiraiNetworkRequest? request, final Map? widgetJson, final String? assetPath, final String? routeName, @@ -314,7 +314,7 @@ abstract class _MiraiNavigateAction extends MiraiNavigateAction { _$_MiraiNavigateAction.fromJson; @override - MiraiRequest? get request; + MiraiNetworkRequest? get request; @override Map? get widgetJson; @override diff --git a/packages/mirai/lib/src/action_parsers/mirai_navigate_action/mirai_navigate_action.g.dart b/packages/mirai/lib/src/action_parsers/mirai_navigate/mirai_navigate_action.g.dart similarity index 94% rename from packages/mirai/lib/src/action_parsers/mirai_navigate_action/mirai_navigate_action.g.dart rename to packages/mirai/lib/src/action_parsers/mirai_navigate/mirai_navigate_action.g.dart index cc320d18..f313d29d 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_navigate_action/mirai_navigate_action.g.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_navigate/mirai_navigate_action.g.dart @@ -11,7 +11,8 @@ _$_MiraiNavigateAction _$$_MiraiNavigateActionFromJson( _$_MiraiNavigateAction( request: json['request'] == null ? null - : MiraiRequest.fromJson(json['request'] as Map), + : MiraiNetworkRequest.fromJson( + json['request'] as Map), widgetJson: json['widgetJson'] as Map?, assetPath: json['assetPath'] as String?, routeName: json['routeName'] as String?, diff --git a/packages/mirai/lib/src/action_parsers/mirai_navigate_action/mirai_navigate_action_parser.dart b/packages/mirai/lib/src/action_parsers/mirai_navigate/mirai_navigate_action_parser.dart similarity index 93% rename from packages/mirai/lib/src/action_parsers/mirai_navigate_action/mirai_navigate_action_parser.dart rename to packages/mirai/lib/src/action_parsers/mirai_navigate/mirai_navigate_action_parser.dart index 0bea07e6..f69e5898 100644 --- a/packages/mirai/lib/src/action_parsers/mirai_navigate_action/mirai_navigate_action_parser.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_navigate/mirai_navigate_action_parser.dart @@ -1,11 +1,13 @@ import 'dart:async'; import 'package:flutter/material.dart'; -import 'package:mirai/src/action_parsers/mirai_navigate_action/mirai_navigate_action.dart'; +import 'package:mirai/src/action_parsers/mirai_navigate/mirai_navigate_action.dart'; import 'package:mirai/src/framework/framework.dart'; import 'package:mirai/src/utils/action_type.dart'; import 'package:mirai_framework/mirai_framework.dart'; +export 'mirai_navigate_action.dart'; + class MiraiNavigateActionParser extends MiraiActionParser { const MiraiNavigateActionParser(); @@ -22,7 +24,7 @@ class MiraiNavigateActionParser extends MiraiActionParser { if (model.widgetJson != null) { widget = Mirai.fromJson(model.widgetJson, context); } else if (model.request != null) { - widget = Mirai.fromNetwork(model.request!); + widget = Mirai.fromNetwork(model.request!, context); } else if (model.assetPath != null) { widget = Mirai.fromAssets(model.assetPath!); } diff --git a/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request.dart b/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request.dart new file mode 100644 index 00000000..2cbdee30 --- /dev/null +++ b/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request.dart @@ -0,0 +1,28 @@ +import 'package:freezed_annotation/freezed_annotation.dart'; +import 'package:mirai/src/action_parsers/mirai_network_result/mirai_network_result.dart'; + +part 'mirai_network_request.freezed.dart'; +part 'mirai_network_request.g.dart'; + +enum Method { + get, + post, + put, + delete, +} + +@freezed +class MiraiNetworkRequest with _$MiraiNetworkRequest { + const factory MiraiNetworkRequest({ + required String url, + @Default(Method.get) Method method, + Map? queryParameters, + Map? headers, + String? contentType, + dynamic body, + @Default([]) List results, + }) = _MiraiNetworkRequest; + + factory MiraiNetworkRequest.fromJson(Map json) => + _$MiraiNetworkRequestFromJson(json); +} diff --git a/packages/mirai/lib/src/network/mirai_request.freezed.dart b/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request.freezed.dart similarity index 62% rename from packages/mirai/lib/src/network/mirai_request.freezed.dart rename to packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request.freezed.dart index 647eba0f..0774955b 100644 --- a/packages/mirai/lib/src/network/mirai_request.freezed.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request.freezed.dart @@ -3,7 +3,7 @@ // ignore_for_file: type=lint // ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark -part of 'mirai_request.dart'; +part of 'mirai_network_request.dart'; // ************************************************************************** // FreezedGenerator @@ -14,31 +14,32 @@ T _$identity(T value) => value; final _privateConstructorUsedError = UnsupportedError( 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods'); -MiraiRequest _$MiraiRequestFromJson(Map json) { - return _MiraiRequest.fromJson(json); +MiraiNetworkRequest _$MiraiNetworkRequestFromJson(Map json) { + return _MiraiNetworkRequest.fromJson(json); } /// @nodoc -mixin _$MiraiRequest { +mixin _$MiraiNetworkRequest { String get url => throw _privateConstructorUsedError; Method get method => throw _privateConstructorUsedError; Map? get queryParameters => throw _privateConstructorUsedError; Map? get headers => throw _privateConstructorUsedError; String? get contentType => throw _privateConstructorUsedError; - Map? get data => throw _privateConstructorUsedError; + dynamic get body => throw _privateConstructorUsedError; + List get results => throw _privateConstructorUsedError; Map toJson() => throw _privateConstructorUsedError; @JsonKey(ignore: true) - $MiraiRequestCopyWith get copyWith => + $MiraiNetworkRequestCopyWith get copyWith => throw _privateConstructorUsedError; } /// @nodoc -abstract class $MiraiRequestCopyWith<$Res> { - factory $MiraiRequestCopyWith( - MiraiRequest value, $Res Function(MiraiRequest) then) = - _$MiraiRequestCopyWithImpl<$Res, MiraiRequest>; +abstract class $MiraiNetworkRequestCopyWith<$Res> { + factory $MiraiNetworkRequestCopyWith( + MiraiNetworkRequest value, $Res Function(MiraiNetworkRequest) then) = + _$MiraiNetworkRequestCopyWithImpl<$Res, MiraiNetworkRequest>; @useResult $Res call( {String url, @@ -46,13 +47,14 @@ abstract class $MiraiRequestCopyWith<$Res> { Map? queryParameters, Map? headers, String? contentType, - Map? data}); + dynamic body, + List results}); } /// @nodoc -class _$MiraiRequestCopyWithImpl<$Res, $Val extends MiraiRequest> - implements $MiraiRequestCopyWith<$Res> { - _$MiraiRequestCopyWithImpl(this._value, this._then); +class _$MiraiNetworkRequestCopyWithImpl<$Res, $Val extends MiraiNetworkRequest> + implements $MiraiNetworkRequestCopyWith<$Res> { + _$MiraiNetworkRequestCopyWithImpl(this._value, this._then); // ignore: unused_field final $Val _value; @@ -67,7 +69,8 @@ class _$MiraiRequestCopyWithImpl<$Res, $Val extends MiraiRequest> Object? queryParameters = freezed, Object? headers = freezed, Object? contentType = freezed, - Object? data = freezed, + Object? body = freezed, + Object? results = null, }) { return _then(_value.copyWith( url: null == url @@ -90,20 +93,24 @@ class _$MiraiRequestCopyWithImpl<$Res, $Val extends MiraiRequest> ? _value.contentType : contentType // ignore: cast_nullable_to_non_nullable as String?, - data: freezed == data - ? _value.data - : data // ignore: cast_nullable_to_non_nullable - as Map?, + body: freezed == body + ? _value.body + : body // ignore: cast_nullable_to_non_nullable + as dynamic, + results: null == results + ? _value.results + : results // ignore: cast_nullable_to_non_nullable + as List, ) as $Val); } } /// @nodoc -abstract class _$$_MiraiRequestCopyWith<$Res> - implements $MiraiRequestCopyWith<$Res> { - factory _$$_MiraiRequestCopyWith( - _$_MiraiRequest value, $Res Function(_$_MiraiRequest) then) = - __$$_MiraiRequestCopyWithImpl<$Res>; +abstract class _$$_MiraiNetworkRequestCopyWith<$Res> + implements $MiraiNetworkRequestCopyWith<$Res> { + factory _$$_MiraiNetworkRequestCopyWith(_$_MiraiNetworkRequest value, + $Res Function(_$_MiraiNetworkRequest) then) = + __$$_MiraiNetworkRequestCopyWithImpl<$Res>; @override @useResult $Res call( @@ -112,15 +119,16 @@ abstract class _$$_MiraiRequestCopyWith<$Res> Map? queryParameters, Map? headers, String? contentType, - Map? data}); + dynamic body, + List results}); } /// @nodoc -class __$$_MiraiRequestCopyWithImpl<$Res> - extends _$MiraiRequestCopyWithImpl<$Res, _$_MiraiRequest> - implements _$$_MiraiRequestCopyWith<$Res> { - __$$_MiraiRequestCopyWithImpl( - _$_MiraiRequest _value, $Res Function(_$_MiraiRequest) _then) +class __$$_MiraiNetworkRequestCopyWithImpl<$Res> + extends _$MiraiNetworkRequestCopyWithImpl<$Res, _$_MiraiNetworkRequest> + implements _$$_MiraiNetworkRequestCopyWith<$Res> { + __$$_MiraiNetworkRequestCopyWithImpl(_$_MiraiNetworkRequest _value, + $Res Function(_$_MiraiNetworkRequest) _then) : super(_value, _then); @pragma('vm:prefer-inline') @@ -131,9 +139,10 @@ class __$$_MiraiRequestCopyWithImpl<$Res> Object? queryParameters = freezed, Object? headers = freezed, Object? contentType = freezed, - Object? data = freezed, + Object? body = freezed, + Object? results = null, }) { - return _then(_$_MiraiRequest( + return _then(_$_MiraiNetworkRequest( url: null == url ? _value.url : url // ignore: cast_nullable_to_non_nullable @@ -154,34 +163,40 @@ class __$$_MiraiRequestCopyWithImpl<$Res> ? _value.contentType : contentType // ignore: cast_nullable_to_non_nullable as String?, - data: freezed == data - ? _value._data - : data // ignore: cast_nullable_to_non_nullable - as Map?, + body: freezed == body + ? _value.body + : body // ignore: cast_nullable_to_non_nullable + as dynamic, + results: null == results + ? _value._results + : results // ignore: cast_nullable_to_non_nullable + as List, )); } } /// @nodoc @JsonSerializable() -class _$_MiraiRequest implements _MiraiRequest { - const _$_MiraiRequest( +class _$_MiraiNetworkRequest implements _MiraiNetworkRequest { + const _$_MiraiNetworkRequest( {required this.url, - required this.method, + this.method = Method.get, final Map? queryParameters, final Map? headers, this.contentType, - final Map? data}) + this.body, + final List results = const []}) : _queryParameters = queryParameters, _headers = headers, - _data = data; + _results = results; - factory _$_MiraiRequest.fromJson(Map json) => - _$$_MiraiRequestFromJson(json); + factory _$_MiraiNetworkRequest.fromJson(Map json) => + _$$_MiraiNetworkRequestFromJson(json); @override final String url; @override + @JsonKey() final Method method; final Map? _queryParameters; @override @@ -205,26 +220,27 @@ class _$_MiraiRequest implements _MiraiRequest { @override final String? contentType; - final Map? _data; @override - Map? get data { - final value = _data; - if (value == null) return null; - if (_data is EqualUnmodifiableMapView) return _data; + final dynamic body; + final List _results; + @override + @JsonKey() + List get results { + if (_results is EqualUnmodifiableListView) return _results; // ignore: implicit_dynamic_type - return EqualUnmodifiableMapView(value); + return EqualUnmodifiableListView(_results); } @override String toString() { - return 'MiraiRequest(url: $url, method: $method, queryParameters: $queryParameters, headers: $headers, contentType: $contentType, data: $data)'; + return 'MiraiNetworkRequest(url: $url, method: $method, queryParameters: $queryParameters, headers: $headers, contentType: $contentType, body: $body, results: $results)'; } @override bool operator ==(dynamic other) { return identical(this, other) || (other.runtimeType == runtimeType && - other is _$_MiraiRequest && + other is _$_MiraiNetworkRequest && (identical(other.url, url) || other.url == url) && (identical(other.method, method) || other.method == method) && const DeepCollectionEquality() @@ -232,7 +248,8 @@ class _$_MiraiRequest implements _MiraiRequest { const DeepCollectionEquality().equals(other._headers, _headers) && (identical(other.contentType, contentType) || other.contentType == contentType) && - const DeepCollectionEquality().equals(other._data, _data)); + const DeepCollectionEquality().equals(other.body, body) && + const DeepCollectionEquality().equals(other._results, _results)); } @JsonKey(ignore: true) @@ -244,33 +261,36 @@ class _$_MiraiRequest implements _MiraiRequest { const DeepCollectionEquality().hash(_queryParameters), const DeepCollectionEquality().hash(_headers), contentType, - const DeepCollectionEquality().hash(_data)); + const DeepCollectionEquality().hash(body), + const DeepCollectionEquality().hash(_results)); @JsonKey(ignore: true) @override @pragma('vm:prefer-inline') - _$$_MiraiRequestCopyWith<_$_MiraiRequest> get copyWith => - __$$_MiraiRequestCopyWithImpl<_$_MiraiRequest>(this, _$identity); + _$$_MiraiNetworkRequestCopyWith<_$_MiraiNetworkRequest> get copyWith => + __$$_MiraiNetworkRequestCopyWithImpl<_$_MiraiNetworkRequest>( + this, _$identity); @override Map toJson() { - return _$$_MiraiRequestToJson( + return _$$_MiraiNetworkRequestToJson( this, ); } } -abstract class _MiraiRequest implements MiraiRequest { - const factory _MiraiRequest( +abstract class _MiraiNetworkRequest implements MiraiNetworkRequest { + const factory _MiraiNetworkRequest( {required final String url, - required final Method method, + final Method method, final Map? queryParameters, final Map? headers, final String? contentType, - final Map? data}) = _$_MiraiRequest; + final dynamic body, + final List results}) = _$_MiraiNetworkRequest; - factory _MiraiRequest.fromJson(Map json) = - _$_MiraiRequest.fromJson; + factory _MiraiNetworkRequest.fromJson(Map json) = + _$_MiraiNetworkRequest.fromJson; @override String get url; @@ -283,9 +303,11 @@ abstract class _MiraiRequest implements MiraiRequest { @override String? get contentType; @override - Map? get data; + dynamic get body; + @override + List get results; @override @JsonKey(ignore: true) - _$$_MiraiRequestCopyWith<_$_MiraiRequest> get copyWith => + _$$_MiraiNetworkRequestCopyWith<_$_MiraiNetworkRequest> get copyWith => throw _privateConstructorUsedError; } diff --git a/packages/mirai/lib/src/network/mirai_request.g.dart b/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request.g.dart similarity index 56% rename from packages/mirai/lib/src/network/mirai_request.g.dart rename to packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request.g.dart index 631cb1b1..8db8dc02 100644 --- a/packages/mirai/lib/src/network/mirai_request.g.dart +++ b/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request.g.dart @@ -1,29 +1,38 @@ // GENERATED CODE - DO NOT MODIFY BY HAND -part of 'mirai_request.dart'; +part of 'mirai_network_request.dart'; // ************************************************************************** // JsonSerializableGenerator // ************************************************************************** -_$_MiraiRequest _$$_MiraiRequestFromJson(Map json) => - _$_MiraiRequest( +_$_MiraiNetworkRequest _$$_MiraiNetworkRequestFromJson( + Map json) => + _$_MiraiNetworkRequest( url: json['url'] as String, - method: $enumDecode(_$MethodEnumMap, json['method']), + method: + $enumDecodeNullable(_$MethodEnumMap, json['method']) ?? Method.get, queryParameters: json['queryParameters'] as Map?, headers: json['headers'] as Map?, contentType: json['contentType'] as String?, - data: json['data'] as Map?, + body: json['body'], + results: (json['results'] as List?) + ?.map( + (e) => MiraiNetworkResult.fromJson(e as Map)) + .toList() ?? + const [], ); -Map _$$_MiraiRequestToJson(_$_MiraiRequest instance) => +Map _$$_MiraiNetworkRequestToJson( + _$_MiraiNetworkRequest instance) => { 'url': instance.url, 'method': _$MethodEnumMap[instance.method]!, 'queryParameters': instance.queryParameters, 'headers': instance.headers, 'contentType': instance.contentType, - 'data': instance.data, + 'body': instance.body, + 'results': instance.results, }; const _$MethodEnumMap = { 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 new file mode 100644 index 00000000..a40a6fc8 --- /dev/null +++ b/packages/mirai/lib/src/action_parsers/mirai_network_request/mirai_network_request_parser.dart @@ -0,0 +1,66 @@ +import 'dart:async'; + +import 'package:dio/dio.dart'; +import 'package:flutter/widgets.dart'; +import 'package:mirai/mirai.dart'; +import 'package:mirai/src/utils/action_type.dart'; +import 'package:mirai/src/utils/log.dart'; + +export 'mirai_network_request.dart'; + +class MiraiNetworkRequestParser extends MiraiActionParser { + const MiraiNetworkRequestParser(); + + @override + String get actionType => ActionType.networkRequest.name; + + @override + MiraiNetworkRequest getModel(Map json) => + MiraiNetworkRequest.fromJson(json); + + @override + FutureOr onCall(BuildContext context, MiraiNetworkRequest model) async { + Response? response; + try { + response = await MiraiNetworkService.request(model, context); + Log.d(response); + } on DioException catch (e) { + response = e.response; + Log.e(e.response); + } + + 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); + } + } +} diff --git a/packages/mirai/lib/src/action_parsers/mirai_network_result/mirai_network_result.dart b/packages/mirai/lib/src/action_parsers/mirai_network_result/mirai_network_result.dart new file mode 100644 index 00000000..62eed003 --- /dev/null +++ b/packages/mirai/lib/src/action_parsers/mirai_network_result/mirai_network_result.dart @@ -0,0 +1,15 @@ +import 'package:freezed_annotation/freezed_annotation.dart'; + +part 'mirai_network_result.freezed.dart'; +part 'mirai_network_result.g.dart'; + +@freezed +class MiraiNetworkResult with _$MiraiNetworkResult { + const factory MiraiNetworkResult({ + required int statusCode, + required Map action, + }) = _MiraiNetworkResult; + + factory MiraiNetworkResult.fromJson(Map json) => + _$MiraiNetworkResultFromJson(json); +} diff --git a/packages/mirai/lib/src/action_parsers/mirai_network_result/mirai_network_result.freezed.dart b/packages/mirai/lib/src/action_parsers/mirai_network_result/mirai_network_result.freezed.dart new file mode 100644 index 00000000..d8d08451 --- /dev/null +++ b/packages/mirai/lib/src/action_parsers/mirai_network_result/mirai_network_result.freezed.dart @@ -0,0 +1,179 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: type=lint +// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark + +part of 'mirai_network_result.dart'; + +// ************************************************************************** +// FreezedGenerator +// ************************************************************************** + +T _$identity(T value) => value; + +final _privateConstructorUsedError = UnsupportedError( + 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods'); + +MiraiNetworkResult _$MiraiNetworkResultFromJson(Map json) { + return _MiraiNetworkResult.fromJson(json); +} + +/// @nodoc +mixin _$MiraiNetworkResult { + int get statusCode => throw _privateConstructorUsedError; + Map get action => throw _privateConstructorUsedError; + + Map toJson() => throw _privateConstructorUsedError; + @JsonKey(ignore: true) + $MiraiNetworkResultCopyWith get copyWith => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $MiraiNetworkResultCopyWith<$Res> { + factory $MiraiNetworkResultCopyWith( + MiraiNetworkResult value, $Res Function(MiraiNetworkResult) then) = + _$MiraiNetworkResultCopyWithImpl<$Res, MiraiNetworkResult>; + @useResult + $Res call({int statusCode, Map action}); +} + +/// @nodoc +class _$MiraiNetworkResultCopyWithImpl<$Res, $Val extends MiraiNetworkResult> + implements $MiraiNetworkResultCopyWith<$Res> { + _$MiraiNetworkResultCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? statusCode = null, + Object? action = null, + }) { + return _then(_value.copyWith( + statusCode: null == statusCode + ? _value.statusCode + : statusCode // ignore: cast_nullable_to_non_nullable + as int, + action: null == action + ? _value.action + : action // ignore: cast_nullable_to_non_nullable + as Map, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$_MiraiNetworkResultCopyWith<$Res> + implements $MiraiNetworkResultCopyWith<$Res> { + factory _$$_MiraiNetworkResultCopyWith(_$_MiraiNetworkResult value, + $Res Function(_$_MiraiNetworkResult) then) = + __$$_MiraiNetworkResultCopyWithImpl<$Res>; + @override + @useResult + $Res call({int statusCode, Map action}); +} + +/// @nodoc +class __$$_MiraiNetworkResultCopyWithImpl<$Res> + extends _$MiraiNetworkResultCopyWithImpl<$Res, _$_MiraiNetworkResult> + implements _$$_MiraiNetworkResultCopyWith<$Res> { + __$$_MiraiNetworkResultCopyWithImpl( + _$_MiraiNetworkResult _value, $Res Function(_$_MiraiNetworkResult) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? statusCode = null, + Object? action = null, + }) { + return _then(_$_MiraiNetworkResult( + statusCode: null == statusCode + ? _value.statusCode + : statusCode // ignore: cast_nullable_to_non_nullable + as int, + action: null == action + ? _value._action + : action // ignore: cast_nullable_to_non_nullable + as Map, + )); + } +} + +/// @nodoc +@JsonSerializable() +class _$_MiraiNetworkResult implements _MiraiNetworkResult { + const _$_MiraiNetworkResult( + {required this.statusCode, required final Map action}) + : _action = action; + + factory _$_MiraiNetworkResult.fromJson(Map json) => + _$$_MiraiNetworkResultFromJson(json); + + @override + final int statusCode; + final Map _action; + @override + Map get action { + if (_action is EqualUnmodifiableMapView) return _action; + // ignore: implicit_dynamic_type + return EqualUnmodifiableMapView(_action); + } + + @override + String toString() { + return 'MiraiNetworkResult(statusCode: $statusCode, action: $action)'; + } + + @override + bool operator ==(dynamic other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$_MiraiNetworkResult && + (identical(other.statusCode, statusCode) || + other.statusCode == statusCode) && + const DeepCollectionEquality().equals(other._action, _action)); + } + + @JsonKey(ignore: true) + @override + int get hashCode => Object.hash( + runtimeType, statusCode, const DeepCollectionEquality().hash(_action)); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$_MiraiNetworkResultCopyWith<_$_MiraiNetworkResult> get copyWith => + __$$_MiraiNetworkResultCopyWithImpl<_$_MiraiNetworkResult>( + this, _$identity); + + @override + Map toJson() { + return _$$_MiraiNetworkResultToJson( + this, + ); + } +} + +abstract class _MiraiNetworkResult implements MiraiNetworkResult { + const factory _MiraiNetworkResult( + {required final int statusCode, + required final Map action}) = _$_MiraiNetworkResult; + + factory _MiraiNetworkResult.fromJson(Map json) = + _$_MiraiNetworkResult.fromJson; + + @override + int get statusCode; + @override + Map get action; + @override + @JsonKey(ignore: true) + _$$_MiraiNetworkResultCopyWith<_$_MiraiNetworkResult> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/packages/mirai/lib/src/action_parsers/mirai_network_result/mirai_network_result.g.dart b/packages/mirai/lib/src/action_parsers/mirai_network_result/mirai_network_result.g.dart new file mode 100644 index 00000000..af9df5f2 --- /dev/null +++ b/packages/mirai/lib/src/action_parsers/mirai_network_result/mirai_network_result.g.dart @@ -0,0 +1,21 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'mirai_network_result.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$_MiraiNetworkResult _$$_MiraiNetworkResultFromJson( + Map json) => + _$_MiraiNetworkResult( + statusCode: json['statusCode'] as int, + action: json['action'] as Map, + ); + +Map _$$_MiraiNetworkResultToJson( + _$_MiraiNetworkResult instance) => + { + 'statusCode': instance.statusCode, + 'action': instance.action, + }; diff --git a/packages/mirai/lib/src/action_parsers/mirai_none_action/mirai_none_action_parser.dart b/packages/mirai/lib/src/action_parsers/mirai_none_action/mirai_none_action_parser.dart deleted file mode 100644 index 6b56c922..00000000 --- a/packages/mirai/lib/src/action_parsers/mirai_none_action/mirai_none_action_parser.dart +++ /dev/null @@ -1,18 +0,0 @@ -import 'dart:async'; - -import 'package:flutter/widgets.dart'; -import 'package:mirai/src/utils/action_type.dart'; -import 'package:mirai_framework/mirai_framework.dart'; - -class MiraiNoneActionParser extends MiraiActionParser { - const MiraiNoneActionParser(); - - @override - String get actionType => ActionType.none.name; - - @override - getModel(Map json) => json; - - @override - FutureOr onCall(BuildContext context, model) {} -} diff --git a/packages/mirai/lib/src/action_parsers/mirai_request_action/mirai_request_action_parser.dart b/packages/mirai/lib/src/action_parsers/mirai_request_action/mirai_request_action_parser.dart deleted file mode 100644 index 2de685a2..00000000 --- a/packages/mirai/lib/src/action_parsers/mirai_request_action/mirai_request_action_parser.dart +++ /dev/null @@ -1,22 +0,0 @@ -import 'dart:async'; - -import 'package:flutter/widgets.dart'; -import 'package:mirai/src/network/mirai_network.dart'; -import 'package:mirai/src/network/mirai_request.dart'; -import 'package:mirai/src/utils/action_type.dart'; -import 'package:mirai_framework/mirai_framework.dart'; - -class MiraiRequestActionParser extends MiraiActionParser { - const MiraiRequestActionParser(); - - @override - String get actionType => ActionType.request.name; - - @override - getModel(Map json) => MiraiRequest.fromJson(json); - - @override - FutureOr onCall(BuildContext context, MiraiRequest model) { - return MiraiNetwork.request(model); - } -} diff --git a/packages/mirai/lib/src/framework/mirai.dart b/packages/mirai/lib/src/framework/mirai.dart index ca8a30fc..1e249b25 100644 --- a/packages/mirai/lib/src/framework/mirai.dart +++ b/packages/mirai/lib/src/framework/mirai.dart @@ -6,17 +6,15 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:mirai/src/action_parsers/action_parsers.dart'; import 'package:mirai/src/framework/mirai_registry.dart'; -import 'package:mirai/src/network/mirai_network.dart'; -import 'package:mirai/src/network/mirai_request.dart'; import 'package:mirai/src/parsers/mirai_align/mirai_align_parser.dart'; import 'package:mirai/src/parsers/mirai_center/mirai_center_parser.dart'; import 'package:mirai/src/parsers/mirai_check_box_widget/mirai_check_box_widget_parser.dart'; import 'package:mirai/src/parsers/mirai_form/mirai_form_parser.dart'; -import 'package:mirai/src/parsers/mirai_form_field/mirai_form_field_parser.dart'; import 'package:mirai/src/parsers/mirai_fractionally_sized_box/mirai_fractionally_sized_box_parser.dart'; import 'package:mirai/src/parsers/mirai_switch/mirai_switch_parser.dart'; import 'package:mirai/src/parsers/mirai_tab/mirai_tab_parser.dart'; import 'package:mirai/src/parsers/parsers.dart'; +import 'package:mirai/src/services/mirai_network_service.dart'; import 'package:mirai/src/utils/log.dart'; import 'package:mirai_framework/mirai_framework.dart'; @@ -61,7 +59,6 @@ class Mirai { const MiraiAlertDialogParser(), const MiraiTabParser(), const MiraiFormParser(), - const MiraiFormFieldParser(), const MiraiCheckBoxWidgetParser(), const MiraiExpandedParser(), const MiraiFlexibleParser(), @@ -71,11 +68,11 @@ class Mirai { ]; static final _actionParsers = [ - const MiraiNoneActionParser(), const MiraiNavigateActionParser(), - const MiraiRequestActionParser(), + const MiraiNetworkRequestParser(), const MiraiModalBottomSheetActionParser(), const MiraiDialogActionParser(), + const MiraiFormDataActionParser(), ]; static Future initialize({ @@ -87,7 +84,7 @@ class Mirai { _actionParsers.addAll(actionParsers); MiraiRegistry.instance.registerAll(_parsers); MiraiRegistry.instance.registerAllActions(_actionParsers); - MiraiNetwork.initialize(dio ?? Dio()); + MiraiNetworkService.initialize(dio ?? Dio()); } static Widget? fromJson(Map? json, BuildContext context) { @@ -131,12 +128,13 @@ class Mirai { } static Widget fromNetwork( - MiraiRequest request, { + MiraiNetworkRequest request, + BuildContext context, { LoadingWidgetBuilder? loadingWidget, ErrorWidgetBuilder? errorWidget, }) { return FutureBuilder( - future: MiraiNetwork.request(request), + future: MiraiNetworkService.request(request, context), builder: (context, snapshot) { switch (snapshot.connectionState) { case ConnectionState.waiting: diff --git a/packages/mirai/lib/src/network/mirai_network.dart b/packages/mirai/lib/src/network/mirai_network.dart deleted file mode 100644 index d1984421..00000000 --- a/packages/mirai/lib/src/network/mirai_network.dart +++ /dev/null @@ -1,71 +0,0 @@ -import 'package:dio/dio.dart'; -import 'package:mirai/src/network/mirai_request.dart'; - -class MiraiNetwork { - const MiraiNetwork._(); - - static late Dio _dio; - - static void initialize(Dio dio) => _dio = dio; - - static Future request(MiraiRequest request) { - switch (request.method) { - case Method.get: - return getRequest(request); - case Method.post: - return postRequest(request); - case Method.put: - return putRequest(request); - case Method.delete: - return deleteRequest(request); - } - } - - static Future getRequest(MiraiRequest request) { - return _dio.get( - request.url, - data: request.data, - queryParameters: request.queryParameters, - options: Options( - contentType: request.contentType, - headers: request.headers, - ), - ); - } - - static Future postRequest(MiraiRequest request) { - return _dio.post( - request.url, - data: request.data, - queryParameters: request.queryParameters, - options: Options( - contentType: request.contentType, - headers: request.headers, - ), - ); - } - - static Future putRequest(MiraiRequest request) async { - return _dio.put( - request.url, - data: request.data, - queryParameters: request.queryParameters, - options: Options( - contentType: request.contentType, - headers: request.headers, - ), - ); - } - - static Future deleteRequest(MiraiRequest request) async { - return _dio.delete( - request.url, - data: request.data, - queryParameters: request.queryParameters, - options: Options( - contentType: request.contentType, - headers: request.headers, - ), - ); - } -} diff --git a/packages/mirai/lib/src/network/mirai_request.dart b/packages/mirai/lib/src/network/mirai_request.dart deleted file mode 100644 index c9f01474..00000000 --- a/packages/mirai/lib/src/network/mirai_request.dart +++ /dev/null @@ -1,26 +0,0 @@ -import 'package:freezed_annotation/freezed_annotation.dart'; - -part 'mirai_request.freezed.dart'; -part 'mirai_request.g.dart'; - -@freezed -class MiraiRequest with _$MiraiRequest { - const factory MiraiRequest({ - required String url, - required Method method, - Map? queryParameters, - Map? headers, - String? contentType, - Map? data, - }) = _MiraiRequest; - - factory MiraiRequest.fromJson(Map json) => - _$MiraiRequestFromJson(json); -} - -enum Method { - get, - post, - put, - delete, -} diff --git a/packages/mirai/lib/src/network/network.dart b/packages/mirai/lib/src/network/network.dart deleted file mode 100644 index 51f86b70..00000000 --- a/packages/mirai/lib/src/network/network.dart +++ /dev/null @@ -1,4 +0,0 @@ -library network; - -export 'mirai_network.dart'; -export 'mirai_request.dart'; diff --git a/packages/mirai/lib/src/parsers/mirai_align/mirai_align.freezed.dart b/packages/mirai/lib/src/parsers/mirai_align/mirai_align.freezed.dart index c286282e..e5b1cd10 100644 --- a/packages/mirai/lib/src/parsers/mirai_align/mirai_align.freezed.dart +++ b/packages/mirai/lib/src/parsers/mirai_align/mirai_align.freezed.dart @@ -84,11 +84,11 @@ class _$MiraiAlignCopyWithImpl<$Res, $Val extends MiraiAlign> } /// @nodoc -abstract class _$$MiraiAlignImplCopyWith<$Res> +abstract class _$$_MiraiAlignCopyWith<$Res> implements $MiraiAlignCopyWith<$Res> { - factory _$$MiraiAlignImplCopyWith( - _$MiraiAlignImpl value, $Res Function(_$MiraiAlignImpl) then) = - __$$MiraiAlignImplCopyWithImpl<$Res>; + factory _$$_MiraiAlignCopyWith( + _$_MiraiAlign value, $Res Function(_$_MiraiAlign) then) = + __$$_MiraiAlignCopyWithImpl<$Res>; @override @useResult $Res call( @@ -99,11 +99,11 @@ abstract class _$$MiraiAlignImplCopyWith<$Res> } /// @nodoc -class __$$MiraiAlignImplCopyWithImpl<$Res> - extends _$MiraiAlignCopyWithImpl<$Res, _$MiraiAlignImpl> - implements _$$MiraiAlignImplCopyWith<$Res> { - __$$MiraiAlignImplCopyWithImpl( - _$MiraiAlignImpl _value, $Res Function(_$MiraiAlignImpl) _then) +class __$$_MiraiAlignCopyWithImpl<$Res> + extends _$MiraiAlignCopyWithImpl<$Res, _$_MiraiAlign> + implements _$$_MiraiAlignCopyWith<$Res> { + __$$_MiraiAlignCopyWithImpl( + _$_MiraiAlign _value, $Res Function(_$_MiraiAlign) _then) : super(_value, _then); @pragma('vm:prefer-inline') @@ -114,7 +114,7 @@ class __$$MiraiAlignImplCopyWithImpl<$Res> Object? heightFactor = freezed, Object? child = freezed, }) { - return _then(_$MiraiAlignImpl( + return _then(_$_MiraiAlign( alignment: null == alignment ? _value.alignment : alignment // ignore: cast_nullable_to_non_nullable @@ -137,16 +137,16 @@ class __$$MiraiAlignImplCopyWithImpl<$Res> /// @nodoc @JsonSerializable() -class _$MiraiAlignImpl implements _MiraiAlign { - const _$MiraiAlignImpl( +class _$_MiraiAlign implements _MiraiAlign { + const _$_MiraiAlign( {this.alignment = MiraiAlignmentDirectional.center, this.widthFactor, this.heightFactor, final Map? child}) : _child = child; - factory _$MiraiAlignImpl.fromJson(Map json) => - _$$MiraiAlignImplFromJson(json); + factory _$_MiraiAlign.fromJson(Map json) => + _$$_MiraiAlignFromJson(json); @override @JsonKey() @@ -174,7 +174,7 @@ class _$MiraiAlignImpl implements _MiraiAlign { bool operator ==(dynamic other) { return identical(this, other) || (other.runtimeType == runtimeType && - other is _$MiraiAlignImpl && + other is _$_MiraiAlign && (identical(other.alignment, alignment) || other.alignment == alignment) && (identical(other.widthFactor, widthFactor) || @@ -192,12 +192,12 @@ class _$MiraiAlignImpl implements _MiraiAlign { @JsonKey(ignore: true) @override @pragma('vm:prefer-inline') - _$$MiraiAlignImplCopyWith<_$MiraiAlignImpl> get copyWith => - __$$MiraiAlignImplCopyWithImpl<_$MiraiAlignImpl>(this, _$identity); + _$$_MiraiAlignCopyWith<_$_MiraiAlign> get copyWith => + __$$_MiraiAlignCopyWithImpl<_$_MiraiAlign>(this, _$identity); @override Map toJson() { - return _$$MiraiAlignImplToJson( + return _$$_MiraiAlignToJson( this, ); } @@ -208,10 +208,10 @@ abstract class _MiraiAlign implements MiraiAlign { {final MiraiAlignmentDirectional alignment, final double? widthFactor, final double? heightFactor, - final Map? child}) = _$MiraiAlignImpl; + final Map? child}) = _$_MiraiAlign; factory _MiraiAlign.fromJson(Map json) = - _$MiraiAlignImpl.fromJson; + _$_MiraiAlign.fromJson; @override MiraiAlignmentDirectional get alignment; @@ -223,6 +223,6 @@ abstract class _MiraiAlign implements MiraiAlign { Map? get child; @override @JsonKey(ignore: true) - _$$MiraiAlignImplCopyWith<_$MiraiAlignImpl> get copyWith => + _$$_MiraiAlignCopyWith<_$_MiraiAlign> get copyWith => throw _privateConstructorUsedError; } diff --git a/packages/mirai/lib/src/parsers/mirai_align/mirai_align.g.dart b/packages/mirai/lib/src/parsers/mirai_align/mirai_align.g.dart index 6b56d670..4d886f4c 100644 --- a/packages/mirai/lib/src/parsers/mirai_align/mirai_align.g.dart +++ b/packages/mirai/lib/src/parsers/mirai_align/mirai_align.g.dart @@ -6,8 +6,8 @@ part of 'mirai_align.dart'; // JsonSerializableGenerator // ************************************************************************** -_$MiraiAlignImpl _$$MiraiAlignImplFromJson(Map json) => - _$MiraiAlignImpl( +_$_MiraiAlign _$$_MiraiAlignFromJson(Map json) => + _$_MiraiAlign( alignment: $enumDecodeNullable( _$MiraiAlignmentDirectionalEnumMap, json['alignment']) ?? MiraiAlignmentDirectional.center, @@ -16,7 +16,7 @@ _$MiraiAlignImpl _$$MiraiAlignImplFromJson(Map json) => child: json['child'] as Map?, ); -Map _$$MiraiAlignImplToJson(_$MiraiAlignImpl instance) => +Map _$$_MiraiAlignToJson(_$_MiraiAlign instance) => { 'alignment': _$MiraiAlignmentDirectionalEnumMap[instance.alignment]!, 'widthFactor': instance.widthFactor, diff --git a/packages/mirai/lib/src/parsers/mirai_check_box_widget/mirai_check_box_widget_parser.dart b/packages/mirai/lib/src/parsers/mirai_check_box_widget/mirai_check_box_widget_parser.dart index a170d174..dbd44a14 100644 --- a/packages/mirai/lib/src/parsers/mirai_check_box_widget/mirai_check_box_widget_parser.dart +++ b/packages/mirai/lib/src/parsers/mirai_check_box_widget/mirai_check_box_widget_parser.dart @@ -1,7 +1,5 @@ import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mirai/src/parsers/mirai_check_box_widget/mirai_check_box_widget.dart'; -import 'package:mirai/src/parsers/mirai_form/cubit/mirai_form_cubit.dart'; import 'package:mirai/src/parsers/mirai_material_color/mirai_material_color.dart'; import 'package:mirai/src/utils/color_utils.dart'; import 'package:mirai/src/utils/widget_type.dart'; @@ -40,9 +38,9 @@ class __MiraiCheckBoxWidgetState extends State<_MiraiCheckBoxWidget> { @override void initState() { if (widget.model.id != null) { - context - .read() - .registerValue(widget.model.id!, widget.model.value); + // context + // .read() + // .registerValue(widget.model.id!, widget.model.value); } super.initState(); @@ -57,12 +55,12 @@ class __MiraiCheckBoxWidgetState extends State<_MiraiCheckBoxWidget> { isMarkChecked = !isMarkChecked; }); if (widget.model.id != null) { - context - .read() - .updateValue(widget.model.id!, isMarkChecked); - context - .read() - .updateValidation(widget.model.id!, isMarkChecked); + // context + // .read() + // .updateValue(widget.model.id!, isMarkChecked); + // context + // .read() + // .updateValidation(widget.model.id!, isMarkChecked); } }, activeColor: widget.model.activeColor.toColor, diff --git a/packages/mirai/lib/src/parsers/mirai_form/cubit/mirai_form_cubit.dart b/packages/mirai/lib/src/parsers/mirai_form/cubit/mirai_form_cubit.dart deleted file mode 100644 index c91caaab..00000000 --- a/packages/mirai/lib/src/parsers/mirai_form/cubit/mirai_form_cubit.dart +++ /dev/null @@ -1,99 +0,0 @@ -import 'package:flutter_bloc/flutter_bloc.dart'; - -part 'mirai_form_state.dart'; - -class MiraiFormCubit extends Cubit { - MiraiFormCubit() : super(const MiraiFormState()); - - void validateFrom() { - final values = state.values.entries.toList(); - for (var i = 0; i < values.length; i++) { - final data = values[i].value.data; - final isValidated = values[i].value.validated; - - if (data == null || - (data is String && data.isEmpty) || - (data is bool && data == false) || - !isValidated) { - emit( - state.copyWith(formState: MiraiFromStateType.unvalidated), - ); - return; - } - } - - emit( - state.copyWith(formState: MiraiFromStateType.validated), - ); - } - - void registerValue(String key, dynamic value) { - Map? values = - Map.from(state.values); - - values.addAll({key: FormFieldModel(data: value, validated: false)}); - - emit( - MiraiFormState( - values: values, - ), - ); - - validateFrom(); - } - - bool? getValidation(String key) { - return state.values[key]?.validated; - } - - String? getValue(String key) { - return state.values[key]?.data; - } - - void updateValue(String key, dynamic value) { - Map? values = - Map.from(state.values); - - values[key] = values[key]?.copyWith(data: value) ?? FormFieldModel(); - - emit( - state.copyWith(values: values), - ); - - validateFrom(); - } - - void updateValidation(String key, bool value) { - Map values = - Map.from(state.values); - - values[key] = values[key]?.copyWith(validated: value) ?? FormFieldModel(); - - emit( - state.copyWith(values: values), - ); - - validateFrom(); - } - - void removeValue(String key) { - Map values = - Map.from(state.values); - - values.remove(key); - - emit( - state.copyWith(values: values), - ); - - validateFrom(); - } - - void removeAll() { - emit( - state.copyWith(values: {}), - ); - - validateFrom(); - } -} diff --git a/packages/mirai/lib/src/parsers/mirai_form/cubit/mirai_form_state.dart b/packages/mirai/lib/src/parsers/mirai_form/cubit/mirai_form_state.dart deleted file mode 100644 index 90fe7cd4..00000000 --- a/packages/mirai/lib/src/parsers/mirai_form/cubit/mirai_form_state.dart +++ /dev/null @@ -1,41 +0,0 @@ -part of 'mirai_form_cubit.dart'; - -enum MiraiFromStateType { unvalidated, validated, loading, submitted } - -class MiraiFormState { - const MiraiFormState({ - this.values = const {}, - this.formState = MiraiFromStateType.unvalidated, - }); - - final Map values; - final MiraiFromStateType formState; - - MiraiFormState copyWith({ - Map? values, - MiraiFromStateType? formState, - }) => - MiraiFormState( - values: values ?? this.values, - formState: formState ?? this.formState, - ); -} - -class FormFieldModel { - FormFieldModel({ - this.data, - this.validated = false, - }); - - final dynamic data; - final bool validated; - - FormFieldModel copyWith({ - dynamic data, - bool? validated, - }) => - FormFieldModel( - data: data ?? this.data, - validated: validated ?? this.validated, - ); -} diff --git a/packages/mirai/lib/src/parsers/mirai_form/mirai_form_parser.dart b/packages/mirai/lib/src/parsers/mirai_form/mirai_form_parser.dart index 1e37b497..448242ef 100644 --- a/packages/mirai/lib/src/parsers/mirai_form/mirai_form_parser.dart +++ b/packages/mirai/lib/src/parsers/mirai_form/mirai_form_parser.dart @@ -1,8 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mirai/src/framework/framework.dart'; -import 'package:mirai/src/parsers/mirai_form/cubit/mirai_form_cubit.dart'; import 'package:mirai/src/parsers/mirai_form/mirai_form.dart'; +import 'package:mirai/src/utils/log.dart'; import 'package:mirai/src/utils/widget_type.dart'; import 'package:mirai_framework/mirai_framework.dart'; @@ -17,15 +16,73 @@ class MiraiFormParser extends MiraiParser { @override Widget parse(BuildContext context, MiraiForm model) { - return BlocProvider( - create: (_) => MiraiFormCubit(), - child: Form( - autovalidateMode: model.autovalidateMode, - child: BlocBuilder( - builder: (context, state) { - return Mirai.fromJson(model.child, context) ?? const SizedBox(); - }, - )), + return FormBuilder( + model: model, ); } } + +class FormBuilder extends StatefulWidget { + const FormBuilder({ + super.key, + required this.model, + }); + + final MiraiForm model; + + @override + State createState() => _FormBuilderState(); +} + +class _FormBuilderState extends State { + final Map _formData = {"key": "val"}; + final _formKey = GlobalKey(); + + Map getFormData() => _formData; + + void setFormData({required String key, required value}) { + _formData[key] = value; + } + + @override + Widget build(BuildContext context) { + return MiraiFormScope( + formData: _formData, + child: Builder(builder: (context) { + return Form( + key: _formKey, + onChanged: () { + Log.d("onChanged: ${MiraiFormScope.of(context).formData}"); + }, + child: + Mirai.fromJson(widget.model.child, context) ?? const SizedBox(), + ); + }), + ); + } +} + +class MiraiFormScope extends InheritedWidget { + const MiraiFormScope({ + super.key, + required super.child, + required this.formData, + }); + + final Map formData; + + static MiraiFormScope of(BuildContext context) { + final MiraiFormScope? result = + context.dependOnInheritedWidgetOfExactType(); + + if (result != null) { + return result; + } else { + throw FlutterError( + 'MiraiFormScope.of() called with a context that does not contain a MiraiFormScope.'); + } + } + + @override + bool updateShouldNotify(MiraiFormScope oldWidget) => false; +} diff --git a/packages/mirai/lib/src/parsers/mirai_form_field/mirai_form_field.dart b/packages/mirai/lib/src/parsers/mirai_form_field/mirai_form_field.dart deleted file mode 100644 index 5d04cbca..00000000 --- a/packages/mirai/lib/src/parsers/mirai_form_field/mirai_form_field.dart +++ /dev/null @@ -1,14 +0,0 @@ -import 'package:freezed_annotation/freezed_annotation.dart'; - -part 'mirai_form_field.freezed.dart'; -part 'mirai_form_field.g.dart'; - -@freezed -class MiraiFormField with _$MiraiFormField { - const factory MiraiFormField({ - Map? child, - }) = _MiraiFormField; - - factory MiraiFormField.fromJson(Map json) => - _$MiraiFormFieldFromJson(json); -} diff --git a/packages/mirai/lib/src/parsers/mirai_form_field/mirai_form_field.freezed.dart b/packages/mirai/lib/src/parsers/mirai_form_field/mirai_form_field.freezed.dart deleted file mode 100644 index 95e99ead..00000000 --- a/packages/mirai/lib/src/parsers/mirai_form_field/mirai_form_field.freezed.dart +++ /dev/null @@ -1,160 +0,0 @@ -// coverage:ignore-file -// GENERATED CODE - DO NOT MODIFY BY HAND -// ignore_for_file: type=lint -// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark - -part of 'mirai_form_field.dart'; - -// ************************************************************************** -// FreezedGenerator -// ************************************************************************** - -T _$identity(T value) => value; - -final _privateConstructorUsedError = UnsupportedError( - 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods'); - -MiraiFormField _$MiraiFormFieldFromJson(Map json) { - return _MiraiFormField.fromJson(json); -} - -/// @nodoc -mixin _$MiraiFormField { - Map? get child => throw _privateConstructorUsedError; - - Map toJson() => throw _privateConstructorUsedError; - @JsonKey(ignore: true) - $MiraiFormFieldCopyWith get copyWith => - throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class $MiraiFormFieldCopyWith<$Res> { - factory $MiraiFormFieldCopyWith( - MiraiFormField value, $Res Function(MiraiFormField) then) = - _$MiraiFormFieldCopyWithImpl<$Res, MiraiFormField>; - @useResult - $Res call({Map? child}); -} - -/// @nodoc -class _$MiraiFormFieldCopyWithImpl<$Res, $Val extends MiraiFormField> - implements $MiraiFormFieldCopyWith<$Res> { - _$MiraiFormFieldCopyWithImpl(this._value, this._then); - - // ignore: unused_field - final $Val _value; - // ignore: unused_field - final $Res Function($Val) _then; - - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? child = freezed, - }) { - return _then(_value.copyWith( - child: freezed == child - ? _value.child - : child // ignore: cast_nullable_to_non_nullable - as Map?, - ) as $Val); - } -} - -/// @nodoc -abstract class _$$_MiraiFormFieldCopyWith<$Res> - implements $MiraiFormFieldCopyWith<$Res> { - factory _$$_MiraiFormFieldCopyWith( - _$_MiraiFormField value, $Res Function(_$_MiraiFormField) then) = - __$$_MiraiFormFieldCopyWithImpl<$Res>; - @override - @useResult - $Res call({Map? child}); -} - -/// @nodoc -class __$$_MiraiFormFieldCopyWithImpl<$Res> - extends _$MiraiFormFieldCopyWithImpl<$Res, _$_MiraiFormField> - implements _$$_MiraiFormFieldCopyWith<$Res> { - __$$_MiraiFormFieldCopyWithImpl( - _$_MiraiFormField _value, $Res Function(_$_MiraiFormField) _then) - : super(_value, _then); - - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? child = freezed, - }) { - return _then(_$_MiraiFormField( - child: freezed == child - ? _value._child - : child // ignore: cast_nullable_to_non_nullable - as Map?, - )); - } -} - -/// @nodoc -@JsonSerializable() -class _$_MiraiFormField implements _MiraiFormField { - const _$_MiraiFormField({final Map? child}) : _child = child; - - factory _$_MiraiFormField.fromJson(Map json) => - _$$_MiraiFormFieldFromJson(json); - - final Map? _child; - @override - Map? get child { - final value = _child; - if (value == null) return null; - if (_child is EqualUnmodifiableMapView) return _child; - // ignore: implicit_dynamic_type - return EqualUnmodifiableMapView(value); - } - - @override - String toString() { - return 'MiraiFormField(child: $child)'; - } - - @override - bool operator ==(dynamic other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _$_MiraiFormField && - const DeepCollectionEquality().equals(other._child, _child)); - } - - @JsonKey(ignore: true) - @override - int get hashCode => - Object.hash(runtimeType, const DeepCollectionEquality().hash(_child)); - - @JsonKey(ignore: true) - @override - @pragma('vm:prefer-inline') - _$$_MiraiFormFieldCopyWith<_$_MiraiFormField> get copyWith => - __$$_MiraiFormFieldCopyWithImpl<_$_MiraiFormField>(this, _$identity); - - @override - Map toJson() { - return _$$_MiraiFormFieldToJson( - this, - ); - } -} - -abstract class _MiraiFormField implements MiraiFormField { - const factory _MiraiFormField({final Map? child}) = - _$_MiraiFormField; - - factory _MiraiFormField.fromJson(Map json) = - _$_MiraiFormField.fromJson; - - @override - Map? get child; - @override - @JsonKey(ignore: true) - _$$_MiraiFormFieldCopyWith<_$_MiraiFormField> get copyWith => - throw _privateConstructorUsedError; -} diff --git a/packages/mirai/lib/src/parsers/mirai_form_field/mirai_form_field.g.dart b/packages/mirai/lib/src/parsers/mirai_form_field/mirai_form_field.g.dart deleted file mode 100644 index 233c9daa..00000000 --- a/packages/mirai/lib/src/parsers/mirai_form_field/mirai_form_field.g.dart +++ /dev/null @@ -1,17 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'mirai_form_field.dart'; - -// ************************************************************************** -// JsonSerializableGenerator -// ************************************************************************** - -_$_MiraiFormField _$$_MiraiFormFieldFromJson(Map json) => - _$_MiraiFormField( - child: json['child'] as Map?, - ); - -Map _$$_MiraiFormFieldToJson(_$_MiraiFormField instance) => - { - 'child': instance.child, - }; diff --git a/packages/mirai/lib/src/parsers/mirai_form_field/mirai_form_field_parser.dart b/packages/mirai/lib/src/parsers/mirai_form_field/mirai_form_field_parser.dart deleted file mode 100644 index 22354fac..00000000 --- a/packages/mirai/lib/src/parsers/mirai_form_field/mirai_form_field_parser.dart +++ /dev/null @@ -1,35 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:mirai/src/framework/framework.dart'; -import 'package:mirai/src/parsers/mirai_form/cubit/mirai_form_cubit.dart'; -import 'package:mirai/src/parsers/mirai_form_field/mirai_form_field.dart'; -import 'package:mirai/src/utils/log.dart'; -import 'package:mirai/src/utils/widget_type.dart'; -import 'package:mirai_framework/mirai_framework.dart'; - -class MiraiFormFieldParser extends MiraiParser { - const MiraiFormFieldParser(); - - @override - MiraiFormField getModel(Map json) => - MiraiFormField.fromJson(json); - - @override - String get type => WidgetType.formField.name; - - @override - Widget parse(BuildContext context, MiraiFormField model) { - Map child = Map.from(model.child ?? {}); - - try { - final formState = context.read().state.formState; - if (formState == MiraiFromStateType.unvalidated) { - child['onPressed'] = null; - } - } catch (e) { - Log.d(e); - } - - return Mirai.fromJson(child, context) ?? const SizedBox(); - } -} diff --git a/packages/mirai/lib/src/parsers/mirai_text_form_field/mirai_text_form_field_parser.dart b/packages/mirai/lib/src/parsers/mirai_text_form_field/mirai_text_form_field_parser.dart index f39d57ce..334f43be 100644 --- a/packages/mirai/lib/src/parsers/mirai_text_form_field/mirai_text_form_field_parser.dart +++ b/packages/mirai/lib/src/parsers/mirai_text_form_field/mirai_text_form_field_parser.dart @@ -1,8 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mirai/src/framework/framework.dart'; import 'package:mirai/src/parsers/mirai_edge_insets/mirai_edge_insets.dart'; -import 'package:mirai/src/parsers/mirai_form/cubit/mirai_form_cubit.dart'; +import 'package:mirai/src/parsers/mirai_form/mirai_form_parser.dart'; import 'package:mirai/src/parsers/mirai_form_field_validator/mirai_form_validator.dart'; import 'package:mirai/src/parsers/mirai_input_decoration/mirai_input_decoration.dart'; import 'package:mirai/src/parsers/mirai_input_formatters/mirai_input_formatter.dart'; @@ -39,10 +38,10 @@ class _TextFormFieldWidget extends StatefulWidget { final MiraiTextFormField model; @override - State<_TextFormFieldWidget> createState() => __TextFormFieldWidgetState(); + State<_TextFormFieldWidget> createState() => _TextFormFieldWidgetState(); } -class __TextFormFieldWidgetState extends State<_TextFormFieldWidget> { +class _TextFormFieldWidgetState extends State<_TextFormFieldWidget> { TextEditingController controller = TextEditingController(); FocusNode? focusNode = FocusNode(); bool obscureText = false; @@ -50,9 +49,9 @@ class __TextFormFieldWidgetState extends State<_TextFormFieldWidget> { @override void initState() { if (widget.model.id != null) { - context - .read() - .registerValue(widget.model.id!, widget.model.initialValue ?? ""); + // context + // .read() + // .registerValue(widget.model.id!, widget.model.initialValue ?? ""); } controller = TextEditingController(text: widget.model.initialValue); @@ -62,15 +61,19 @@ class __TextFormFieldWidgetState extends State<_TextFormFieldWidget> { @override Widget build(BuildContext context) { + final formScope = MiraiFormScope.of(context); return TextFormField( controller: controller, focusNode: focusNode, onChanged: (value) { + Log.d("id: ${widget.model.id}"); if (widget.model.id != null) { - context.read().updateValue( - widget.model.id!, - value, - ); + formScope.formData[widget.model.id!] = value; + Log.d(formScope.formData); + // context.read().updateValue( + // widget.model.id!, + // value, + // ); } }, keyboardType: widget.model.keyboardType?.value, @@ -115,9 +118,9 @@ class __TextFormFieldWidgetState extends State<_TextFormFieldWidget> { ); if (widget.model.id != null) { - context - .read() - .updateValidation(widget.model.id!, validation == null); + // context + // .read() + // .updateValidation(widget.model.id!, validation == null); } return validation; @@ -154,9 +157,9 @@ class __TextFormFieldWidgetState extends State<_TextFormFieldWidget> { String? compareVal; if (widget.model.compareId != null) { try { - compareVal = context - .read() - .getValue(widget.model.compareId!); + // compareVal = context + // .read() + // .getValue(widget.model.compareId!); } catch (e) { Log.e(e); } diff --git a/packages/mirai/lib/src/services/mirai_network_service.dart b/packages/mirai/lib/src/services/mirai_network_service.dart new file mode 100644 index 00000000..5e2c0501 --- /dev/null +++ b/packages/mirai/lib/src/services/mirai_network_service.dart @@ -0,0 +1,130 @@ +import 'dart:io'; + +import 'package:dio/dio.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:mirai/src/action_parsers/mirai_network_request/mirai_network_request.dart'; +import 'package:mirai/src/framework/framework.dart'; +import 'package:mirai/src/utils/log.dart'; + +class MiraiNetworkService { + const MiraiNetworkService._(); + + static late Dio _dio; + + static void initialize(Dio dio) => _dio = dio; + + static Future request( + MiraiNetworkRequest request, + BuildContext context, + ) async { + Map headers = + Map.from(request.headers ?? {}); + headers = await _updateBody(context.mounted ? context : context, headers); + _dio.options.headers = headers; + _dio.options.contentType = request.contentType; + + switch (request.method) { + case Method.get: + return getRequest(request); + case Method.post: + return postRequest(request, context.mounted ? context : context); + case Method.put: + return putRequest(request); + case Method.delete: + return deleteRequest(request); + } + } + + static Future getRequest(MiraiNetworkRequest request) async { + return _dio.get( + request.url, + data: request.body, + queryParameters: request.queryParameters, + ); + } + + static Future postRequest( + MiraiNetworkRequest request, + BuildContext context, + ) async { + final body = await _updateBody(context, request.body); + + Map headers = + Map.from(request.headers ?? {}); + headers = await _updateBody(context.mounted ? context : context, headers); + return _dio.post( + request.url, + data: body, + queryParameters: request.queryParameters, + ); + } + + static Future putRequest(MiraiNetworkRequest request) async { + return _dio.put( + request.url, + data: request.body, + queryParameters: request.queryParameters, + ); + } + + static Future deleteRequest(MiraiNetworkRequest request) async { + return _dio.delete( + request.url, + data: request.body, + queryParameters: request.queryParameters, + ); + } + + static Future _updateBody( + BuildContext context, + dynamic body, + ) async { + 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')) { + Log.d("Loading from an action callback"); + + final dynamic callbackValue = await Future.value( + Mirai.onCallFromJson(value as Map, context), + ); + + body[key] = callbackValue; + continue; + } 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); + } + + if (mapEntry.value is List) { + for (Map listItem in mapEntry.value) { + _updateBody(context.mounted ? context : context, listItem); + } + } + } + } else if (body is List) { + List updatedList = []; + for (dynamic value in body) { + final updatedValue = await _updateBody(context, value); + updatedList.add(updatedValue); + } + + body = updatedList; + } + + return body; + } +} diff --git a/packages/mirai/lib/src/services/services.dart b/packages/mirai/lib/src/services/services.dart new file mode 100644 index 00000000..68279e3a --- /dev/null +++ b/packages/mirai/lib/src/services/services.dart @@ -0,0 +1,3 @@ +library services; + +export 'mirai_network_service.dart'; diff --git a/packages/mirai/lib/src/utils/action_type.dart b/packages/mirai/lib/src/utils/action_type.dart index 5cf54e09..c8eee0bd 100644 --- a/packages/mirai/lib/src/utils/action_type.dart +++ b/packages/mirai/lib/src/utils/action_type.dart @@ -1,7 +1,6 @@ enum ActionType { navigate, - none, - request, + networkRequest, showModalBottomSheet, showDialog, } diff --git a/packages/mirai/pubspec.yaml b/packages/mirai/pubspec.yaml index 951f0e0d..2fadb2ef 100644 --- a/packages/mirai/pubspec.yaml +++ b/packages/mirai/pubspec.yaml @@ -11,7 +11,6 @@ dependencies: flutter: sdk: flutter freezed_annotation: ^2.2.0 - flutter_bloc: ^8.1.2 json_annotation: ^4.8.1 logger: ^1.4.0 dio: ^5.2.0+1 diff --git a/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview.freezed.dart b/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview.freezed.dart index 419ed802..4e956d39 100644 --- a/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview.freezed.dart +++ b/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview.freezed.dart @@ -91,11 +91,11 @@ class _$MiraiWebViewCopyWithImpl<$Res, $Val extends MiraiWebView> } /// @nodoc -abstract class _$$_MiraiWebViewCopyWith<$Res> +abstract class _$$MiraiWebViewImplCopyWith<$Res> implements $MiraiWebViewCopyWith<$Res> { - factory _$$_MiraiWebViewCopyWith( - _$_MiraiWebView value, $Res Function(_$_MiraiWebView) then) = - __$$_MiraiWebViewCopyWithImpl<$Res>; + factory _$$MiraiWebViewImplCopyWith( + _$MiraiWebViewImpl value, $Res Function(_$MiraiWebViewImpl) then) = + __$$MiraiWebViewImplCopyWithImpl<$Res>; @override @useResult $Res call( @@ -107,11 +107,11 @@ abstract class _$$_MiraiWebViewCopyWith<$Res> } /// @nodoc -class __$$_MiraiWebViewCopyWithImpl<$Res> - extends _$MiraiWebViewCopyWithImpl<$Res, _$_MiraiWebView> - implements _$$_MiraiWebViewCopyWith<$Res> { - __$$_MiraiWebViewCopyWithImpl( - _$_MiraiWebView _value, $Res Function(_$_MiraiWebView) _then) +class __$$MiraiWebViewImplCopyWithImpl<$Res> + extends _$MiraiWebViewCopyWithImpl<$Res, _$MiraiWebViewImpl> + implements _$$MiraiWebViewImplCopyWith<$Res> { + __$$MiraiWebViewImplCopyWithImpl( + _$MiraiWebViewImpl _value, $Res Function(_$MiraiWebViewImpl) _then) : super(_value, _then); @pragma('vm:prefer-inline') @@ -123,7 +123,7 @@ class __$$_MiraiWebViewCopyWithImpl<$Res> Object? userAgent = freezed, Object? enableZoom = null, }) { - return _then(_$_MiraiWebView( + return _then(_$MiraiWebViewImpl( url: null == url ? _value.url : url // ignore: cast_nullable_to_non_nullable @@ -150,16 +150,16 @@ class __$$_MiraiWebViewCopyWithImpl<$Res> /// @nodoc @JsonSerializable() -class _$_MiraiWebView implements _MiraiWebView { - const _$_MiraiWebView( +class _$MiraiWebViewImpl implements _MiraiWebView { + const _$MiraiWebViewImpl( {required this.url, this.javaScriptMode = JavaScriptMode.unrestricted, this.backgroundColor = "#000000", this.userAgent, this.enableZoom = false}); - factory _$_MiraiWebView.fromJson(Map json) => - _$$_MiraiWebViewFromJson(json); + factory _$MiraiWebViewImpl.fromJson(Map json) => + _$$MiraiWebViewImplFromJson(json); @override final String url; @@ -184,7 +184,7 @@ class _$_MiraiWebView implements _MiraiWebView { bool operator ==(dynamic other) { return identical(this, other) || (other.runtimeType == runtimeType && - other is _$_MiraiWebView && + other is _$MiraiWebViewImpl && (identical(other.url, url) || other.url == url) && (identical(other.javaScriptMode, javaScriptMode) || other.javaScriptMode == javaScriptMode) && @@ -204,12 +204,12 @@ class _$_MiraiWebView implements _MiraiWebView { @JsonKey(ignore: true) @override @pragma('vm:prefer-inline') - _$$_MiraiWebViewCopyWith<_$_MiraiWebView> get copyWith => - __$$_MiraiWebViewCopyWithImpl<_$_MiraiWebView>(this, _$identity); + _$$MiraiWebViewImplCopyWith<_$MiraiWebViewImpl> get copyWith => + __$$MiraiWebViewImplCopyWithImpl<_$MiraiWebViewImpl>(this, _$identity); @override Map toJson() { - return _$$_MiraiWebViewToJson( + return _$$MiraiWebViewImplToJson( this, ); } @@ -221,10 +221,10 @@ abstract class _MiraiWebView implements MiraiWebView { final JavaScriptMode javaScriptMode, final String backgroundColor, final String? userAgent, - final bool enableZoom}) = _$_MiraiWebView; + final bool enableZoom}) = _$MiraiWebViewImpl; factory _MiraiWebView.fromJson(Map json) = - _$_MiraiWebView.fromJson; + _$MiraiWebViewImpl.fromJson; @override String get url; @@ -238,6 +238,6 @@ abstract class _MiraiWebView implements MiraiWebView { bool get enableZoom; @override @JsonKey(ignore: true) - _$$_MiraiWebViewCopyWith<_$_MiraiWebView> get copyWith => + _$$MiraiWebViewImplCopyWith<_$MiraiWebViewImpl> get copyWith => throw _privateConstructorUsedError; } diff --git a/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview.g.dart b/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview.g.dart index 3ef18f8e..a1f6c50d 100644 --- a/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview.g.dart +++ b/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview.g.dart @@ -6,8 +6,8 @@ part of 'mirai_webview.dart'; // JsonSerializableGenerator // ************************************************************************** -_$_MiraiWebView _$$_MiraiWebViewFromJson(Map json) => - _$_MiraiWebView( +_$MiraiWebViewImpl _$$MiraiWebViewImplFromJson(Map json) => + _$MiraiWebViewImpl( url: json['url'] as String, javaScriptMode: $enumDecodeNullable( _$JavaScriptModeEnumMap, json['javaScriptMode']) ?? @@ -17,7 +17,7 @@ _$_MiraiWebView _$$_MiraiWebViewFromJson(Map json) => enableZoom: json['enableZoom'] as bool? ?? false, ); -Map _$$_MiraiWebViewToJson(_$_MiraiWebView instance) => +Map _$$MiraiWebViewImplToJson(_$MiraiWebViewImpl instance) => { 'url': instance.url, 'javaScriptMode': _$JavaScriptModeEnumMap[instance.javaScriptMode]!,