<commit_before>//
//  ViewController.swift
//  Gallery
//
//  Created by <NAME> on 4/5/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import CoreData
import StoreKit


class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, SKProductsRequestDelegate, SKPaymentTransactionObserver {

    @IBOutlet weak var collectionView: UICollectionView!
    
    var gallery = [Art]()
    var products = [SKProduct]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.delegate = self
        collectionView.dataSource = self
        
        updateGallery()
        
        if gallery.count == 0 {
            createArt("Mona Lisa", imageName: "mona-lisa.jpg", productIdentifier: "", purchased: true)
            createArt("The Starry Night", imageName: "starry-night.jpg", productIdentifier: "StarryNight", purchased: false)
            createArt("The Scream", imageName: "the-scream.jpg", productIdentifier: "Scream", purchased: false)
            createArt("The Persistence of Memory", imageName: "the-persistence-of-memory-1931.jpg", productIdentifier: "PersistenceOfMemory", purchased: false)
            updateGallery()
            collectionView.reloadData()
        }
        
        requestProductsForSale()
    }
    
    
    // MARK: IAP Functions / StoreKit Delegate Methods
    
    func requestProductsForSale() {
        let ids: Set<String> = ["StarryNight", "Scream", "PersistenceOfMemory"]
        let productsRequest = SKProductsRequest(productIdentifiers: ids)
        productsRequest.delegate = self
        productsRequest.start()
    }
    
    
    func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
        print("Received requested products")
        print("Products Ready: \(response.products.count)")
        print("Invalid Products: \(response.invalidProductIdentifiers.count)")
        
        for product in response.products {
            print("Product: \(product.productIdentifier), Price: \(product.price)")
        }
        
        products = response.products
        collectionView.reloadData()
    }
    
    
    // MARK: Payment Transaction Observer Delegate Method
    
    func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch transaction.transactionState {
            case .Purchased:
                print("Purchased")
                SKPaymentQueue.defaultQueue().finishTransaction(transaction)
                
            case .Failed:<commit_msg>Provide user with the purchase by unlocking the product<commit_after>//
//  ViewController.swift
//  Gallery
//
//  Created by <NAME> on 4/5/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import CoreData
import StoreKit


class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, SKProductsRequestDelegate, SKPaymentTransactionObserver {

    @IBOutlet weak var collectionView: UICollectionView!
    
    var gallery = [Art]()
    var products = [SKProduct]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.delegate = self
        collectionView.dataSource = self
        
        updateGallery()
        
        if gallery.count == 0 {
            createArt("Mona Lisa", imageName: "mona-lisa.jpg", productIdentifier: "", purchased: true)
            createArt("The Starry Night", imageName: "starry-night.jpg", productIdentifier: "StarryNight", purchased: false)
            createArt("The Scream", imageName: "the-scream.jpg", productIdentifier: "Scream", purchased: false)
            createArt("The Persistence of Memory", imageName: "the-persistence-of-memory-1931.jpg", productIdentifier: "PersistenceOfMemory", purchased: false)
            updateGallery()
            collectionView.reloadData()
        }
        
        requestProductsForSale()
    }
    
    
    // MARK: IAP Functions / StoreKit Delegate Methods
    
    func requestProductsForSale() {
        let ids: Set<String> = ["StarryNight", "Scream", "PersistenceOfMemory"]
        let productsRequest = SKProductsRequest(productIdentifiers: ids)
        productsRequest.delegate = self
        productsRequest.start()
    }
    
    
    func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
        print("Received requested products")
        print("Products Ready: \(response.products.count)")
        print("Invalid Products: \(response.invalidProductIdentifiers.count)")
        
        for product in response.products {
            print("Product: \(product.productIdentifier), Price: \(product.price)")
        }
        
        products = response.products
        collectionView.reloadData()
    }
    
    
    func unlockProduct(productIdentifier: String) {
        for art in gallery {
            if art.productIdentifier == productIdentifier {
                art.purchased = 1
                
                let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                let context = appDelegate.managedObjectContext
                
                do {
                    try context.save()
                } catch{}
                
                collectionView.reloadData()
            }
        }
    }
    
    
    // MARK: Payment Transaction Observer Delegate Method
    
    func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch transaction.transactionState {
            case .Purchased:
                print("Purchased")
                unlockProduct(transaction.payment.productIdentifier)
                SKPaymentQueue.defaultQueue().finishTransaction(transaction)
                
            case .Failed:
{'fraction_non_alphanumeric': 0.08963122209360014, 'fraction_numerical': 0.004465023978832479, 'mean_word_length': 2.426628895184136, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '111350', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1472, 'n_words': 353}