<commit_before>import Foundation
import StoreKit

class MStore
{
    typealias PurchaseId = String
    
    var mapItems:[PurchaseId:MStoreItem]?
    var error:String?
    private weak var controller:CStore!
    private let priceFormatter:NumberFormatter
    
    init(controller:CStore)
    {
        self.controller = controller
        priceFormatter = NumberFormatter()
        priceFormatter.numberStyle = NumberFormatter.Style.currencyISOCode
        
        DispatchQueue.global(qos:DispatchQoS.QoSClass.background).async
        { [weak self] in
            
            self?.loadFirebasePurchases()
        }
    }
    
    //MARK: private
    
    private func loadFirebasePurchases()
    {
        let pathPurchases:String = FDatabase.Parent.purchase.rawValue
        
        FMain.sharedInstance.database.listenOnce(
            path:pathPurchases,
            modelType:FDatabaseModelPurchaseList.self)
        { [weak self] (purchaseList) in
            
            guard
                
                let purchaseListStrong:FDatabaseModelPurchaseList = purchaseList
                
            else
            {
                self?.error = NSLocalizedString("MStore_errorLoading", comment:"")
                self?.controller.noFirebasePurchases()
                
                return
            }
            
            self?.listLoaded(purchaseList:purchaseListStrong)
        }
    }
    
    private func listLoaded(purchaseList:FDatabaseModelPurchaseList)
    {
        var mapItems:[PurchaseId:MStoreItem] = [:]
        var itemsSet:Set<PurchaseId> = Set<PurchaseId>()
        
        let itemKeys:[FDatabaseModelPurchase.PurchaseId] = Array(purchaseList.items.keys)
        
        for itemKey:FDatabaseModelPurchase.PurchaseId in itemKeys
        {
            let firebasePurchase:FDatabaseModelPurchase = purchaseList.items[itemKey]!
            let item:MStoreItem = MStoreItem(
                firebasePurchase:firebasePurchase)
            mapItems[itemKey] = item
            itemsSet.insert(itemKey)
        }
        
        self.mapItems = mapItems
        
        controller.checkAvailabilities(itemsSet:itemsSet)
    }
    
    //MARK: public
    
    func loadSkProduct(skProduct:SKProduct)
    {/*
         let productId:String = skProduct.productIdentifier
         
         guard
         
         let mappedItem:MStorePurchaseItem = mapItems[productId]
         
         else
         {
         return
         }
         
         mappedItem.skProduct = skProduct
         priceFormatter.locale = skProduct.priceLocale
         
         let priceNumber:NSDecimalNumber = skProduct.price
         let priceString:String? = priceFormatter.string(from:priceNumber)
         mappedItem.price = priceString*/
    }
    
    func updateTransactions(transactions:[SKPaymentTransaction])
    {/*
         for skPaymentTransaction:SKPaymentTransaction in transactions
         {
         let productId:String = skPaymentTransaction.payment.productIdentifier
         
         guard
         
         let mappedItem:MStorePurchaseItem = mapItems[productId]
         
         else
         {
         continue
         }
         
         switch skPaymentTransaction.transactionState
         {
         case SKPaymentTransactionState.deferred:
         
         mappedItem.status = MStorePurchaseItemStatusDeferred()
         
         break
         
         case SKPaymentTransactionState.failed:
         
         mappedItem.status = MStorePurchaseItemStatusNew()
         SKPaymentQueue.default().finishTransaction(skPaymentTransaction)
         
         break
         
         case SKPaymentTransactionState.purchased,
         SKPaymentTransactionState.restored:
         
         mappedItem.purchased()
         SKPaymentQueue.default().finishTransaction(skPaymentTransaction)
         
         break
         
         case SKPaymentTransactionState.purchasing:
         
         mappedItem.status = MStorePurchaseItemStatusPurchasing()
         
         break
         }
         }*/
    }
}
<commit_msg>load products and update transactions<commit_after>import Foundation
import StoreKit

