Show activity when loading donations

This commit is contained in:
Davide De Rosa 2019-04-08 22:25:12 +02:00
parent 84c5493348
commit 1a7f2d745b
4 changed files with 49 additions and 9 deletions

View File

@ -279,6 +279,23 @@
</subviews> </subviews>
</tableViewCellContentView> </tableViewCellContentView>
</tableViewCell> </tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="ActivityTableViewCell" textLabel="h9G-iq-Crc" style="IBUITableViewCellStyleDefault" id="7c4-6U-inz" userLabel="ActivityTableViewCell" customClass="ActivityTableViewCell" customModule="Passepartout" customModuleProvider="target">
<rect key="frame" x="0.0" y="99.5" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="7c4-6U-inz" id="q8a-Vg-EsQ">
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="h9G-iq-Crc">
<rect key="frame" x="16" y="0.0" width="343" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
</tableViewCell>
</prototypes> </prototypes>
<connections> <connections>
<outlet property="dataSource" destination="SoF-ZH-NT1" id="Zei-ZZ-9w7"/> <outlet property="dataSource" destination="SoF-ZH-NT1" id="Zei-ZZ-9w7"/>

View File

@ -32,6 +32,8 @@ class DonationViewController: UITableViewController, TableModelHost {
private var productsByIdentifier: [String: SKProduct] = [:] private var productsByIdentifier: [String: SKProduct] = [:]
private var isLoading = true
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
@ -43,11 +45,20 @@ class DonationViewController: UITableViewController, TableModelHost {
// MARK: TableModel // MARK: TableModel
var model: TableModel<SectionType, RowType> = TableModel() var model: TableModel<SectionType, RowType> = TableModel()
func reloadModel() { func reloadModel() {
donationList = [] donationList = []
model.clear() model.clear()
model.add(.oneTime)
model.setHeader(L10n.Donation.Sections.OneTime.header, for: .oneTime)
model.setFooter(L10n.Donation.Sections.OneTime.footer, for: .oneTime)
guard !isLoading else {
model.set([.loading], in: .oneTime)
return
}
let completeList: [InApp.Donation] = [.tiny, .small, .medium, .big, .huge, .maxi] let completeList: [InApp.Donation] = [.tiny, .small, .medium, .big, .huge, .maxi]
for row in completeList { for row in completeList {
guard let _ = productsByIdentifier[row.rawValue] else { guard let _ = productsByIdentifier[row.rawValue] else {
@ -55,9 +66,6 @@ class DonationViewController: UITableViewController, TableModelHost {
} }
donationList.append(row) donationList.append(row)
} }
model.add(.oneTime)
model.setHeader(L10n.Donation.Sections.OneTime.header, for: .oneTime)
model.setFooter(L10n.Donation.Sections.OneTime.footer, for: .oneTime)
model.set(.donation, count: donationList.count, in: .oneTime) model.set(.donation, count: donationList.count, in: .oneTime)
} }
@ -67,16 +75,16 @@ class DonationViewController: UITableViewController, TableModelHost {
super.viewDidLoad() super.viewDidLoad()
title = L10n.Donation.title title = L10n.Donation.title
reloadModel()
let inApp = InAppHelper.shared let inApp = InAppHelper.shared
if inApp.products.isEmpty { if inApp.products.isEmpty {
let hud = HUD()
hud.show()
inApp.requestProducts { inApp.requestProducts {
hud.hide() self.isLoading = false
self.setProducts($0) self.setProducts($0)
} }
} else { } else {
isLoading = false
setProducts(inApp.products) setProducts(inApp.products)
} }
} }
@ -105,6 +113,11 @@ class DonationViewController: UITableViewController, TableModelHost {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch model.row(at: indexPath) { switch model.row(at: indexPath) {
case .loading:
let cell = Cells.activity.dequeue(from: tableView, for: indexPath)
cell.textLabel?.text = L10n.Donation.Cells.Loading.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 {
@ -129,6 +142,9 @@ class DonationViewController: UITableViewController, TableModelHost {
InAppHelper.shared.purchase(product: product) { InAppHelper.shared.purchase(product: product) {
self.handlePurchase(result: $0, error: $1) self.handlePurchase(result: $0, error: $1)
} }
default:
break
} }
} }
@ -151,12 +167,12 @@ class DonationViewController: UITableViewController, TableModelHost {
extension DonationViewController { extension DonationViewController {
enum SectionType { enum SectionType {
case inProgress
case oneTime case oneTime
} }
enum RowType { enum RowType {
case loading
case donation case donation
} }

View File

@ -251,6 +251,7 @@
"donation.sections.one_time.header" = "One time"; "donation.sections.one_time.header" = "One time";
"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.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

@ -314,6 +314,12 @@ public enum L10n {
} }
} }
} }
public enum Cells {
public enum Loading {
/// Loading donations
public static let caption = L10n.tr("Localizable", "donation.cells.loading.caption")
}
}
public enum Sections { public enum Sections {
public enum OneTime { public enum OneTime {
/// 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. /// 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.