2019-10-28 09:07:38 +00:00
|
|
|
//
|
|
|
|
// PurchaseViewController.swift
|
|
|
|
// Passepartout-iOS
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 10/27/19.
|
2020-01-11 08:27:22 +00:00
|
|
|
// Copyright (c) 2020 Davide De Rosa. All rights reserved.
|
2019-10-28 09:07:38 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2019-10-30 12:12:58 +00:00
|
|
|
import StoreKit
|
|
|
|
import SwiftyBeaver
|
|
|
|
import Convenience
|
2019-10-28 09:07:38 +00:00
|
|
|
|
2019-10-30 12:12:58 +00:00
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
2019-12-04 13:16:50 +00:00
|
|
|
protocol PurchaseViewControllerDelegate: class {
|
|
|
|
func purchaseController(_ purchaseController: PurchaseViewController, didPurchase product: Product)
|
|
|
|
}
|
|
|
|
|
2019-10-30 12:12:58 +00:00
|
|
|
class PurchaseViewController: UITableViewController, StrongTableHost {
|
|
|
|
private var isLoading = true
|
|
|
|
|
|
|
|
var feature: Product!
|
2019-12-04 13:16:50 +00:00
|
|
|
|
|
|
|
weak var delegate: PurchaseViewControllerDelegate?
|
2019-10-30 12:12:58 +00:00
|
|
|
|
|
|
|
private var skFeature: SKProduct?
|
|
|
|
|
|
|
|
private var skFullVersion: SKProduct?
|
2019-11-09 17:03:07 +00:00
|
|
|
|
|
|
|
private var fullVersionExtra: String?
|
2019-10-30 12:12:58 +00:00
|
|
|
|
|
|
|
// MARK: StrongTableHost
|
|
|
|
|
|
|
|
var model: StrongTableModel<SectionType, RowType> = StrongTableModel()
|
|
|
|
|
|
|
|
func reloadModel() {
|
|
|
|
model.clear()
|
|
|
|
model.add(.products)
|
2019-11-09 17:03:07 +00:00
|
|
|
model.setFooter(L10n.App.Purchase.Sections.Products.footer, forSection: .products)
|
|
|
|
|
2019-10-30 12:12:58 +00:00
|
|
|
var rows: [RowType] = []
|
|
|
|
let pm = ProductManager.shared
|
|
|
|
if let skFullVersion = pm.product(withIdentifier: .fullVersion) {
|
|
|
|
self.skFullVersion = skFullVersion
|
|
|
|
rows.append(.fullVersion)
|
|
|
|
}
|
2019-11-09 17:05:21 +00:00
|
|
|
if let skFeature = pm.product(withIdentifier: feature) {
|
|
|
|
self.skFeature = skFeature
|
|
|
|
rows.append(.feature)
|
|
|
|
}
|
2019-10-30 12:12:58 +00:00
|
|
|
rows.append(.restore)
|
|
|
|
model.set(rows, forSection: .products)
|
2019-11-09 12:51:52 +00:00
|
|
|
|
|
|
|
let featureBulletsList: [String] = ProductManager.shared.featureProducts(includingFullVersion: false).map {
|
|
|
|
return "- \($0.localizedTitle)"
|
|
|
|
}.sortedCaseInsensitive()
|
|
|
|
let featureBullets = featureBulletsList.joined(separator: "\n")
|
2019-11-09 17:03:07 +00:00
|
|
|
fullVersionExtra = L10n.App.Purchase.Cells.FullVersion.extraDescription(featureBullets)
|
2019-10-30 12:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIViewController
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
guard let _ = feature else {
|
|
|
|
fatalError("No feature set for purchase")
|
|
|
|
}
|
|
|
|
|
|
|
|
title = L10n.App.Purchase.title
|
|
|
|
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(close))
|
|
|
|
|
|
|
|
isLoading = true
|
|
|
|
tableView.reloadData()
|
2019-11-04 18:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
2019-10-30 12:12:58 +00:00
|
|
|
|
|
|
|
let hud = HUD(view: view)
|
2019-11-30 10:42:21 +00:00
|
|
|
ProductManager.shared.listProducts { [weak self] (_, _) in
|
2019-10-30 12:12:58 +00:00
|
|
|
self?.reloadModel()
|
|
|
|
self?.isLoading = false
|
|
|
|
self?.tableView.reloadData()
|
|
|
|
hud.hide()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
private func purchaseFeature() {
|
|
|
|
guard let sk = skFeature else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
purchase(sk)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func purchaseFullVersion() {
|
|
|
|
guard let sk = skFullVersion else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
purchase(sk)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func restorePurchases() {
|
|
|
|
let hud = HUD(view: view)
|
|
|
|
ProductManager.shared.restorePurchases { [weak self] in
|
|
|
|
hud.hide()
|
|
|
|
guard $0 == nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self?.dismiss(animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func purchase(_ skProduct: SKProduct) {
|
|
|
|
let hud = HUD(view: view)
|
|
|
|
ProductManager.shared.purchase(skProduct) { [weak self] in
|
|
|
|
hud.hide()
|
|
|
|
guard $0 == .success else {
|
|
|
|
if let error = $1 {
|
|
|
|
self?.reportPurchaseError(withProduct: skProduct, error: error)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-12-04 13:16:50 +00:00
|
|
|
|
|
|
|
self?.dismiss(animated: true) {
|
|
|
|
guard let weakSelf = self else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let product = weakSelf.feature.matchesStoreKitProduct(skProduct) ? weakSelf.feature! : .fullVersion
|
|
|
|
weakSelf.delegate?.purchaseController(weakSelf, didPurchase: product)
|
|
|
|
}
|
2019-10-30 12:12:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func reportPurchaseError(withProduct product: SKProduct, error: Error) {
|
|
|
|
log.error("Unable to purchase \(product): \(error)")
|
|
|
|
|
|
|
|
let alert = UIAlertController.asAlert(product.localizedTitle, error.localizedDescription)
|
|
|
|
alert.addCancelAction(L10n.Core.Global.ok)
|
|
|
|
present(alert, animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func close() {
|
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension PurchaseViewController {
|
|
|
|
enum SectionType {
|
|
|
|
case products
|
|
|
|
}
|
|
|
|
|
|
|
|
enum RowType {
|
|
|
|
case feature
|
|
|
|
|
|
|
|
case fullVersion
|
|
|
|
|
|
|
|
case restore
|
|
|
|
}
|
|
|
|
|
2019-11-09 12:51:52 +00:00
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
|
|
|
return model.numberOfSections
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
|
|
|
|
return model.footer(forSection: section)
|
|
|
|
}
|
|
|
|
|
2019-10-30 12:12:58 +00:00
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
guard !isLoading else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return model.numberOfRows(forSection: section)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "PurchaseTableViewCell", for: indexPath) as! PurchaseTableViewCell
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .feature:
|
|
|
|
guard let product = skFeature else {
|
|
|
|
fatalError("Loaded feature cell, yet no corresponding product?")
|
|
|
|
}
|
|
|
|
cell.fill(product: product)
|
|
|
|
|
|
|
|
case .fullVersion:
|
|
|
|
guard let product = skFullVersion else {
|
|
|
|
fatalError("Loaded full version cell, yet no corresponding product?")
|
|
|
|
}
|
2019-11-09 17:03:07 +00:00
|
|
|
cell.fill(product: product, customDescription: fullVersionExtra)
|
2019-10-30 12:12:58 +00:00
|
|
|
|
|
|
|
case .restore:
|
|
|
|
cell.fill(
|
|
|
|
title: L10n.App.Purchase.Cells.Restore.title,
|
|
|
|
description: L10n.App.Purchase.Cells.Restore.description
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
2019-10-28 09:07:38 +00:00
|
|
|
|
2019-10-30 12:12:58 +00:00
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .feature:
|
|
|
|
purchaseFeature()
|
|
|
|
|
|
|
|
case .fullVersion:
|
|
|
|
purchaseFullVersion()
|
|
|
|
|
|
|
|
case .restore:
|
|
|
|
restorePurchases()
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 09:07:38 +00:00
|
|
|
}
|