Add donation view controller

This commit is contained in:
Davide De Rosa 2019-04-06 23:58:19 +02:00
parent 6f57d3503a
commit 26453f9612
9 changed files with 232 additions and 9 deletions

View File

@ -246,13 +246,80 @@
</objects>
<point key="canvasLocation" x="-1727" y="-357"/>
</scene>
<!--Donation View Controller-->
<scene sceneID="NrW-UF-gN3">
<objects>
<tableViewController id="SoF-ZH-NT1" customClass="DonationViewController" customModule="Passepartout" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" id="2Io-7F-8Ed">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="SettingTableViewCell" textLabel="52Q-2q-7xf" detailTextLabel="5ba-c6-s1I" style="IBUITableViewCellStyleValue1" id="Ukv-2T-HRq" customClass="SettingTableViewCell" customModule="Passepartout" customModuleProvider="target">
<rect key="frame" x="0.0" y="28" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="Ukv-2T-HRq" id="9sw-RZ-ki7">
<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="52Q-2q-7xf">
<rect key="frame" x="16" y="12" width="33.5" height="20.5"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="Detail" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="5ba-c6-s1I">
<rect key="frame" x="315" y="12" width="44" height="20.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"/>
<outlet property="delegate" destination="SoF-ZH-NT1" id="CIL-Vb-tKS"/>
</connections>
</tableView>
<navigationItem key="navigationItem" id="B6p-Ga-iiI">
<barButtonItem key="leftBarButtonItem" systemItem="stop" id="CuS-kE-f9X">
<connections>
<action selector="close" destination="SoF-ZH-NT1" id="3lz-X4-dEb"/>
</connections>
</barButtonItem>
</navigationItem>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="UbZ-6W-wgU" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-1646" y="-2076"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="B4n-eO-M22">
<objects>
<navigationController id="5ku-HX-5yk" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="Z4x-kj-jBr">
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
<segue destination="SoF-ZH-NT1" kind="relationship" relationship="rootViewController" id="2ax-VJ-siI"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="XWm-9m-gcV" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-2478" y="-2075"/>
</scene>
<!--About-->
<scene sceneID="xOy-mR-afs">
<objects>
<viewControllerPlaceholder storyboardName="About" id="POR-9u-PsE" sceneMemberID="viewController"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="mZu-dH-Wex" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-2991" y="-1301"/>
<point key="canvasLocation" x="-3060" y="-1565"/>
</scene>
<!--Organizer View Controller-->
<scene sceneID="H6i-Tl-Gnq">
@ -302,6 +369,7 @@
<segue destination="z6E-m6-Op0" kind="presentation" identifier="ShowImportedHostsSegueIdentifier" modalPresentationStyle="formSheet" id="TZv-OK-8vU"/>
<segue destination="BIO-UT-cpg" kind="presentation" identifier="SiriShortcutsSegueIdentifier" id="I9b-aK-jPT"/>
<segue destination="POR-9u-PsE" kind="presentation" identifier="AboutSegueIdentifier" id="78g-ra-Yhp"/>
<segue destination="5ku-HX-5yk" kind="presentation" identifier="DonateSegueIdentifier" id="H4q-BS-PmK"/>
</connections>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="bGp-H5-24W" userLabel="First Responder" sceneMemberID="firstResponder"/>

View File

@ -26,17 +26,30 @@
import Foundation
struct InApp {
struct Donations {
static let tiny = "com.algoritmico.ios.Passepartout.donations.Tiny"
enum Donation: String {
static let all: [Donation] = [
.tiny,
.small,
.medium,
.big,
.huge,
.maxi
]
static let small = "com.algoritmico.ios.Passepartout.donations.Small"
case tiny = "com.algoritmico.ios.Passepartout.donations.Tiny"
static let medium = "com.algoritmico.ios.Passepartout.donations.Medium"
case small = "com.algoritmico.ios.Passepartout.donations.Small"
static let big = "com.algoritmico.ios.Passepartout.donations.Big"
case medium = "com.algoritmico.ios.Passepartout.donations.Medium"
static let huge = "com.algoritmico.ios.Passepartout.donations.Huge"
case big = "com.algoritmico.ios.Passepartout.donations.Big"
static let maxi = "com.algoritmico.ios.Passepartout.donations.Maxi"
case huge = "com.algoritmico.ios.Passepartout.donations.Huge"
case maxi = "com.algoritmico.ios.Passepartout.donations.Maxi"
static func allIdentifiers() -> Set<String> {
return Set<String>(all.map { $0.rawValue })
}
}
}

View File

@ -26,6 +26,7 @@ internal enum StoryboardSegue {
internal enum Organizer: String, SegueType {
case aboutSegueIdentifier = "AboutSegueIdentifier"
case addProviderSegueIdentifier = "AddProviderSegueIdentifier"
case donateSegueIdentifier = "DonateSegueIdentifier"
case importHostSegueIdentifier = "ImportHostSegueIdentifier"
case selectProfileSegueIdentifier = "SelectProfileSegueIdentifier"
case showImportedHostsSegueIdentifier = "ShowImportedHostsSegueIdentifier"

View File

@ -25,6 +25,7 @@
import UIKit
import MessageUI
import StoreKit
import Passepartout_Core
extension UIColor {
@ -165,3 +166,12 @@ extension Pool {
return ImageAsset(name: country.lowercased()).image
}
}
extension SKProduct {
var localizedPrice: String? {
let fmt = NumberFormatter()
fmt.numberStyle = .currency
fmt.locale = priceLocale
return fmt.string(from: price)
}
}

View File

@ -0,0 +1,120 @@
//
// 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 Passepartout_Core
class DonationViewController: UITableViewController, TableModelHost {
private var productsByIdentifier: [String: SKProduct] = [:]
private func setProducts(_ products: [SKProduct]) {
for p in products {
productsByIdentifier[p.productIdentifier] = p
}
reloadModel()
tableView.reloadData()
}
// MARK: TableModel
var model: TableModel<SectionType, InApp.Donation> = TableModel()
func reloadModel() {
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)
}
model.add(.oneTime)
// model.add(.recurring)
model.set(list, in: .oneTime)
}
// MARK: UIViewController
override func viewDidLoad() {
super.viewDidLoad()
title = L10n.Donation.title
let req = SKProductsRequest(productIdentifiers: InApp.Donation.allIdentifiers())
req.delegate = self
req.start()
}
@IBAction private func close() {
dismiss(animated: true, completion: nil)
}
// MARK: UITableViewController
override func numberOfSections(in tableView: UITableView) -> Int {
return model.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.count(for: section)
}
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")
}
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
cell.leftText = product.localizedTitle
cell.rightText = product.localizedPrice
return cell
}
}
extension DonationViewController {
enum SectionType {
case oneTime
case recurring
}
private func productIdentifier(at indexPath: IndexPath) -> String {
return model.row(at: indexPath).rawValue
}
}
extension DonationViewController: SKProductsRequestDelegate {
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
DispatchQueue.main.async {
self.setProducts(response.products)
}
}
func request(_ request: SKRequest, didFailWithError error: Error) {
}
}

