2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// PaywallView+Purchase.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/12/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
|
2023-04-04 07:50:45 +00:00
|
|
|
import SwiftUI
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
extension PaywallView {
|
|
|
|
struct PurchaseView: View {
|
|
|
|
fileprivate enum PurchaseState {
|
2023-12-20 19:43:39 +00:00
|
|
|
case purchasing(InAppProduct)
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
case restoring
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@Environment(\.scenePhase) private var scenePhase
|
|
|
|
|
|
|
|
@ObservedObject private var productManager: ProductManager
|
2022-04-13 19:01:06 +00:00
|
|
|
|
|
|
|
@Binding private var isPresented: Bool
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private let feature: LocalProduct?
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@State private var purchaseState: PurchaseState?
|
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
@State private var didPurchaseAppleTV = false
|
|
|
|
|
2022-04-13 19:01:06 +00:00
|
|
|
init(isPresented: Binding<Bool>, feature: LocalProduct? = nil) {
|
2022-04-12 13:09:14 +00:00
|
|
|
productManager = .shared
|
2022-04-13 19:01:06 +00:00
|
|
|
_isPresented = isPresented
|
2022-04-12 13:09:14 +00:00
|
|
|
self.feature = feature
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
var body: some View {
|
|
|
|
List {
|
2024-01-11 16:52:05 +00:00
|
|
|
if feature != .appleTV {
|
|
|
|
skFullVersion.map {
|
|
|
|
fullFeaturesSection(withHeader: $0.localizedTitle)
|
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
}
|
2023-12-23 11:10:34 +00:00
|
|
|
purchaseSection
|
|
|
|
.disabled(purchaseState != nil)
|
|
|
|
restoreSection
|
2022-04-12 13:09:14 +00:00
|
|
|
.disabled(purchaseState != nil)
|
2023-12-16 19:58:54 +00:00
|
|
|
}
|
|
|
|
.navigationTitle(Unlocalized.appName)
|
|
|
|
.alert(Unlocalized.Other.appleTV, isPresented: $didPurchaseAppleTV) {
|
|
|
|
Button(L10n.Global.Strings.ok) {
|
|
|
|
isPresented = false
|
|
|
|
}
|
|
|
|
} message: {
|
|
|
|
Text(L10n.Paywall.Alerts.Purchase.Appletv.Success.message)
|
|
|
|
}
|
2022-04-20 17:53:18 +00:00
|
|
|
|
|
|
|
// reloading
|
2023-12-21 07:09:52 +00:00
|
|
|
.task {
|
|
|
|
await productManager.refreshProducts()
|
|
|
|
}
|
|
|
|
.onChange(of: scenePhase) { newValue in
|
2022-04-20 17:53:18 +00:00
|
|
|
if newValue == .active {
|
2023-12-21 07:09:52 +00:00
|
|
|
Task {
|
|
|
|
await productManager.refreshProducts()
|
|
|
|
}
|
2022-04-20 17:53:18 +00:00
|
|
|
}
|
2023-12-21 07:09:52 +00:00
|
|
|
}
|
|
|
|
.themeAnimation(on: productManager.isRefreshingProducts)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-23 11:38:00 +00:00
|
|
|
private struct FeatureModel: Identifiable, Comparable {
|
|
|
|
let productIdentifier: String
|
|
|
|
|
|
|
|
let title: String
|
|
|
|
|
|
|
|
let subtitle: String?
|
|
|
|
|
|
|
|
init(localProduct: LocalProduct, title: String) {
|
|
|
|
productIdentifier = localProduct.rawValue
|
|
|
|
self.title = title
|
|
|
|
subtitle = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
init(product: InAppProduct) {
|
|
|
|
productIdentifier = product.productIdentifier
|
|
|
|
title = product.localizedTitle
|
|
|
|
let description = product.localizedDescription
|
|
|
|
subtitle = description != title ? description : nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var id: String {
|
|
|
|
productIdentifier
|
|
|
|
}
|
|
|
|
|
|
|
|
static func < (lhs: Self, rhs: Self) -> Bool {
|
|
|
|
lhs.title.lowercased() < rhs.title.lowercased()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
private struct PurchaseRow: View {
|
2023-12-20 19:43:39 +00:00
|
|
|
var product: InAppProduct?
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
let title: String
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
let action: () -> Void
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
let purchaseState: PaywallView.PurchaseView.PurchaseState?
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
var body: some View {
|
2023-12-23 11:10:34 +00:00
|
|
|
actionButton
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension PaywallView.PurchaseView {
|
2023-12-16 19:58:54 +00:00
|
|
|
func fullFeaturesSection(withHeader header: String) -> some View {
|
2023-12-23 11:10:34 +00:00
|
|
|
Section {
|
2023-12-16 19:58:54 +00:00
|
|
|
ForEach(fullFeatures) { feature in
|
2023-12-23 11:10:34 +00:00
|
|
|
VStack(alignment: .leading) {
|
2023-12-23 11:38:00 +00:00
|
|
|
Text(feature.title)
|
2023-12-23 11:10:34 +00:00
|
|
|
.themeCellTitleStyle()
|
2023-12-23 11:38:00 +00:00
|
|
|
feature.subtitle.map {
|
|
|
|
Text($0)
|
2023-12-23 11:10:34 +00:00
|
|
|
.themeCellSubtitleStyle()
|
|
|
|
.themeSecondaryTextStyle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
} header: {
|
|
|
|
Text(header)
|
2023-12-23 11:10:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var purchaseSection: some View {
|
2023-07-03 14:54:43 +00:00
|
|
|
Section {
|
|
|
|
if !productManager.isRefreshingProducts {
|
2023-12-23 11:10:34 +00:00
|
|
|
ForEach(productRowModels, id: \.productIdentifier, content: productRow)
|
2023-07-03 14:54:43 +00:00
|
|
|
} else {
|
|
|
|
ProgressView()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
} header: {
|
|
|
|
Text(L10n.Paywall.title)
|
|
|
|
} footer: {
|
|
|
|
Text(L10n.Paywall.Sections.Products.footer)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
|
2023-12-23 11:10:34 +00:00
|
|
|
var restoreSection: some View {
|
|
|
|
Section {
|
|
|
|
restoreRow
|
|
|
|
} footer: {
|
|
|
|
Text(L10n.Paywall.Items.Restore.description)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func productRow(_ product: InAppProduct) -> some View {
|
2023-07-03 14:54:43 +00:00
|
|
|
PurchaseRow(
|
2023-12-23 11:10:34 +00:00
|
|
|
product: product,
|
|
|
|
title: product.localizedTitle,
|
2023-07-03 14:54:43 +00:00
|
|
|
action: {
|
2023-12-23 11:10:34 +00:00
|
|
|
purchaseProduct(product)
|
2023-07-03 14:54:43 +00:00
|
|
|
},
|
|
|
|
purchaseState: purchaseState
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var restoreRow: some View {
|
|
|
|
PurchaseRow(
|
|
|
|
title: L10n.Paywall.Items.Restore.title,
|
|
|
|
action: restorePurchases,
|
|
|
|
purchaseState: purchaseState
|
|
|
|
)
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
private extension PaywallView.PurchaseView {
|
2023-12-20 19:43:39 +00:00
|
|
|
var skFullVersion: InAppProduct? {
|
2024-01-11 15:46:52 +00:00
|
|
|
productManager.product(withIdentifier: .fullVersion)
|
2023-12-16 19:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var fullFeatures: [FeatureModel] {
|
2023-12-23 11:10:34 +00:00
|
|
|
productManager.featureProducts(excluding: {
|
2023-12-16 19:58:54 +00:00
|
|
|
$0 == .fullVersion || $0 == .appleTV || $0.isLegacyPlatformVersion
|
2023-12-23 11:10:34 +00:00
|
|
|
})
|
2023-12-23 11:38:00 +00:00
|
|
|
.map {
|
|
|
|
FeatureModel(product: $0)
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
.sorted()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-23 11:10:34 +00:00
|
|
|
var productRowModels: [InAppProduct] {
|
2024-01-11 15:46:52 +00:00
|
|
|
productManager
|
|
|
|
.purchasableProducts(withFeature: feature)
|
|
|
|
.compactMap { productManager.product(withIdentifier: $0) }
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
private extension PurchaseRow {
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@ViewBuilder
|
2023-07-03 14:54:43 +00:00
|
|
|
var actionButton: some View {
|
2023-12-20 19:43:39 +00:00
|
|
|
if let product {
|
2022-04-12 13:09:14 +00:00
|
|
|
purchaseButton(product)
|
|
|
|
} else {
|
|
|
|
restoreButton
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
func purchaseButton(_ product: InAppProduct) -> some View {
|
2022-04-12 13:09:14 +00:00
|
|
|
HStack {
|
|
|
|
Button(title, action: action)
|
|
|
|
Spacer()
|
|
|
|
if case .purchasing(let pending) = purchaseState, pending.productIdentifier == product.productIdentifier {
|
|
|
|
ProgressView()
|
|
|
|
} else {
|
|
|
|
product.localizedPrice.map {
|
|
|
|
Text($0)
|
2022-04-23 09:42:26 +00:00
|
|
|
.themeSecondaryTextStyle()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
var restoreButton: some View {
|
2022-04-12 13:09:14 +00:00
|
|
|
HStack {
|
|
|
|
Button(title, action: action)
|
|
|
|
Spacer()
|
|
|
|
if case .restoring = purchaseState {
|
|
|
|
ProgressView()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
// IMPORTANT: resync shared profiles after purchasing Apple TV feature (drop time limit)
|
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
private extension PaywallView.PurchaseView {
|
2023-12-20 19:43:39 +00:00
|
|
|
func purchaseProduct(_ product: InAppProduct) {
|
2023-07-03 14:54:43 +00:00
|
|
|
purchaseState = .purchasing(product)
|
|
|
|
|
2023-12-21 07:09:52 +00:00
|
|
|
Task {
|
|
|
|
do {
|
2023-12-16 19:58:54 +00:00
|
|
|
let wasEligibleForAppleTV = productManager.isEligible(forFeature: .appleTV)
|
2023-12-21 07:09:52 +00:00
|
|
|
let result = try await productManager.purchase(product)
|
2023-12-16 19:58:54 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
switch result {
|
|
|
|
case .done:
|
2023-12-16 19:58:54 +00:00
|
|
|
if !wasEligibleForAppleTV && productManager.isEligible(forFeature: .appleTV) {
|
|
|
|
ProfileManager.shared.refreshSharedProfiles()
|
|
|
|
didPurchaseAppleTV = true
|
|
|
|
} else {
|
|
|
|
isPresented = false
|
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
|
|
|
|
case .cancelled:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
purchaseState = nil
|
2023-12-21 07:09:52 +00:00
|
|
|
} catch {
|
2023-07-03 14:54:43 +00:00
|
|
|
pp_log.error("Unable to purchase: \(error)")
|
|
|
|
ErrorHandler.shared.handle(
|
|
|
|
title: product.localizedTitle,
|
|
|
|
message: AppError(error).localizedDescription
|
|
|
|
) {
|
|
|
|
purchaseState = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func restorePurchases() {
|
|
|
|
purchaseState = .restoring
|
|
|
|
|
2023-12-21 07:09:52 +00:00
|
|
|
Task {
|
|
|
|
do {
|
2023-12-16 19:58:54 +00:00
|
|
|
let wasEligibleForAppleTV = productManager.isEligible(forFeature: .appleTV)
|
2023-12-21 07:09:52 +00:00
|
|
|
try await productManager.restorePurchases()
|
2023-12-16 19:58:54 +00:00
|
|
|
|
|
|
|
if !wasEligibleForAppleTV && productManager.isEligible(forFeature: .appleTV) {
|
|
|
|
ProfileManager.shared.refreshSharedProfiles()
|
|
|
|
didPurchaseAppleTV = true
|
|
|
|
} else {
|
|
|
|
isPresented = false
|
|
|
|
}
|
|
|
|
|
2023-12-21 07:09:52 +00:00
|
|
|
purchaseState = nil
|
|
|
|
} catch {
|
2023-07-03 14:54:43 +00:00
|
|
|
pp_log.error("Unable to restore purchases: \(error)")
|
|
|
|
ErrorHandler.shared.handle(
|
|
|
|
title: L10n.Paywall.Items.Restore.title,
|
|
|
|
message: AppError(error).localizedDescription
|
|
|
|
) {
|
|
|
|
purchaseState = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|