Merge branch 'donations-progress-indicators'

This commit is contained in:
Davide De Rosa 2019-04-08 23:06:04 +02:00
commit 0a82d2bcaa
9 changed files with 186 additions and 33 deletions

View File

@ -279,6 +279,23 @@
</subviews>
</tableViewCellContentView>
</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>
<connections>
<outlet property="dataSource" destination="SoF-ZH-NT1" id="Zei-ZZ-9w7"/>

View File

@ -0,0 +1,49 @@
//
// ActivityTableViewCell.swift
// Passepartout-iOS
//
// Created by Davide De Rosa on 4/8/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
extension Cells {
static let activity = ActivityTableViewCell.Provider()
}
class ActivityTableViewCell: UITableViewCell {
private lazy var activityIndicator = UIActivityIndicatorView()
}
extension ActivityTableViewCell {
class Provider: CellProvider {
typealias T = ActivityTableViewCell
func dequeue(from tableView: UITableView, for indexPath: IndexPath) -> ActivityTableViewCell {
let cell = tableView.dequeue(T.self, identifier: Provider.identifier, for: indexPath)
cell.apply(Theme.current)
cell.activityIndicator.startAnimating()
cell.accessoryView = cell.activityIndicator
cell.selectionStyle = .none
return cell
}
}
}

View File

