Dismiss paywall predictably with Binding<Bool>

This commit is contained in:
Davide De Rosa 2022-04-13 21:01:06 +02:00
parent e7a5d82056
commit 320513dd38
5 changed files with 32 additions and 10 deletions

View File

@ -84,7 +84,7 @@ struct AddProviderView: View {
.navigationTitle(L10n.AddProfile.Shared.title)
.sheet(isPresented: $viewModel.isPaywallPresented) {
NavigationView {
PaywallView()
PaywallView(isPresented: $viewModel.isPaywallPresented)
}.themeGlobal()
}.onAppear {
refreshProviders()

View File

@ -164,7 +164,10 @@ extension OrganizerView {
case .presentPaywallShortcuts:
NavigationView {
PaywallView(feature: .siriShortcuts)
PaywallView(
modalType: $modalType,
feature: .siriShortcuts
)
}.themeGlobal()
}
}

View File

@ -53,9 +53,9 @@ extension PaywallView {
@Environment(\.scenePhase) private var scenePhase
@Environment(\.presentationMode) private var presentationMode
@ObservedObject private var productManager: ProductManager
@Binding private var isPresented: Bool
private let feature: LocalProduct?
@ -63,8 +63,9 @@ extension PaywallView {
@State private var purchaseState: PurchaseState?
init(feature: LocalProduct? = nil) {
init(isPresented: Binding<Bool>, feature: LocalProduct? = nil) {
productManager = .shared
_isPresented = isPresented
self.feature = feature
}
@ -149,7 +150,7 @@ extension PaywallView.PurchaseView {
case .success(let result):
switch result {
case .done:
presentationMode.wrappedValue.dismiss()
isPresented = false
case .cancelled:
break
@ -172,7 +173,7 @@ extension PaywallView.PurchaseView {
alertType = .restoreFailed(error)
return
}
presentationMode.wrappedValue.dismiss()
isPresented = false
purchaseState = nil
}
}

View File

@ -28,10 +28,24 @@ import SwiftUI
struct PaywallView: View {
@ObservedObject private var productManager: ProductManager
private let feature: LocalProduct?
@Binding private var isPresented: Bool
init(feature: LocalProduct? = nil) {
private let feature: LocalProduct?
init<MT>(modalType: Binding<MT?>, feature: LocalProduct? = nil) {
let isPresented = Binding<Bool> {
modalType.wrappedValue != nil
} set: {
if !$0 {
modalType.wrappedValue = nil
}
}
self.init(isPresented: isPresented, feature: feature)
}
init(isPresented: Binding<Bool>, feature: LocalProduct? = nil) {
productManager = .shared
_isPresented = isPresented
self.feature = feature
}
@ -40,7 +54,10 @@ struct PaywallView: View {
if productManager.cfg.appType == .beta {
BetaView()
} else {
PurchaseView(feature: feature)
PurchaseView(
isPresented: $isPresented,
feature: feature
)
}
}.themeSecondaryView()
}

View File

@ -126,6 +126,7 @@ struct ProfileView: View {
case .paywallTrustedNetworks:
NavigationView {
PaywallView(
modalType: $modalType,
feature: .trustedNetworks
)
}.themeGlobal()