Files
eqn.ios/Sources/Earthquake Network/Controllers/InApp/SubscriptionDetailViewController.swift
T
2020-07-30 21:09:03 +02:00

121 lines
4.0 KiB
Swift

//
// SubscriptionDetailViewController.swift
// Earthquake Network
//
// Created by Busi Andrea on 29/07/2020.
// Copyright © 2020 Earthquake Network. All rights reserved.
//
import UIKit
import SafariServices
class SubscriptionDetailViewController: UIViewController {
var product: SKProduct? {
didSet {
updateUI()
}
}
@IBOutlet private weak var productTitleLabel: UILabel!
@IBOutlet private weak var productImageView: UIImageView!
@IBOutlet private weak var productDescriptionLabel: UILabel!
@IBOutlet private weak var subscriptionDetailsLabel: UILabel!
@IBOutlet private weak var openPrivacyButton: UIButton!
@IBOutlet private weak var openTermsButton: UIButton!
@IBOutlet private weak var purchaseRecapLabel: UILabel!
@IBOutlet private weak var productPriceLabel: UILabel!
@IBOutlet private weak var purchaseButton: UIButton!
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(handlePurchaseNotification(_:)),
name: .IAPHelperPurchaseNotification,
object: nil)
updateUI()
}
// MARK: - Private
private func updateUI() {
guard let product = product, isViewLoaded else { return }
productTitleLabel.text = product.localizedTitle
productDescriptionLabel.text = product.localizedDescription
var purchaseRecapString = ""
switch product.productIdentifier {
case VersioneProProducts.Identifier.Subscription10kMonthly:
productImageView.image = UIImage.init(named: "top_10k")
purchaseRecapString = "pagerai al mese:"
case VersioneProProducts.Identifier.Subscription100kMonthly:
productImageView.image = UIImage.init(named: "top_100k")
purchaseRecapString = "pagerai al mese:"
case VersioneProProducts.Identifier.Subscription100kYearly, VersioneProProducts.Identifier.Subscription100kYearlyDiscounted:
productImageView.image = UIImage.init(named: "top_100k")
purchaseRecapString = "pagerai all'anno:"
case VersioneProProducts.Identifier.Subscription10kYearly, VersioneProProducts.Identifier.Subscription10kYearlyDiscounted:
productImageView.image = UIImage.init(named: "top_10k")
purchaseRecapString = "pagerai all'anno:"
default:
break
}
purchaseRecapLabel.text = "\(product.localizedDescription), \(NSLocalizedString(purchaseRecapString, comment: ""))"
priceFormatter.locale = product.priceLocale
productPriceLabel.text = priceFormatter.string(from: product.price)
}
// MARK: - Notifications
@objc func handlePurchaseNotification(_ notification: Notification) {
navigationController?.popViewController(animated: true)
}
// MARK: - Actions
@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)
}
}
@IBAction func subscribeTapped(_ sender: UIButton) {
guard let product = product else { return }
VersioneProProducts.store.buyProduct(product)
}
// MARK: - Helper
private var priceFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.formatterBehavior = .behavior10_4
formatter.numberStyle = .currency
return formatter
}()
}