passepartout-apple/Passepartout/Library/Sources/UILibrary/Views/UI/PaywallModifier.swift

195 lines
5.1 KiB
Swift
Raw Normal View History

2024-09-23 13:02:26 +00:00
//
// PaywallModifier.swift
// Passepartout
//
// Created by Davide De Rosa on 9/11/24.
// Copyright (c) 2024 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 CommonLibrary
2024-09-23 13:02:26 +00:00
import SwiftUI
public struct PaywallModifier: ViewModifier {
2024-09-23 13:02:26 +00:00
@EnvironmentObject
private var iapManager: IAPManager
2024-09-23 13:02:26 +00:00
@Binding
private var reason: PaywallReason?
2024-09-23 13:02:26 +00:00
private let okTitle: String?
private let okAction: (() -> Void)?
@State
private var isConfirming = false
2024-09-23 13:02:26 +00:00
@State
private var isRestricted = false
2024-09-23 13:02:26 +00:00
@State
private var isPurchasing = false
2024-09-23 13:02:26 +00:00
public init(
reason: Binding<PaywallReason?>,
okTitle: String? = nil,
okAction: (() -> Void)? = nil
) {
_reason = reason
self.okTitle = okTitle
self.okAction = okAction
}
public func body(content: Content) -> some View {
2024-09-23 13:02:26 +00:00
content
.alert(
Strings.Views.Paywall.Alerts.Confirmation.title,
isPresented: $isConfirming,
actions: confirmationActions,
message: confirmationMessage
)
2024-09-23 13:02:26 +00:00
.alert(
Strings.Views.Paywall.Alerts.Restricted.title,
isPresented: $isRestricted,
actions: restrictedActions,
message: restrictedMessage
2024-09-23 13:02:26 +00:00
)
.themeModal(isPresented: $isPurchasing, content: modalDestination)
.onChange(of: isRestricted) {
if !$0 {
reason = nil
}
}
.onChange(of: isPurchasing) {
if !$0 {
reason = nil
2024-10-28 19:08:13 +00:00
}
2024-09-23 13:02:26 +00:00
}
.onChange(of: reason) {
guard let reason = $0 else {
return
}
if !iapManager.isRestricted {
if reason.needsConfirmation {
isConfirming = true
} else {
isPurchasing = true
}
} else {
isRestricted = true
2024-09-23 13:02:26 +00:00
}
}
}
}
private extension PaywallModifier {
var ineligibleFeatures: [String] {
guard let reason else {
return []
}
return iapManager
.excludingEligible(from: reason.requiredFeatures)
.map(\.localizedDescription)
.sorted()
}
func alertMessage(startingWith header: String, features: [String]) -> String {
header + "\n\n" + features
.joined(separator: "\n")
}
}
private extension IAPManager {
func excludingEligible(from features: Set<AppFeature>) -> Set<AppFeature> {
features.filter {
!isEligible(for: $0)
}
}
}
// MARK: - Confirmation alert
private extension PaywallModifier {
@ViewBuilder
func confirmationActions() -> some View {
Button(Strings.Global.Actions.purchase) {
// IMPORTANT: retain reason because it serves paywall content
isPurchasing = true
}
if let okTitle {
Button(okTitle) {
reason = nil
okAction?()
2024-09-23 13:02:26 +00:00
}
}
Button(Strings.Global.Actions.cancel, role: .cancel) {
reason = nil
}
2024-09-23 13:02:26 +00:00
}
func confirmationMessage() -> some View {
Text(confirmationMessageString)
}
var confirmationMessageString: String {
alertMessage(
startingWith: Strings.Views.Paywall.Alerts.Confirmation.message,
features: ineligibleFeatures
)
}
2024-09-23 13:02:26 +00:00
}
// MARK: - Restricted alert
private extension PaywallModifier {
func restrictedActions() -> some View {
Button(Strings.Global.Nouns.ok) {
//
}
}
func restrictedMessage() -> some View {
Text(restrictedMessageString)
}
var restrictedMessageString: String {
alertMessage(
startingWith: Strings.Views.Paywall.Alerts.Restricted.message,
features: ineligibleFeatures
)
}
}
// MARK: - Paywall
private extension PaywallModifier {
func modalDestination() -> some View {
reason.map {
PaywallView(
isPresented: $isPurchasing,
features: iapManager.excludingEligible(from: $0.requiredFeatures),
suggestedProduct: $0.suggestedProduct
)
.themeNavigationStack()
}
}
}