updated find interaction controller get and set search text implementation

This commit is contained in:
Lorenzo Pichilli 2022-10-12 12:23:15 +02:00
parent 34d776e079
commit 17bdf84362
6 changed files with 100 additions and 33 deletions

View File

@ -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());

View File

@ -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;
}
}

View File

@ -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"

View File

@ -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, *) {

View File

@ -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)
}
}

View File

@ -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<void> findAll({required String find}) async {
Future<void> findAll({String? find}) async {
Map<String, dynamic> args = <String, dynamic>{};
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<void> findNext({required bool forward}) async {
Future<void> findNext({bool forward = true}) async {
Map<String, dynamic> args = <String, dynamic>{};
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<void> setSearchText(String? searchText) async {
Map<String, dynamic> args = <String, dynamic>{};
@ -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<String?> getSearchText() async {
Map<String, dynamic> args = <String, dynamic>{};