From b4558d356a6f80ace5babb144833a5738e37aa0e Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Wed, 12 Oct 2022 10:13:05 +0200 Subject: [PATCH] added FindSession to Android --- .../FindInteractionChannelDelegate.java | 13 ++++ .../FindInteractionController.java | 4 + .../types/FindSession.java | 76 +++++++++++++++++++ .../find_interactions.dart | 7 +- .../ios/Flutter/flutter_export_environment.sh | 5 +- .../find_interaction_controller.dart | 5 +- 6 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 android/src/main/java/com/pichillilorenzo/flutter_inappwebview/types/FindSession.java 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 ffda5420..359722af 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 @@ -4,6 +4,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.pichillilorenzo.flutter_inappwebview.types.ChannelDelegateImpl; +import com.pichillilorenzo.flutter_inappwebview.types.FindSession; import java.util.HashMap; import java.util.Map; @@ -43,6 +44,13 @@ public class FindInteractionChannelDelegate extends ChannelDelegateImpl { } result.success(true); break; + case "getActiveFindSession": + if (findInteractionController != null && findInteractionController.activeFindSession != null) { + result.success(findInteractionController.activeFindSession.toMap()); + } else { + result.success(null); + } + break; default: result.notImplemented(); } @@ -51,6 +59,11 @@ public class FindInteractionChannelDelegate extends ChannelDelegateImpl { public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) { MethodChannel channel = getChannel(); if (channel == null) return; + + if (isDoneCounting && findInteractionController != null && findInteractionController.webView != null) { + findInteractionController.activeFindSession = new FindSession(numberOfMatches, activeMatchOrdinal); + } + Map obj = new HashMap<>(); obj.put("activeMatchOrdinal", activeMatchOrdinal); obj.put("numberOfMatches", numberOfMatches); 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 4f7f8033..5e94a2a3 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 @@ -5,6 +5,7 @@ import androidx.annotation.Nullable; import com.pichillilorenzo.flutter_inappwebview.InAppWebViewFlutterPlugin; import com.pichillilorenzo.flutter_inappwebview.types.Disposable; +import com.pichillilorenzo.flutter_inappwebview.types.FindSession; import com.pichillilorenzo.flutter_inappwebview.webview.InAppWebViewInterface; import io.flutter.plugin.common.MethodChannel; @@ -16,6 +17,8 @@ public class FindInteractionController implements Disposable { @Nullable public InAppWebViewInterface webView; @Nullable + public FindSession activeFindSession; + @Nullable public FindInteractionChannelDelegate channelDelegate; @Nullable public FindInteractionSettings settings; @@ -38,5 +41,6 @@ public class FindInteractionController implements Disposable { channelDelegate = null; } webView = null; + activeFindSession = null; } } diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/types/FindSession.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/types/FindSession.java new file mode 100644 index 00000000..5e5ddea4 --- /dev/null +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/types/FindSession.java @@ -0,0 +1,76 @@ +package com.pichillilorenzo.flutter_inappwebview.types; + +import java.util.HashMap; +import java.util.Map; + +public class FindSession { + private int resultCount; + private int highlightedResultIndex; + private int searchResultDisplayStyle = 2; // matches NONE of iOS + + public FindSession(int resultCount, int highlightedResultIndex) { + this.resultCount = resultCount; + this.highlightedResultIndex = highlightedResultIndex; + } + + public Map toMap() { + Map obj = new HashMap<>(); + obj.put("resultCount", resultCount); + obj.put("highlightedResultIndex", highlightedResultIndex); + obj.put("searchResultDisplayStyle", searchResultDisplayStyle); + return obj; + } + + public int getResultCount() { + return resultCount; + } + + public void setResultCount(int resultCount) { + this.resultCount = resultCount; + } + + public int getHighlightedResultIndex() { + return highlightedResultIndex; + } + + public void setHighlightedResultIndex(int highlightedResultIndex) { + this.highlightedResultIndex = highlightedResultIndex; + } + + public int getSearchResultDisplayStyle() { + return searchResultDisplayStyle; + } + + public void setSearchResultDisplayStyle(int searchResultDisplayStyle) { + this.searchResultDisplayStyle = searchResultDisplayStyle; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + FindSession that = (FindSession) o; + + if (resultCount != that.resultCount) return false; + if (highlightedResultIndex != that.highlightedResultIndex) return false; + return searchResultDisplayStyle == that.searchResultDisplayStyle; + } + + @Override + public int hashCode() { + int result = resultCount; + result = 31 * result + highlightedResultIndex; + result = 31 * result + searchResultDisplayStyle; + return result; + } + + @Override + public String toString() { + return "FindSession{" + + "resultCount=" + resultCount + + ", highlightedResultIndex=" + highlightedResultIndex + + ", searchResultDisplayStyle=" + searchResultDisplayStyle + + '}'; + } +} diff --git a/example/integration_test/find_interaction_controller/find_interactions.dart b/example/integration_test/find_interaction_controller/find_interactions.dart index f51f9d10..d0f05f34 100644 --- a/example/integration_test/find_interaction_controller/find_interactions.dart +++ b/example/integration_test/find_interaction_controller/find_interactions.dart @@ -50,9 +50,12 @@ void findInteractions() { if ([TargetPlatform.iOS, TargetPlatform.macOS] .contains(defaultTargetPlatform)) { expect(await findInteractionController.getSearchText(), firstSearchText); - final session = await findInteractionController.getActiveFindSession(); - expect(session!.resultCount, 2); } + if ([TargetPlatform.android].contains(defaultTargetPlatform)) { + await Future.delayed(Duration(seconds: 1)); + } + final session = await findInteractionController.getActiveFindSession(); + expect(session!.resultCount, 2); await expectLater( findInteractionController.findNext(forward: true), completes); await expectLater( diff --git a/example/ios/Flutter/flutter_export_environment.sh b/example/ios/Flutter/flutter_export_environment.sh index 9e98dd5e..fae63896 100755 --- a/example/ios/Flutter/flutter_export_environment.sh +++ b/example/ios/Flutter/flutter_export_environment.sh @@ -3,12 +3,11 @@ 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=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example/lib/main.dart" +export "FLUTTER_TARGET=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=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example/.dart_tool/package_config.json" +export "PACKAGE_CONFIG=.dart_tool/package_config.json" diff --git a/lib/src/find_interaction/find_interaction_controller.dart b/lib/src/find_interaction/find_interaction_controller.dart index eda3b359..67ed1a6b 100644 --- a/lib/src/find_interaction/find_interaction_controller.dart +++ b/lib/src/find_interaction/find_interaction_controller.dart @@ -210,11 +210,12 @@ class FindInteractionController { await _channel?.invokeMethod('dismissFindNavigator', args); } - ///If there's a currently active find session (implying [isFindNavigatorVisible] is `true`), returns the active find session. + ///If there's a currently active find session (on iOS, implying [isFindNavigatorVisible] is `true`), returns the active find session. /// - ///**NOTE**: available only on iOS and only if [InAppWebViewSettings.isFindInteractionEnabled] is `true`. + ///**NOTE**: available on iOS only if [InAppWebViewSettings.isFindInteractionEnabled] is `true`. /// ///**Supported Platforms/Implementations**: + ///- Android native WebView ///- iOS ([Official API - UIFindInteraction.activeFindSession](https://developer.apple.com/documentation/uikit/uifindinteraction/3975825-activefindsession?changes=_7____4_8&language=objc)) Future getActiveFindSession() async { Map args = {};