diff --git a/ios/Sources/Capacitor/CAPPlugin+Bridge.swift b/ios/Sources/Capacitor/CAPPlugin+Bridge.swift index fca17588f..2cfdc62ad 100644 --- a/ios/Sources/Capacitor/CAPPlugin+Bridge.swift +++ b/ios/Sources/Capacitor/CAPPlugin+Bridge.swift @@ -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 { @@ -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) { @@ -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 */ diff --git a/ios/Tests/CapacitorTests/CAPPluginTests.swift b/ios/Tests/CapacitorTests/CAPPluginTests.swift index ac0a1ce00..d112606aa 100644 --- a/ios/Tests/CapacitorTests/CAPPluginTests.swift +++ b/ios/Tests/CapacitorTests/CAPPluginTests.swift @@ -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() @@ -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) + } }