Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Responds to selector when services has Implement delegate method #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output
.DS_Store
6 changes: 4 additions & 2 deletions Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ class AppDelegate: PluggableApplicationDelegate {
LoggerApplicationService()
]
}
}

override init() {
super.init()
extension AppDelegate {
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
26 changes: 20 additions & 6 deletions Sources/AppServicesManager+UNUserNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,30 @@ extension PluggableApplicationDelegate: UNUserNotificationCenterDelegate {

@available(iOS 10.0, *)
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
for service in _services {
service.userNotificationCenter?(center, willPresent: notification, withCompletionHandler: completionHandler)
}
apply({ (service, completion) -> Void? in
service.userNotificationCenter?(
center,
willPresent: notification,
withCompletionHandler: { opt in
completion(opt)
})
}, completionHandler: { options in
completionHandler(UNNotificationPresentationOptions(options))
})
}

@available(iOS 10.0, *)
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
for service in _services {
service.userNotificationCenter?(center, didReceive: response, withCompletionHandler: completionHandler)
}
apply({ (service, completion) -> Void? in
service.userNotificationCenter?(
center,
didReceive: response,
withCompletionHandler: {
completion(())
})
}, completionHandler: { _ in
completionHandler()
})
}


Expand Down
32 changes: 32 additions & 0 deletions Sources/AppServicesManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ open class PluggableApplicationDelegate: UIResponder, UIApplicationDelegate {
lazy var _services: [ApplicationService] = {
return self.services
}()

lazy var _methodRespondsCache: [String: Bool] = {
print(NSDate())
func generateProtocolMethods(_ protocol: Protocol, methodPrifix: String) -> [String] {
var count: UInt32 = 0
guard let list = protocol_copyMethodDescriptionList(`protocol`, false, true, &count) else { return [] }
return (0..<count)
.map({ list[Int($0)] })
.compactMap({ $0.name })
.map({ NSStringFromSelector($0) })
.filter({ $0.hasPrefix(methodPrifix) })
}
var methods = generateProtocolMethods(UIApplicationDelegate.self, methodPrifix: "application")
if #available(iOS 10.0, *) {
methods.append(contentsOf: generateProtocolMethods(UNUserNotificationCenterDelegate.self, methodPrifix: "userNotificationCenter"))
}
let result = methods.reduce([String: Bool]()) { (res, selector) in
var res = res
res[selector] = _services.contains(where: { $0.responds(to: Selector.init(selector)) })
return res
}
print(NSDate())
return result
}()

@discardableResult
internal func apply<T, S>(_ work: (ApplicationService, @escaping (T) -> Void) -> S?, completionHandler: @escaping ([T]) -> Void) -> [S] {
Expand All @@ -55,4 +79,12 @@ open class PluggableApplicationDelegate: UIResponder, UIApplicationDelegate {

return returns
}

override open func responds(to aSelector: Selector!) -> Bool {
if let isHaveServicesResponds = _methodRespondsCache[NSStringFromSelector(aSelector)] {
return isHaveServicesResponds
} else {
return super.responds(to: aSelector)
}
}
}