Remap donations to local RowType

Allow different cells easily.
This commit is contained in:
Davide De Rosa 2019-04-08 22:12:44 +02:00
parent bb299335ad
commit 84c5493348
1 changed files with 37 additions and 24 deletions

View File

@ -28,6 +28,8 @@ import StoreKit
import Passepartout_Core
class DonationViewController: UITableViewController, TableModelHost {
private var donationList: [InApp.Donation] = []
private var productsByIdentifier: [String: SKProduct] = [:]
private func setProducts(_ products: [SKProduct]) {
@ -40,25 +42,23 @@ class DonationViewController: UITableViewController, TableModelHost {
// MARK: TableModel
var model: TableModel<SectionType, InApp.Donation> = TableModel()
var model: TableModel<SectionType, RowType> = TableModel()
func reloadModel() {
donationList = []
model.clear()
let completeList: [InApp.Donation] = [.tiny, .small, .medium, .big, .huge, .maxi]
var list: [InApp.Donation] = []
for row in completeList {
guard let _ = productsByIdentifier[row.rawValue] else {
continue
}
list.append(row)
donationList.append(row)
}
model.add(.oneTime)
// model.add(.recurring)
model.setHeader(L10n.Donation.Sections.OneTime.header, for: .oneTime)
model.setFooter(L10n.Donation.Sections.OneTime.footer, for: .oneTime)
// model.setHeader(L10n.Donation.Sections.Recurring.header, for: .recurring)
model.set(list, in: .oneTime)
model.set(.donation, count: donationList.count, in: .oneTime)
}
// MARK: UIViewController
@ -104,6 +104,8 @@ class DonationViewController: UITableViewController, TableModelHost {
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch model.row(at: indexPath) {
case .donation:
let productId = productIdentifier(at: indexPath)
guard let product = productsByIdentifier[productId] else {
fatalError("Row with no associated product")
@ -113,8 +115,11 @@ class DonationViewController: UITableViewController, TableModelHost {
cell.rightText = product.localizedPrice
return cell
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch model.row(at: indexPath) {
case .donation:
tableView.deselectRow(at: indexPath, animated: true)
let productId = productIdentifier(at: indexPath)
@ -125,6 +130,7 @@ class DonationViewController: UITableViewController, TableModelHost {
self.handlePurchase(result: $0, error: $1)
}
}
}
private func handlePurchase(result: InAppHelper.PurchaseResult, error: Error?) {
let alert: UIAlertController
@ -145,12 +151,19 @@ class DonationViewController: UITableViewController, TableModelHost {
extension DonationViewController {
enum SectionType {
case oneTime
case inProgress
case recurring
case oneTime
}
enum RowType {
case donation
}
private func productIdentifier(at indexPath: IndexPath) -> String {
return model.row(at: indexPath).rawValue
guard model.row(at: indexPath) == .donation else {
fatalError("Not a donation row")
}
return donationList[indexPath.row].rawValue
}
}