Last active
January 14, 2023 01:02
-
-
Save danielt1263/fbdaf6010a4ee9a025bf85327b8a821e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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