Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Customer Center]: fix navigation when embedded in NavigationStack #4622

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ import SwiftUI
@available(watchOS, unavailable)
struct CompatibilityNavigationStack<Content: View>: View {

@ViewBuilder var content: Content
@ViewBuilder var content: () -> Content

init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}

var body: some View {
if #available(iOS 16.0, *) {
NavigationStack {
content
content()
}
} else {
NavigationView {
content
content()
}
}
}
Expand Down
46 changes: 38 additions & 8 deletions RevenueCatUI/CustomerCenter/Views/CustomerCenterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,53 @@ public struct CustomerCenterView: View {

private let mode: CustomerCenterPresentationMode

/// A flag indicating whether the view is already embedded in a navigation stack.
///
/// - When set to `true`, the view must be part of an existing NavigationStack / NavigationView.
/// - When set to `false`, the view is not part of an external NavigationStack / NavigationView and uses
/// one internally.
private let isEmbeddedInNavigationStack: Bool

/// Create a view to handle common customer support tasks
/// - Parameters:
/// - customerCenterActionHandler: An optional `CustomerCenterActionHandler` to handle actions
/// from the Customer Center.
public init(customerCenterActionHandler: CustomerCenterActionHandler? = nil) {
self.init(customerCenterActionHandler: customerCenterActionHandler, mode: .default)
/// - isEmbeddedInNavigationStack: Whether this view is already inside a NavigationStack / NavigationView
public init(
customerCenterActionHandler: CustomerCenterActionHandler? = nil,
isEmbeddedInNavigationStack: Bool = false
) {
self.init(
customerCenterActionHandler: customerCenterActionHandler,
mode: .default,
isEmbeddedInNavigationStack: isEmbeddedInNavigationStack
)
}

/// Create a view to handle common customer support tasks
/// - Parameters:
/// - customerCenterActionHandler: An optional `CustomerCenterActionHandler` to handle actions
/// from the Customer Center.
init(customerCenterActionHandler: CustomerCenterActionHandler? = nil,
mode: CustomerCenterPresentationMode) {
/// - mode: The presentation mode for the Customer Center
/// - isEmbeddedInNavigationStack: Whether this view is already inside a navigation stack
init(
customerCenterActionHandler: CustomerCenterActionHandler? = nil,
mode: CustomerCenterPresentationMode,
isEmbeddedInNavigationStack: Bool = false
) {
self._viewModel = .init(wrappedValue:
CustomerCenterViewModel(customerCenterActionHandler: customerCenterActionHandler))
self.mode = mode
self.isEmbeddedInNavigationStack = isEmbeddedInNavigationStack
}

fileprivate init(viewModel: CustomerCenterViewModel,
mode: CustomerCenterPresentationMode = CustomerCenterPresentationMode.default) {
fileprivate init(
viewModel: CustomerCenterViewModel,
mode: CustomerCenterPresentationMode = CustomerCenterPresentationMode.default
) {
self._viewModel = .init(wrappedValue: viewModel)
self.mode = mode
self.isEmbeddedInNavigationStack = false
}

// swiftlint:disable:next missing_docs
Expand Down Expand Up @@ -157,8 +181,14 @@ private extension CustomerCenterView {
let accentColor = Color.from(colorInformation: configuration.appearance.accentColor,
for: self.colorScheme)

CompatibilityNavigationStack {
destinationContent(configuration: configuration)
Group {
if isEmbeddedInNavigationStack {
destinationContent(configuration: configuration)
} else {
CompatibilityNavigationStack {
destinationContent(configuration: configuration)
}
}
}
.applyIf(accentColor != nil, apply: { $0.tint(accentColor) })
}
Expand Down