Skip to content

Commit

Permalink
Added ViewModifier to work with Env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
facumenzella committed Jan 20, 2025
1 parent 36cafff commit 28d82b6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,54 @@ struct CompatibilityNavigationStack<Content: View>: View {

}

/// A `ViewModifier` that provides backward-compatible navigation, supporting both `NavigationStack` (iOS 16+) and
/// `NavigationView` (earlier versions). The modifier leverages the `isPresented` binding to determine whether the
/// destination view should be presented.
///
/// This modifier reads the `navigationOptions` from the environment to decide whether to use `NavigationStack` or
/// fallback to a hidden `NavigationLink`.
///
/// - Note: The `navigationOptions` environment value must be set in the view hierarchy for this modifier to work as
/// intended.
///
/// - Parameters:
/// - isPresented: A binding to a Boolean that determines whether the destination is presented.
/// - destination: A closure that returns the destination view to be presented.
struct CompatibleNavigationModifier<Destination: View>: ViewModifier {
/// A binding to a Boolean that determines whether the destination is presented.
@Binding var isPresented: Bool

/// A closure that returns the destination view.
let destination: () -> Destination

/// The navigation options provided via the environment.
/// This determines whether `NavigationStack` or `NavigationView` should be used.
@Environment(\.navigationOptions)
var navigationOptions

func body(content: Content) -> some View {
Group {
if #available(iOS 16.0, *), navigationOptions.usesNavigationStack {
content
.navigationDestination(isPresented: $isPresented) {
destination()
}
} else {
content
.background(
NavigationLink(
destination: destination(),
isActive: $isPresented
) {
EmptyView()
}
.hidden()
)
}
}
}
}

extension View {
/// Adds backward-compatible navigation, supporting both iOS 16+ and earlier versions.
/// - Parameters:
Expand All @@ -48,22 +96,7 @@ extension View {
isPresented: Binding<Bool>,
@ViewBuilder destination: @escaping () -> Destination
) -> some View {
@Environment(\.navigationOptions)
var navigationOptions

if #available(iOS 16.0, *), navigationOptions.usesNavigationStack {
self.navigationDestination(isPresented: isPresented, destination: destination)
} else {
self.background(
NavigationLink(
destination: destination(),
isActive: isPresented
) {
EmptyView()
}
.hidden() // Keeps the NavigationLink invisible
)
}
modifier(CompatibleNavigationModifier(isPresented: isPresented, destination: destination))
}
}

Expand Down
2 changes: 1 addition & 1 deletion RevenueCatUI/CustomerCenter/Views/CustomerCenterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public struct CustomerCenterView: View {
.environment(\.localization, configuration.localization)
.environment(\.appearance, configuration.appearance)
.environment(\.supportInformation, configuration.support)
// .environment(\.customerCenterPresentationMode, self.mode)
.environment(\.customerCenterPresentationMode, self.mode)
.environment(\.navigationOptions, self.navigationOptions)
} else {
TintedProgressView()
Expand Down
16 changes: 11 additions & 5 deletions RevenueCatUI/CustomerCenter/Views/ManageSubscriptionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ struct ManageSubscriptionsView: View {
}

var body: some View {
content.compatibleNavigation(item: $viewModel.feedbackSurveyData) { feedbackSurveyData in
FeedbackSurveyView(feedbackSurveyData: feedbackSurveyData,
customerCenterActionHandler: self.customerCenterActionHandler,
isPresented: .isNotNil(self.$viewModel.feedbackSurveyData))
content.compatibleNavigation(
item: $viewModel.feedbackSurveyData
) { feedbackSurveyData in
FeedbackSurveyView(
feedbackSurveyData: feedbackSurveyData,
customerCenterActionHandler: self.customerCenterActionHandler,
isPresented: .isNotNil(self.$viewModel.feedbackSurveyData))
}
}

Expand Down Expand Up @@ -124,7 +127,10 @@ struct ManageSubscriptionsView: View {
}, content: { inAppBrowserURL in
SafariView(url: inAppBrowserURL.url)
})
.dismissCircleButtonToolbar()
.applyIf(self.viewModel.screen.type == .management, apply: {
$0.navigationTitle(self.viewModel.screen.title)
.navigationBarTitleDisplayMode(.inline)
})
}

}
Expand Down

0 comments on commit 28d82b6

Please sign in to comment.