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

217 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
2019-04-06 21:58:19 +00:00
class DonationViewController: UITableViewController, TableModelHost {
private var donationList: [InApp.Donation] = []
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: TableModel
var model: TableModel<SectionType, RowType> = TableModel()
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, for: .oneTime)
model.setFooter(L10n.Core.Donation.Sections.OneTime.footer, for: .oneTime)
2019-04-08 20:25:12 +00:00
guard !isLoading else {
model.set([.loading], in: .oneTime)
return
}
2019-04-06 21:58:19 +00:00
let completeList: [InApp.Donation] = [.tiny, .small, .medium, .big, .huge, .maxi]
for row in completeList {
guard let _ = productsByIdentifier[row.rawValue] else {
continue
}
donationList.append(row)
2019-04-06 21:58:19 +00:00
}
model.set(.donation, count: donationList.count, in: .oneTime)
2019-04-08 20:53:00 +00:00
if isPurchasing {
model.add(.activity)
model.set([.purchasing], in: .activity)
}
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
2019-04-06 22:36:44 +00:00
let inApp = InAppHelper.shared
if inApp.products.isEmpty {
2019-04-07 10:53:30 +00:00
inApp.requestProducts {
2019-04-08 20:25:12 +00:00
self.isLoading = false
2019-04-07 10:53:30 +00:00
self.setProducts($0)
}
2019-04-06 22:36:44 +00:00
} else {
2019-04-08 20:25:12 +00:00
isLoading = false
2019-04-06 22:36:44 +00:00
setProducts(inApp.products)
}
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.count
}
2019-04-06 22:41:00 +00:00
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return model.header(for: section)
}
2019-04-07 10:21:26 +00:00
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return model.footer(for: section)
}
2019-04-06 21:58:19 +00:00
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.count(for: section)
}
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()
InAppHelper.shared.purchase(product: 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: InAppHelper.PurchaseResult, error: Error?) {
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 = Macros.alert(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 = Macros.alert(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
}
}