From ad1fce2aa5e4b915855704f0bea7b76d35914ce9 Mon Sep 17 00:00:00 2001 From: reasje Date: Tue, 19 Sep 2023 20:46:24 +0330 Subject: [PATCH] fix: Handled clipboard bug in special cases --- .../subfeatures/open_dapp/open_dapp_page.dart | 12 ++++----- .../open_dapp/open_dapp_presenter.dart | 25 +++++++++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/features/dapps/subfeatures/open_dapp/open_dapp_page.dart b/lib/features/dapps/subfeatures/open_dapp/open_dapp_page.dart index 5db5de7a..7b6b5069 100644 --- a/lib/features/dapps/subfeatures/open_dapp/open_dapp_page.dart +++ b/lib/features/dapps/subfeatures/open_dapp/open_dapp_page.dart @@ -58,6 +58,9 @@ class OpenAppPage extends HookConsumerWidget { initialUrlRequest: URLRequest( url: Uri.parse(url), ), + onLoadStop: (controller, url) { + presenter.injectCopyHandling(); + }, onLoadError: (controller, url, code, message) => collectLog('onLoadError: $code: $message'), onLoadHttpError: (controller, url, statusCode, description) => @@ -101,12 +104,10 @@ class OpenAppPage extends HookConsumerWidget { break; } }, - initialOptions: InAppWebViewGroupOptions( crossPlatform: InAppWebViewOptions( - useShouldOverrideUrlLoading: true, - mediaPlaybackRequiresUserGesture: false - ), + useShouldOverrideUrlLoading: true, + mediaPlaybackRequiresUserGesture: false), android: AndroidInAppWebViewOptions( useWideViewPort: true, geolocationEnabled: true, @@ -128,8 +129,7 @@ class OpenAppPage extends HookConsumerWidget { action: PermissionRequestResponseAction.GRANT, ); }, - androidOnGeolocationPermissionsHidePrompt: (controller) { - }, + androidOnGeolocationPermissionsHidePrompt: (controller) {}, androidOnGeolocationPermissionsShowPrompt: (InAppWebViewController controller, String origin) async { return GeolocationPermissionShowPromptResponse( diff --git a/lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart b/lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart index 6db8718c..b0b47de8 100644 --- a/lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart +++ b/lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart @@ -47,8 +47,9 @@ class OpenDAppPresenter extends CompletePresenter { return super.dispose(); } - void onWebViewCreated(InAppWebViewController controller) => - notify(() => state.webviewController = controller); + void onWebViewCreated(InAppWebViewController controller) { + notify(() => state.webviewController = controller); + } Future _estimatedFee( String from, @@ -219,4 +220,24 @@ class OpenDAppPresenter extends CompletePresenter { } }"""; } + + void copy(List params) { + Clipboard.setData(ClipboardData(text: params[0])); + } + + Future paste(List params) async { + return (await Clipboard.getData('text/plain'))?.text.toString() ?? ''; + } + + void injectCopyHandling() { + state.webviewController!.evaluateJavascript( + source: + 'javascript:navigator.clipboard.writeText = (msg) => { return window.flutter_inappwebview?.callHandler("axs-wallet-copy-clipboard", msg); }'); + state.webviewController!.addJavaScriptHandler( + handlerName: 'axs-wallet-copy-clipboard', + callback: (args) { + copy(args); + }, + ); + } }