// // PurchaseProVersionViewController.swift // Earthquake Network // // Created by Busi Andrea on 29/07/2020. // Copyright © 2020 Earthquake Network. All rights reserved. // import UIKit import SafariServices import StoreKit class PurchaseProVersionViewController: UIViewController { @IBOutlet private weak var discountTextLabel: UILabel! @IBOutlet private weak var descriptionTextLabel: UILabel! @IBOutlet private weak var openPrivacyButton: UIButton! @IBOutlet private weak var openTermsButton: UIButton! @IBOutlet private weak var priceLabel: UILabel! @IBOutlet private weak var purchaseButton: UIButton! // MARK: - Internal private var products = [SKProduct]() private var proProduct: SKProduct? private var restoreTapped = false /// Time remaining (in hours) for discounted price. If zero, no discount available private var discountTimeRemaining: Int = 0 // MARK: - View Lifecycle override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(handlePurchaseNotification(_:)), name: .IAPHelperPurchaseNotification, object: nil) configureUI() loadProducts() checkDiscountPrice() } // MARK: - Private private func configureUI() { let restoreButton = UIBarButtonItem(title: NSLocalizedString("purchase_pro_restore", comment: ""), style: .plain, target: self, action: #selector(restoreTapped(_:))) navigationItem.rightBarButtonItem = restoreButton purchaseButton.isEnabled = false descriptionTextLabel.text = NSLocalizedString("purchase_pro_description", comment: "") discountTextLabel.text = NSLocalizedString("purchase_pro_discount", comment: "") discountTextLabel.isHidden = true } private func updateUI() { // search for the Pro product let isDiscountEnabled = discountTimeRemaining > 0 let identifier = isDiscountEnabled ? VersioneProProducts.Identifier.ProVersionDiscounted : VersioneProProducts.Identifier.ProVersionFullPrice guard let proProduct = products.first(where: { $0.productIdentifier.lowercased() == identifier.lowercased() }) else { return } self.proProduct = proProduct priceFormatter.locale = proProduct.priceLocale if isDiscountEnabled { discountTextLabel.isHidden = false let string = NSLocalizedString("purchase_pro_discount", comment: "") discountTextLabel.text = String(format: string, discountTimeRemaining) } priceLabel.text = priceFormatter.string(from: proProduct.price) purchaseButton.isEnabled = true if UserDefaults.standard.bool(forKey: VersioneProProducts.Identifier.ProVersionFullPrice) || UserDefaults.standard.bool(forKey: VersioneProProducts.Identifier.ProVersionDiscounted) || UserDefaults.standard.bool(forKey: VersioneProProducts.Identifier.Subscription10kYearly) || UserDefaults.standard.bool(forKey: VersioneProProducts.Identifier.Subscription10kYearlyDiscounted) || UserDefaults.standard.bool(forKey: VersioneProProducts.Identifier.Subscription100kYearly) || UserDefaults.standard.bool(forKey: VersioneProProducts.Identifier.Subscription100kYearlyDiscounted) { purchaseButton.isEnabled = false priceLabel.text = "-" } } private func loadProducts() { VersioneProProducts.store.requestProducts { [weak self] success, products in guard let self = self, let products = products, success == true else { return } self.products = products self.updateUI() } } private func checkDiscountPrice() { EQNPurchaseUtility.offerTimeRemaining { (timeRemaining) in DispatchQueue.main.async { self.discountTimeRemaining = timeRemaining self.updateUI() } } } // MARK: - Actions @objc func restoreTapped(_ sender: Any) { restoreTapped = true VersioneProProducts.store.restorePurchases() } @IBAction func purchaseTapped(_ sender: UIButton) { guard let product = proProduct else { return } VersioneProProducts.store.buyProduct(product) } @IBAction func openExternalLinkTapped(_ sender: UIButton) { var linkUrl: URL? if sender == openPrivacyButton { linkUrl = URL(string: "\(EQNWebsiteAddress)/privacy/") } else if sender == openTermsButton { linkUrl = URL(string: "\(EQNWebsiteAddress)/terms-conditions/") } if let url = linkUrl { let controller = SFSafariViewController(url: url) present(controller, animated: true, completion: nil) } } // MARK: - Notifications @objc func handlePurchaseNotification(_ notification: Notification) { guard let productId = notification.object as? String, products.contains(where: { $0.productIdentifier == productId }) else { print("[PurchasePro] Unable to find the product") return } if restoreTapped { restoreTapped = false let alert = UIAlertController(title: NSLocalizedString("purchase_pro_restore_alert_title", comment: ""), message: NSLocalizedString("purchase_pro_restore_alert_message", comment: ""), preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default) { [weak self] _ in self?.navigationController?.popViewController(animated: true) }) present(alert, animated: true) } else { navigationController?.popViewController(animated: true) } } // MARK: - Helper private var priceFormatter: NumberFormatter = { let formatter = NumberFormatter() formatter.formatterBehavior = .behavior10_4 formatter.numberStyle = .currency return formatter }() }