From 3b9e3879ca811d24c9d9a9b10c2d7f6293ffa8d8 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Fri, 16 Aug 2019 00:15:19 +0200 Subject: [PATCH] Move InAppHelper to Core --- Passepartout-iOS/AppDelegate.swift | 2 +- Passepartout-iOS/Global/InApp.swift | 100 ------------------ .../Organizer/DonationViewController.swift | 2 +- Passepartout.xcodeproj/project.pbxproj | 4 + Submodules/Core | 2 +- 5 files changed, 7 insertions(+), 103 deletions(-) diff --git a/Passepartout-iOS/AppDelegate.swift b/Passepartout-iOS/AppDelegate.swift index 97163b67..316910b2 100644 --- a/Passepartout-iOS/AppDelegate.swift +++ b/Passepartout-iOS/AppDelegate.swift @@ -54,7 +54,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele // splitViewController.preferredDisplayMode = .primaryOverlay } - InAppHelper.shared.requestProducts(completionHandler: nil) + InAppHelper.shared.requestProducts(withIdentifiers: InApp.allIdentifiers(), completionHandler: nil) return true } diff --git a/Passepartout-iOS/Global/InApp.swift b/Passepartout-iOS/Global/InApp.swift index a7dbb213..70132db5 100644 --- a/Passepartout-iOS/Global/InApp.swift +++ b/Passepartout-iOS/Global/InApp.swift @@ -54,103 +54,3 @@ struct InApp { return Set(Donation.all.map { $0.rawValue }) } } - -class InAppHelper: NSObject { - enum PurchaseResult { - case success - - case failure - - case cancelled - } - - typealias ProductObserver = ([SKProduct]) -> Void - - typealias TransactionObserver = (PurchaseResult, Error?) -> Void - - static let shared = InAppHelper() - - private(set) var products: [SKProduct] - - private var productObservers: [ProductObserver] - - private var transactionObservers: [String: TransactionObserver] - - private override init() { - products = [] - productObservers = [] - transactionObservers = [:] - super.init() - - SKPaymentQueue.default().add(self) - } - - deinit { - SKPaymentQueue.default().remove(self) - } - - func requestProducts(completionHandler: ProductObserver?) { - let req = SKProductsRequest(productIdentifiers: InApp.allIdentifiers()) - req.delegate = self - if let observer = completionHandler { - productObservers.append(observer) - } - req.start() - } - - private func receiveProducts(_ products: [SKProduct]) { - self.products = products - productObservers.forEach { $0(products) } - productObservers.removeAll() - } - - func purchase(product: SKProduct, completionHandler: @escaping TransactionObserver) { - let queue = SKPaymentQueue.default() - let payment = SKPayment(product: product) - transactionObservers[product.productIdentifier] = completionHandler - queue.add(payment) - } -} - -extension InAppHelper: SKProductsRequestDelegate { - func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { - DispatchQueue.main.async { - self.receiveProducts(response.products) - } - } - - func request(_ request: SKRequest, didFailWithError error: Error) { - transactionObservers.removeAll() - } -} - -extension InAppHelper: SKPaymentTransactionObserver { - func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { - for tx in transactions { - let observer = transactionObservers[tx.payment.productIdentifier] - - switch tx.transactionState { - case .purchased, .restored: - queue.finishTransaction(tx) - DispatchQueue.main.async { - observer?(.success, nil) - } - - case .failed: - queue.finishTransaction(tx) - if let skError = tx.error as? SKError, skError.code == .paymentCancelled { - DispatchQueue.main.async { - observer?(.cancelled, nil) - } - } else { - DispatchQueue.main.async { - observer?(.failure, tx.error) - } - } - - default: - break - } - } - } -} diff --git a/Passepartout-iOS/Scenes/Organizer/DonationViewController.swift b/Passepartout-iOS/Scenes/Organizer/DonationViewController.swift index 16658d1d..0dc7defb 100644 --- a/Passepartout-iOS/Scenes/Organizer/DonationViewController.swift +++ b/Passepartout-iOS/Scenes/Organizer/DonationViewController.swift @@ -86,7 +86,7 @@ class DonationViewController: UITableViewController, TableModelHost { let inApp = InAppHelper.shared if inApp.products.isEmpty { - inApp.requestProducts { + inApp.requestProducts(withIdentifiers: InApp.allIdentifiers()) { self.isLoading = false self.setProducts($0) } diff --git a/Passepartout.xcodeproj/project.pbxproj b/Passepartout.xcodeproj/project.pbxproj index 41ddb952..e33304ff 100644 --- a/Passepartout.xcodeproj/project.pbxproj +++ b/Passepartout.xcodeproj/project.pbxproj @@ -92,6 +92,7 @@ 0ECEB10A224FECEA00E9E551 /* DataUnit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0ECEB109224FECEA00E9E551 /* DataUnit.swift */; }; 0ECEE44E20E1122200A6BB43 /* TableModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0ECEE44D20E1122200A6BB43 /* TableModel.swift */; }; 0ECEE45020E1182E00A6BB43 /* Theme+Cells.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0ECEE44F20E1182E00A6BB43 /* Theme+Cells.swift */; }; + 0ECF12D7230612F5008E4924 /* InAppHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0ECF12D6230612F5008E4924 /* InAppHelper.swift */; }; 0ED31C2920CF2A340027975F /* AccountViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0ED31C2820CF2A340027975F /* AccountViewController.swift */; }; 0ED31C2C20CF2D6F0027975F /* ProviderPoolViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0ED31C2B20CF2D6F0027975F /* ProviderPoolViewController.swift */; }; 0ED31C3A20CF39510027975F /* NetworkExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0ED31C3920CF39510027975F /* NetworkExtension.framework */; }; @@ -270,6 +271,7 @@ 0ECEB109224FECEA00E9E551 /* DataUnit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataUnit.swift; sourceTree = ""; }; 0ECEE44D20E1122200A6BB43 /* TableModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableModel.swift; sourceTree = ""; }; 0ECEE44F20E1182E00A6BB43 /* Theme+Cells.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Theme+Cells.swift"; sourceTree = ""; }; + 0ECF12D6230612F5008E4924 /* InAppHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InAppHelper.swift; path = Submodules/Core/Passepartout/Sources/InAppHelper.swift; sourceTree = SOURCE_ROOT; }; 0ED31C0F20CF09A30027975F /* Pool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pool.swift; sourceTree = ""; }; 0ED31C1120CF0ABA0027975F /* Infrastructure.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Infrastructure.swift; sourceTree = ""; }; 0ED31C2820CF2A340027975F /* AccountViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountViewController.swift; sourceTree = ""; }; @@ -587,6 +589,7 @@ 0E39BCF2214DA9310035E9DE /* AppConstants.swift */, 0E6BE13920CFB76800A6DD36 /* ApplicationError.swift */, 0EDE8DED20C93E4C004C739C /* GroupConstants.swift */, + 0ECF12D6230612F5008E4924 /* InAppHelper.swift */, 0E78179E21BE852200950C58 /* Reviewer.swift */, 0E4FD7ED20D539A0002221FF /* Utils.swift */, ); @@ -972,6 +975,7 @@ 0E3152CC223FA04D00F61841 /* WebServices.swift in Sources */, 0E3152BB223FA03D00F61841 /* AppConstants.swift in Sources */, 0E3152CA223FA04D00F61841 /* InfrastructurePreset.swift in Sources */, + 0ECF12D7230612F5008E4924 /* InAppHelper.swift in Sources */, 0E3152CE223FA05400F61841 /* ConnectionService.swift in Sources */, 0ED993B1223FF8C700B0F9C9 /* IntentDispatcher.swift in Sources */, 0E3152BE223FA03D00F61841 /* Reviewer.swift in Sources */, diff --git a/Submodules/Core b/Submodules/Core index 5a3fae40..97c8c81e 160000 --- a/Submodules/Core +++ b/Submodules/Core @@ -1 +1 @@ -Subproject commit 5a3fae40bc2a89e4553167de134f70ea7905b195 +Subproject commit 97c8c81e16f54f6763c5b3cba2912ff9b7c908ae