Skip to content

Commit

Permalink
Always reload CustomerInfo when Customer Center is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
fire-at-will authored Dec 11, 2024
1 parent 24d6660 commit fa27846
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ protocol CustomerCenterPurchasesType: Sendable {
var isSandbox: Bool { get }

@Sendable
func customerInfo() async throws -> CustomerInfo
func customerInfo(
fetchPolicy: CacheFetchPolicy
) async throws -> CustomerInfo

@Sendable
func products(_ productIdentifiers: [String]) async -> [StoreProduct]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ final class CustomerCenterPurchases: CustomerCenterPurchasesType {
return Purchases.shared.isSandbox
}

func customerInfo() async throws -> RevenueCat.CustomerInfo {
try await Purchases.shared.customerInfo()
func customerInfo(
fetchPolicy: CacheFetchPolicy
) async throws -> RevenueCat.CustomerInfo {
try await Purchases.shared.customerInfo(fetchPolicy: fetchPolicy)
}

func products(_ productIdentifiers: [String]) async -> [StoreProduct] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class LoadPromotionalOfferUseCase: LoadPromotionalOfferUseCaseType {
promoOfferDetails: CustomerCenterConfigData.HelpPath.PromotionalOffer
) async -> Result<PromotionalOfferData, Error> {
do {
let customerInfo = try await self.purchasesProvider.customerInfo()
let customerInfo = try await self.purchasesProvider.customerInfo(fetchPolicy: .default)

let subscribedProduct = try await getActiveSubscription(customerInfo)
let discount = try findDiscount(for: subscribedProduct,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import RevenueCat

func loadPurchaseInformation() async {
do {
let customerInfo = try await purchasesProvider.customerInfo()
let customerInfo = try await purchasesProvider.customerInfo(fetchPolicy: .fetchCurrent)
let hasActiveProducts =
!customerInfo.activeSubscriptions.isEmpty || !customerInfo.nonSubscriptions.isEmpty

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ class CustomerCenterViewModelTests: TestCase {
expect(viewModel.isLoaded) == true
}

func testLoadPurchaseInformationAlwaysRefreshesCustomerInfo() async throws {
let mockPurchases = MockCustomerCenterPurchases(
customerInfo: CustomerCenterViewModelTests.customerInfoWithAppleSubscriptions
)

let viewModel = CustomerCenterViewModel(
customerCenterActionHandler: nil,
purchasesProvider: mockPurchases
)

await viewModel.loadPurchaseInformation()

expect(mockPurchases.customerInfoFetchPolicy) == .fetchCurrent
}

func testLoadHasSubscriptionsApple() async throws {
let mockPurchases =
MockCustomerCenterPurchases(customerInfo: CustomerCenterViewModelTests.customerInfoWithAppleSubscriptions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ final class MockCustomerCenterPurchases: @unchecked Sendable, CustomerCenterPurc
self.beginRefundShouldFail = beginRefundShouldFail
}

func customerInfo() async throws -> RevenueCat.CustomerInfo {
var customerInfoFetchPolicy: CacheFetchPolicy?
func customerInfo(fetchPolicy: CacheFetchPolicy) async throws -> RevenueCat.CustomerInfo {
customerInfoFetchPolicy = fetchPolicy

if let customerInfoError {
throw customerInfoError
}
Expand Down

0 comments on commit fa27846

Please sign in to comment.