Add PromiseKit dependency
- Added PromiseKit dependency
This commit is contained in:
1
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/PMKStoreKit.h
vendored
Normal file
1
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/PMKStoreKit.h
vendored
Normal file
@@ -0,0 +1 @@
|
||||
#import "SKRequest+AnyPromise.h"
|
45
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/SKPayment+Promise.swift
vendored
Normal file
45
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/SKPayment+Promise.swift
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
#if !PMKCocoaPods
|
||||
import PromiseKit
|
||||
#endif
|
||||
import StoreKit
|
||||
|
||||
extension SKPayment {
|
||||
public func promise() -> Promise<SKPaymentTransaction> {
|
||||
return PaymentObserver(payment: self).promise
|
||||
}
|
||||
}
|
||||
|
||||
private class PaymentObserver: NSObject, SKPaymentTransactionObserver {
|
||||
let (promise, seal) = Promise<SKPaymentTransaction>.pending()
|
||||
let payment: SKPayment
|
||||
var retainCycle: PaymentObserver?
|
||||
|
||||
init(payment: SKPayment) {
|
||||
self.payment = payment
|
||||
super.init()
|
||||
SKPaymentQueue.default().add(self)
|
||||
SKPaymentQueue.default().add(payment)
|
||||
retainCycle = self
|
||||
}
|
||||
|
||||
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
|
||||
guard let transaction = transactions.first(where: { $0.payment == payment }) else {
|
||||
return
|
||||
}
|
||||
switch transaction.transactionState {
|
||||
case .purchased, .restored:
|
||||
queue.finishTransaction(transaction)
|
||||
seal.fulfill(transaction)
|
||||
queue.remove(self)
|
||||
retainCycle = nil
|
||||
case .failed:
|
||||
let error = transaction.error ?? PMKError.cancelled
|
||||
queue.finishTransaction(transaction)
|
||||
seal.reject(error)
|
||||
queue.remove(self)
|
||||
retainCycle = nil
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
55
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/SKPaymentQueue+Promise.swift
vendored
Normal file
55
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/SKPaymentQueue+Promise.swift
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
#if !PMKCocoaPods
|
||||
import PromiseKit
|
||||
#endif
|
||||
import StoreKit
|
||||
|
||||
public extension SKPaymentQueue {
|
||||
func restoreCompletedTransactions(_: PMKNamespacer) -> Promise<[SKPaymentTransaction]> {
|
||||
return PaymentObserver(self).promise
|
||||
}
|
||||
|
||||
func restoreCompletedTransactions(_: PMKNamespacer, withApplicationUsername username: String?) -> Promise<[SKPaymentTransaction]> {
|
||||
return PaymentObserver(self, withApplicationUsername: true, userName: username).promise
|
||||
}
|
||||
}
|
||||
|
||||
private class PaymentObserver: NSObject, SKPaymentTransactionObserver {
|
||||
let (promise, seal) = Promise<[SKPaymentTransaction]>.pending()
|
||||
var retainCycle: PaymentObserver?
|
||||
var finishedTransactions = [SKPaymentTransaction]()
|
||||
|
||||
//TODO:PMK7: this is weird, just have a `String?` parameter
|
||||
init(_ paymentQueue: SKPaymentQueue, withApplicationUsername: Bool = false, userName: String? = nil) {
|
||||
super.init()
|
||||
paymentQueue.add(self)
|
||||
withApplicationUsername ?
|
||||
paymentQueue.restoreCompletedTransactions() :
|
||||
paymentQueue.restoreCompletedTransactions(withApplicationUsername: userName)
|
||||
retainCycle = self
|
||||
}
|
||||
|
||||
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
|
||||
for transaction in transactions where transaction.transactionState == .restored {
|
||||
finishedTransactions.append(transaction)
|
||||
queue.finishTransaction(transaction)
|
||||
}
|
||||
}
|
||||
|
||||
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
|
||||
resolve(queue, nil)
|
||||
}
|
||||
|
||||
func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
|
||||
resolve(queue, error)
|
||||
}
|
||||
|
||||
func resolve(_ queue: SKPaymentQueue, _ error: Error?) {
|
||||
if let error = error {
|
||||
seal.reject(error)
|
||||
} else {
|
||||
seal.fulfill(finishedTransactions)
|
||||
}
|
||||
queue.remove(self)
|
||||
retainCycle = nil
|
||||
}
|
||||
}
|
52
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/SKProductsRequest+Promise.swift
vendored
Normal file
52
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/SKProductsRequest+Promise.swift
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
import StoreKit
|
||||
#if !PMKCocoaPods
|
||||
import PromiseKit
|
||||
#endif
|
||||
|
||||
/**
|
||||
To import the `SKRequest` category:
|
||||
|
||||
use_frameworks!
|
||||
pod "PromiseKit/StoreKit"
|
||||
|
||||
And then in your sources:
|
||||
|
||||
import PromiseKit
|
||||
*/
|
||||
extension SKProductsRequest {
|
||||
/**
|
||||
Sends the request to the Apple App Store.
|
||||
|
||||
- Returns: A promise that fulfills if the request succeeds.
|
||||
*/
|
||||
public func start(_: PMKNamespacer) -> Promise<SKProductsResponse> {
|
||||
let proxy = SKDelegate()
|
||||
delegate = proxy
|
||||
proxy.retainCycle = proxy
|
||||
start()
|
||||
return proxy.promise
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fileprivate class SKDelegate: NSObject, SKProductsRequestDelegate {
|
||||
let (promise, seal) = Promise<SKProductsResponse>.pending()
|
||||
var retainCycle: SKDelegate?
|
||||
|
||||
@objc fileprivate func request(_ request: SKRequest, didFailWithError error: Error) {
|
||||
seal.reject(error)
|
||||
retainCycle = nil
|
||||
}
|
||||
|
||||
@objc fileprivate func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
|
||||
seal.fulfill(response)
|
||||
retainCycle = nil
|
||||
}
|
||||
}
|
||||
|
||||
// perhaps one day Apple will actually make their errors into Errors…
|
||||
//extension SKError: CancellableError {
|
||||
// public var isCancelled: Bool {
|
||||
// return true
|
||||
// }
|
||||
//}
|
35
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/SKReceiptRefreshRequest+Promise.swift
vendored
Normal file
35
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/SKReceiptRefreshRequest+Promise.swift
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
#if !PMKCocoaPods
|
||||
import PromiseKit
|
||||
#endif
|
||||
import StoreKit
|
||||
|
||||
extension SKReceiptRefreshRequest {
|
||||
public func promise() -> Promise<SKReceiptRefreshRequest> {
|
||||
return ReceiptRefreshObserver(request: self).promise
|
||||
}
|
||||
}
|
||||
|
||||
private class ReceiptRefreshObserver: NSObject, SKRequestDelegate {
|
||||
let (promise, seal) = Promise<SKReceiptRefreshRequest>.pending()
|
||||
let request: SKReceiptRefreshRequest
|
||||
var retainCycle: ReceiptRefreshObserver?
|
||||
|
||||
init(request: SKReceiptRefreshRequest) {
|
||||
self.request = request
|
||||
super.init()
|
||||
request.delegate = self
|
||||
request.start()
|
||||
retainCycle = self
|
||||
}
|
||||
|
||||
|
||||
func requestDidFinish(_: SKRequest) {
|
||||
seal.fulfill(request)
|
||||
retainCycle = nil
|
||||
}
|
||||
|
||||
func request(_: SKRequest, didFailWithError error: Error) {
|
||||
seal.reject(error)
|
||||
retainCycle = nil
|
||||
}
|
||||
}
|
31
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/SKRequest+AnyPromise.h
vendored
Normal file
31
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/SKRequest+AnyPromise.h
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// PromiseKit+StoreKit.h
|
||||
// Aluxoft SCP
|
||||
//
|
||||
// Created by Josejulio Martínez on 16/05/14.
|
||||
|
||||
#import <PromiseKit/AnyPromise.h>
|
||||
#import <StoreKit/SKRequest.h>
|
||||
|
||||
/**
|
||||
To import the `SKRequest` category:
|
||||
|
||||
use_frameworks!
|
||||
pod "PromiseKit/StoreKit"
|
||||
|
||||
And then in your sources:
|
||||
|
||||
@import PromiseKit;
|
||||
*/
|
||||
@interface SKRequest (PromiseKit)
|
||||
|
||||
/**
|
||||
Sends the request to the Apple App Store.
|
||||
|
||||
@return A promise that fulfills when the request succeeds. If the
|
||||
receiver is an SKProductsRequest, the promise fulfills with its
|
||||
`SKProductsResponse`, otherwise the promise is void.
|
||||
*/
|
||||
- (AnyPromise *)promise NS_REFINED_FOR_SWIFT;
|
||||
|
||||
@end
|
48
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/SKRequest+AnyPromise.m
vendored
Normal file
48
Carthage/Checkouts/PromiseKit/Extensions/StoreKit/Sources/SKRequest+AnyPromise.m
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// PromiseKit+StoreKit.m
|
||||
// Aluxoft SCP
|
||||
//
|
||||
// Created by Josejulio Martínez on 16/05/14.
|
||||
//
|
||||
|
||||
#import "SKRequest+AnyPromise.h"
|
||||
#import <StoreKit/SKProductsRequest.h>
|
||||
|
||||
//TODO do categories work on inherited classes? As would solve our swift SKProductsRequest problem
|
||||
|
||||
@interface PMKSKRequestDelegate : NSObject <SKProductsRequestDelegate> {
|
||||
@public
|
||||
PMKResolver resolve;
|
||||
id retainCycle;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation PMKSKRequestDelegate
|
||||
|
||||
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
|
||||
resolve(response);
|
||||
retainCycle = request.delegate = nil;
|
||||
}
|
||||
|
||||
- (void)requestDidFinish:(SKRequest *)request {
|
||||
resolve(nil);
|
||||
retainCycle = request.delegate = nil;
|
||||
}
|
||||
|
||||
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
|
||||
resolve(error);
|
||||
retainCycle = request.delegate = nil;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation SKProductsRequest (PromiseKit)
|
||||
|
||||
- (AnyPromise *)promise {
|
||||
PMKSKRequestDelegate *d = [PMKSKRequestDelegate new];
|
||||
d->retainCycle = self.delegate = d;
|
||||
[self start];
|
||||
return [[AnyPromise alloc] initWithResolver:&d->resolve];
|
||||
}
|
||||
|
||||
@end
|
Reference in New Issue
Block a user