Skip to content

Commit

Permalink
@jamesrb1 suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
vegaro committed Jun 26, 2024
1 parent a5baa89 commit 9c89313
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import RevenueCat
typealias CustomerInfoFetcher = @Sendable () async throws -> CustomerInfo

@Published
var hasSubscriptions: Bool = false
private(set) var hasSubscriptions: Bool = false
@Published
var subscriptionsAreFromApple: Bool = false
private(set) var subscriptionsAreFromApple: Bool = false
@Published
var state: CustomerCenterViewState {
private(set) var state: CustomerCenterViewState {
didSet {
if case let .error(stateError) = state {
self.error = stateError
Expand Down Expand Up @@ -82,11 +82,10 @@ import RevenueCat
let customerInfo = try await self.customerInfoFetcher()
let hasSubscriptions = customerInfo.activeSubscriptions.count > 0

let subscriptionsAreFromApple = customerInfo.entitlements.active.first(where: {
$0.value.store == .appStore || $0.value.store == .macAppStore
}).map { entitlement in
let subscriptionsAreFromApple = customerInfo.entitlements.active.contains(where: { entitlement in
entitlement.value.store == .appStore || entitlement.value.store == .macAppStore &&
customerInfo.activeSubscriptions.contains(entitlement.value.productIdentifier)
} ?? false
})

self.hasSubscriptions = hasSubscriptions
self.subscriptionsAreFromApple = subscriptionsAreFromApple
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import RevenueCat
class ManageSubscriptionsViewModel: ObservableObject {

@Published
var subscriptionInformation: SubscriptionInformation?
private(set) var subscriptionInformation: SubscriptionInformation?
@Published
var refundRequestStatusMessage: String?
private(set) var refundRequestStatusMessage: String?
@Published
var configuration: CustomerCenterConfigData?
private(set) var configuration: CustomerCenterConfigData?
@Published
var showRestoreAlert: Bool = false
@Published
Expand Down Expand Up @@ -79,7 +79,7 @@ class ManageSubscriptionsViewModel: ObservableObject {
}
}

func loadSubscriptionInformation() async throws {
private func loadSubscriptionInformation() async throws {
let customerInfo = try await purchasesProvider.customerInfo()
guard let currentEntitlementDict = customerInfo.entitlements.active.first,
let subscribedProductID = customerInfo.activeSubscriptions.first,
Expand All @@ -104,17 +104,17 @@ class ManageSubscriptionsViewModel: ObservableObject {
)
}

func loadCustomerCenterConfig() async {
private func loadCustomerCenterConfig() {
self.configuration = CustomerCenterConfigTestData.customerCenterData
}

#if os(iOS) || targetEnvironment(macCatalyst)
func handleAction(for path: CustomerCenterConfigData.HelpPath) {
func handleAction(for path: CustomerCenterConfigData.HelpPath) async {
switch path.type {
case .missingPurchase:
self.showRestoreAlert = true
case .refundRequest:
Task {
do {
guard let subscriptionInformation = self.subscriptionInformation else { return }
let productId = subscriptionInformation.productIdentifier
let status = try await purchasesProvider.beginRefundRequest(forProduct: productId)
Expand All @@ -126,10 +126,14 @@ class ManageSubscriptionsViewModel: ObservableObject {
case .userCancelled:
self.refundRequestStatusMessage = String(localized: "Refund canceled")
}
} catch {
self.refundRequestStatusMessage = String(localized: "An error occurred while processing the refund request.")
}
case .changePlans, .cancel:
Task {
do {
try await purchasesProvider.showManageSubscriptions()
} catch {
self.state = .error(error)
}
default:
break
Expand All @@ -155,11 +159,11 @@ private final class ManageSubscriptionPurchases: ManageSubscriptionsPurchaseType
}

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

func products(_ productIdentifiers: [String]) async -> [StoreProduct] {
return await Purchases.shared.products(productIdentifiers)
await Purchases.shared.products(productIdentifiers)
}

}
Expand Down
10 changes: 6 additions & 4 deletions RevenueCatUI/CustomerCenter/Views/ManageSubscriptionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct ManageSubscriptionsView: View {
}
}
.task {
await checkAndLoadInformation()
await loadInformationIfNeeded()
}
}

Expand All @@ -69,7 +69,7 @@ struct ManageSubscriptionsView: View {
@available(visionOS, unavailable)
private extension ManageSubscriptionsView {

func checkAndLoadInformation() async {
func loadInformationIfNeeded() async {
if !viewModel.isLoaded {
await viewModel.loadScreen()
}
Expand Down Expand Up @@ -157,8 +157,10 @@ struct ManageSubscriptionsButtonsView: View {
#endif
}
ForEach(filteredPaths, id: \.id) { path in
Button(path.title) {
self.viewModel.handleAction(for: path)
AsyncButton(action: {
await self.viewModel.handleAction(for: path)
}) {
Text(path.title)
}
.restorePurchasesAlert(isPresented: $viewModel.showRestoreAlert)
.buttonStyle(ManageSubscriptionsButtonStyle())
Expand Down

0 comments on commit 9c89313

Please sign in to comment.