2019-10-28 09:07:38 +00:00
|
|
|
//
|
|
|
|
// PurchaseViewController.swift
|
2021-01-01 16:53:28 +00:00
|
|
|
// Passepartout
|
2019-10-28 09:07:38 +00:00
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 10/27/19.
|
2020-12-27 16:30:25 +00:00
|
|
|
// Copyright (c) 2021 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
|
2020-12-25 20:04:56 +00:00
|
|
|
import PassepartoutCore
|
2019-10-30 12:12:58 +00:00
|
|
|
import SwiftyBeaver
|
2021-10-27 16:47:08 +00:00
|
|
|
import ConvenienceUI
|
2019-10-28 09:07:38 +00:00
|
|
|
|
2019-10-30 12:12:58 +00:00
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
2021-06-26 07:49:48 +00:00
|
|
|
protocol PurchaseViewControllerDelegate: AnyObject {
|
2021-09-27 15:59:01 +00:00
|
|
|
func purchaseController(_ purchaseController: PurchaseViewController, didPurchase product: LocalProduct?)
|
2019-12-04 13:16:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 12:12:58 +00:00
|
|
|
class PurchaseViewController: UITableViewController, StrongTableHost {
|
|
|
|
private var isLoading = true
|
|
|
|
|
2021-09-27 15:59:01 +00:00
|
|
|
var feature: LocalProduct?
|
2019-12-04 13:16:50 +00:00
|
|
|
|
|
|
|
weak var delegate: PurchaseViewControllerDelegate?
|
2019-10-30 12:12:58 +00:00
|
|
|
|
|
|
|
private var skFeature: SKProduct?
|
2021-02-04 12:42:57 +00:00
|
|
|
|
|
|
|
private var skPlatformVersion: SKProduct?
|
2019-10-30 12:12:58 +00:00
|
|
|
|
|
|
|
private var skFullVersion: SKProduct?
|
2019-11-09 17:03:07 +00:00
|
|
|
|
2021-02-04 12:42:57 +00:00
|
|
|
private var platformVersionExtra: String?
|
|
|
|
|
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)
|
2021-08-07 11:57:22 +00:00
|
|
|
model.setFooter(L10n.Purchase.Sections.Products.footer, forSection: .products)
|
2019-11-09 17:03:07 +00:00
|
|
|
|
2019-10-30 12:12:58 +00:00
|
|
|
var rows: [RowType] = []
|
|
|
|
let pm = ProductManager.shared
|
2021-02-04 12:42:57 +00:00
|
|
|
if let skPlatformVersion = pm.product(withIdentifier: .fullVersion_iOS) {
|
|
|
|
self.skPlatformVersion = skPlatformVersion
|
|
|
|
rows.append(.platformVersion)
|
2021-02-07 11:50:39 +00:00
|
|
|
|
|
|
|
let bullets: [String] = ProductManager.shared.featureProducts(excluding: [.fullVersion, .fullVersion_iOS, .fullVersion_macOS]).map {
|
|
|
|
return $0.localizedTitle
|
|
|
|
}.sortedCaseInsensitive()
|
|
|
|
platformVersionExtra = bullets.joined(separator: "\n")
|
2021-02-04 12:42:57 +00:00
|
|
|
}
|
2021-02-07 14:28:30 +00:00
|
|
|
if !pm.hasPurchased(.fullVersion_macOS), let skFullVersion = pm.product(withIdentifier: .fullVersion) {
|
2019-10-30 12:12:58 +00:00
|
|
|
self.skFullVersion = skFullVersion
|
|
|
|
rows.append(.fullVersion)
|
2021-02-07 11:50:39 +00:00
|
|
|
|
|
|
|
let bullets: [String] = ProductManager.shared.featureProducts(including: [.fullVersion_iOS, .fullVersion_macOS]).map {
|
|
|
|
return $0.localizedTitle
|
|
|
|
}.sortedCaseInsensitive()
|
|
|
|
fullVersionExtra = bullets.joined(separator: "\n")
|
2019-10-30 12:12:58 +00:00
|
|
|
}
|
2021-02-07 10:16:57 +00:00
|
|
|
if let feature = feature, let skFeature = pm.product(withIdentifier: feature) {
|
2019-11-09 17:05:21 +00:00
|
|
|
self.skFeature = skFeature
|
|
|
|
rows.append(.feature)
|
|
|
|
}
|
2019-10-30 12:12:58 +00:00
|
|
|
rows.append(.restore)
|
|
|
|
model.set(rows, forSection: .products)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIViewController
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2021-08-07 11:57:22 +00:00
|
|
|
title = L10n.Purchase.title
|
2019-10-30 12:12:58 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-02-04 12:42:57 +00:00
|
|
|
|
|
|
|
private func purchasePlatformVersion() {
|
|
|
|
guard let sk = skPlatformVersion else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
purchase(sk)
|
|
|
|
}
|
2019-10-30 12:12:58 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-09-27 15:59:01 +00:00
|
|
|
let product = LocalProduct(rawValue: skProduct.productIdentifier)
|
2019-12-04 13:16:50 +00:00
|
|
|
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)
|
2021-08-07 11:57:22 +00:00
|
|
|
alert.addCancelAction(L10n.Global.ok)
|
2019-10-30 12:12:58 +00:00
|
|
|
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
|
|
|
|
|
2021-02-04 12:42:57 +00:00
|
|
|
case platformVersion
|
2019-10-30 12:12:58 +00:00
|
|
|
|
2021-02-04 12:42:57 +00:00
|
|
|
case fullVersion
|
|
|
|
|
2019-10-30 12:12:58 +00:00
|
|
|
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)
|
|
|
|
|
2021-02-04 12:42:57 +00:00
|
|
|
case .platformVersion:
|
|
|
|
guard let product = skPlatformVersion else {
|
|
|
|
fatalError("Loaded platform version cell, yet no corresponding product?")
|
|
|
|
}
|
|
|
|
cell.fill(product: product, customDescription: platformVersionExtra)
|
|
|
|
|
2019-10-30 12:12:58 +00:00
|
|
|
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(
|
2021-08-07 11:57:22 +00:00
|
|
|
title: L10n.Purchase.Cells.Restore.title,
|
|
|
|
description: L10n.Purchase.Cells.Restore.description
|
2019-10-30 12:12:58 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
|
2021-02-04 12:42:57 +00:00
|
|
|
case .platformVersion:
|
|
|
|
purchasePlatformVersion()
|
|
|
|
|
2019-10-30 12:12:58 +00:00
|
|
|
case .fullVersion:
|
|
|
|
purchaseFullVersion()
|
|
|
|
|
|
|
|
case .restore:
|
|
|
|
restorePurchases()
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 09:07:38 +00:00
|
|
|
}
|