-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
532 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
RevenueCatUI/CustomerCenter/ViewModels/FeedbackSurveyViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// 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 | ||
// | ||
// FeedbackSurveyViewModel.swift | ||
// | ||
// | ||
// Created by Cesar de la Vega on 17/6/24. | ||
// | ||
|
||
import Foundation | ||
import RevenueCat | ||
|
||
#if !os(macOS) && !os(tvOS) && !os(watchOS) && !os(visionOS) | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
@available(macOS, unavailable) | ||
@available(tvOS, unavailable) | ||
@available(watchOS, unavailable) | ||
@MainActor | ||
class FeedbackSurveyViewModel: ObservableObject { | ||
|
||
typealias CustomerInfoFetcher = @Sendable () async throws -> CustomerInfo | ||
|
||
@Published | ||
var feedbackSurveyData: FeedbackSurveyData | ||
@Published | ||
var isShowingPromotionalOffer: Bool = false | ||
@Published | ||
var loadingStates: [String: Bool] = [:] | ||
|
||
var promotionalOffer: PromotionalOffer? { | ||
return promotionalOfferViewModel.promotionalOffer | ||
} | ||
|
||
var product: StoreProduct? { | ||
return promotionalOfferViewModel.product | ||
} | ||
|
||
private var customerInfoFetcher: CustomerInfoFetcher | ||
private var promotionalOfferViewModel: PromotionalOfferViewModel | ||
|
||
convenience init(feedbackSurveyData: FeedbackSurveyData) { | ||
self.init(feedbackSurveyData: feedbackSurveyData, | ||
promotionalOfferViewModel: PromotionalOfferViewModel(), | ||
customerInfoFetcher: { | ||
guard Purchases.isConfigured else { | ||
throw PaywallError.purchasesNotConfigured | ||
} | ||
|
||
return try await Purchases.shared.customerInfo() | ||
}) | ||
} | ||
|
||
// @PublicForExternalTesting | ||
init(feedbackSurveyData: FeedbackSurveyData, | ||
promotionalOfferViewModel: PromotionalOfferViewModel, | ||
customerInfoFetcher: @escaping CustomerInfoFetcher) { | ||
self.feedbackSurveyData = feedbackSurveyData | ||
self.promotionalOfferViewModel = promotionalOfferViewModel | ||
self.customerInfoFetcher = customerInfoFetcher | ||
} | ||
|
||
func handleAction(for option: CustomerCenterConfigData.HelpPath.FeedbackSurvey.Option) async { | ||
if let promotionalOffer = option.promotionalOffer { | ||
self.loadingStates[option.id] = true | ||
await promotionalOfferViewModel.loadPromo(promotionalOfferId: promotionalOffer.iosOfferId) | ||
self.isShowingPromotionalOffer = true | ||
} else { | ||
self.feedbackSurveyData.action() | ||
} | ||
} | ||
|
||
func handleSheetDismiss() { | ||
self.feedbackSurveyData.action() | ||
self.loadingStates.removeAll() | ||
} | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
RevenueCatUI/CustomerCenter/ViewModels/PromotionalOfferViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// | ||
// 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 | ||
// | ||
// PromotionalOfferViewModel.swift | ||
// | ||
// | ||
// Created by Cesar de la Vega on 17/6/24. | ||
// | ||
|
||
import Foundation | ||
import RevenueCat | ||
|
||
#if !os(macOS) && !os(tvOS) && !os(watchOS) && !os(visionOS) | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
@available(macOS, unavailable) | ||
@available(tvOS, unavailable) | ||
@available(watchOS, unavailable) | ||
@MainActor | ||
class PromotionalOfferViewModel: ObservableObject { | ||
|
||
typealias CustomerInfoFetcher = @Sendable () async throws -> CustomerInfo | ||
|
||
@Published | ||
var product: StoreProduct? | ||
@Published | ||
var promotionalOffer: PromotionalOffer? | ||
@Published | ||
var transaction: StoreTransaction? | ||
|
||
private var customerInfoFetcher: CustomerInfoFetcher | ||
|
||
convenience init() { | ||
self.init(product: nil, promotionalOffer: nil) | ||
} | ||
|
||
convenience init(product: StoreProduct?, | ||
promotionalOffer: PromotionalOffer?) { | ||
self.init(product: product, | ||
promotionalOffer: promotionalOffer, | ||
customerInfoFetcher: { | ||
guard Purchases.isConfigured else { | ||
throw PaywallError.purchasesNotConfigured | ||
} | ||
|
||
return try await Purchases.shared.customerInfo() | ||
}) | ||
} | ||
|
||
// @PublicForExternalTesting | ||
init(product: StoreProduct?, | ||
promotionalOffer: PromotionalOffer?, | ||
customerInfoFetcher: @escaping CustomerInfoFetcher) { | ||
self.product = product | ||
self.promotionalOffer = promotionalOffer | ||
self.customerInfoFetcher = customerInfoFetcher | ||
} | ||
|
||
func purchasePromo() async { | ||
guard let promotionalOffer = self.promotionalOffer, | ||
let product = self.product else { | ||
print("Promotional offer not loaded") | ||
return | ||
} | ||
do { | ||
let purchase = try await Purchases.shared.purchase(product: product, promotionalOffer: promotionalOffer) | ||
self.transaction = purchase.transaction | ||
} catch { | ||
print("Error purchasing product with promotional offer: \(error)") | ||
} | ||
} | ||
|
||
func loadPromo(promotionalOfferId: String) async { | ||
do { | ||
let customerInfo = try await self.customerInfoFetcher() | ||
let activeSubscriptionProductIds = customerInfo.activeSubscriptions | ||
|
||
guard let appStoreSubscription = customerInfo.entitlements.active.first(where: { | ||
$0.value.store == .appStore | ||
}) else { | ||
print("No active App Store subscriptions found") | ||
return | ||
} | ||
|
||
let productId = appStoreSubscription.value.productIdentifier | ||
let products = await Purchases.shared.products([productId]) | ||
guard let product = products.first(where: { product in | ||
product.discounts.contains { $0.offerIdentifier == promotionalOfferId } | ||
}) else { | ||
print("No active product found with the given promotional offer ID") | ||
return | ||
} | ||
|
||
self.product = product | ||
|
||
if let discount = product.discounts.first(where: { $0.offerIdentifier == promotionalOfferId }) { | ||
do { | ||
let promotionalOffer = try await Purchases.shared.promotionalOffer(forProductDiscount: discount, | ||
product: product) | ||
self.promotionalOffer = promotionalOffer | ||
} catch { | ||
print("Error fetching promotional offer") | ||
return | ||
} | ||
} | ||
} catch { | ||
print("Error fetching promotional offer for active product: \(error)") | ||
return | ||
} | ||
} | ||
|
||
} | ||
|
||
#endif |
Oops, something went wrong.