2018-10-11 07:13:19 +00:00
|
|
|
//
|
2024-09-23 13:02:26 +00:00
|
|
|
// PaywallView.swift
|
2021-01-01 10:13:41 +00:00
|
|
|
// Passepartout
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
2024-09-23 13:02:26 +00:00
|
|
|
// Created by Davide De Rosa on 9/10/24.
|
2024-01-14 13:34:21 +00:00
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
2018-11-03 21:33:30 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
//
|
|
|
|
|
2024-11-03 12:16:13 +00:00
|
|
|
import CommonLibrary
|
2024-11-07 17:27:36 +00:00
|
|
|
import CommonUtils
|
|
|
|
import StoreKit
|
2022-04-12 13:09:14 +00:00
|
|
|
import SwiftUI
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
struct PaywallView: View {
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
@EnvironmentObject
|
|
|
|
private var iapManager: IAPManager
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
@Binding
|
|
|
|
var isPresented: Bool
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2024-11-18 16:43:01 +00:00
|
|
|
let features: Set<AppFeature>
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2024-11-05 17:55:57 +00:00
|
|
|
let suggestedProduct: AppProduct?
|
|
|
|
|
2024-11-07 17:27:36 +00:00
|
|
|
@State
|
|
|
|
private var oneTimeProduct: InAppProduct?
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var recurringProducts: [InAppProduct] = []
|
|
|
|
|
|
|
|
@State
|
2024-11-07 22:02:10 +00:00
|
|
|
private var isFetchingProducts = true
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var purchasingIdentifier: String?
|
|
|
|
|
|
|
|
@State
|
|
|
|
private var isPurchasePendingConfirmation = false
|
2024-11-07 17:27:36 +00:00
|
|
|
|
|
|
|
@StateObject
|
|
|
|
private var errorHandler: ErrorHandler = .default()
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
var body: some View {
|
2024-11-10 11:00:07 +00:00
|
|
|
paywallView
|
|
|
|
.themeProgress(if: isFetchingProducts)
|
2024-11-24 00:01:04 +00:00
|
|
|
#if !os(tvOS)
|
2024-11-10 11:00:07 +00:00
|
|
|
.toolbar(content: toolbarContent)
|
2024-11-24 00:01:04 +00:00
|
|
|
#endif
|
2024-11-10 11:00:07 +00:00
|
|
|
.alert(
|
2024-11-23 19:31:22 +00:00
|
|
|
Strings.Global.Actions.purchase,
|
2024-11-10 11:00:07 +00:00
|
|
|
isPresented: $isPurchasePendingConfirmation,
|
|
|
|
actions: pendingActions,
|
|
|
|
message: pendingMessage
|
|
|
|
)
|
2024-11-18 16:43:01 +00:00
|
|
|
.task(id: features) {
|
2024-11-10 11:00:07 +00:00
|
|
|
await fetchAvailableProducts()
|
2024-11-07 17:27:36 +00:00
|
|
|
}
|
2024-11-10 11:00:07 +00:00
|
|
|
.withErrorHandler(errorHandler)
|
2024-11-07 17:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension PaywallView {
|
|
|
|
var title: String {
|
2024-11-23 19:31:22 +00:00
|
|
|
Strings.Global.Actions.purchase
|
2024-11-07 17:27:36 +00:00
|
|
|
}
|
|
|
|
|
2024-11-24 00:01:04 +00:00
|
|
|
var otherFeatures: [AppFeature] {
|
|
|
|
AppFeature.allCases.filter {
|
|
|
|
!features.contains($0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 11:00:07 +00:00
|
|
|
var paywallView: some View {
|
|
|
|
Form {
|
2024-11-18 16:43:01 +00:00
|
|
|
requiredFeaturesView
|
2024-11-10 11:00:07 +00:00
|
|
|
productsView
|
2024-11-18 16:43:01 +00:00
|
|
|
otherFeaturesView
|
2024-11-10 11:00:07 +00:00
|
|
|
restoreView
|
|
|
|
}
|
|
|
|
.themeForm()
|
|
|
|
.disabled(purchasingIdentifier != nil)
|
|
|
|
}
|
|
|
|
|
2024-11-07 17:27:36 +00:00
|
|
|
@ViewBuilder
|
|
|
|
var productsView: some View {
|
|
|
|
oneTimeProduct.map {
|
2024-11-07 22:02:10 +00:00
|
|
|
PaywallProductView(
|
|
|
|
iapManager: iapManager,
|
|
|
|
style: .oneTime,
|
|
|
|
product: $0,
|
|
|
|
purchasingIdentifier: $purchasingIdentifier,
|
|
|
|
onComplete: onComplete,
|
|
|
|
onError: onError
|
|
|
|
)
|
2024-11-23 19:31:22 +00:00
|
|
|
.themeSection(header: Strings.Views.Paywall.Sections.OneTime.header)
|
2024-11-07 17:27:36 +00:00
|
|
|
}
|
|
|
|
ForEach(recurringProducts, id: \.productIdentifier) {
|
2024-11-07 22:02:10 +00:00
|
|
|
PaywallProductView(
|
|
|
|
iapManager: iapManager,
|
|
|
|
style: .recurring,
|
|
|
|
product: $0,
|
|
|
|
purchasingIdentifier: $purchasingIdentifier,
|
|
|
|
onComplete: onComplete,
|
|
|
|
onError: onError
|
|
|
|
)
|
2024-11-07 17:27:36 +00:00
|
|
|
}
|
2024-11-23 19:31:22 +00:00
|
|
|
.themeSection(header: Strings.Views.Paywall.Sections.Recurring.header)
|
2024-11-07 17:27:36 +00:00
|
|
|
}
|
|
|
|
|
2024-11-18 16:43:01 +00:00
|
|
|
var requiredFeaturesView: some View {
|
|
|
|
FeatureListView(
|
|
|
|
style: .list,
|
2024-11-23 19:31:22 +00:00
|
|
|
header: Strings.Views.Paywall.Sections.Features.Required.header,
|
2024-11-24 00:01:04 +00:00
|
|
|
features: Array(features),
|
|
|
|
content: {
|
|
|
|
featureView(for: $0)
|
|
|
|
.fontWeight(.bold)
|
|
|
|
}
|
|
|
|
)
|
2024-11-07 17:27:36 +00:00
|
|
|
}
|
2024-11-18 16:43:01 +00:00
|
|
|
|
|
|
|
var otherFeaturesView: some View {
|
|
|
|
FeatureListView(
|
|
|
|
style: otherFeaturesStyle,
|
2024-11-23 19:31:22 +00:00
|
|
|
header: Strings.Views.Paywall.Sections.Features.Other.header,
|
2024-11-24 00:01:04 +00:00
|
|
|
features: otherFeatures,
|
|
|
|
content: featureView(for:)
|
|
|
|
)
|
2024-11-07 17:27:36 +00:00
|
|
|
}
|
2024-11-18 16:43:01 +00:00
|
|
|
|
|
|
|
var otherFeaturesStyle: FeatureListViewStyle {
|
|
|
|
#if os(iOS) || os(tvOS)
|
|
|
|
.list
|
|
|
|
#else
|
|
|
|
.table
|
2024-11-07 17:27:36 +00:00
|
|
|
#endif
|
2024-11-18 16:43:01 +00:00
|
|
|
}
|
2024-11-07 17:27:36 +00:00
|
|
|
|
2024-11-24 00:01:04 +00:00
|
|
|
func featureView(for feature: AppFeature) -> some View {
|
|
|
|
Text(feature.localizedDescription)
|
2024-11-26 14:55:04 +00:00
|
|
|
.scrollableOnTV()
|
2024-11-24 00:01:04 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 17:27:36 +00:00
|
|
|
var restoreView: some View {
|
2024-11-07 22:02:10 +00:00
|
|
|
RestorePurchasesButton(errorHandler: errorHandler)
|
2024-11-07 17:27:36 +00:00
|
|
|
.themeSectionWithSingleRow(
|
2024-11-23 19:31:22 +00:00
|
|
|
header: Strings.Views.Paywall.Sections.Restore.header,
|
|
|
|
footer: Strings.Views.Paywall.Sections.Restore.footer,
|
2024-11-07 17:27:36 +00:00
|
|
|
above: true
|
|
|
|
)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension PaywallView {
|
|
|
|
|
|
|
|
@ToolbarContentBuilder
|
|
|
|
func toolbarContent() -> some ToolbarContent {
|
|
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
|
|
Button {
|
|
|
|
isPresented = false
|
|
|
|
} label: {
|
2024-11-07 17:27:36 +00:00
|
|
|
ThemeCloseLabel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pendingActions() -> some View {
|
2024-11-23 19:31:22 +00:00
|
|
|
Button(Strings.Global.Nouns.ok) {
|
2024-11-07 17:27:36 +00:00
|
|
|
isPresented = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pendingMessage() -> some View {
|
2024-11-23 19:31:22 +00:00
|
|
|
Text(Strings.Views.Paywall.Alerts.Pending.message)
|
2024-11-07 17:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension PaywallView {
|
|
|
|
func fetchAvailableProducts() async {
|
|
|
|
isFetchingProducts = true
|
2024-11-07 22:02:10 +00:00
|
|
|
defer {
|
|
|
|
isFetchingProducts = false
|
|
|
|
}
|
2024-11-07 17:27:36 +00:00
|
|
|
|
|
|
|
var list: [AppProduct] = []
|
|
|
|
if let suggestedProduct {
|
|
|
|
list.append(suggestedProduct)
|
|
|
|
}
|
|
|
|
list.append(.Full.Recurring.yearly)
|
|
|
|
list.append(.Full.Recurring.monthly)
|
|
|
|
|
2024-11-07 22:02:10 +00:00
|
|
|
do {
|
|
|
|
let availableProducts = try await iapManager.purchasableProducts(for: list)
|
|
|
|
guard !availableProducts.isEmpty else {
|
|
|
|
throw AppError.emptyProducts
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2024-11-07 22:02:10 +00:00
|
|
|
oneTimeProduct = availableProducts.first {
|
|
|
|
guard let suggestedProduct else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return $0.productIdentifier.hasSuffix(suggestedProduct.rawValue)
|
|
|
|
}
|
|
|
|
recurringProducts = availableProducts.filter {
|
|
|
|
$0.productIdentifier != oneTimeProduct?.productIdentifier
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
onError(error, dismissing: true)
|
2024-11-07 17:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func onComplete(_ productIdentifier: String, result: InAppPurchaseResult) {
|
|
|
|
switch result {
|
|
|
|
case .done:
|
2024-11-19 10:36:36 +00:00
|
|
|
Task {
|
|
|
|
await iapManager.reloadReceipt()
|
|
|
|
}
|
2024-11-07 17:27:36 +00:00
|
|
|
isPresented = false
|
|
|
|
|
|
|
|
case .pending:
|
2024-11-07 22:02:10 +00:00
|
|
|
isPurchasePendingConfirmation = true
|
2024-11-07 17:27:36 +00:00
|
|
|
|
|
|
|
case .cancelled:
|
|
|
|
break
|
|
|
|
|
|
|
|
case .notFound:
|
|
|
|
fatalError("Product not found: \(productIdentifier)")
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-07 17:27:36 +00:00
|
|
|
func onError(_ error: Error) {
|
2024-11-07 22:02:10 +00:00
|
|
|
onError(error, dismissing: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func onError(_ error: Error, dismissing: Bool) {
|
2024-11-23 19:31:22 +00:00
|
|
|
errorHandler.handle(error, title: Strings.Global.Actions.purchase) {
|
2024-11-07 22:02:10 +00:00
|
|
|
if dismissing {
|
|
|
|
isPresented = false
|
|
|
|
}
|
|
|
|
}
|
2024-11-07 17:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Previews
|
|
|
|
|
|
|
|
#Preview {
|
|
|
|
PaywallView(
|
|
|
|
isPresented: .constant(true),
|
2024-11-18 16:43:01 +00:00
|
|
|
features: [.appleTV],
|
2024-11-07 17:27:36 +00:00
|
|
|
suggestedProduct: .Features.appleTV
|
|
|
|
)
|
|
|
|
.withMockEnvironment()
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|