View File

@ -192,7 +192,7 @@ class OrganizerViewController: UITableViewController, TableModelHost {
}
private func donateToDeveloper() {
// TODO
perform(segue: StoryboardSegue.Organizer.donateSegueIdentifier, sender: nil)
}
private func visitPatreon() {

View File

@ -18,6 +18,7 @@
0E1D72B4213C118500BA1586 /* ConfigurationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E1D72B3213C118500BA1586 /* ConfigurationViewController.swift */; };
0E24273A225950450064A1A3 /* About.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0E24273C225950450064A1A3 /* About.storyboard */; };
0E242740225951B00064A1A3 /* InApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E24273F225951B00064A1A3 /* InApp.swift */; };
0E242742225956AC0064A1A3 /* DonationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E242741225956AC0064A1A3 /* DonationViewController.swift */; };
0E2B494020FCFF990094784C /* Theme+Titles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E2B493F20FCFF990094784C /* Theme+Titles.swift */; };
0E3152A4223F9EF500F61841 /* Passepartout_Core.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0E31529B223F9EF400F61841 /* Passepartout_Core.framework */; };
0E3152AD223F9EF500F61841 /* Passepartout_Core.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E31529D223F9EF500F61841 /* Passepartout_Core.h */; settings = {ATTRIBUTES = (Public, ); }; };
@ -179,6 +180,7 @@
0E242735225944060064A1A3 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Intents.strings; sourceTree = "<group>"; };
0E24273B225950450064A1A3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/About.storyboard; sourceTree = "<group>"; };
0E24273F225951B00064A1A3 /* InApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InApp.swift; sourceTree = "<group>"; };
0E242741225956AC0064A1A3 /* DonationViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DonationViewController.swift; sourceTree = "<group>"; };
0E2B493F20FCFF990094784C /* Theme+Titles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Theme+Titles.swift"; sourceTree = "<group>"; };
0E2B494120FD16540094784C /* TransientStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransientStore.swift; sourceTree = "<group>"; };
0E2D11B9217DBEDE0096822C /* ConnectionService+Configurations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ConnectionService+Configurations.swift"; sourceTree = "<group>"; };
@ -456,6 +458,7 @@
0E89DFCC213EEDE700741BA1 /* Organizer */ = {
isa = PBXGroup;
children = (
0E242741225956AC0064A1A3 /* DonationViewController.swift */,
0EB67D6A2184581E00BA6200 /* ImportedHostsViewController.swift */,
0EBE3A78213C4E5400BFA2F5 /* OrganizerViewController.swift */,
0ED38AD8213F33150004D387 /* WizardHostViewController.swift */,
@ -1055,6 +1058,7 @@
0E36D25C224034AD006AF062 /* ShortcutsConnectToViewController.swift in Sources */,
0E05C61D20D27C82006EE732 /* Theme.swift in Sources */,
0ECC60DE2256B68A0020BEAC /* SwiftGen+Assets.swift in Sources */,
0E242742225956AC0064A1A3 /* DonationViewController.swift in Sources */,
0ED38AEC2141260D0004D387 /* ConfigurationModificationDelegate.swift in Sources */,
0ECEE45020E1182E00A6BB43 /* Theme+Cells.swift in Sources */,
0E242740225951B00064A1A3 /* InApp.swift in Sources */,

View File

@ -247,6 +247,8 @@
"about.cells.join_community.caption" = "Join community";
"about.cells.write_review.caption" = "Write a review";
"donation.title" = "Donate";
"share.message" = "Passepartout is an user-friendly, open source OpenVPN client for iOS and macOS";
"version.title" = "Version";

View File

@ -295,6 +295,11 @@ public enum L10n {
}
}
public enum Donation {
/// Donate
public static let title = L10n.tr("Localizable", "donation.title")
}
public enum Endpoint {
public enum Cells {
public enum AnyAddress {