2021-02-02 19:28:29 +00:00
|
|
|
//
|
|
|
|
// PurchaseViewController.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/2/21.
|
|
|
|
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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 Cocoa
|
|
|
|
import StoreKit
|
2021-10-27 16:47:08 +00:00
|
|
|
import PassepartoutConstants
|
2021-02-02 19:28:29 +00:00
|
|
|
import PassepartoutCore
|
|
|
|
import SwiftyBeaver
|
|
|
|
import Convenience
|
|
|
|
|
|
|
|
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?)
|
2021-02-02 19:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class PurchaseViewController: NSViewController {
|
|
|
|
private struct Columns {
|
|
|
|
static let product = NSUserInterfaceItemIdentifier("ProductCellIdentifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBOutlet private weak var tableView: NSTableView!
|
|
|
|
|
|
|
|
@IBOutlet private weak var labelFooter: NSTextField!
|
|
|
|
|
|
|
|
@IBOutlet private weak var labelRestore: NSTextField!
|
|
|
|
|
|
|
|
@IBOutlet private weak var activityPurchase: NSProgressIndicator!
|
|
|
|
|
|
|
|
@IBOutlet private weak var buttonPurchase: NSButton!
|
|
|
|
|
|
|
|
@IBOutlet private weak var buttonRestore: NSButton!
|
|
|
|
|
2021-09-27 15:59:01 +00:00
|
|
|
var feature: LocalProduct?
|
2021-02-02 19:28:29 +00:00
|
|
|
|
|
|
|
weak var delegate: PurchaseViewControllerDelegate?
|
|
|
|
|
|
|
|
private var skFeature: SKProduct?
|
|
|
|
|
2021-02-04 12:42:57 +00:00
|
|
|
private var skPlatformVersion: SKProduct?
|
|
|
|
|
2021-02-02 19:28:29 +00:00
|
|
|
private var skFullVersion: SKProduct?
|
|
|
|
|
2021-02-04 12:42:57 +00:00
|
|
|
private var platformVersionExtra: String?
|
|
|
|
|
2021-02-02 19:28:29 +00:00
|
|
|
private var fullVersionExtra: String?
|
|
|
|
|
|
|
|
var rows: [RowType] = []
|
|
|
|
|
|
|
|
func reloadModel() {
|
|
|
|
rows = []
|
|
|
|
let pm = ProductManager.shared
|
2021-02-04 12:42:57 +00:00
|
|
|
if let skPlatformVersion = pm.product(withIdentifier: .fullVersion_macOS) {
|
|
|
|
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, .siriShortcuts]).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_iOS), let skFullVersion = pm.product(withIdentifier: .fullVersion) {
|
2021-02-02 19:28:29 +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")
|
2021-02-02 19:28:29 +00:00
|
|
|
}
|
2021-02-07 10:16:57 +00:00
|
|
|
if let feature = feature, let skFeature = pm.product(withIdentifier: feature) {
|
2021-02-02 19:28:29 +00:00
|
|
|
self.skFeature = skFeature
|
|
|
|
rows.append(.feature)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: NSViewController
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2021-08-07 11:57:22 +00:00
|
|
|
title = L10n.Purchase.title
|
|
|
|
labelFooter.stringValue = L10n.Purchase.Sections.Products.footer
|
|
|
|
labelRestore.stringValue = L10n.Purchase.Cells.Restore.description
|
|
|
|
buttonPurchase.title = L10n.Purchase.title
|
|
|
|
buttonRestore.title = L10n.Purchase.Cells.Restore.title
|
2021-02-02 19:28:29 +00:00
|
|
|
|
|
|
|
tableView.usesAutomaticRowHeights = true
|
|
|
|
tableView.reloadData()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear() {
|
|
|
|
super.viewWillAppear()
|
|
|
|
|
|
|
|
view.window?.styleMask = [.closable, .titled]
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidAppear() {
|
|
|
|
super.viewDidAppear()
|
|
|
|
|
|
|
|
startWaiting()
|
|
|
|
ProductManager.shared.listProducts { [weak self] (_, _) in
|
|
|
|
self?.reloadModel()
|
|
|
|
self?.tableView.reloadData()
|
|
|
|
self?.stopWaiting()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@IBAction private func doPurchase(_ sender: Any) {
|
|
|
|
guard tableView.selectedRow != -1 else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch rows[tableView.selectedRow] {
|
|
|
|
case .feature:
|
|
|
|
purchaseFeature()
|
|
|
|
|
2021-02-04 12:42:57 +00:00
|
|
|
case .platformVersion:
|
|
|
|
purchasePlatformVersion()
|
|
|
|
|
2021-02-02 19:28:29 +00:00
|
|
|
case .fullVersion:
|
|
|
|
purchaseFullVersion()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction private func doRestorePurchases(_ sender: Any) {
|
|
|
|
startWaiting()
|
|
|
|
ProductManager.shared.restorePurchases { [weak self] in
|
|
|
|
self?.stopWaiting()
|
|
|
|
guard $0 == nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self?.dismiss(nil)
|
|
|
|
}
|
|
|
|
}
|
2021-02-11 22:40:16 +00:00
|
|
|
|
2021-02-02 19:28:29 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-02-02 19:28:29 +00:00
|
|
|
private func purchaseFullVersion() {
|
|
|
|
guard let sk = skFullVersion else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
purchase(sk)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func purchase(_ skProduct: SKProduct) {
|
|
|
|
startWaiting()
|
|
|
|
ProductManager.shared.purchase(skProduct) { [weak self] in
|
|
|
|
self?.stopWaiting()
|
|
|
|
guard $0 == .success else {
|
|
|
|
if let error = $1 {
|
|
|
|
self?.reportPurchaseError(withProduct: skProduct, error: error)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let weakSelf = self else {
|
|
|
|
return
|
|
|
|
}
|
2021-09-27 15:59:01 +00:00
|
|
|
let product = LocalProduct(rawValue: skProduct.productIdentifier)
|
2021-02-02 19:28:29 +00:00
|
|
|
weakSelf.delegate?.purchaseController(weakSelf, didPurchase: product)
|
|
|
|
|
|
|
|
self?.dismiss(nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func reportPurchaseError(withProduct product: SKProduct, error: Error) {
|
|
|
|
log.error("Unable to purchase \(product): \(error)")
|
|
|
|
|
|
|
|
let alert = Macros.warning(product.localizedTitle, error.localizedDescription)
|
2021-08-07 11:57:22 +00:00
|
|
|
_ = alert.presentModally(withOK: L10n.Global.ok, cancel: nil)
|
2021-02-02 19:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func close() {
|
|
|
|
dismiss(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Helpers
|
|
|
|
|
|
|
|
private func startWaiting() {
|
|
|
|
tableView.isEnabled = false
|
|
|
|
buttonPurchase.isEnabled = false
|
|
|
|
buttonRestore.isEnabled = false
|
|
|
|
activityPurchase.isHidden = false
|
|
|
|
activityPurchase.startAnimation(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func stopWaiting() {
|
|
|
|
activityPurchase.stopAnimation(nil)
|
|
|
|
tableView.isEnabled = true
|
|
|
|
buttonPurchase.isEnabled = true
|
|
|
|
buttonRestore.isEnabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension PurchaseViewController: NSTableViewDataSource, NSTableViewDelegate {
|
|
|
|
enum RowType {
|
|
|
|
case feature
|
|
|
|
|
2021-02-04 12:42:57 +00:00
|
|
|
case platformVersion
|
|
|
|
|
2021-02-02 19:28:29 +00:00
|
|
|
case fullVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
func numberOfRows(in tableView: NSTableView) -> Int {
|
|
|
|
return rows.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
|
|
|
|
guard let view = tableView.makeView(withIdentifier: Columns.product, owner: nil) as? PurchaseProductView else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch rows[row] {
|
|
|
|
case .feature:
|
|
|
|
guard let product = skFeature else {
|
|
|
|
fatalError("Loaded feature cell, yet no corresponding product?")
|
|
|
|
}
|
|
|
|
view.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?")
|
|
|
|
}
|
|
|
|
view.fill(product: product, customDescription: platformVersionExtra)
|
|
|
|
|
2021-02-02 19:28:29 +00:00
|
|
|
case .fullVersion:
|
|
|
|
guard let product = skFullVersion else {
|
|
|
|
fatalError("Loaded full version cell, yet no corresponding product?")
|
|
|
|
}
|
|
|
|
view.fill(product: product, customDescription: fullVersionExtra)
|
|
|
|
}
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
}
|