From 17bdf843624ac359f23e6bca78930d1555205855 Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Wed, 12 Oct 2022 12:23:15 +0200 Subject: [PATCH] updated find interaction controller get and set search text implementation --- .../FindInteractionChannelDelegate.java | 27 +++++++++++---- .../FindInteractionController.java | 27 +++++++++++++++ .../ios/Flutter/flutter_export_environment.sh | 5 +-- .../FindInteractionChannelDelegate.swift | 22 +++--------- .../FindInteractionController.swift | 34 ++++++++++++++++++- .../find_interaction_controller.dart | 18 ++++++---- 6 files changed, 100 insertions(+), 33 deletions(-) diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/find_interaction/FindInteractionChannelDelegate.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/find_interaction/FindInteractionChannelDelegate.java index c2f76c7b..530ec760 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/find_interaction/FindInteractionChannelDelegate.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/find_interaction/FindInteractionChannelDelegate.java @@ -25,25 +25,40 @@ public class FindInteractionChannelDelegate extends ChannelDelegateImpl { public void onMethodCall(@NonNull MethodCall call, @NonNull final MethodChannel.Result result) { switch (call.method) { case "findAll": - if (findInteractionController != null && findInteractionController.webView != null) { + if (findInteractionController != null) { String find = (String) call.argument("find"); - findInteractionController.webView.findAllAsync(find); + findInteractionController.findAll(find); } result.success(true); break; case "findNext": - if (findInteractionController != null && findInteractionController.webView != null) { + if (findInteractionController != null) { Boolean forward = (Boolean) call.argument("forward"); - findInteractionController.webView.findNext(forward); + findInteractionController.findNext(forward); } result.success(true); break; case "clearMatches": - if (findInteractionController != null && findInteractionController.webView != null) { - findInteractionController.webView.clearMatches(); + if (findInteractionController != null) { + findInteractionController.clearMatches(); } result.success(true); break; + case "setSearchText": + if (findInteractionController != null) { + findInteractionController.searchText = (String) call.argument("searchText"); + result.success(true); + } else { + result.success(false); + } + break; + case "getSearchText": + if (findInteractionController != null) { + result.success(findInteractionController.searchText); + } else { + result.success(false); + } + break; case "getActiveFindSession": if (findInteractionController != null && findInteractionController.activeFindSession != null) { result.success(findInteractionController.activeFindSession.toMap()); diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/find_interaction/FindInteractionController.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/find_interaction/FindInteractionController.java index 5e94a2a3..2628ef31 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/find_interaction/FindInteractionController.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/find_interaction/FindInteractionController.java @@ -22,6 +22,8 @@ public class FindInteractionController implements Disposable { public FindInteractionChannelDelegate channelDelegate; @Nullable public FindInteractionSettings settings; + @Nullable + public String searchText; public FindInteractionController(@NonNull InAppWebViewInterface webView, @NonNull InAppWebViewFlutterPlugin plugin, @NonNull Object id, @Nullable FindInteractionSettings settings) { @@ -35,6 +37,30 @@ public class FindInteractionController implements Disposable { } + public void findAll(@Nullable String find) { + if (find == null) { + find = searchText; + } else { + // updated searchText + searchText = find; + } + if (webView != null && find != null) { + webView.findAllAsync(find); + } + } + + public void findNext(boolean forward) { + if (webView != null) { + webView.findNext(forward); + } + } + + public void clearMatches() { + if (webView != null) { + webView.clearMatches(); + } + } + public void dispose() { if (channelDelegate != null) { channelDelegate.dispose(); @@ -42,5 +68,6 @@ public class FindInteractionController implements Disposable { } webView = null; activeFindSession = null; + searchText = null; } } diff --git a/example/ios/Flutter/flutter_export_environment.sh b/example/ios/Flutter/flutter_export_environment.sh index fae63896..9e98dd5e 100755 --- a/example/ios/Flutter/flutter_export_environment.sh +++ b/example/ios/Flutter/flutter_export_environment.sh @@ -3,11 +3,12 @@ export "FLUTTER_ROOT=/Users/lorenzopichilli/fvm/versions/2.10.4" export "FLUTTER_APPLICATION_PATH=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example" export "COCOAPODS_PARALLEL_CODE_SIGN=true" -export "FLUTTER_TARGET=lib/main.dart" +export "FLUTTER_TARGET=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example/lib/main.dart" export "FLUTTER_BUILD_DIR=build" export "FLUTTER_BUILD_NAME=1.0.0" export "FLUTTER_BUILD_NUMBER=1" +export "DART_DEFINES=Zmx1dHRlci5pbnNwZWN0b3Iuc3RydWN0dXJlZEVycm9ycz10cnVl,RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ==" export "DART_OBFUSCATION=false" export "TRACK_WIDGET_CREATION=true" export "TREE_SHAKE_ICONS=false" -export "PACKAGE_CONFIG=.dart_tool/package_config.json" +export "PACKAGE_CONFIG=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example/.dart_tool/package_config.json" diff --git a/ios/Classes/FindInteraction/FindInteractionChannelDelegate.swift b/ios/Classes/FindInteraction/FindInteractionChannelDelegate.swift index fb195062..c0bd9383 100644 --- a/ios/Classes/FindInteraction/FindInteractionChannelDelegate.swift +++ b/ios/Classes/FindInteraction/FindInteractionChannelDelegate.swift @@ -61,28 +61,16 @@ public class FindInteractionChannelDelegate : ChannelDelegate { } break case "setSearchText": - if #available(iOS 16.0, *) { - if let interaction = findInteractionController?.webView?.findInteraction { - let searchText = arguments!["searchText"] as? String - interaction.searchText = searchText - result(true) - } else { - result(false) - } + if let findInteractionController = findInteractionController { + let searchText = arguments!["searchText"] as? String + findInteractionController.searchText = searchText + result(true) } else { result(false) } break case "getSearchText": - if #available(iOS 16.0, *) { - if let interaction = findInteractionController?.webView?.findInteraction { - result(interaction.searchText) - } else { - result(nil) - } - } else { - result(nil) - } + result(findInteractionController?.searchText) break case "isFindNavigatorVisible": if #available(iOS 16.0, *) { diff --git a/ios/Classes/FindInteraction/FindInteractionController.swift b/ios/Classes/FindInteraction/FindInteractionController.swift index 73be4d69..d23b78ee 100644 --- a/ios/Classes/FindInteraction/FindInteractionController.swift +++ b/ios/Classes/FindInteraction/FindInteractionController.swift @@ -16,6 +16,22 @@ public class FindInteractionController : NSObject, Disposable { var settings: FindInteractionSettings? var shouldCallOnRefresh = false + private var _searchText: String? = nil + var searchText: String? { + get { + if #available(iOS 16.0, *), let interaction = webView?.findInteraction { + return interaction.searchText + } + return _searchText + } + set { + if #available(iOS 16.0, *), let interaction = webView?.findInteraction { + interaction.searchText = newValue + } + self._searchText = newValue + } + } + private var _activeFindSession: FindSession? = nil var activeFindSession: FindSession? { get { @@ -54,6 +70,22 @@ public class FindInteractionController : NSObject, Disposable { } return } + + var find = find + if find == nil { + find = searchText + } else { + // updated searchText + searchText = find + } + + guard let find else { + if let completionHandler = completionHandler { + completionHandler(nil, nil) + } + return + } + if #available(iOS 16.0, *), webView.isFindInteractionEnabled { if let interaction = webView.findInteraction { interaction.searchText = find @@ -63,7 +95,7 @@ public class FindInteractionController : NSObject, Disposable { completionHandler(nil, nil) } } else { - let startSearch = "window.\(JAVASCRIPT_BRIDGE_NAME)._findAllAsync('\(find ?? "")');" + let startSearch = "window.\(JAVASCRIPT_BRIDGE_NAME)._findAllAsync('\(find)');" webView.evaluateJavaScript(startSearch, completionHandler: completionHandler) } } diff --git a/lib/src/find_interaction/find_interaction_controller.dart b/lib/src/find_interaction/find_interaction_controller.dart index b22ca35f..b7f79ffe 100644 --- a/lib/src/find_interaction/find_interaction_controller.dart +++ b/lib/src/find_interaction/find_interaction_controller.dart @@ -106,7 +106,7 @@ class FindInteractionController { ///**Supported Platforms/Implementations**: ///- Android native WebView ([Official API - WebView.findAllAsync](https://developer.android.com/reference/android/webkit/WebView#findAllAsync(java.lang.String))) ///- iOS (if [InAppWebViewSettings.isFindInteractionEnabled] is `true`: [Official API - UIFindInteraction.presentFindNavigator](https://developer.apple.com/documentation/uikit/uifindinteraction/3975832-presentfindnavigator?changes=_2) with [Official API - UIFindInteraction.searchText](https://developer.apple.com/documentation/uikit/uifindinteraction/3975834-searchtext?changes=_2)) - Future findAll({required String find}) async { + Future findAll({String? find}) async { Map args = {}; args.putIfAbsent('find', () => find); await _channel?.invokeMethod('findAll', args); @@ -114,7 +114,7 @@ class FindInteractionController { ///Highlights and scrolls to the next match found by [findAll]. Notifies [FindInteractionController.onFindResultReceived] listener. /// - ///[forward] represents the direction to search. + ///[forward] represents the direction to search. The default value is `true`, meaning forward. /// ///**NOTE**: on iOS, if [InAppWebViewSettings.isFindInteractionEnabled] is `true`, ///it uses the built-in find interaction native UI, @@ -123,7 +123,7 @@ class FindInteractionController { ///**Supported Platforms/Implementations**: ///- Android native WebView ([Official API - WebView.findNext](https://developer.android.com/reference/android/webkit/WebView#findNext(boolean))) ///- iOS (if [InAppWebViewSettings.isFindInteractionEnabled] is `true`: [Official API - UIFindInteraction.findNext](https://developer.apple.com/documentation/uikit/uifindinteraction/3975829-findnext?changes=_2) and ([Official API - UIFindInteraction.findPrevious](https://developer.apple.com/documentation/uikit/uifindinteraction/3975830-findprevious?changes=_2))) - Future findNext({required bool forward}) async { + Future findNext({bool forward = true}) async { Map args = {}; args.putIfAbsent('forward', () => forward); await _channel?.invokeMethod('findNext', args); @@ -143,11 +143,13 @@ class FindInteractionController { await _channel?.invokeMethod('clearMatches', args); } - ///Pre-populate the system find panel's search text field with a search query. + ///Pre-populate the search text to be used. /// - ///**NOTE**: available only on iOS and only if [InAppWebViewSettings.isFindInteractionEnabled] is `true`. + ///On iOS, if [InAppWebViewSettings.isFindInteractionEnabled] is `true, + ///it will pre-populate the system find panel's search text field with a search query. /// ///**Supported Platforms/Implementations**: + ///- Android native WebView ///- iOS ([Official API - UIFindInteraction.searchText](https://developer.apple.com/documentation/uikit/uifindinteraction/3975834-searchtext?changes=_2)) Future setSearchText(String? searchText) async { Map args = {}; @@ -155,11 +157,13 @@ class FindInteractionController { await _channel?.invokeMethod('setSearchText', args); } - ///Get the system find panel's search text field value. + ///Get the search text used. /// - ///**NOTE**: available only on iOS and only if [InAppWebViewSettings.isFindInteractionEnabled] is `true`. + ///On iOS, if [InAppWebViewSettings.isFindInteractionEnabled] is `true, + ///it will get the system find panel's search text field value. /// ///**Supported Platforms/Implementations**: + ///- Android native WebView ///- iOS ([Official API - UIFindInteraction.searchText](https://developer.apple.com/documentation/uikit/uifindinteraction/3975834-searchtext?changes=_2)) Future getSearchText() async { Map args = {};