class MStore
{
    typealias PurchaseId = String
    
    var mapItems:[PurchaseId:MStoreItem]?
    var error:String?
    private weak var controller:CStore!
    private let priceFormatter:NumberFormatter
    
    init(controller:CStore)
    {
        self.controller = controller
        priceFormatter = NumberFormatter()
        priceFormatter.numberStyle = NumberFormatter.Style.currencyISOCode
        
        DispatchQueue.global(qos:DispatchQoS.QoSClass.background).async
        { [weak self] in
            
            self?.loadFirebasePurchases()
        }
    }
    
    //MARK: private
    
    private func loadFirebasePurchases()
    {
        let pathPurchases:String = FDatabase.Parent.purchase.rawValue
        
        FMain.sharedInstance.database.listenOnce(
            path:pathPurchases,
            modelType:FDatabaseModelPurchaseList.self)
        { [weak self] (purchaseList) in
            
            guard
                
                let purchaseListStrong:FDatabaseModelPurchaseList = purchaseList
                
            else
            {
                self?.error = NSLocalizedString("MStore_errorLoading", comment:"")
                self?.controller.noFirebasePurchases()
                
                return
            }
            
            self?.listLoaded(purchaseList:purchaseListStrong)
        }
    }
    
    private func listLoaded(purchaseList:FDatabaseModelPurchaseList)
    {
        var mapItems:[PurchaseId:MStoreItem] = [:]
        var itemsSet:Set<PurchaseId> = Set<PurchaseId>()
        
        let itemKeys:[FDatabaseModelPurchase.PurchaseId] = Array(purchaseList.items.keys)
        
        for itemKey:FDatabaseModelPurchase.PurchaseId in itemKeys
        {
            let firebasePurchase:FDatabaseModelPurchase = purchaseList.items[itemKey]!
            let item:MStoreItem = MStoreItem(
                firebasePurchase:firebasePurchase)
            mapItems[itemKey] = item
            itemsSet.insert(itemKey)
        }
        
        self.mapItems = mapItems
        
        controller.checkAvailabilities(itemsSet:itemsSet)
    }
    
    //MARK: public
    
    func loadSkProduct(skProduct:SKProduct)
    {
        let productId:String = skProduct.productIdentifier
        
        guard
            
            let mappedItem:MStoreItem = mapItems?[productId]
            
        else
        {
            return
        }
        
        mappedItem.skProduct = skProduct
        priceFormatter.locale = skProduct.priceLocale
        
        let priceNumber:NSDecimalNumber = skProduct.price
        let priceString:String? = priceFormatter.string(from:priceNumber)
        mappedItem.price = priceString
    }
    
    func updateTransactions(transactions:[SKPaymentTransaction])
    {
        for skPaymentTransaction:SKPaymentTransaction in transactions
        {
            let productId:String = skPaymentTransaction.payment.productIdentifier
            
            guard
                
                let mappedItem:MStoreItem = mapItems?[productId]
                
            else
            {
                continue
            }
            
            switch skPaymentTransaction.transactionState
            {
                case SKPaymentTransactionState.deferred:
                    
                    mappedItem.statusDeferred()
                    
                    break
                    
                case SKPaymentTransactionState.failed:
                    
                    mappedItem.statusNew()
                    SKPaymentQueue.default().finishTransaction(skPaymentTransaction)
                    
                    break
                    
                case SKPaymentTransactionState.purchased,
                     SKPaymentTransactionState.restored:
                    
                    mappedItem.statusPurchased()
                    SKPaymentQueue.default().finishTransaction(skPaymentTransaction)
                    
                    break
                    
                case SKPaymentTransactionState.purchasing:
                    
                    mappedItem.statusPurchasing()
                    
                    break
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.059752910430030885, 'fraction_numerical': 0.0, 'mean_word_length': 1.5904615384615384, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7200371', 'n_tokens_mistral': 2073, 'n_tokens_neox': 2038, 'n_words': 301}