From 5d215bb6ac89e31aef9b5b98905bf36127fc37cf Mon Sep 17 00:00:00 2001 From: Divyanshu Bhargava Date: Tue, 21 Jan 2025 17:09:18 +0400 Subject: [PATCH 1/7] Update package versions for mirai_webview: webview_flutter, freezed_annotation, json_annotation, mirai_framework --- packages/mirai_webview/pubspec.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/mirai_webview/pubspec.yaml b/packages/mirai_webview/pubspec.yaml index 8ce5dabc..9c98dc49 100644 --- a/packages/mirai_webview/pubspec.yaml +++ b/packages/mirai_webview/pubspec.yaml @@ -10,10 +10,10 @@ environment: dependencies: flutter: sdk: flutter - webview_flutter: ^4.2.4 - freezed_annotation: ^2.2.0 - json_annotation: ^4.8.1 - mirai_framework: ^0.0.1 + webview_flutter: ^4.10.0 + freezed_annotation: ^2.4.4 + json_annotation: ^4.9.0 + mirai_framework: ^0.0.3 dev_dependencies: flutter_test: From 02c92d9c53a02d61bc73fc1bb2a93920b575f289 Mon Sep 17 00:00:00 2001 From: Divyanshu Bhargava Date: Tue, 21 Jan 2025 17:09:26 +0400 Subject: [PATCH 2/7] Remove `mirai_webview_test.dart` file --- packages/mirai_webview/test/mirai_webview_test.dart | 1 - 1 file changed, 1 deletion(-) delete mode 100644 packages/mirai_webview/test/mirai_webview_test.dart diff --git a/packages/mirai_webview/test/mirai_webview_test.dart b/packages/mirai_webview/test/mirai_webview_test.dart deleted file mode 100644 index 8b137891..00000000 --- a/packages/mirai_webview/test/mirai_webview_test.dart +++ /dev/null @@ -1 +0,0 @@ - From cc9dcbbb897ea05cb6746a95586258710631aa1b Mon Sep 17 00:00:00 2001 From: Divyanshu Bhargava Date: Tue, 21 Jan 2025 17:09:56 +0400 Subject: [PATCH 3/7] feat: Update Mirai webview parser --- .../parsers/mirai_webview/mirai_webview.dart | 25 +++- .../mirai_webview/mirai_webview.freezed.dart | 118 ++++++++++++++++-- .../mirai_webview/mirai_webview.g.dart | 11 +- .../mirai_webview/mirai_webview_parser.dart | 39 +++++- 4 files changed, 176 insertions(+), 17 deletions(-) diff --git a/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview.dart b/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview.dart index 72ddbd52..3fc1d0cb 100644 --- a/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview.dart +++ b/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview.dart @@ -1,3 +1,4 @@ +import 'package:flutter/material.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:webview_flutter/webview_flutter.dart'; @@ -6,14 +7,36 @@ export 'mirai_webview_parser.dart'; part 'mirai_webview.freezed.dart'; part 'mirai_webview.g.dart'; +/// `MiraiWebView` is a widget that renders a web page within a flutter application. @freezed class MiraiWebView with _$MiraiWebView { + /// `MiraiWebView` constructor. const factory MiraiWebView({ + /// The URL to load in the `WebView`. required String url, + + /// Sets whether JavaScript execution is enabled. + /// + /// Default value is `JavaScriptMode.unrestricted`. @Default(JavaScriptMode.unrestricted) JavaScriptMode javaScriptMode, - @Default("#000000") String backgroundColor, + + /// Background color of the `WebView`. + /// + /// Default value is `#FFFFFF`. + @Default("#FFFFFF") String backgroundColor, + + /// The user agent for the `WebView`. String? userAgent, + + /// Sets whether zoom is enabled for the `WebView`. + /// + /// Default value is `false`. @Default(false) bool enableZoom, + + /// The layout direction for the `WebView`. + /// + /// Default value is `TextDirection.ltr`. + @Default(TextDirection.ltr) TextDirection layoutDirection, }) = _MiraiWebView; factory MiraiWebView.fromJson(Map json) => 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 d3764152..be173d52 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 @@ -20,14 +20,38 @@ MiraiWebView _$MiraiWebViewFromJson(Map json) { /// @nodoc mixin _$MiraiWebView { + /// The URL to load in the `WebView`. String get url => throw _privateConstructorUsedError; + + /// Sets whether JavaScript execution is enabled. + /// + /// Default value is `JavaScriptMode.unrestricted`. JavaScriptMode get javaScriptMode => throw _privateConstructorUsedError; + + /// Background color of the `WebView`. + /// + /// Default value is `#FFFFFF`. String get backgroundColor => throw _privateConstructorUsedError; + + /// The user agent for the `WebView`. String? get userAgent => throw _privateConstructorUsedError; + + /// Sets whether zoom is enabled for the `WebView`. + /// + /// Default value is `false`. bool get enableZoom => throw _privateConstructorUsedError; + /// The layout direction for the `WebView`. + /// + /// Default value is `TextDirection.ltr`. + TextDirection get layoutDirection => throw _privateConstructorUsedError; + + /// Serializes this MiraiWebView to a JSON map. Map toJson() => throw _privateConstructorUsedError; - @JsonKey(ignore: true) + + /// Create a copy of MiraiWebView + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) $MiraiWebViewCopyWith get copyWith => throw _privateConstructorUsedError; } @@ -43,7 +67,8 @@ abstract class $MiraiWebViewCopyWith<$Res> { JavaScriptMode javaScriptMode, String backgroundColor, String? userAgent, - bool enableZoom}); + bool enableZoom, + TextDirection layoutDirection}); } /// @nodoc @@ -56,6 +81,8 @@ class _$MiraiWebViewCopyWithImpl<$Res, $Val extends MiraiWebView> // ignore: unused_field final $Res Function($Val) _then; + /// Create a copy of MiraiWebView + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -64,6 +91,7 @@ class _$MiraiWebViewCopyWithImpl<$Res, $Val extends MiraiWebView> Object? backgroundColor = null, Object? userAgent = freezed, Object? enableZoom = null, + Object? layoutDirection = null, }) { return _then(_value.copyWith( url: null == url @@ -86,6 +114,10 @@ class _$MiraiWebViewCopyWithImpl<$Res, $Val extends MiraiWebView> ? _value.enableZoom : enableZoom // ignore: cast_nullable_to_non_nullable as bool, + layoutDirection: null == layoutDirection + ? _value.layoutDirection + : layoutDirection // ignore: cast_nullable_to_non_nullable + as TextDirection, ) as $Val); } } @@ -103,7 +135,8 @@ abstract class _$$MiraiWebViewImplCopyWith<$Res> JavaScriptMode javaScriptMode, String backgroundColor, String? userAgent, - bool enableZoom}); + bool enableZoom, + TextDirection layoutDirection}); } /// @nodoc @@ -114,6 +147,8 @@ class __$$MiraiWebViewImplCopyWithImpl<$Res> _$MiraiWebViewImpl _value, $Res Function(_$MiraiWebViewImpl) _then) : super(_value, _then); + /// Create a copy of MiraiWebView + /// with the given fields replaced by the non-null parameter values. @pragma('vm:prefer-inline') @override $Res call({ @@ -122,6 +157,7 @@ class __$$MiraiWebViewImplCopyWithImpl<$Res> Object? backgroundColor = null, Object? userAgent = freezed, Object? enableZoom = null, + Object? layoutDirection = null, }) { return _then(_$MiraiWebViewImpl( url: null == url @@ -144,6 +180,10 @@ class __$$MiraiWebViewImplCopyWithImpl<$Res> ? _value.enableZoom : enableZoom // ignore: cast_nullable_to_non_nullable as bool, + layoutDirection: null == layoutDirection + ? _value.layoutDirection + : layoutDirection // ignore: cast_nullable_to_non_nullable + as TextDirection, )); } } @@ -154,30 +194,53 @@ class _$MiraiWebViewImpl implements _MiraiWebView { const _$MiraiWebViewImpl( {required this.url, this.javaScriptMode = JavaScriptMode.unrestricted, - this.backgroundColor = "#000000", + this.backgroundColor = "#FFFFFF", this.userAgent, - this.enableZoom = false}); + this.enableZoom = false, + this.layoutDirection = TextDirection.ltr}); factory _$MiraiWebViewImpl.fromJson(Map json) => _$$MiraiWebViewImplFromJson(json); + /// The URL to load in the `WebView`. @override final String url; + + /// Sets whether JavaScript execution is enabled. + /// + /// Default value is `JavaScriptMode.unrestricted`. @override @JsonKey() final JavaScriptMode javaScriptMode; + + /// Background color of the `WebView`. + /// + /// Default value is `#FFFFFF`. @override @JsonKey() final String backgroundColor; + + /// The user agent for the `WebView`. @override final String? userAgent; + + /// Sets whether zoom is enabled for the `WebView`. + /// + /// Default value is `false`. @override @JsonKey() final bool enableZoom; + /// The layout direction for the `WebView`. + /// + /// Default value is `TextDirection.ltr`. + @override + @JsonKey() + final TextDirection layoutDirection; + @override String toString() { - return 'MiraiWebView(url: $url, javaScriptMode: $javaScriptMode, backgroundColor: $backgroundColor, userAgent: $userAgent, enableZoom: $enableZoom)'; + return 'MiraiWebView(url: $url, javaScriptMode: $javaScriptMode, backgroundColor: $backgroundColor, userAgent: $userAgent, enableZoom: $enableZoom, layoutDirection: $layoutDirection)'; } @override @@ -193,15 +256,19 @@ class _$MiraiWebViewImpl implements _MiraiWebView { (identical(other.userAgent, userAgent) || other.userAgent == userAgent) && (identical(other.enableZoom, enableZoom) || - other.enableZoom == enableZoom)); + other.enableZoom == enableZoom) && + (identical(other.layoutDirection, layoutDirection) || + other.layoutDirection == layoutDirection)); } - @JsonKey(ignore: true) + @JsonKey(includeFromJson: false, includeToJson: false) @override - int get hashCode => Object.hash( - runtimeType, url, javaScriptMode, backgroundColor, userAgent, enableZoom); + int get hashCode => Object.hash(runtimeType, url, javaScriptMode, + backgroundColor, userAgent, enableZoom, layoutDirection); - @JsonKey(ignore: true) + /// Create a copy of MiraiWebView + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) @override @pragma('vm:prefer-inline') _$$MiraiWebViewImplCopyWith<_$MiraiWebViewImpl> get copyWith => @@ -221,23 +288,48 @@ abstract class _MiraiWebView implements MiraiWebView { final JavaScriptMode javaScriptMode, final String backgroundColor, final String? userAgent, - final bool enableZoom}) = _$MiraiWebViewImpl; + final bool enableZoom, + final TextDirection layoutDirection}) = _$MiraiWebViewImpl; factory _MiraiWebView.fromJson(Map json) = _$MiraiWebViewImpl.fromJson; + /// The URL to load in the `WebView`. @override String get url; + + /// Sets whether JavaScript execution is enabled. + /// + /// Default value is `JavaScriptMode.unrestricted`. @override JavaScriptMode get javaScriptMode; + + /// Background color of the `WebView`. + /// + /// Default value is `#FFFFFF`. @override String get backgroundColor; + + /// The user agent for the `WebView`. @override String? get userAgent; + + /// Sets whether zoom is enabled for the `WebView`. + /// + /// Default value is `false`. @override bool get enableZoom; + + /// The layout direction for the `WebView`. + /// + /// Default value is `TextDirection.ltr`. + @override + TextDirection get layoutDirection; + + /// Create a copy of MiraiWebView + /// with the given fields replaced by the non-null parameter values. @override - @JsonKey(ignore: true) + @JsonKey(includeFromJson: false, includeToJson: false) _$$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 a1f6c50d..7471bf1c 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 @@ -12,9 +12,12 @@ _$MiraiWebViewImpl _$$MiraiWebViewImplFromJson(Map json) => javaScriptMode: $enumDecodeNullable( _$JavaScriptModeEnumMap, json['javaScriptMode']) ?? JavaScriptMode.unrestricted, - backgroundColor: json['backgroundColor'] as String? ?? "#000000", + backgroundColor: json['backgroundColor'] as String? ?? "#FFFFFF", userAgent: json['userAgent'] as String?, enableZoom: json['enableZoom'] as bool? ?? false, + layoutDirection: $enumDecodeNullable( + _$TextDirectionEnumMap, json['layoutDirection']) ?? + TextDirection.ltr, ); Map _$$MiraiWebViewImplToJson(_$MiraiWebViewImpl instance) => @@ -24,9 +27,15 @@ Map _$$MiraiWebViewImplToJson(_$MiraiWebViewImpl instance) => 'backgroundColor': instance.backgroundColor, 'userAgent': instance.userAgent, 'enableZoom': instance.enableZoom, + 'layoutDirection': _$TextDirectionEnumMap[instance.layoutDirection]!, }; const _$JavaScriptModeEnumMap = { JavaScriptMode.disabled: 'disabled', JavaScriptMode.unrestricted: 'unrestricted', }; + +const _$TextDirectionEnumMap = { + TextDirection.rtl: 'rtl', + TextDirection.ltr: 'ltr', +}; diff --git a/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview_parser.dart b/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview_parser.dart index 85c59984..06abe983 100644 --- a/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview_parser.dart +++ b/packages/mirai_webview/lib/parsers/mirai_webview/mirai_webview_parser.dart @@ -3,12 +3,17 @@ import 'package:mirai_framework/mirai_framework.dart'; import 'package:mirai_webview/parsers/mirai_webview/mirai_webview.dart'; import 'package:webview_flutter/webview_flutter.dart'; +/// [MiraiWebViewParser] is a class that parses the `webView` widget from JSON. class MiraiWebViewParser extends MiraiParser { const MiraiWebViewParser(); + /// [webView] is the type defined for MiraiWebViewParser. @override String get type => 'webView'; + /// [getModel] method parses the JSON data and returns a [MiraiWebView] object. + /// + /// {@macro getModel} @override MiraiWebView getModel(Map json) => MiraiWebView.fromJson(json); @@ -19,37 +24,65 @@ class MiraiWebViewParser extends MiraiParser { } } +/// [_WebView] is a stateful widget that displays a webview. class _WebView extends StatefulWidget { + /// [_WebView] constructor. const _WebView({required this.model}); + /// `model` is the data that will be used to configure the webview. final MiraiWebView model; + /// [createState] method creates the state for the [_WebView] widget. @override State<_WebView> createState() => _WebViewState(); } +/// [_WebViewState] is the state for the [_WebView] widget. class _WebViewState extends State<_WebView> { + /// `_controller` is the controller for the webview. late final WebViewController _controller; @override void initState() { super.initState(); + + /// [_controller] is the controller for the webview. _controller = WebViewController() + + /// Loads the request. ..loadRequest(Uri.parse(widget.model.url)) + + /// Sets the JavaScript mode. ..setJavaScriptMode(widget.model.javaScriptMode) + + /// Sets the background color. ..setBackgroundColor( - widget.model.backgroundColor.toColor ?? Colors.black, + widget.model.backgroundColor.toColor ?? Colors.white, ) + + /// Sets the user agent. ..setUserAgent(widget.model.userAgent) + + /// Enables or disables zoom. ..enableZoom(widget.model.enableZoom); } + /// [build] method builds the widget. + /// + /// {@macro build} @override Widget build(BuildContext context) { - return WebViewWidget(controller: _controller); + /// [WebViewWidget] is a widget that displays a webview. + return WebViewWidget( + controller: _controller, + layoutDirection: widget.model.layoutDirection, + ); } } +/// Extension on [String] to convert to [Color]. +/// +/// {@macro toColor} extension ColorExt on String? { Color? get toColor { if (this?.isEmpty ?? true) return null; @@ -57,6 +90,8 @@ extension ColorExt on String? { final buffer = StringBuffer(); if (this!.length == 6 || this!.length == 7) buffer.write('ff'); buffer.write(this!.replaceFirst('#', '')); + + /// convert to int int? intColor = int.tryParse(buffer.toString(), radix: 16); intColor = intColor ?? 0x00000000; return Color(intColor); From babf09b2849cd04bbb9a1df702a09e1a554774df Mon Sep 17 00:00:00 2001 From: Divyanshu Bhargava Date: Tue, 21 Jan 2025 17:10:12 +0400 Subject: [PATCH 4/7] chore: Update mirai webview readme --- packages/mirai_webview/README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/mirai_webview/README.md b/packages/mirai_webview/README.md index 1a6940e9..53a0be15 100644 --- a/packages/mirai_webview/README.md +++ b/packages/mirai_webview/README.md @@ -1,11 +1,19 @@ # Mirai Web View -A webView support for Mirai. +A webView support for [Mirai](https://pub.dev/packages/mirai). ## Usage 1. Add `mirai_webview` as a dependency in your pubspec.yaml file. +Install the plugin by running the following command from the project root: + +```bash +flutter pub add mirai_webview +``` + +or add it manually in your `pubspec.yaml` file: + ```yaml dependencies: mirai_webview: From 6c1f1e83963eb6fa55ae9ce0f49264bb1d288dd1 Mon Sep 17 00:00:00 2001 From: Divyanshu Bhargava Date: Tue, 21 Jan 2025 17:18:20 +0400 Subject: [PATCH 5/7] docs: Add WebView documentation --- website/docs/widgets/webview.md | 53 +++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 website/docs/widgets/webview.md diff --git a/website/docs/widgets/webview.md b/website/docs/widgets/webview.md new file mode 100644 index 00000000..17574c87 --- /dev/null +++ b/website/docs/widgets/webview.md @@ -0,0 +1,53 @@ +# WebView + +The Mirai WebView allows you to display WebView widget using JSON in your app. It is based on the [webview_flutter](https://pub.dev/packages/webview_flutter) plugin. + +## Usage + +1. Add `mirai_webview` as a dependency in your pubspec.yaml file. + +Install the plugin by running the following command from the project root: + +```bash +flutter pub add mirai_webview +``` + +or add it manually in your `pubspec.yaml` file: + +```yaml + dependencies: + mirai_webview: +``` + +2. Add `MiraiWebViewParser` in Mirai initialize. + +```dart +void main() async { + await Mirai.initialize( + parsers: const [ + MiraiWebViewParser(), + ], + ); + + runApp(const MyApp()); +} +``` +## Properties + +| Property | Type | Description | +|-------------------|------------------|-----------------------------------------------------------------------------------------| +| `url` | `String` | The URL to load in the `WebView`. | +| `javaScriptMode` | `JavaScriptMode` | Sets whether JavaScript execution is enabled. Default is `JavaScriptMode.unrestricted`. | +| `backgroundColor` | `String` | Background color of the `WebView`. Default is `#FFFFFF`. | +| `userAgent` | `String?` | The user agent for the `WebView`. | +| `enableZoom` | `bool` | Sets whether zoom is enabled for the `WebView`. Default is `false`. | +| `layoutDirection` | `TextDirection` | The layout direction for the `WebView`. Default is `TextDirection.ltr`. | + +## Example JSON + +```json +{ + "type": "webView", + "url": "https://github.com/buildMirai/mirai" +} +``` \ No newline at end of file From 8369161977ebaa8bdf66d70b454016122d1205ef Mon Sep 17 00:00:00 2001 From: Divyanshu Bhargava Date: Tue, 21 Jan 2025 17:23:03 +0400 Subject: [PATCH 6/7] chore: Update mirai_webview package version to 0.0.4 --- packages/mirai_webview/CHANGELOG.md | 6 ++++++ packages/mirai_webview/pubspec.yaml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/mirai_webview/CHANGELOG.md b/packages/mirai_webview/CHANGELOG.md index c0666d0f..be7a3432 100644 --- a/packages/mirai_webview/CHANGELOG.md +++ b/packages/mirai_webview/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.0.4 + +* Update webview_flutter to 4.10.0 +* Add layoutDirection property to the WebView +* Add documentation for the WebView + ## 0.0.3 * minor changes diff --git a/packages/mirai_webview/pubspec.yaml b/packages/mirai_webview/pubspec.yaml index 9c98dc49..cf372c20 100644 --- a/packages/mirai_webview/pubspec.yaml +++ b/packages/mirai_webview/pubspec.yaml @@ -1,6 +1,6 @@ name: mirai_webview description: A WebView Parser for Mirai. A wrapper of `webview_flutter` package for Mirai. -version: 0.0.3 +version: 0.0.4 homepage: https://github.com/buildMirai/mirai environment: From 3ab2d6436a26ee5d69ba1e82d267e2fc6c34eabf Mon Sep 17 00:00:00 2001 From: Divyanshu Bhargava Date: Tue, 21 Jan 2025 17:26:40 +0400 Subject: [PATCH 7/7] Update mirai_webview package version in pubspec.lock --- examples/mirai_gallery/pubspec.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/mirai_gallery/pubspec.lock b/examples/mirai_gallery/pubspec.lock index 3f09c13f..38afc84f 100644 --- a/examples/mirai_gallery/pubspec.lock +++ b/examples/mirai_gallery/pubspec.lock @@ -452,7 +452,7 @@ packages: path: "../../packages/mirai_webview" relative: true source: path - version: "0.0.3" + version: "0.0.4" nested: dependency: transitive description: