ProductsInfoController.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // ProductsInfoController.swift
  3. // SwiftyStoreKit
  4. //
  5. // Copyright (c) 2015 Andrea Bizzotto (bizz84@gmail.com)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. import StoreKit
  26. protocol InAppProductRequestBuilder: AnyObject {
  27. func request(productIds: Set<String>, callback: @escaping InAppProductRequestCallback) -> InAppProductRequest
  28. }
  29. class InAppProductQueryRequestBuilder: InAppProductRequestBuilder {
  30. func request(productIds: Set<String>, callback: @escaping InAppProductRequestCallback) -> InAppProductRequest {
  31. return InAppProductQueryRequest(productIds: productIds, callback: callback)
  32. }
  33. }
  34. class ProductsInfoController: NSObject {
  35. struct InAppProductQuery {
  36. let request: InAppProductRequest
  37. var completionHandlers: [InAppProductRequestCallback]
  38. }
  39. let inAppProductRequestBuilder: InAppProductRequestBuilder
  40. init(inAppProductRequestBuilder: InAppProductRequestBuilder = InAppProductQueryRequestBuilder()) {
  41. self.inAppProductRequestBuilder = inAppProductRequestBuilder
  42. }
  43. // As we can have multiple inflight requests, we store them in a dictionary by product ids
  44. private var inflightRequests: [Set<String>: InAppProductQuery] = [:]
  45. @discardableResult
  46. func retrieveProductsInfo(_ productIds: Set<String>, completion: @escaping (RetrieveResults) -> Void) -> InAppProductRequest {
  47. if inflightRequests[productIds] == nil {
  48. let request = inAppProductRequestBuilder.request(productIds: productIds) { results in
  49. if let query = self.inflightRequests[productIds] {
  50. for completion in query.completionHandlers {
  51. completion(results)
  52. }
  53. self.inflightRequests[productIds] = nil
  54. } else {
  55. // should not get here, but if it does it seems reasonable to call the outer completion block
  56. completion(results)
  57. }
  58. }
  59. inflightRequests[productIds] = InAppProductQuery(request: request, completionHandlers: [completion])
  60. request.start()
  61. return request
  62. } else {
  63. inflightRequests[productIds]!.completionHandlers.append(completion)
  64. let query = inflightRequests[productIds]!
  65. if query.request.hasCompleted {
  66. query.completionHandlers.forEach {
  67. $0(query.request.cachedResults!)
  68. }
  69. }
  70. return inflightRequests[productIds]!.request
  71. }
  72. }
  73. }