implemented iOS getActiveFindSession also when isFindInteractionEnabled is false

This commit is contained in:
Lorenzo Pichilli 2022-10-12 10:59:17 +02:00
parent e9b059b2e2
commit 34d776e079
5 changed files with 55 additions and 9 deletions

View File

@ -120,5 +120,7 @@ void findInteractions() {
await controller.findAll(find: "InAppWebViewInitialFileTest");
final int numberOfMatches = await numberOfMatchesCompleter.future;
expect(numberOfMatches, 2);
final session = await findInteractionController.getActiveFindSession();
expect(session!.resultCount, 2);
}, skip: shouldSkip);
}

View File

@ -132,12 +132,8 @@ public class FindInteractionChannelDelegate : ChannelDelegate {
}
break
case "getActiveFindSession":
if #available(iOS 16.0, *) {
if let interaction = findInteractionController?.webView?.findInteraction {
result(interaction.activeFindSession?.toMap())
} else {
result(nil)
}
if let findInteractionController = findInteractionController {
result(findInteractionController.activeFindSession?.toMap())
} else {
result(nil)
}
@ -149,6 +145,12 @@ public class FindInteractionChannelDelegate : ChannelDelegate {
}
public func onFindResultReceived(activeMatchOrdinal: Int, numberOfMatches: Int, isDoneCounting: Bool) {
if isDoneCounting, let findInteractionController = findInteractionController {
findInteractionController.activeFindSession = FindSession(resultCount: activeMatchOrdinal,
highlightedResultIndex: numberOfMatches,
searchResultDisplayStyle: 2) // matches UIFindSession.SearchResultDisplayStyle.none
}
let arguments: [String : Any?] = [
"activeMatchOrdinal": activeMatchOrdinal,
"numberOfMatches": numberOfMatches,

View File

@ -16,6 +16,22 @@ public class FindInteractionController : NSObject, Disposable {
var settings: FindInteractionSettings?
var shouldCallOnRefresh = false
private var _activeFindSession: FindSession? = nil
var activeFindSession: FindSession? {
get {
if #available(iOS 16.0, *), let interaction = webView?.findInteraction {
if let activeFindSession = interaction.activeFindSession {
return FindSession.fromUIFindSession(uiFindSession: activeFindSession)
}
return nil
}
return _activeFindSession
}
set {
self._activeFindSession = newValue
}
}
public init(registrar: FlutterPluginRegistrar, id: Any, webView: InAppWebView, settings: FindInteractionSettings?) {
super.init()
self.webView = webView
@ -99,6 +115,7 @@ public class FindInteractionController : NSObject, Disposable {
channelDelegate?.dispose()
channelDelegate = nil
webView = nil
activeFindSession = nil
}
deinit {

View File

@ -7,6 +7,33 @@
import Foundation
public class FindSession: NSObject {
var resultCount: Int
var highlightedResultIndex: Int
var searchResultDisplayStyle: Int
public init(resultCount: Int, highlightedResultIndex: Int, searchResultDisplayStyle: Int) {
self.resultCount = resultCount
self.highlightedResultIndex = highlightedResultIndex
self.searchResultDisplayStyle = searchResultDisplayStyle
}
@available(iOS 16.0, *)
public static func fromUIFindSession(uiFindSession: UIFindSession) -> FindSession {
return FindSession(resultCount: uiFindSession.resultCount,
highlightedResultIndex: uiFindSession.highlightedResultIndex,
searchResultDisplayStyle: uiFindSession.searchResultDisplayStyle.rawValue)
}
public func toMap () -> [String:Any?] {
return [
"resultCount": resultCount,
"highlightedResultIndex": highlightedResultIndex,
"searchResultDisplayStyle": searchResultDisplayStyle
]
}
}
@available(iOS 16.0, *)
extension UIFindSession {
public func toMap () -> [String:Any?] {

View File

@ -210,9 +210,7 @@ class FindInteractionController {
await _channel?.invokeMethod('dismissFindNavigator', args);
}
///If there's a currently active find session (on iOS, implying [isFindNavigatorVisible] is `true`), returns the active find session.
///
///**NOTE**: available on iOS only if [InAppWebViewSettings.isFindInteractionEnabled] is `true`.
///If there's a currently active find session, returns the active find session.
///
///**Supported Platforms/Implementations**:
///- Android native WebView