Skip to content

Commit

Permalink
Add feedbackSurveyCompleted event to Customer Center events (#4194)
Browse files Browse the repository at this point in the history
Sends a `feedbackSurveyCompleted` event when an option in the feedback
survey is pressed
  • Loading branch information
vegaro authored Aug 30, 2024
1 parent 960a25c commit 465ab0f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
2 changes: 2 additions & 0 deletions RevenueCatUI/CustomerCenter/Data/CustomerCenterAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public enum CustomerCenterAction {
case refundRequestStarted(_ productId: String)
/// Refund request process finished, with result provided.
case refundRequestCompleted(_ refundRequestStatus: RefundRequestStatus)
/// An option of the feedback survey has been selected
case feedbackSurveyCompleted(_ feedbackSurveyOptionId: String)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,31 @@ class FeedbackSurveyViewModel: ObservableObject {

private var purchasesProvider: CustomerCenterPurchasesType
private let loadPromotionalOfferUseCase: LoadPromotionalOfferUseCaseType
private let customerCenterActionHandler: CustomerCenterActionHandler?

convenience init(feedbackSurveyData: FeedbackSurveyData) {
convenience init(feedbackSurveyData: FeedbackSurveyData,
customerCenterActionHandler: CustomerCenterActionHandler?) {
self.init(feedbackSurveyData: feedbackSurveyData,
purchasesProvider: CustomerCenterPurchases(),
loadPromotionalOfferUseCase: LoadPromotionalOfferUseCase())
loadPromotionalOfferUseCase: LoadPromotionalOfferUseCase(),
customerCenterActionHandler: customerCenterActionHandler)
}

init(feedbackSurveyData: FeedbackSurveyData,
purchasesProvider: CustomerCenterPurchasesType,
loadPromotionalOfferUseCase: LoadPromotionalOfferUseCaseType) {
loadPromotionalOfferUseCase: LoadPromotionalOfferUseCaseType,
customerCenterActionHandler: CustomerCenterActionHandler?) {
self.feedbackSurveyData = feedbackSurveyData
self.purchasesProvider = purchasesProvider
self.loadPromotionalOfferUseCase = loadPromotionalOfferUseCase
self.customerCenterActionHandler = customerCenterActionHandler
}

func handleAction(for option: CustomerCenterConfigData.HelpPath.FeedbackSurvey.Option) async {
if let customerCenterActionHandler = self.customerCenterActionHandler {
customerCenterActionHandler(.feedbackSurveyCompleted(option.id))
}

if let promotionalOffer = option.promotionalOffer,
promotionalOffer.eligible {
self.loadingState = option.id
Expand Down
6 changes: 4 additions & 2 deletions RevenueCatUI/CustomerCenter/Views/FeedbackSurveyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ struct FeedbackSurveyView: View {
@Environment(\.colorScheme)
private var colorScheme

init(feedbackSurveyData: FeedbackSurveyData) {
let viewModel = FeedbackSurveyViewModel(feedbackSurveyData: feedbackSurveyData)
init(feedbackSurveyData: FeedbackSurveyData,
customerCenterActionHandler: CustomerCenterActionHandler?) {
let viewModel = FeedbackSurveyViewModel(feedbackSurveyData: feedbackSurveyData,
customerCenterActionHandler: customerCenterActionHandler)
self._viewModel = StateObject(wrappedValue: viewModel)
}

Expand Down
20 changes: 14 additions & 6 deletions RevenueCatUI/CustomerCenter/Views/ManageSubscriptionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,36 @@ struct ManageSubscriptionsView: View {
@StateObject
private var viewModel: ManageSubscriptionsViewModel

private let customerCenterActionHandler: CustomerCenterActionHandler?

init(screen: CustomerCenterConfigData.Screen,
customerCenterActionHandler: CustomerCenterActionHandler?) {
let viewModel = ManageSubscriptionsViewModel(screen: screen,
customerCenterActionHandler: customerCenterActionHandler)
self._viewModel = .init(wrappedValue: viewModel)
self.init(viewModel: viewModel, customerCenterActionHandler: customerCenterActionHandler)
}

fileprivate init(viewModel: ManageSubscriptionsViewModel) {
fileprivate init(viewModel: ManageSubscriptionsViewModel,
customerCenterActionHandler: CustomerCenterActionHandler?) {
self._viewModel = .init(wrappedValue: viewModel)
self.customerCenterActionHandler = customerCenterActionHandler
}

var body: some View {
if #available(iOS 16.0, *) {
content
.navigationDestination(isPresented: .isNotNil(self.$viewModel.feedbackSurveyData)) {
if let feedbackSurveyData = self.viewModel.feedbackSurveyData {
FeedbackSurveyView(feedbackSurveyData: feedbackSurveyData)
FeedbackSurveyView(feedbackSurveyData: feedbackSurveyData,
customerCenterActionHandler: self.customerCenterActionHandler)
}
}
} else {
content
.background(NavigationLink(
destination: self.viewModel.feedbackSurveyData.map { data in
FeedbackSurveyView(feedbackSurveyData: data)
FeedbackSurveyView(feedbackSurveyData: data,
customerCenterActionHandler: self.customerCenterActionHandler)
},
isActive: .isNotNil(self.$viewModel.feedbackSurveyData)
) {
Expand Down Expand Up @@ -209,7 +215,8 @@ struct ManageSubscriptionsView_Previews: PreviewProvider {
subscriptionInformation: CustomerCenterConfigTestData.subscriptionInformationMonthlyRenewing,
customerCenterActionHandler: nil,
refundRequestStatusMessage: "Refund granted successfully!")
ManageSubscriptionsView(viewModel: viewModelMonthlyRenewing)
ManageSubscriptionsView(viewModel: viewModelMonthlyRenewing,
customerCenterActionHandler: nil)
.previewDisplayName("Monthly renewing")
.environment(\.localization, CustomerCenterConfigTestData.customerCenterData.localization)
.environment(\.appearance, CustomerCenterConfigTestData.customerCenterData.appearance)
Expand All @@ -220,7 +227,8 @@ struct ManageSubscriptionsView_Previews: PreviewProvider {
screen: CustomerCenterConfigTestData.customerCenterData.screens[.management]!,
subscriptionInformation: CustomerCenterConfigTestData.subscriptionInformationYearlyExpiring,
customerCenterActionHandler: nil)
ManageSubscriptionsView(viewModel: viewModelYearlyExpiring)
ManageSubscriptionsView(viewModel: viewModelYearlyExpiring,
customerCenterActionHandler: nil)
.previewDisplayName("Yearly expiring")
.environment(\.localization, CustomerCenterConfigTestData.customerCenterData.localization)
.environment(\.appearance, CustomerCenterConfigTestData.customerCenterData.appearance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct SamplePaywallsList: View {
)
case .customerCenter:
#if CUSTOMER_CENTER_ENABLED
CustomerCenterView()
CustomerCenterView(customerCenterActionHandler: self.handleCustomerCenterAction)
#endif
#if PAYWALL_COMPONENTS
case .componentPaywall(let data):
Expand Down Expand Up @@ -248,6 +248,8 @@ extension SamplePaywallsList {
print("CustomerCenter: refundRequestStarted. ProductId: \(productId)")
case .refundRequestCompleted(let status):
print("CustomerCenter: refundRequestCompleted. Result: \(status)")
case .feedbackSurveyCompleted(let option):
print("CustomerCenter: feedbackSurveyCompleted. Option selected: \(option)")
}
}
}
Expand Down

0 comments on commit 465ab0f

Please sign in to comment.