Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// SKProductsRequest+Rx.swift
//
// Created by Daniel Tartaglia on 8/28/19.
// Copyright © 2019 Daniel Tartaglia. MIT License.
//
import RxSwift
import RxCocoa
import StoreKit
extension Reactive where Base: SKProductsRequest {
var delegate: DelegateProxy<SKProductsRequest, SKProductsRequestDelegate> {
return SKProductsRequestDelegateProxy.proxy(for: base)
}
var didReceive: Observable<SKProductsResponse> {
return SKProductsRequestDelegateProxy.proxy(for: base).didReceiveResponse.asObservable()
}
var didFinish: Observable<Void> {
return delegate.methodInvoked(#selector(SKProductsRequestDelegate.requestDidFinish(_:)))
.map { _ in }
}
var didFail: Observable<Error> {
return delegate.methodInvoked(#selector(SKProductsRequestDelegate.request(_:didFailWithError:)))
.map { $0[1] as! Error }
}
}
final class SKProductsRequestDelegateProxy
: DelegateProxy<SKProductsRequest, SKProductsRequestDelegate>
, DelegateProxyType
, SKProductsRequestDelegate {
init(request: SKProductsRequest) {
super.init(parentObject: request, delegateProxy: SKProductsRequestDelegateProxy.self)
}
static func registerKnownImplementations() {
self.register { SKProductsRequestDelegateProxy(request: $0) }
}
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
didReceiveResponse.onNext(response)
}
deinit {
didReceiveResponse.onCompleted()
}
fileprivate let didReceiveResponse = PublishSubject<SKProductsResponse>()
}
extension SKProductsRequest: HasDelegate { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.