txtodo

[DORMANT] a minimalist todo list app inspired by jeff huang
git clone git://git.figbert.com/txtodo.git
Log | Files | Refs | README

StoreManager.swift (3097B)


      1 //
      2 //  StoreManager.swift
      3 //  txtodo
      4 //
      5 //  Created by FIGBERT on 11/20/20.
      6 //
      7 
      8 import Foundation
      9 import StoreKit
     10 
     11 class StoreManager: NSObject, ObservableObject, SKProductsRequestDelegate, SKPaymentTransactionObserver {
     12     
     13     @Published var myProducts = [SKProduct]()
     14     @Published var transactionState: SKPaymentTransactionState?
     15     var request: SKProductsRequest!
     16     
     17     func getProducts(productIDs: [String]) {
     18         let request = SKProductsRequest(productIdentifiers: Set(productIDs))
     19         request.delegate = self
     20         request.start()
     21     }
     22     
     23     func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
     24         if !response.products.isEmpty {
     25             for fetchedProduct in response.products {
     26                 DispatchQueue.main.async {
     27                     self.myProducts.append(fetchedProduct)
     28                 }
     29             }
     30         }
     31         
     32         for invalidIdentifier in response.invalidProductIdentifiers {
     33             print("Invalid identifiers found: \(invalidIdentifier)")
     34         }
     35     }
     36     
     37     func request(_ request: SKRequest, didFailWithError error: Error) {
     38         print("Request did fail: \(error)")
     39     }
     40     
     41     func purchaseProduct(product: SKProduct) {
     42         if SKPaymentQueue.canMakePayments() {
     43             let payment = SKPayment(product: product)
     44             SKPaymentQueue.default().add(payment)
     45         } else {
     46             print("User can't make payment.")
     47         }
     48     }
     49     
     50     func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
     51         for transaction in transactions {
     52             switch transaction.transactionState {
     53             case .purchasing:
     54                 transactionState = .purchasing
     55             case .purchased:
     56                 UserDefaults.standard.setValue(true, forKey: transaction.payment.productIdentifier)
     57                 queue.finishTransaction(transaction)
     58                 transactionState = .purchased
     59             case .restored:
     60                 UserDefaults.standard.setValue(true, forKey: transaction.payment.productIdentifier)
     61                 queue.finishTransaction(transaction)
     62                 transactionState = .restored
     63             case .failed, .deferred:
     64                 print("Payment Queue Error: \(String(describing: transaction.error))")
     65                 queue.finishTransaction(transaction)
     66                 transactionState = .failed
     67             default:
     68                 queue.finishTransaction(transaction)
     69             }
     70         }
     71     }
     72     
     73     func restoreProducts() {
     74         SKPaymentQueue.default().restoreCompletedTransactions()
     75     }
     76 }
     77 
     78 extension SKProduct {
     79     func priceFormatted() -> String {
     80         let roundedPrice = self.price.doubleValue.rounded()
     81         let formatter = NumberFormatter()
     82         formatter.locale = self.priceLocale
     83         formatter.minimumFractionDigits = 0
     84         formatter.maximumFractionDigits = String(roundedPrice).contains(".00") ? 0 : 2
     85         formatter.numberStyle = .currency
     86         return formatter.string(from: NSNumber(value: roundedPrice))!
     87     }
     88 }