Configure SSPurchase in `perform(…) instead of in a convenience init.

Partial #562

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
This commit is contained in:
Ross Goldberg 2024-10-10 23:38:24 -04:00
parent c01f7c541e
commit a45308f2f2
No known key found for this signature in database
2 changed files with 6 additions and 20 deletions

View file

@ -34,9 +34,7 @@ func downloadAll(_ appIDs: [UInt64], purchase: Bool = false) -> Promise<Void> {
}
}
private func downloadWithRetries(
_ appID: UInt64, purchase: Bool = false, attempts: Int = 3
) -> Promise<Void> {
private func downloadWithRetries(_ appID: UInt64, purchase: Bool = false, attempts: Int = 3) -> Promise<Void> {
download(appID, purchase: purchase).recover { error -> Promise<Void> in
guard attempts > 1 else {
throw error
@ -59,17 +57,11 @@ private func downloadWithRetries(
/// Downloads an app, printing progress to the console.
///
/// - Parameter appID: The ID of the app to be downloaded
/// - Parameter purchase: Flag indicating whether the app needs to be purchased.
/// Only works for free apps. Defaults to false.
/// - Parameter purchase: Flag indicating whether the app needs to be purchased. Only works for free apps.
/// - Returns: A promise the completes when the download is complete.
private func download(_ appID: UInt64, purchase: Bool = false) -> Promise<Void> {
private func download(_ appID: UInt64, purchase: Bool) -> Promise<Void> {
Promise<SSPurchase> { seal in
guard let purchase = SSPurchase(adamId: appID, purchase: purchase) else {
seal.reject(MASError.notSignedIn)
return
}
purchase.perform { purchase, _, error, response in
SSPurchase().perform(adamId: appID, purchase: purchase) { purchase, _, error, response in
if let error {
seal.reject(MASError.purchaseFailed(error: error as NSError?))
return

View file

@ -13,9 +13,7 @@ typealias SSPurchaseCompletion =
(_ purchase: SSPurchase?, _ completed: Bool, _ error: Error?, _ response: SSPurchaseResponse?) -> Void
extension SSPurchase {
convenience init?(adamId: UInt64, purchase: Bool = false) {
self.init()
func perform(adamId: UInt64, purchase: Bool, _ completion: @escaping SSPurchaseCompletion) {
var parameters: [String: Any] = [
"productType": "C",
"price": 0,
@ -56,14 +54,10 @@ extension SSPurchase {
isRedownload = false
}
let downloadMetadata = SSDownloadMetadata()
downloadMetadata = SSDownloadMetadata()
downloadMetadata.kind = "software"
downloadMetadata.itemIdentifier = adamId
self.downloadMetadata = downloadMetadata
}
func perform(_ completion: @escaping SSPurchaseCompletion) {
CKPurchaseController.shared().perform(self, withOptions: 0, completionHandler: completion)
}
}