added FindSession to Android

This commit is contained in:
Lorenzo Pichilli 2022-10-12 10:13:05 +02:00
parent b790039696
commit b4558d356a
6 changed files with 103 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.pichillilorenzo.flutter_inappwebview.types.ChannelDelegateImpl; import com.pichillilorenzo.flutter_inappwebview.types.ChannelDelegateImpl;
import com.pichillilorenzo.flutter_inappwebview.types.FindSession;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -43,6 +44,13 @@ public class FindInteractionChannelDelegate extends ChannelDelegateImpl {
} }
result.success(true); result.success(true);
break; break;
case "getActiveFindSession":
if (findInteractionController != null && findInteractionController.activeFindSession != null) {
result.success(findInteractionController.activeFindSession.toMap());
} else {
result.success(null);
}
break;
default: default:
result.notImplemented(); result.notImplemented();
} }
@ -51,6 +59,11 @@ public class FindInteractionChannelDelegate extends ChannelDelegateImpl {
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) { public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
MethodChannel channel = getChannel(); MethodChannel channel = getChannel();
if (channel == null) return; if (channel == null) return;
if (isDoneCounting && findInteractionController != null && findInteractionController.webView != null) {
findInteractionController.activeFindSession = new FindSession(numberOfMatches, activeMatchOrdinal);
}
Map<String, Object> obj = new HashMap<>(); Map<String, Object> obj = new HashMap<>();
obj.put("activeMatchOrdinal", activeMatchOrdinal); obj.put("activeMatchOrdinal", activeMatchOrdinal);
obj.put("numberOfMatches", numberOfMatches); obj.put("numberOfMatches", numberOfMatches);

View File

@ -5,6 +5,7 @@ import androidx.annotation.Nullable;
import com.pichillilorenzo.flutter_inappwebview.InAppWebViewFlutterPlugin; import com.pichillilorenzo.flutter_inappwebview.InAppWebViewFlutterPlugin;
import com.pichillilorenzo.flutter_inappwebview.types.Disposable; import com.pichillilorenzo.flutter_inappwebview.types.Disposable;
import com.pichillilorenzo.flutter_inappwebview.types.FindSession;
import com.pichillilorenzo.flutter_inappwebview.webview.InAppWebViewInterface; import com.pichillilorenzo.flutter_inappwebview.webview.InAppWebViewInterface;
import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel;
@ -16,6 +17,8 @@ public class FindInteractionController implements Disposable {
@Nullable @Nullable
public InAppWebViewInterface webView; public InAppWebViewInterface webView;
@Nullable @Nullable
public FindSession activeFindSession;
@Nullable
public FindInteractionChannelDelegate channelDelegate; public FindInteractionChannelDelegate channelDelegate;
@Nullable @Nullable
public FindInteractionSettings settings; public FindInteractionSettings settings;
@ -38,5 +41,6 @@ public class FindInteractionController implements Disposable {
channelDelegate = null; channelDelegate = null;
} }
webView = null; webView = null;
activeFindSession = null;
} }
} }

View File

@ -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<String, Object> toMap() {
Map<String, Object> 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 +
'}';
}
}

View File

@ -50,9 +50,12 @@ void findInteractions() {
if ([TargetPlatform.iOS, TargetPlatform.macOS] if ([TargetPlatform.iOS, TargetPlatform.macOS]
.contains(defaultTargetPlatform)) { .contains(defaultTargetPlatform)) {
expect(await findInteractionController.getSearchText(), firstSearchText); expect(await findInteractionController.getSearchText(), firstSearchText);
}
if ([TargetPlatform.android].contains(defaultTargetPlatform)) {
await Future.delayed(Duration(seconds: 1));
}
final session = await findInteractionController.getActiveFindSession(); final session = await findInteractionController.getActiveFindSession();
expect(session!.resultCount, 2); expect(session!.resultCount, 2);
}
await expectLater( await expectLater(
findInteractionController.findNext(forward: true), completes); findInteractionController.findNext(forward: true), completes);
await expectLater( await expectLater(

View File

@ -3,12 +3,11 @@
export "FLUTTER_ROOT=/Users/lorenzopichilli/fvm/versions/2.10.4" export "FLUTTER_ROOT=/Users/lorenzopichilli/fvm/versions/2.10.4"
export "FLUTTER_APPLICATION_PATH=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example" export "FLUTTER_APPLICATION_PATH=/Users/lorenzopichilli/Desktop/flutter_inappwebview/example"
export "COCOAPODS_PARALLEL_CODE_SIGN=true" 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_DIR=build"
export "FLUTTER_BUILD_NAME=1.0.0" export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1" export "FLUTTER_BUILD_NUMBER=1"
export "DART_DEFINES=Zmx1dHRlci5pbnNwZWN0b3Iuc3RydWN0dXJlZEVycm9ycz10cnVl,RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ=="
export "DART_OBFUSCATION=false" export "DART_OBFUSCATION=false"
export "TRACK_WIDGET_CREATION=true" export "TRACK_WIDGET_CREATION=true"
export "TREE_SHAKE_ICONS=false" 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"

View File

@ -210,11 +210,12 @@ class FindInteractionController {
await _channel?.invokeMethod('dismissFindNavigator', args); 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**: ///**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)) ///- iOS ([Official API - UIFindInteraction.activeFindSession](https://developer.apple.com/documentation/uikit/uifindinteraction/3975825-activefindsession?changes=_7____4_8&language=objc))
Future<FindSession?> getActiveFindSession() async { Future<FindSession?> getActiveFindSession() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};