passepartout-apple/Passepartout/App/Views/DonateView.swift

173 lines
4.7 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// DonateView.swift
// Passepartout
//
// Created by Davide De Rosa on 2/8/22.
2023-03-17 15:56:19 +00:00
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
2022-04-12 13:09:14 +00:00
//
// 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/>.
//
2022-06-23 21:31:01 +00:00
import PassepartoutLibrary
import StoreKit
import SwiftUI
2022-04-12 13:09:14 +00:00
struct DonateView: View {
enum AlertType: Identifiable {
case thankYou
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
// XXX: alert ids
var id: Int {
switch self {
case .thankYou: return 1
}
}
}
2023-03-17 20:55:47 +00:00
@Environment(\.scenePhase) private var scenePhase
2022-04-12 13:09:14 +00:00
@ObservedObject private var productManager: ProductManager
2023-03-17 20:55:47 +00:00
@State private var isAlertPresented = false
2022-04-12 13:09:14 +00:00
@State private var alertType: AlertType?
@State private var pendingDonationIdentifier: String?
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
init() {
productManager = .shared
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var body: some View {
List {
productsSection
.disabled(pendingDonationIdentifier != nil)
}.themeSecondaryView()
.navigationTitle(L10n.Donate.title)
.alert(
L10n.Donate.title,
isPresented: $isAlertPresented,
presenting: alertType,
actions: alertActions,
message: alertMessage
)
// reloading
.onAppear {
productManager.refreshProducts()
}.onChange(of: scenePhase) { newValue in
if newValue == .active {
productManager.refreshProducts()
}
2022-04-23 10:08:24 +00:00
}.themeAnimation(on: productManager.isRefreshingProducts)
2022-04-12 13:09:14 +00:00
}
}
// MARK: -
2022-04-12 13:09:14 +00:00
private extension DonateView {
func alertActions(_ alertType: AlertType) -> some View {
2022-04-12 13:09:14 +00:00
switch alertType {
case .thankYou:
return Button(role: .cancel) {
} label: {
Text(L10n.Global.Strings.ok)
}
}
}
func alertMessage(_ alertType: AlertType) -> some View {
switch alertType {
case .thankYou:
return Text(L10n.Donate.Alerts.Purchase.Success.message)
2022-04-12 13:09:14 +00:00
}
}
2023-03-17 20:55:47 +00:00
var productsSection: some View {
Section {
if !productManager.isRefreshingProducts {
ForEach(productManager.donations, id: \.productIdentifier, content: productRow)
} else {
ProgressView()
}
} header: {
Text(L10n.Donate.Sections.OneTime.header)
} footer: {
Text(L10n.Donate.Sections.OneTime.footer)
2022-04-12 13:09:14 +00:00
}
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@ViewBuilder
func productRow(_ product: SKProduct) -> some View {
2022-04-12 13:09:14 +00:00
HStack {
Button(product.localizedTitle) {
purchaseProduct(product)
}
Spacer()
if let pending = pendingDonationIdentifier, pending == product.productIdentifier {
ProgressView()
} else {
product.localizedPrice.map {
Text($0)
.themeSecondaryTextStyle()
2022-04-12 13:09:14 +00:00
}
}
}
}
}
private extension ProductManager {
var donations: [SKProduct] {
products.filter { product in
LocalProduct.allDonations.contains {
$0.matchesStoreKitProduct(product)
}
}.sorted {
$0.price.decimalValue < $1.price.decimalValue
}
}
}
// MARK: -
private extension DonateView {
func purchaseProduct(_ product: SKProduct) {
2022-04-12 13:09:14 +00:00
pendingDonationIdentifier = product.productIdentifier
productManager.purchase(product, completionHandler: handlePurchaseResult)
}
2023-03-17 20:55:47 +00:00
func handlePurchaseResult(_ result: Result<InAppPurchaseResult, Error>) {
2022-04-12 13:09:14 +00:00
switch result {
case .success(let value):
if case .done = value {
alertType = .thankYou
isAlertPresented = true
2022-04-12 13:09:14 +00:00
} else {
// cancelled
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
case .failure(let error):
2023-07-02 10:51:50 +00:00
ErrorHandler.shared.handle(
title: L10n.Donate.title,
message: L10n.Donate.Alerts.Purchase.Failure.message(AppError(error).localizedDescription)
)
2022-04-12 13:09:14 +00:00
}
pendingDonationIdentifier = nil
}
}