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"); await controller.findAll(find: "InAppWebViewInitialFileTest");
final int numberOfMatches = await numberOfMatchesCompleter.future; final int numberOfMatches = await numberOfMatchesCompleter.future;
expect(numberOfMatches, 2); expect(numberOfMatches, 2);
final session = await findInteractionController.getActiveFindSession();
expect(session!.resultCount, 2);
}, skip: shouldSkip); }, skip: shouldSkip);
} }

View File

@ -132,12 +132,8 @@ public class FindInteractionChannelDelegate : ChannelDelegate {
} }
break break
case "getActiveFindSession": case "getActiveFindSession":
if #available(iOS 16.0, *) { if let findInteractionController = findInteractionController {
if let interaction = findInteractionController?.webView?.findInteraction { result(findInteractionController.activeFindSession?.toMap())
result(interaction.activeFindSession?.toMap())
} else {
result(nil)
}
} else { } else {
result(nil) result(nil)
} }
@ -149,6 +145,12 @@ public class FindInteractionChannelDelegate : ChannelDelegate {
} }
public func onFindResultReceived(activeMatchOrdinal: Int, numberOfMatches: Int, isDoneCounting: Bool) { 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?] = [ let arguments: [String : Any?] = [
"activeMatchOrdinal": activeMatchOrdinal, "activeMatchOrdinal": activeMatchOrdinal,
"numberOfMatches": numberOfMatches, "numberOfMatches": numberOfMatches,

View File

@ -16,6 +16,22 @@ public class FindInteractionController : NSObject, Disposable {
var settings: FindInteractionSettings? var settings: FindInteractionSettings?
var shouldCallOnRefresh = false 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?) { public init(registrar: FlutterPluginRegistrar, id: Any, webView: InAppWebView, settings: FindInteractionSettings?) {
super.init() super.init()
self.webView = webView self.webView = webView
@ -99,6 +115,7 @@ public class FindInteractionController : NSObject, Disposable {
channelDelegate?.dispose() channelDelegate?.dispose()
channelDelegate = nil channelDelegate = nil
webView = nil webView = nil
activeFindSession = nil
} }
deinit { deinit {

View File

@ -7,6 +7,33 @@
import Foundation 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, *) @available(iOS 16.0, *)
extension UIFindSession { extension UIFindSession {
public func toMap () -> [String:Any?] { public func toMap () -> [String:Any?] {

View File

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