Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions RevenueCat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@
356979E02CCFDAA100EE6A9E /* CustomerInfoFixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 356979DF2CCFDA9C00EE6A9E /* CustomerInfoFixtures.swift */; };
356E2DE82CD3CF930055AABB /* StoredEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 356E2DE72CD3CF8F0055AABB /* StoredEventTests.swift */; };
357349012C3BEB5C000EEB86 /* CustomerCenterConfigDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 357348FF2C3BEB0A000EEB86 /* CustomerCenterConfigDataTests.swift */; };
357C43AF2D80510A00B96769 /* CustomerCenterManagementOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = 357C43AE2D80510400B96769 /* CustomerCenterManagementOption.swift */; };
357CEC702C5940CE00A80837 /* ColorFromAppearance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 357CEC6F2C5940CE00A80837 /* ColorFromAppearance.swift */; };
3592E8862C2ED51700D7F91D /* CustomerCenterConfigCallback.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3592E8852C2ED51700D7F91D /* CustomerCenterConfigCallback.swift */; };
3592E88A2C2ED54A00D7F91D /* CustomerCenterConfigData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3592E8882C2ED54A00D7F91D /* CustomerCenterConfigData.swift */; };
Expand Down Expand Up @@ -1670,6 +1671,7 @@
356979DF2CCFDA9C00EE6A9E /* CustomerInfoFixtures.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomerInfoFixtures.swift; sourceTree = "<group>"; };
356E2DE72CD3CF8F0055AABB /* StoredEventTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoredEventTests.swift; sourceTree = "<group>"; };
357348FF2C3BEB0A000EEB86 /* CustomerCenterConfigDataTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomerCenterConfigDataTests.swift; sourceTree = "<group>"; };
357C43AE2D80510400B96769 /* CustomerCenterManagementOption.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomerCenterManagementOption.swift; sourceTree = "<group>"; };
357C9BC022725CFA006BC624 /* iAd.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = iAd.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.2.sdk/System/Library/Frameworks/iAd.framework; sourceTree = DEVELOPER_DIR; };
357CEC6F2C5940CE00A80837 /* ColorFromAppearance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorFromAppearance.swift; sourceTree = "<group>"; };
3592E8852C2ED51700D7F91D /* CustomerCenterConfigCallback.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomerCenterConfigCallback.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3754,6 +3756,7 @@
353756562C382C2800A1B8D6 /* Data */ = {
isa = PBXGroup;
children = (
357C43AE2D80510400B96769 /* CustomerCenterManagementOption.swift */,
355FDB5A2D783FC400D20E65 /* CustomerCenterActionWrapper.swift */,
355FDB5B2D783FC400D20E65 /* CustomerCenterActionViewModifier.swift */,
77791ECE2C6B851F00BCEF03 /* SemanticVersion.swift */,
Expand Down Expand Up @@ -6989,6 +6992,7 @@
574D1C712D3E75F9005840CD /* PurchaseHistoryView.swift in Sources */,
574D1C722D3E75F9005840CD /* PurchaseLinkView.swift in Sources */,
88B1BAFC2C813A3C001B7EE5 /* ImageComponentView.swift in Sources */,
357C43AF2D80510A00B96769 /* CustomerCenterManagementOption.swift in Sources */,
2C91068A2CE22D3500189565 /* FlexVStack.swift in Sources */,
571197642D3AE403000BC39E /* CustomerCenterNavigationOptions.swift in Sources */,
4D3BA5B22D47AB4400668AFC /* TimelineComponentView.swift in Sources */,
Expand Down
51 changes: 51 additions & 0 deletions RevenueCatUI/CustomerCenter/CustomerCenterView+Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ extension CustomerCenterView {
/// A closure used for notifying when a feedback survey option is selected in the Customer Center.
public typealias FeedbackSurveyCompletedHandler = @MainActor @Sendable (_ optionId: String) -> Void

/// A closure used for notifying when a management option is selected in the Customer Center.
public typealias ManagementOptionSelectedHandler =
@MainActor @Sendable (_ managementOption: CustomerCenterActionable) -> Void

// MARK: - Preference Keys

struct RestoreStartedPreferenceKey: PreferenceKey {
Expand Down Expand Up @@ -100,6 +104,14 @@ extension CustomerCenterView {
}
}

struct ManagementOptionSelectedPreferenceKey: PreferenceKey {
static var defaultValue: UniqueWrapper<CustomerCenterActionable>?
static func reduce(value: inout UniqueWrapper<CustomerCenterActionable>?,
nextValue: () -> UniqueWrapper<CustomerCenterActionable>?) {
value = nextValue() ?? value
}
}

// MARK: - View Modifiers

fileprivate struct OnRestoreStartedModifier: ViewModifier {
Expand Down Expand Up @@ -192,6 +204,19 @@ extension CustomerCenterView {
}
}
}

fileprivate struct OnManagementOptionModifier: ViewModifier {
let handler: ManagementOptionSelectedHandler

func body(content: Content) -> some View {
content
.onPreferenceChange(ManagementOptionSelectedPreferenceKey.self) { wrapper in
if let wrapper = wrapper {
handler(wrapper.value)
}
}
}
}
}

// MARK: - View Extensions
Expand Down Expand Up @@ -334,6 +359,32 @@ extension View {
) -> some View {
return self.modifier(CustomerCenterView.OnFeedbackSurveyCompletedModifier(handler: handler))
}

/// Invokes the given closure when a management option is selected in the Customer Center.
/// Example:
/// ```swift
/// var body: some View {
/// ContentView()
/// .sheet(isPresented: self.$displayCustomerCenter) {
/// CustomerCenterView()
/// .onCustomerCenterManagementOptionSelected { action in
/// switch action {
/// case is CustomerCenterManagementOption.Cancel:
/// print("Cancel action triggered")
/// case let customUrl as CustomerCenterManagementOption.CustomUrl:
/// print("Opening URL: \(customUrl.url)")
/// default:
/// print("Unknown action")
/// }
/// }
/// }
/// }
/// ```
public func onCustomerCenterManagementOptionSelected(
_ handler: @escaping CustomerCenterView.ManagementOptionSelectedHandler
) -> some View {
return self.modifier(CustomerCenterView.OnManagementOptionModifier(handler: handler))
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ struct CustomerCenterActionViewModifier: ViewModifier {

let actionWrapper: CustomerCenterActionWrapper

// Use counter to track restore events instead of boolean flag
// Each increment creates a unique restore event
@State private var restoreStarted: UniqueWrapper<Void>?
@State private var restoreFailed: UniqueWrapper<NSError>?
@State private var restoreCompleted: UniqueWrapper<CustomerInfo>?
// Counter for manage subscriptions to ensure unique values
@State private var showingManageSubscriptions: UniqueWrapper<Void>?
@State private var refundRequestStarted: UniqueWrapper<String>?
@State private var refundRequestCompleted: UniqueWrapper<(String, RefundRequestStatus)>?
@State private var feedbackSurveyCompleted: UniqueWrapper<String>?
@State private var managementOptionSelected: UniqueWrapper<CustomerCenterActionable>?

func body(content: Content) -> some View {
content
Expand Down Expand Up @@ -79,6 +77,10 @@ struct CustomerCenterActionViewModifier: ViewModifier {
actionWrapper.setFeedbackSurveyCompleted = { reason in
feedbackSurveyCompleted = UniqueWrapper(value: reason)
}

actionWrapper.setManagementOptionSelected = { action in
managementOptionSelected = UniqueWrapper(value: action)
}
}
// Apply preferences based on state
.preference(key: CustomerCenterView.RestoreStartedPreferenceKey.self,
Expand All @@ -95,6 +97,8 @@ struct CustomerCenterActionViewModifier: ViewModifier {
value: refundRequestCompleted)
.preference(key: CustomerCenterView.FeedbackSurveyCompletedPreferenceKey.self,
value: feedbackSurveyCompleted)
.preference(key: CustomerCenterView.ManagementOptionSelectedPreferenceKey.self,
value: managementOptionSelected)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal enum CustomerCenterInternalAction {
case feedbackSurveyCompleted(String)

// New internal-only actions that don't exist in the public legacy CustomerCenterAction
case buttonTapped(buttonId: String)
case buttonTapped(action: CustomerCenterActionable)

/// Converts this internal action to the corresponding legacy action if one exists
/// Returns nil for actions that don't have a legacy CustomerCenterAction equivalent
Expand Down Expand Up @@ -68,7 +68,7 @@ final class CustomerCenterActionWrapper {
var setRefundRequestStarted: (String) -> Void = { _ in }
var setRefundRequestCompleted: (String, RefundRequestStatus) -> Void = { _, _ in }
var setFeedbackSurveyCompleted: (String) -> Void = { _ in }
var setButtonTapped: (String) -> Void = { _ in }
var setManagementOptionSelected: (CustomerCenterActionable) -> Void = { _ in }

// The handler for legacy actions
private let legacyActionHandler: DeprecatedCustomerCenterActionHandler?
Expand Down Expand Up @@ -106,8 +106,44 @@ final class CustomerCenterActionWrapper {
setRefundRequestCompleted(productId, status)
case .feedbackSurveyCompleted(let optionId):
setFeedbackSurveyCompleted(optionId)
case .buttonTapped(let buttonId):
setButtonTapped(buttonId)
case .buttonTapped(let action):
setManagementOptionSelected(action)
}
}
}

// MARK: - Help Path to Management Option Conversion
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
extension CustomerCenterConfigData.HelpPath {

/// Converts this HelpPath to an appropriate CustomerCenterActionable
/// - Returns: A CustomerCenterActionable representing this path
func asAction() -> CustomerCenterActionable? {
switch self.type {
case .missingPurchase:
return CustomerCenterManagementOption.MissingPurchase()

case .refundRequest:
return CustomerCenterManagementOption.RefundRequest()

case .changePlans:
return CustomerCenterManagementOption.ChangePlans()

case .cancel:
return CustomerCenterManagementOption.Cancel()

case .customUrl:
if let url = self.url {
return CustomerCenterManagementOption.CustomUrl(url: url)
}

default:
break
}
return nil
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// CustomerCenterManagementOption.swift
//
// Created by Cesar de la Vega on 11/3/25.

import Foundation

/// Protocol for action types that can be handled by the Customer Center
public protocol CustomerCenterActionable {}

/// Management options that can be triggered by buttons in the Customer Center
public enum CustomerCenterManagementOption {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is painful, not being able to use a "real" enum for this 😢

/// Represents a cancel action
public struct Cancel: CustomerCenterActionable {}

/// Represents an action to open a custom URL
public struct CustomUrl: CustomerCenterActionable {
/// The URL that will be opened
public let url: URL
}

/// Represents a missing purchase (restore) action
public struct MissingPurchase: CustomerCenterActionable {}

/// Represents a refund request action
public struct RefundRequest: CustomerCenterActionable {}

/// Represents a change plans action
public struct ChangePlans: CustomerCenterActionable {}
}
10 changes: 9 additions & 1 deletion RevenueCatUI/CustomerCenter/View+PresentCustomerCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ extension View {
refundRequestStarted: CustomerCenterView.RefundRequestStartedHandler? = nil,
refundRequestCompleted: CustomerCenterView.RefundRequestCompletedHandler? = nil,
feedbackSurveyCompleted: CustomerCenterView.FeedbackSurveyCompletedHandler? = nil,
managementOptionSelected: CustomerCenterView.ManagementOptionSelectedHandler? = nil,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I totally missed this

onDismiss: (() -> Void)? = nil
) -> some View {
self.modifier(
Expand All @@ -161,7 +162,8 @@ extension View {
showingManageSubscriptions: showingManageSubscriptions,
refundRequestStarted: refundRequestStarted,
refundRequestCompleted: refundRequestCompleted,
feedbackSurveyCompleted: feedbackSurveyCompleted
feedbackSurveyCompleted: feedbackSurveyCompleted,
managementOptionSelected: managementOptionSelected
)
)
}
Expand All @@ -181,6 +183,7 @@ private struct PresentingCustomerCenterModifier: ViewModifier {
let refundRequestStarted: CustomerCenterView.RefundRequestStartedHandler?
let refundRequestCompleted: CustomerCenterView.RefundRequestCompletedHandler?
let feedbackSurveyCompleted: CustomerCenterView.FeedbackSurveyCompletedHandler?
let managementOptionSelected: CustomerCenterView.ManagementOptionSelectedHandler?

/// The closure to execute when dismissing the sheet / fullScreen present
let onDismiss: (() -> Void)?
Expand All @@ -197,6 +200,7 @@ private struct PresentingCustomerCenterModifier: ViewModifier {
refundRequestStarted: CustomerCenterView.RefundRequestStartedHandler? = nil,
refundRequestCompleted: CustomerCenterView.RefundRequestCompletedHandler? = nil,
feedbackSurveyCompleted: CustomerCenterView.FeedbackSurveyCompletedHandler? = nil,
managementOptionSelected: CustomerCenterView.ManagementOptionSelectedHandler? = nil,
purchaseHandler: PurchaseHandler? = nil
) {
self._isPresented = isPresented
Expand All @@ -209,6 +213,7 @@ private struct PresentingCustomerCenterModifier: ViewModifier {
self.refundRequestStarted = refundRequestStarted
self.refundRequestCompleted = refundRequestCompleted
self.feedbackSurveyCompleted = feedbackSurveyCompleted
self.managementOptionSelected = managementOptionSelected
self._purchaseHandler = .init(wrappedValue: purchaseHandler ??
PurchaseHandler.default(performPurchase: myAppPurchaseLogic?.performPurchase,
performRestore: myAppPurchaseLogic?.performRestore))
Expand Down Expand Up @@ -265,6 +270,9 @@ private struct PresentingCustomerCenterModifier: ViewModifier {
.onCustomerCenterFeedbackSurveyCompleted { [feedbackSurveyCompleted] optionId in
feedbackSurveyCompleted?(optionId)
}
.onCustomerCenterManagementOptionSelected { action in
managementOptionSelected?(action)
}
.interactiveDismissDisabled(self.purchaseHandler.actionInProgress)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ final class ManageSubscriptionsViewModel: ObservableObject {

#if os(iOS) || targetEnvironment(macCatalyst)
func determineFlow(for path: CustomerCenterConfigData.HelpPath) async {
// Convert the path to an appropriate action using the extension
if let action = path.asAction() {
// Send the action through the action wrapper
self.actionWrapper.handleAction(.buttonTapped(action: action))
}

switch path.detail {
case let .feedbackSurvey(feedbackSurvey):
self.feedbackSurveyData = FeedbackSurveyData(configuration: feedbackSurvey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,22 @@ struct SamplePaywallsList: View {
#if os(iOS)
.presentCustomerCenter(
isPresented: self.$presentingCustomerCenterSheet,
customerCenterActionHandler: self.handleCustomerCenterAction,
managementOptionSelected: { button in
switch button {
case is CustomerCenterManagementOption.Cancel:
print("Cancel action triggered")
case let customUrl as CustomerCenterManagementOption.CustomUrl:
print("Opening URL: \(customUrl.url)")
case is CustomerCenterManagementOption .MissingPurchase:
print("Missing purchase triggered")
case is CustomerCenterManagementOption.RefundRequest:
print("RefundRequest triggered")
case is CustomerCenterManagementOption.ChangePlans:
print("ChangePlans triggered")
default:
print("Unknown action")
}
},
onDismiss: { self.presentingCustomerCenterFullScreen = false }
)
.presentCustomerCenter(
Expand Down