added disposable internal class, fixed some docs

This commit is contained in:
Lorenzo Pichilli 2022-05-13 09:35:50 +02:00
parent 0dd9dfbff6
commit c81cfc964c
16 changed files with 138 additions and 134 deletions

View File

@ -1,5 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_test/flutter_test.dart';

View File

@ -2,7 +2,6 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_test/flutter_test.dart';

View File

@ -13,6 +13,53 @@ import '../in_app_webview/android/in_app_webview_options.dart';
import 'apple/in_app_browser_options.dart';
import '../in_app_webview/apple/in_app_webview_options.dart';
///Class that represents the settings that can be used for an [InAppBrowser] instance.
class InAppBrowserClassSettings {
///Browser settings.
late InAppBrowserSettings browserSettings;
///WebView settings.
late InAppWebViewSettings webViewSettings;
InAppBrowserClassSettings(
{InAppBrowserSettings? browserSettings,
InAppWebViewSettings? webViewSettings}) {
this.browserSettings = browserSettings ?? InAppBrowserSettings();
this.webViewSettings = webViewSettings ?? InAppWebViewSettings();
}
Map<String, dynamic> toMap() {
Map<String, dynamic> options = {};
options.addAll(browserSettings.toMap());
options.addAll(webViewSettings.toMap());
return options;
}
Map<String, dynamic> toJson() {
return toMap();
}
String toString() {
return toMap().toString();
}
factory InAppBrowserClassSettings.fromMap(Map<String, dynamic> options,
{InAppBrowserClassSettings? instance}) {
if (instance == null) {
instance = InAppBrowserClassSettings();
}
instance.browserSettings = InAppBrowserSettings.fromMap(options);
instance.webViewSettings = InAppWebViewSettings.fromMap(options);
return instance;
}
InAppBrowserClassSettings copy() {
return InAppBrowserClassSettings.fromMap(toMap());
}
}
class BrowserOptions {
Map<String, dynamic> toMap() {
return {};
@ -36,54 +83,6 @@ class BrowserOptions {
}
}
///Class that represents the settings that can be used for an [InAppBrowser] instance.
class InAppBrowserClassSettings {
///Browser settings.
late InAppBrowserSettings browserSettings;
///WebView settings.
late InAppWebViewSettings webViewSettings;
InAppBrowserClassSettings(
{InAppBrowserSettings? browserSettings,
InAppWebViewSettings? webViewSettings}) {
this.browserSettings = browserSettings ?? InAppBrowserSettings();
this.webViewSettings = webViewSettings ?? InAppWebViewSettings();
}
Map<String, dynamic> toMap() {
Map<String, dynamic> options = {};
options.addAll(this.browserSettings.toMap());
options.addAll(this.webViewSettings.toMap());
return options;
}
Map<String, dynamic> toJson() {
return this.toMap();
}
@override
String toString() {
return toMap().toString();
}
static InAppBrowserClassSettings fromMap(Map<String, dynamic> options,
{InAppBrowserClassSettings? instance}) {
if (instance == null) {
instance = InAppBrowserClassSettings();
}
instance.browserSettings = InAppBrowserSettings.fromMap(options);
instance.webViewSettings = InAppWebViewSettings.fromMap(options);
return instance;
}
InAppBrowserClassSettings copy() {
return InAppBrowserClassSettings.fromMap(this.toMap());
}
}
///This class represents all [InAppBrowser] settings available.
class InAppBrowserSettings
implements BrowserOptions, AndroidOptions, IosOptions {

View File

@ -14,6 +14,7 @@ import 'in_app_webview_settings.dart';
import '../pull_to_refresh/pull_to_refresh_controller.dart';
import '../pull_to_refresh/pull_to_refresh_settings.dart';
import '../util.dart';
import '../types/disposable.dart';
///Class that represents a WebView in headless mode.
///It can be used to run a WebView in background without attaching an `InAppWebView` to the widget tree.
@ -24,7 +25,7 @@ import '../util.dart';
///- Android native WebView
///- iOS
///- Web
class HeadlessInAppWebView implements WebView {
class HeadlessInAppWebView implements WebView, Disposable {
///View ID.
late final String id;
@ -227,6 +228,7 @@ class HeadlessInAppWebView implements WebView {
///- Android native WebView
///- iOS
///- Web
@override
Future<void> dispose() async {
if (!_running) {
return;

View File

@ -1,7 +1,6 @@
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'android/in_app_webview_options.dart';
import 'apple/in_app_webview_options.dart';
@ -11,32 +10,8 @@ import '../util.dart';
import '../in_app_browser/in_app_browser_settings.dart';
import 'webview.dart';
class WebViewOptions {
Map<String, dynamic> toMap() {
return {};
}
static WebViewOptions fromMap(Map<String, dynamic> map) {
return WebViewOptions();
}
WebViewOptions copy() {
return WebViewOptions.fromMap(this.toMap());
}
Map<String, dynamic> toJson() {
return this.toMap();
}
@override
String toString() {
return toMap().toString();
}
}
///This class represents all the WebView settings available.
class InAppWebViewSettings
implements WebViewOptions, BrowserOptions, AndroidOptions, IosOptions {
class InAppWebViewSettings {
///Set to `true` to be able to listen at the [WebView.shouldOverrideUrlLoading] event. The default value is `false`.
///
///**Supported Platforms/Implementations**:
@ -139,7 +114,7 @@ class InAppWebViewSettings
///- Web
bool horizontalScrollBarEnabled;
///List of custom schemes that the WebView must handle. Use the [WebView.onLoadResourceCustomScheme] event to intercept resource requests with custom scheme.
///List of custom schemes that the WebView must handle. Use the [WebView.onLoadResourceWithCustomScheme] event to intercept resource requests with custom scheme.
///
///**NOTE**: available on iOS 11.0+.
///
@ -949,7 +924,7 @@ class InAppWebViewSettings
///- [WebView.onAjaxProgress]
///- [WebView.shouldInterceptFetchRequest]
///- [WebView.onConsoleMessage]
///- [WebView.onPrint]
///- [WebView.onPrintRequest]
///- [WebView.onWindowFocus]
///- [WebView.onWindowBlur]
///- [WebView.onFindResultReceived]
@ -1200,7 +1175,6 @@ class InAppWebViewSettings
allowingReadAccessTo == null || allowingReadAccessTo!.isScheme("file"));
}
@override
Map<String, dynamic> toMap() {
List<Map<String, Map<String, dynamic>>> contentBlockersMapList = [];
contentBlockers.forEach((contentBlocker) {
@ -1343,7 +1317,8 @@ class InAppWebViewSettings
};
}
static InAppWebViewSettings fromMap(Map<String, dynamic> map) {
///Gets a [InAppWebViewSettings] instance from a [Map] value.
factory InAppWebViewSettings.fromMap(Map<String, dynamic> map) {
List<ContentBlocker> contentBlockers = [];
List<dynamic>? contentBlockersMapList = map["contentBlockers"];
if (contentBlockersMapList != null) {
@ -1537,20 +1512,17 @@ class InAppWebViewSettings
return settings;
}
@override
Map<String, dynamic> toJson() {
return this.toMap();
InAppWebViewSettings copy() {
return InAppWebViewSettings.fromMap(toMap());
}
Map<String, dynamic> toJson() {
return toMap();
}
@override
String toString() {
return toMap().toString();
}
@override
InAppWebViewSettings copy() {
return InAppWebViewSettings.fromMap(this.toMap());
}
}
///Class that represents the options that can be used for a [WebView].
@ -1615,6 +1587,29 @@ class InAppWebViewGroupOptions {
}
}
class WebViewOptions {
Map<String, dynamic> toMap() {
return {};
}
static WebViewOptions fromMap(Map<String, dynamic> map) {
return WebViewOptions();
}
WebViewOptions copy() {
return WebViewOptions.fromMap(this.toMap());
}
Map<String, dynamic> toJson() {
return this.toMap();
}
@override
String toString() {
return toMap().toString();
}
}
///Use [InAppWebViewSettings] instead.
@Deprecated('Use InAppWebViewSettings instead')
class InAppWebViewOptions

View File

@ -1,12 +1,13 @@
import 'package:flutter/services.dart';
import '../types/print_job_info.dart';
import '../in_app_webview/in_app_webview_controller.dart';
import '../types/disposable.dart';
///A completion handler for the [PrintJobController].
typedef PrintJobCompletionHandler = Future<void> Function(bool completed, String? error)?;
///Class representing a print job eventually returned by [InAppWebViewController.printCurrentPage].
class PrintJobController {
class PrintJobController implements Disposable {
///Print job ID.
final String id;
@ -96,6 +97,7 @@ class PrintJobController {
///**Supported Platforms/Implementations**:
///- Android native WebView
///- iOS
@override
Future<void> dispose() async {
Map<String, dynamic> args = <String, dynamic>{};
await _channel.invokeMethod('dispose', args);

View File

@ -1,5 +1,4 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter_inappwebview/src/types/print_job_media_size.dart';
import '../types/main.dart';
import '../util.dart';
@ -178,37 +177,36 @@ class PrintJobSettings {
this.footerHeight,
this.headerHeight});
///Gets a possible [PrintJobSettings] instance from a [Map] value.
static PrintJobSettings fromMap(Map<String, dynamic> map) {
var settings = PrintJobSettings();
settings.handledByClient = map["handledByClient"];
settings.jobName = map["jobName"];
settings.animated = map["animated"];
settings.orientation = PrintJobOrientation.fromValue(map["orientation"]);
settings.numberOfPages = map["numberOfPages"];
settings.forceRenderingQuality =
PrintJobRenderingQuality.fromValue(map["forceRenderingQuality"]);
settings.margins =
MapEdgeInsets.fromMap(map["margins"]?.cast<String, dynamic>());
settings.mediaSize =
PrintJobMediaSize.fromMap(map["mediaSize"]?.cast<String, dynamic>());
settings.colorMode = PrintJobColorMode.fromValue(map["colorMode"]);
settings.duplexMode = PrintJobDuplexMode.fromNativeValue(map["duplexMode"]);
settings.outputType = PrintJobOutputType.fromValue(map["outputType"]);
settings.resolution =
PrintJobResolution.fromMap(map["resolution"]?.cast<String, dynamic>());
settings.showsNumberOfCopies = map["showsNumberOfCopies"];
settings.showsPaperSelectionForLoadedPapers =
map["showsPaperSelectionForLoadedPapers"];
settings.showsPaperOrientation = map["showsPaperOrientation"];
settings.maximumContentHeight = map["maximumContentHeight"];
settings.maximumContentWidth = map["maximumContentWidth"];
settings.footerHeight = map["footerHeight"];
settings.headerHeight = map["headerHeight"];
return settings;
///Gets a [PrintJobSettings] instance from a [Map] value.
factory PrintJobSettings.fromMap(Map<String, dynamic> map) {
return PrintJobSettings(
handledByClient: map["handledByClient"],
jobName: map["jobName"],
animated: map["animated"],
orientation: PrintJobOrientation.fromValue(map["orientation"]),
numberOfPages: map["numberOfPages"],
forceRenderingQuality:
PrintJobRenderingQuality.fromValue(map["forceRenderingQuality"]),
margins:
MapEdgeInsets.fromMap(map["margins"]?.cast<String, dynamic>()),
mediaSize:
PrintJobMediaSize.fromMap(map["mediaSize"]?.cast<String, dynamic>()),
colorMode: PrintJobColorMode.fromValue(map["colorMode"]),
duplexMode: PrintJobDuplexMode.fromNativeValue(map["duplexMode"]),
outputType: PrintJobOutputType.fromValue(map["outputType"]),
resolution:
PrintJobResolution.fromMap(map["resolution"]?.cast<String, dynamic>()),
showsNumberOfCopies: map["showsNumberOfCopies"],
showsPaperSelectionForLoadedPapers:
map["showsPaperSelectionForLoadedPapers"],
showsPaperOrientation: map["showsPaperOrientation"],
maximumContentHeight: map["maximumContentHeight"],
maximumContentWidth: map["maximumContentWidth"],
footerHeight: map["footerHeight"],
headerHeight: map["headerHeight"],
);
}
///Converts instance to a map.
Map<String, dynamic> toMap() {
return {
"handledByClient": handledByClient,
@ -233,18 +231,15 @@ class PrintJobSettings {
};
}
///Gets a copy of the current instance.
PrintJobSettings copy() {
return PrintJobSettings.fromMap(this.toMap());
return PrintJobSettings.fromMap(toMap());
}
///Converts instance to a map.
Map<String, dynamic> toJson() {
return this.toMap();
return toMap();
}
@override
String toString() {
return toMap().toString();
}
}
}

View File

@ -55,7 +55,7 @@ class PullToRefreshSettings {
}
Map<String, dynamic> toJson() {
return this.toMap();
return toMap();
}
@override

View File

@ -2,7 +2,7 @@ import 'dart:typed_data';
import '../in_app_webview/webview.dart';
///Class representing the response returned by the [WebView.onLoadResourceCustomScheme] event.
///Class representing the response returned by the [WebView.onLoadResourceWithCustomScheme] event.
///It allows to load a specific resource. The resource data must be encoded to `base64`.
class CustomSchemeResponse {
///Data enconded to 'base64'.

View File

@ -0,0 +1,3 @@
abstract class Disposable {
void dispose();
}

View File

@ -1,4 +1,3 @@
import 'print_job_state.dart';
import '../print_job/main.dart';
import '../types/main.dart';

View File

@ -4,8 +4,9 @@ import 'package:flutter/widgets.dart';
import 'in_app_web_view_web_element.dart';
import '../util.dart';
import '../types/disposable.dart';
class HeadlessInAppWebViewWebElement {
class HeadlessInAppWebViewWebElement implements Disposable {
String id;
late BinaryMessenger _messenger;
InAppWebViewWebElement? webView;
@ -55,6 +56,7 @@ class HeadlessInAppWebViewWebElement {
webView?.iframe.style.height = size.height.toString() + "px";
}
@override
void dispose() {
_channel?.setMethodCallHandler(null);
_channel = null;

View File

@ -8,8 +8,9 @@ import 'dart:js' as js;
import 'web_platform_manager.dart';
import '../in_app_webview/in_app_webview_settings.dart';
import '../types/main.dart';
import '../types/disposable.dart';
class InAppWebViewWebElement {
class InAppWebViewWebElement implements Disposable {
late dynamic _viewId;
late BinaryMessenger _messenger;
late IFrameElement iframe;
@ -540,6 +541,7 @@ class InAppWebViewWebElement {
await _channel?.invokeMethod("onInjectedScriptError", [id]);
}
@override
void dispose() {
_channel?.setMethodCallHandler(null);
_channel = null;

View File

@ -4,8 +4,9 @@ import 'package:flutter/services.dart';
import 'dart:js' as js;
import 'web_platform_manager.dart';
import '../types/disposable.dart';
class PlatformUtil {
class PlatformUtil implements Disposable {
late BinaryMessenger _messenger;
late MethodChannel? _channel;
@ -41,6 +42,7 @@ class PlatformUtil {
return bridgeJsObject.callMethod("getCookieExpirationDate", [timestamp]);
}
@override
void dispose() {
_channel?.setMethodCallHandler(null);
_channel = null;

View File

@ -24,8 +24,11 @@ class FlutterInAppWebViewWebPlatform {
}
static void registerWith(Registrar registrar) {
// ignore: unused_local_variable
final pluginInstance = FlutterInAppWebViewWebPlatform(registrar);
// ignore: unused_local_variable
final platformUtil = PlatformUtil(messenger: registrar);
// ignore: unused_local_variable
final headlessManager = HeadlessInAppWebViewManager(messenger: registrar);
_nativeCommunication = allowInterop(_dartNativeCommunication);
}

View File

@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
import '../util.dart';
import '../debug_logging_settings.dart';
import '../types/main.dart';
import '../types/disposable.dart';
import 'web_authenticate_session_settings.dart';
@ -32,7 +33,7 @@ typedef WebAuthenticationSessionCompletionHandler = Future<void> Function(Uri? u
///
///**Supported Platforms/Implementations**:
///- iOS
class WebAuthenticationSession {
class WebAuthenticationSession implements Disposable {
///Debug settings.
static DebugLoggingSettings debugLoggingSettings = DebugLoggingSettings();
@ -175,6 +176,7 @@ class WebAuthenticationSession {
///
///**Supported Platforms/Implementations**:
///- iOS
@override
Future<void> dispose() async {
Map<String, dynamic> args = <String, dynamic>{};
await _channel.invokeMethod("dispose", args);