145 lines
4.9 KiB
Swift
145 lines
4.9 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
|
|
import StoreKit
|
|
|
|
|
|
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
|
|
}()
|
|
}
|
|
|
|
|
|
extension SubscriptionDetailViewController {
|
|
override var canBecomeFirstResponder: Bool {
|
|
false
|
|
}
|
|
|
|
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
|
|
guard let product = product, event?.subtype == .motionShake else {
|
|
return
|
|
}
|
|
|
|
let alert = UIAlertController(title: "🧑💻", message: "Please select an action", preferredStyle: .alert)
|
|
alert.addAction(UIAlertAction(title: "Reset all purchases", style: .default) { action in
|
|
EQNPurchaseUtility.resetInAppPurchases()
|
|
})
|
|
alert.addAction(UIAlertAction(title: "Activate this subscription", style: .default) { action in
|
|
EQNPurchaseUtility.simulateProPurchase(identifier: product.productIdentifier)
|
|
})
|
|
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
|
|
present(alert, animated: true, completion: nil)
|
|
}
|
|
}
|