passepartout-apple/Passepartout-iOS/Scenes/Organizer/DonationViewController.swift

213 lines
6.6 KiB
Swift
Raw Normal View History

2019-04-06 21:58:19 +00:00
//
// DonationViewController.swift
// Passepartout-iOS
//
// Created by Davide De Rosa on 4/6/19.
// Copyright (c) 2019 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 UIKit
import StoreKit
import PassepartoutCore
import Convenience
import SwiftyBeaver
private let log = SwiftyBeaver.self
2019-04-06 21:58:19 +00:00
class DonationViewController: UITableViewController, StrongTableHost {
private var donationList: [Product] = []
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()
}
// MARK: StrongTableModel
2019-04-06 21:58:19 +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() {
donationList = []
2019-04-06 21:58:19 +00:00
model.clear()
2019-04-08 20:25:12 +00:00
model.add(.oneTime)
model.setHeader(L10n.Core.Donation.Sections.OneTime.header, forSection: .oneTime)
model.setFooter(L10n.Core.Donation.Sections.OneTime.footer, forSection: .oneTime)
2019-04-08 20:25:12 +00:00
guard !isLoading else {
model.set([.loading], forSection: .oneTime)
2019-04-08 20:25:12 +00:00
return
}
donationList.append(contentsOf: Product.allDonations.filter { productsByIdentifier[$0.rawValue] != nil })
model.set(.donation, count: donationList.count, forSection: .oneTime)
2019-04-08 20:53:00 +00:00
if isPurchasing {
model.add(.activity)
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()
title = L10n.Core.Donation.title
2019-04-08 20:25:12 +00:00
reloadModel()
2019-04-06 21:58:19 +00:00
ProductManager.shared.listProducts {
self.isLoading = false
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 {
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? {
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? {
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 {
return model.numberOfRows(forSection: section)
2019-04-06 21:58:19 +00:00
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch model.row(at: indexPath) {
2019-04-08 20:25:12 +00:00
case .loading:
let cell = Cells.activity.dequeue(from: tableView, for: indexPath)
cell.textLabel?.text = L10n.Core.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)
cell.textLabel?.text = L10n.Core.Donation.Cells.Purchasing.caption
2019-04-08 20:53:00 +00:00
return cell
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
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) {
switch model.row(at: indexPath) {
case .donation:
2019-04-08 20:53:00 +00:00
guard !isPurchasing else {
return
}
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()
ProductManager.shared.purchase(product) {
self.handlePurchase(result: $0, error: $1)
}
2019-04-08 20:25:12 +00:00
default:
break
2019-04-07 10:21:26 +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:
alert = UIAlertController.asAlert(L10n.Core.Donation.Alerts.Purchase.Success.title, L10n.Core.Donation.Alerts.Purchase.Success.message)
2019-04-07 10:21:26 +00:00
case .failure:
alert = UIAlertController.asAlert(title, L10n.Core.Donation.Alerts.Purchase.Failure.message(error?.localizedDescription ?? ""))
2019-04-07 10:21:26 +00:00
}
alert.addCancelAction(L10n.Core.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
}
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
case donation
2019-04-06 21:58:19 +00:00
}
private func productIdentifier(at indexPath: IndexPath) -> String {
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
}
}