@ -61,6 +61,13 @@ extension ToggleTableViewCell {
}
}
extension ActivityTableViewCell {
func apply(_ theme: Theme) {
textLabel?.text = nil
detailTextLabel?.text = nil
}
}
extension SettingTableViewCell {
func applyAction(_ theme: Theme) {
leftTextColor = theme.palette.action

View File

@ -102,6 +102,9 @@ extension Theme {
let toggle = UISwitch.appearance()
toggle.onTintColor = palette.accessory
let activity = UIActivityIndicatorView.appearance()
activity.color = palette.accessory
}
}

View File

@ -28,8 +28,14 @@ import StoreKit
import Passepartout_Core
class DonationViewController: UITableViewController, TableModelHost {
private var donationList: [InApp.Donation] = []
private var productsByIdentifier: [String: SKProduct] = [:]
private var isLoading = true
private var isPurchasing = false
private func setProducts(_ products: [SKProduct]) {
for p in products {
productsByIdentifier[p.productIdentifier] = p
@ -40,25 +46,34 @@ class DonationViewController: UITableViewController, TableModelHost {
// MARK: TableModel
var model: TableModel<SectionType, InApp.Donation> = TableModel()
var model: TableModel<SectionType, RowType> = TableModel()
func reloadModel() {
donationList = []
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]
var list: [InApp.Donation] = []
for row in completeList {
guard let _ = productsByIdentifier[row.rawValue] else {
continue
}
list.append(row)
donationList.append(row)
}
model.set(.donation, count: donationList.count, in: .oneTime)
if isPurchasing {
model.add(.activity)
model.set([.purchasing], in: .activity)
}
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)
}
// MARK: UIViewController
@ -67,16 +82,16 @@ class DonationViewController: UITableViewController, TableModelHost {
super.viewDidLoad()
title = L10n.Donation.title
reloadModel()
let inApp = InAppHelper.shared
if inApp.products.isEmpty {
let hud = HUD()
hud.show()
inApp.requestProducts {
hud.hide()
self.isLoading = false
self.setProducts($0)
}
} else {
isLoading = false
setProducts(inApp.products)
}
}
@ -104,25 +119,52 @@ class DonationViewController: UITableViewController, TableModelHost {
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let productId = productIdentifier(at: indexPath)
guard let product = productsByIdentifier[productId] else {
fatalError("Row with no associated product")
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 .purchasing:
let cell = Cells.activity.dequeue(from: tableView, for: indexPath)
cell.textLabel?.text = L10n.Donation.Cells.Purchasing.caption
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
cell.isTappable = !isPurchasing
return cell
}
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
cell.leftText = product.localizedTitle
cell.rightText = product.localizedPrice
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let productId = productIdentifier(at: indexPath)
guard let product = productsByIdentifier[productId] else {
fatalError("Row with no associated product")
}
InAppHelper.shared.purchase(product: product) {
self.handlePurchase(result: $0, error: $1)
switch model.row(at: indexPath) {
case .donation:
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")
}
isPurchasing = true
reloadModel()
tableView.reloadData()
InAppHelper.shared.purchase(product: product) {
self.handlePurchase(result: $0, error: $1)
}
default:
break
}
}
@ -130,6 +172,9 @@ class DonationViewController: UITableViewController, TableModelHost {
let alert: UIAlertController
switch result {
case .cancelled:
isPurchasing = false
reloadModel()
tableView.reloadData()
return
case .success:
@ -138,19 +183,34 @@ class DonationViewController: UITableViewController, TableModelHost {
case .failure:
alert = Macros.alert(title, L10n.Donation.Alerts.Purchase.Failure.message(error?.localizedDescription ?? ""))
}
alert.addCancelAction(L10n.Global.ok)
present(alert, animated: true, completion: nil)
alert.addCancelAction(L10n.Global.ok) {
self.isPurchasing = false
self.reloadModel()
self.tableView.reloadData()
}
present(alert, animated: true)
}
}
extension DonationViewController {
enum SectionType {
case oneTime
case activity
}
enum RowType {
case loading
case recurring
case purchasing
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
}
}

View File

@ -63,6 +63,7 @@
0E3152DD223FA06100F61841 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 0E05C5DF20D198B9006EE732 /* Localizable.strings */; };
0E3152DE223FA06400F61841 /* Web in Resources */ = {isa = PBXBuildFile; fileRef = 0E0EABC721DF853C0069DAE7 /* Web */; };
0E3152DF223FA1DD00F61841 /* ConnectionService.json in Resources */ = {isa = PBXBuildFile; fileRef = 0EBBE8F121822B4D00106008 /* ConnectionService.json */; };
0E3586FE225BD34800509A4D /* ActivityTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E3586FD225BD34800509A4D /* ActivityTableViewCell.swift */; };
0E36D24D2240234B006AF062 /* ShortcutsAddViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E36D24C2240234B006AF062 /* ShortcutsAddViewController.swift */; };
0E36D25822403469006AF062 /* Shortcuts.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0E36D25A22403469006AF062 /* Shortcuts.storyboard */; };
0E36D25C224034AD006AF062 /* ShortcutsConnectToViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E36D25B224034AD006AF062 /* ShortcutsConnectToViewController.swift */; };
@ -189,6 +190,7 @@
0E31529E223F9EF500F61841 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
0E3152A3223F9EF500F61841 /* Passepartout-CoreTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Passepartout-CoreTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
0E3152AC223F9EF500F61841 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
0E3586FD225BD34800509A4D /* ActivityTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityTableViewCell.swift; sourceTree = "<group>"; };
0E36D24C2240234B006AF062 /* ShortcutsAddViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutsAddViewController.swift; sourceTree = "<group>"; };
0E36D25B224034AD006AF062 /* ShortcutsConnectToViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShortcutsConnectToViewController.swift; sourceTree = "<group>"; };
0E39BCEF214B9EF10035E9DE /* WebServices.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebServices.swift; sourceTree = "<group>"; };
@ -335,6 +337,7 @@
0E1066CA20E0F85C004F98B7 /* Cells */ = {
isa = PBXGroup;
children = (
0E3586FD225BD34800509A4D /* ActivityTableViewCell.swift */,
0E1066C820E0F84A004F98B7 /* Cells.swift */,
0E4C9CBA20DCF0D600A0C59C /* DestructiveTableViewCell.swift */,
0E05C5CE20D139AF006EE732 /* FieldTableViewCell.swift */,
@ -1078,6 +1081,7 @@
0EFBFAC121AC464800887A8C /* CreditsViewController.swift in Sources */,
0EFD943E215BE10800529B64 /* IssueReporter.swift in Sources */,
0EB60FDA2111136E00AD27F3 /* UITextView+Search.swift in Sources */,
0E3586FE225BD34800509A4D /* ActivityTableViewCell.swift in Sources */,
0EB67D6B2184581E00BA6200 /* ImportedHostsViewController.swift in Sources */,
0E57F63E20C83FC5008323CF /* ServiceViewController.swift in Sources */,
0E36D24D2240234B006AF062 /* ShortcutsAddViewController.swift in Sources */,

View File

@ -250,7 +250,8 @@
"donation.title" = "Donate";
"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.recurring.header" = "Recurring";
"donation.cells.loading.caption" = "Loading donations";
"donation.cells.purchasing.caption" = "Performing donation";
"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.failure.message" = "Unable to perform the donation. %@";

View File

@ -250,6 +250,8 @@
"donation.title" = "Donazione";
"donation.sections.one_time.header" = "Unica";
"donation.sections.one_time.footer" = "Se vuoi mostrare gratitudine per il mio lavoro a titolo gratuito, qui trovi varie somme da donare all'istante.\n\nLa donazione ti sarà addebitata solo una volta, e puoi effettuare più donazioni.";
"donation.cells.loading.caption" = "Caricando donazioni";
"donation.cells.purchasing.caption" = "Effettuando donazione";
"donation.alerts.purchase.success.title" = "Grazie";
"donation.alerts.purchase.success.message" = "Questo significa molto per me e spero vivamente che tu continui ad usare e promuovere quest'applicazione.";
"donation.alerts.purchase.failure.message" = "Impossibile effettuare la donazione. %@";

View File

@ -314,6 +314,16 @@ public enum L10n {
}
}
}
public enum Cells {
public enum Loading {
/// Loading donations
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 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.