Show activity when purchasing donation

This commit is contained in:
Davide De Rosa 2019-04-08 22:53:00 +02:00
parent 1a7f2d745b
commit db6aa10590
3 changed files with 39 additions and 3 deletions

View File

@ -34,6 +34,8 @@ class DonationViewController: UITableViewController, TableModelHost {
private var isLoading = true private var isLoading = true
private var isPurchasing = false
private func setProducts(_ products: [SKProduct]) { private func setProducts(_ products: [SKProduct]) {
for p in products { for p in products {
productsByIdentifier[p.productIdentifier] = p productsByIdentifier[p.productIdentifier] = p
@ -67,6 +69,11 @@ class DonationViewController: UITableViewController, TableModelHost {
donationList.append(row) donationList.append(row)
} }
model.set(.donation, count: donationList.count, in: .oneTime) model.set(.donation, count: donationList.count, in: .oneTime)
if isPurchasing {
model.add(.activity)
model.set([.purchasing], in: .activity)
}
} }
// MARK: UIViewController // MARK: UIViewController
@ -118,6 +125,11 @@ class DonationViewController: UITableViewController, TableModelHost {
cell.textLabel?.text = L10n.Donation.Cells.Loading.caption cell.textLabel?.text = L10n.Donation.Cells.Loading.caption
return cell return cell
case .purchasing:
let cell = Cells.activity.dequeue(from: tableView, for: indexPath)
cell.textLabel?.text = L10n.Donation.Cells.Purchasing.caption
return cell
case .donation: case .donation:
let productId = productIdentifier(at: indexPath) let productId = productIdentifier(at: indexPath)
guard let product = productsByIdentifier[productId] else { guard let product = productsByIdentifier[productId] else {
@ -126,6 +138,7 @@ class DonationViewController: UITableViewController, TableModelHost {
let cell = Cells.setting.dequeue(from: tableView, for: indexPath) let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
cell.leftText = product.localizedTitle cell.leftText = product.localizedTitle
cell.rightText = product.localizedPrice cell.rightText = product.localizedPrice
cell.isTappable = !isPurchasing
return cell return cell
} }
} }
@ -133,12 +146,19 @@ class DonationViewController: UITableViewController, TableModelHost {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch model.row(at: indexPath) { switch model.row(at: indexPath) {
case .donation: case .donation:
guard !isPurchasing else {
return
}
tableView.deselectRow(at: indexPath, animated: true) tableView.deselectRow(at: indexPath, animated: true)
let productId = productIdentifier(at: indexPath) let productId = productIdentifier(at: indexPath)
guard let product = productsByIdentifier[productId] else { guard let product = productsByIdentifier[productId] else {
fatalError("Row with no associated product") fatalError("Row with no associated product")
} }
isPurchasing = true
reloadModel()
tableView.reloadData()
InAppHelper.shared.purchase(product: product) { InAppHelper.shared.purchase(product: product) {
self.handlePurchase(result: $0, error: $1) self.handlePurchase(result: $0, error: $1)
} }
@ -152,6 +172,9 @@ class DonationViewController: UITableViewController, TableModelHost {
let alert: UIAlertController let alert: UIAlertController
switch result { switch result {
case .cancelled: case .cancelled:
isPurchasing = false
reloadModel()
tableView.reloadData()
return return
case .success: case .success:
@ -160,19 +183,27 @@ class DonationViewController: UITableViewController, TableModelHost {
case .failure: case .failure:
alert = Macros.alert(title, L10n.Donation.Alerts.Purchase.Failure.message(error?.localizedDescription ?? "")) alert = Macros.alert(title, L10n.Donation.Alerts.Purchase.Failure.message(error?.localizedDescription ?? ""))
} }
alert.addCancelAction(L10n.Global.ok) alert.addCancelAction(L10n.Global.ok) {
present(alert, animated: true, completion: nil) self.isPurchasing = false
self.reloadModel()
self.tableView.reloadData()
}
present(alert, animated: true)
} }
} }
extension DonationViewController { extension DonationViewController {
enum SectionType { enum SectionType {
case oneTime case oneTime
case activity
} }
enum RowType { enum RowType {
case loading case loading
case purchasing
case donation case donation
} }

View File

@ -252,6 +252,7 @@
"donation.sections.one_time.footer" = "If you want to display gratitude for my free work, here are a couple amounts you can donate instantly.\n\nYou will only be charged once per donation, and you can donate multiple times."; "donation.sections.one_time.footer" = "If you want to display gratitude for my free work, here are a couple amounts you can donate instantly.\n\nYou will only be charged once per donation, and you can donate multiple times.";
//"donation.sections.recurring.header" = "Recurring"; //"donation.sections.recurring.header" = "Recurring";
"donation.cells.loading.caption" = "Loading donations"; "donation.cells.loading.caption" = "Loading donations";
"donation.cells.purchasing.caption" = "Performing donation";
"donation.alerts.purchase.success.title" = "Thank you"; "donation.alerts.purchase.success.title" = "Thank you";
"donation.alerts.purchase.success.message" = "This means a lot to me and I really hope you keep using and promoting this app."; "donation.alerts.purchase.success.message" = "This means a lot to me and I really hope you keep using and promoting this app.";
"donation.alerts.purchase.failure.message" = "Unable to perform the donation. %@"; "donation.alerts.purchase.failure.message" = "Unable to perform the donation. %@";

View File

@ -319,6 +319,10 @@ public enum L10n {
/// Loading donations /// Loading donations
public static let caption = L10n.tr("Localizable", "donation.cells.loading.caption") public static let caption = L10n.tr("Localizable", "donation.cells.loading.caption")
} }
public enum Purchasing {
/// Performing donation
public static let caption = L10n.tr("Localizable", "donation.cells.purchasing.caption")
}
} }
public enum Sections { public enum Sections {
public enum OneTime { public enum OneTime {