Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions ios/Sources/Capacitor/CAPPlugin+Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,31 @@ import WebKit
return bridge?.config.getPluginConfig(pluginName) ?? PluginConfig(config: JSObject())
}

func notifyListeners(_ eventName: String, data: [String: Any]?) {
/**
* Configure popover sourceRect, sourceView and permittedArrowDirections to show it centered
*/
func setCenteredPopover(_ viewController: UIViewController) {
guard let hostView = bridge?.viewController?.view else { return }
viewController.popoverPresentationController?.sourceRect = CGRect(x: hostView.center.x, y: hostView.center.y, width: 0, height: 0)
viewController.popoverPresentationController?.sourceView = hostView
viewController.popoverPresentationController?.permittedArrowDirections = []
}

func setCenteredPopover(_ viewController: UIViewController, size: CGSize) {
guard let hostView = bridge?.viewController?.view else { return }
viewController.popoverPresentationController?.sourceRect = CGRect(x: hostView.center.x, y: hostView.center.y, width: 0, height: 0)
viewController.preferredContentSize = size
viewController.popoverPresentationController?.sourceView = hostView
viewController.popoverPresentationController?.permittedArrowDirections = []
}
}

@objc extension CAPPlugin {
open func notifyListeners(_ eventName: String, data: [String: Any]?) {
notifyListeners(eventName, data: data, retainUntilConsumed: false)
}

func notifyListeners(_ eventName: String, data: [String: Any]?, retainUntilConsumed retain: Bool) {
open func notifyListeners(_ eventName: String, data: [String: Any]?, retainUntilConsumed retain: Bool) {
guard let listenersForEvent = eventListeners?.object(forKey: eventName) as? NSArray,
listenersForEvent.count > 0 else {
if retain, let data = data {
Expand All @@ -61,7 +81,7 @@ import WebKit
}
}

func removeListener(_ call: CAPPluginCall) {
open func removeListener(_ call: CAPPluginCall) {
guard let eventName = call.options["eventName"] as? String,
let callbackId = call.options["callbackId"] as? String else { return }
if let storedCall = bridge?.savedCall(withID: callbackId) {
Expand All @@ -70,31 +90,11 @@ import WebKit
bridge?.releaseCall(withID: callbackId)
}

func removeAllListeners(_ call: CAPPluginCall) {
open func removeAllListeners(_ call: CAPPluginCall) {
eventListeners?.removeAllObjects()
call.resolve()
}

/**
* Configure popover sourceRect, sourceView and permittedArrowDirections to show it centered
*/
func setCenteredPopover(_ viewController: UIViewController) {
guard let hostView = bridge?.viewController?.view else { return }
viewController.popoverPresentationController?.sourceRect = CGRect(x: hostView.center.x, y: hostView.center.y, width: 0, height: 0)
viewController.popoverPresentationController?.sourceView = hostView
viewController.popoverPresentationController?.permittedArrowDirections = []
}

func setCenteredPopover(_ viewController: UIViewController, size: CGSize) {
guard let hostView = bridge?.viewController?.view else { return }
viewController.popoverPresentationController?.sourceRect = CGRect(x: hostView.center.x, y: hostView.center.y, width: 0, height: 0)
viewController.preferredContentSize = size
viewController.popoverPresentationController?.sourceView = hostView
viewController.popoverPresentationController?.permittedArrowDirections = []
}
}

@objc extension CAPPlugin {
/**
* Default implementation of the capacitor 3.0 permission pattern
*/
Expand Down
63 changes: 63 additions & 0 deletions ios/Tests/CapacitorTests/CAPPluginTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ struct CAPPluginTests {
return (call, recorder)
}

private final class OverridingPlugin: CAPPlugin {
private(set) var notifiedEvents: [String] = []
private(set) var removeListenerCount = 0
private(set) var removeAllListenersCount = 0

override func notifyListeners(_ eventName: String, data: [String: Any]?, retainUntilConsumed retain: Bool) {
notifiedEvents.append(eventName)
super.notifyListeners(eventName, data: data, retainUntilConsumed: retain)
}

override func removeListener(_ call: CAPPluginCall) {
removeListenerCount += 1
super.removeListener(call)
}

override func removeAllListeners(_ call: CAPPluginCall) {
removeAllListenersCount += 1
super.removeAllListeners(call)
}
}

@Test func notifyListenersDeliversToActiveListener() {
let plugin = CAPPlugin()
let (call, recorder) = Self.makeCall()
Expand Down Expand Up @@ -88,4 +109,46 @@ struct CAPPluginTests {
#expect(secondRecorder.received.count == 1)
#expect(secondRecorder.received.first?["value"] as? String == "hello")
}

@Test func notifyListenersOverrideReceivesConvenienceCall() {
let plugin = OverridingPlugin()
let (call, recorder) = Self.makeCall()
plugin.addEventListener("myEvent", listener: call)

plugin.notifyListeners("myEvent", data: ["value": "hello"])

#expect(plugin.notifiedEvents == ["myEvent"])
#expect(recorder.received.count == 1)
}

@Test func notifyListenersOverrideReceivesRetainedReplay() {
let plugin = OverridingPlugin()
plugin.notifyListeners("myEvent", data: ["value": "hello"], retainUntilConsumed: true)

let (call, recorder) = Self.makeCall()
plugin.addEventListener("myEvent", listener: call)

#expect(plugin.notifiedEvents == ["myEvent", "myEvent"])
#expect(recorder.received.count == 1)
}

@Test func removeListenerOverrideReceivesSelectorDispatch() {
let plugin = OverridingPlugin()
let (call, _) = Self.makeCall()

plugin.perform(NSSelectorFromString("removeListener:"), with: call)

#expect(plugin.removeListenerCount == 1)
}

@Test func removeAllListenersOverrideReceivesSelectorDispatch() {
let plugin = OverridingPlugin()
let (call, _) = Self.makeCall()
plugin.addEventListener("myEvent", listener: call)

plugin.perform(NSSelectorFromString("removeAllListeners:"), with: call)

#expect(plugin.removeAllListenersCount == 1)
#expect(plugin.hasListeners("myEvent") == false)
}
}
Loading