Skip to content

Commit

Permalink
ReactiveX#1 Helper methods for UIApplication and UIViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
lsn21 committed Mar 15, 2019
1 parent 0bbfe59 commit 824ced7
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions RxCocoa/iOS/UIApplication+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,46 @@
return ControlEvent(events: source)
}
}


extension UIApplication: HasDelegate {
public typealias Delegate = UIApplicationDelegate
}

public class RxUIApplicationDelegateProxy: DelegateProxy<UIApplication, UIApplicationDelegate>, DelegateProxyType, UIApplicationDelegate {

public init(application: UIApplication) {
super.init(parentObject: application, delegateProxy: RxUIApplicationDelegateProxy.self)
}

public static func registerKnownImplementations() {
self.register { RxUIApplicationDelegateProxy(application: $0) }
}
}

extension Reactive where Base: UIApplication {
public var delegate: RxUIApplicationDelegateProxy {
return RxUIApplicationDelegateProxy.proxy(for: base)
}

public var didRegisterNotificationSettings: Observable<UIUserNotificationSettings> {
return delegate.methodInvoked(#selector(UIApplicationDelegate.application(_:didRegister:))).map { a in
return try castOrThrow(UIUserNotificationSettings.self, a[1])
}
}
}

private func castOptionalOrThrow<T>(_ resultType: T.Type, _ object: Any) throws -> T? {
if NSNull().isEqual(object) {
return nil
}

guard let returnValue = object as? T else {
throw RxCocoaError.castingError(object: object, targetType: resultType)
}

return returnValue
}

#endif

0 comments on commit 824ced7

Please sign in to comment.