2019-04-06 21:58:19 +00:00
|
|
|
//
|
|
|
|
// DonationViewController.swift
|
2021-01-01 16:53:28 +00:00
|
|
|
// Passepartout
|
2019-04-06 21:58:19 +00:00
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 4/6/19.
|
2020-12-27 16:30:25 +00:00
|
|
|
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
|
2019-04-06 21:58:19 +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
|
|
|
|
import StoreKit
|
2021-10-27 16:47:08 +00:00
|
|
|
import SwiftyBeaver
|
2019-05-26 06:56:39 +00:00
|
|
|
import PassepartoutCore
|
2019-10-11 08:56:23 +00:00
|
|
|
import Convenience
|
2021-10-27 16:47:08 +00:00
|
|
|
import ConvenienceUI
|
2019-11-30 10:42:21 +00:00
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
2019-04-06 21:58:19 +00:00
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
class DonationViewController: UITableViewController, StrongTableHost {
|
2021-09-27 15:59:01 +00:00
|
|
|
private var donationList: [LocalProduct] = []
|
2019-04-08 20:12:44 +00:00
|
|
|
|
2019-04-06 21:58:19 +00:00
|
|
|
private var productsByIdentifier: [String: SKProduct] = [:]
|
|
|
|
|
2019-04-08 20:25:12 +00:00
|
|
|
private var isLoading = true
|
|
|
|
|
2019-04-08 20:53:00 +00:00
|
|
|
private var isPurchasing = false
|
|
|
|
|
2019-04-06 21:58:19 +00:00
|
|
|
private func setProducts(_ products: [SKProduct]) {
|
|
|
|
for p in products {
|
|
|
|
productsByIdentifier[p.productIdentifier] = p
|
|
|
|
}
|
|
|
|
reloadModel()
|
|
|
|
tableView.reloadData()
|
|
|
|
}
|
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
// MARK: StrongTableModel
|
2019-04-06 21:58:19 +00:00
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
var model: StrongTableModel<SectionType, RowType> = StrongTableModel()
|
2019-04-08 20:25:12 +00:00
|
|
|
|
2019-04-06 21:58:19 +00:00
|
|
|
func reloadModel() {
|
2019-04-08 20:12:44 +00:00
|
|
|
donationList = []
|
2019-04-06 21:58:19 +00:00
|
|
|
model.clear()
|
|
|
|
|
2019-04-08 20:25:12 +00:00
|
|
|
model.add(.oneTime)
|
2021-08-07 11:57:22 +00:00
|
|
|
model.setHeader(L10n.Donation.Sections.OneTime.header, forSection: .oneTime)
|
|
|
|
model.setFooter(L10n.Donation.Sections.OneTime.footer, forSection: .oneTime)
|
2019-04-08 20:25:12 +00:00
|
|
|
|
|
|
|
guard !isLoading else {
|
2019-10-11 08:56:23 +00:00
|
|
|
model.set([.loading], forSection: .oneTime)
|
2019-04-08 20:25:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-27 15:59:01 +00:00
|
|
|
donationList.append(contentsOf: LocalProduct.allDonations.filter { productsByIdentifier[$0.rawValue] != nil })
|
2019-10-11 08:56:23 +00:00
|
|
|
model.set(.donation, count: donationList.count, forSection: .oneTime)
|
2019-04-08 20:53:00 +00:00
|
|
|
|
|
|
|
if isPurchasing {
|
|
|
|
model.add(.activity)
|
2019-10-11 08:56:23 +00:00
|
|
|
model.set([.purchasing], forSection: .activity)
|
2019-04-08 20:53:00 +00:00
|
|
|
}
|
2019-04-06 21:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIViewController
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2021-08-07 11:57:22 +00:00
|
|
|
title = L10n.Donation.title
|
2019-04-08 20:25:12 +00:00
|
|
|
reloadModel()
|
2019-04-06 21:58:19 +00:00
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
ProductManager.shared.listProducts {
|
|
|
|
self.isLoading = false
|
2019-11-30 10:42:21 +00:00
|
|
|
guard let products = $0 else {
|
|
|
|
log.error("Unable to list products: \($1?.localizedDescription ?? "")")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self.setProducts(products)
|
2019-04-06 22:36:44 +00:00
|
|
|
}
|
2019-04-06 21:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction private func close() {
|
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UITableViewController
|
|
|
|
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.numberOfSections
|
2019-04-06 21:58:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 22:41:00 +00:00
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.header(forSection: section)
|
2019-04-06 22:41:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 10:21:26 +00:00
|
|
|
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.footer(forSection: section)
|
2019-04-07 10:21:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 21:58:19 +00:00
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.numberOfRows(forSection: section)
|
2019-04-06 21:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
2019-04-08 20:12:44 +00:00
|
|
|
switch model.row(at: indexPath) {
|
2019-04-08 20:25:12 +00:00
|
|
|
case .loading:
|
|
|
|
let cell = Cells.activity.dequeue(from: tableView, for: indexPath)
|
2021-08-07 11:57:22 +00:00
|
|
|
cell.textLabel?.text = L10n.Donation.Cells.Loading.caption
|
2019-04-08 20:25:12 +00:00
|
|
|
return cell
|
|
|
|
|
2019-04-08 20:53:00 +00:00
|
|
|
case .purchasing:
|
|
|
|
let cell = Cells.activity.dequeue(from: tableView, for: indexPath)
|
2021-08-07 11:57:22 +00:00
|
|
|
cell.textLabel?.text = L10n.Donation.Cells.Purchasing.caption
|
2019-04-08 20:53:00 +00:00
|
|
|
return cell
|
|
|
|
|
2019-04-08 20:12:44 +00:00
|
|
|
case .donation:
|
|
|
|
let productId = productIdentifier(at: indexPath)
|
|
|
|
guard let product = productsByIdentifier[productId] else {
|
|
|
|
fatalError("Row with no associated product")
|
|
|
|
}
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.leftText = product.localizedTitle
|
|
|
|
cell.rightText = product.localizedPrice
|
2019-04-08 20:53:00 +00:00
|
|
|
cell.isTappable = !isPurchasing
|
2019-04-08 20:12:44 +00:00
|
|
|
return cell
|
2019-04-06 21:58:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-07 10:21:26 +00:00
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2019-04-08 20:12:44 +00:00
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .donation:
|
2019-04-08 20:53:00 +00:00
|
|
|
guard !isPurchasing else {
|
|
|
|
return
|
|
|
|
}
|
2019-04-08 20:12:44 +00:00
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
let productId = productIdentifier(at: indexPath)
|
|
|
|
guard let product = productsByIdentifier[productId] else {
|
|
|
|
fatalError("Row with no associated product")
|
|
|
|
}
|
2019-04-08 20:53:00 +00:00
|
|
|
|
|
|
|
isPurchasing = true
|
|
|
|
reloadModel()
|
|
|
|
tableView.reloadData()
|
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
ProductManager.shared.purchase(product) {
|
2019-04-08 20:12:44 +00:00
|
|
|
self.handlePurchase(result: $0, error: $1)
|
|
|
|
}
|
2019-04-08 20:25:12 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
2019-04-07 10:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
private func handlePurchase(result: InAppPurchaseResult, error: Error?) {
|
2019-04-07 10:21:26 +00:00
|
|
|
let alert: UIAlertController
|
|
|
|
switch result {
|
|
|
|
case .cancelled:
|
2019-04-08 20:53:00 +00:00
|
|
|
isPurchasing = false
|
|
|
|
reloadModel()
|
|
|
|
tableView.reloadData()
|
2019-04-07 10:21:26 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
case .success:
|
2021-08-07 11:57:22 +00:00
|
|
|
alert = UIAlertController.asAlert(L10n.Donation.Alerts.Purchase.Success.title, L10n.Donation.Alerts.Purchase.Success.message)
|
2019-04-07 10:21:26 +00:00
|
|
|
|
|
|
|
case .failure:
|
2021-08-07 11:57:22 +00:00
|
|
|
alert = UIAlertController.asAlert(title, L10n.Donation.Alerts.Purchase.Failure.message(error?.localizedDescription ?? ""))
|
2019-04-07 10:21:26 +00:00
|
|
|
}
|
2021-08-07 11:57:22 +00:00
|
|
|
alert.addCancelAction(L10n.Global.ok) {
|
2019-04-08 20:53:00 +00:00
|
|
|
self.isPurchasing = false
|
|
|
|
self.reloadModel()
|
|
|
|
self.tableView.reloadData()
|
|
|
|
}
|
|
|
|
present(alert, animated: true)
|
2019-04-07 10:21:26 +00:00
|
|
|
}
|
2019-04-06 21:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension DonationViewController {
|
|
|
|
enum SectionType {
|
|
|
|
case oneTime
|
2019-04-08 20:53:00 +00:00
|
|
|
|
|
|
|
case activity
|
2019-04-08 20:12:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum RowType {
|
2019-04-08 20:25:12 +00:00
|
|
|
case loading
|
2019-04-08 20:53:00 +00:00
|
|
|
|
|
|
|
case purchasing
|
2019-04-08 20:25:12 +00:00
|
|
|
|
2019-04-08 20:12:44 +00:00
|
|
|
case donation
|
2019-04-06 21:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func productIdentifier(at indexPath: IndexPath) -> String {
|
2019-04-08 20:12:44 +00:00
|
|
|
guard model.row(at: indexPath) == .donation else {
|
|
|
|
fatalError("Not a donation row")
|
|
|
|
}
|
|
|
|
return donationList[indexPath.row].rawValue
|
2019-04-06 21:58:19 +00:00
|
|
|
}
|
|
|
|
}
|