Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vegaro committed Nov 27, 2024
1 parent a710691 commit 6a7d6e8
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ protocol CustomerCenterPurchasesType: Sendable {
promotionalOffer: PromotionalOffer
) async throws -> PurchaseResultData

@Sendable
func track(customerCenterEvent: CustomerCenterEvent) async throws

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,4 @@ protocol ManageSubscriptionsPurchaseType: Sendable {
@Sendable
func beginRefundRequest(forProduct productID: String) async throws -> RefundRequestStatus

@Sendable
func trackImpression(_ eventData: CustomerCenterEvent.Data) async throws

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ final class CustomerCenterPurchases: CustomerCenterPurchasesType {
promotionalOffer: promotionalOffer
)
}

func track(customerCenterEvent: CustomerCenterEvent) async throws {
await Purchases.shared.track(customerCenterEvent: customerCenterEvent)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import RevenueCat
// We fail open.
private static let defaultAppIsLatestVersion = true

typealias CustomerInfoFetcher = @Sendable () async throws -> CustomerInfo
typealias CurrentVersionFetcher = () -> String?

private lazy var currentAppVersion: String? = currentVersionFetcher()
Expand All @@ -37,6 +36,7 @@ import RevenueCat
private(set) var hasAppleEntitlement: Bool = false
@Published
private(set) var appIsLatestVersion: Bool = defaultAppIsLatestVersion
private(set) var purchasesProvider: CustomerCenterPurchasesType

// @PublicForExternalTesting
@Published
Expand Down Expand Up @@ -68,26 +68,22 @@ import RevenueCat
return state != .notLoaded && configuration != nil
}

private var customerInfoFetcher: CustomerInfoFetcher
private let currentVersionFetcher: CurrentVersionFetcher
internal let customerCenterActionHandler: CustomerCenterActionHandler?

private var error: Error?

init(
customerCenterActionHandler: CustomerCenterActionHandler?,
customerInfoFetcher: @escaping CustomerInfoFetcher = {
guard Purchases.isConfigured else { throw PaywallError.purchasesNotConfigured }
return try await Purchases.shared.customerInfo()
},
currentVersionFetcher: @escaping CurrentVersionFetcher = {
Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
}
},
purchasesProvider: CustomerCenterPurchasesType = CustomerCenterPurchases()
) {
self.state = .notLoaded
self.customerInfoFetcher = customerInfoFetcher
self.currentVersionFetcher = currentVersionFetcher
self.customerCenterActionHandler = customerCenterActionHandler
self.purchasesProvider = purchasesProvider
}

#if DEBUG
Expand All @@ -106,7 +102,7 @@ import RevenueCat

func loadHasActivePurchases() async {
do {
let customerInfo = try await self.customerInfoFetcher()
let customerInfo = try await purchasesProvider.customerInfo()
self.hasActiveProducts = customerInfo.activeSubscriptions.count > 0 ||
customerInfo.nonSubscriptions.count > 0
self.hasAppleEntitlement = customerInfo.entitlements.active.contains { entitlement in
Expand Down Expand Up @@ -139,9 +135,12 @@ import RevenueCat
}
}

func trackImpression() {
// swiftlint:disable:next todo
// TODO: implement tracking impression
func trackImpression(_ eventData: CustomerCenterEvent.Data) {
let event = CustomerCenterEvent.impression(CustomerCenterEvent.CreationData(), eventData)

Task.detached(priority: .background) { [purchasesProvider = self.purchasesProvider] in
try await purchasesProvider.track(customerCenterEvent: event)
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,6 @@ private final class ManageSubscriptionPurchases: ManageSubscriptionsPurchaseType
await Purchases.shared.products(productIdentifiers)
}

func trackImpression(_ eventData: CustomerCenterEvent.Data) async throws {
await Purchases.shared.track(customerCenterEvent: CustomerCenterEvent.impression(.init(), eventData))
}

}

#endif
5 changes: 4 additions & 1 deletion RevenueCatUI/CustomerCenter/Views/CustomerCenterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ private extension CustomerCenterView {
}

func trackImpression() {
viewModel.trackImpression()
let eventData = CustomerCenterEvent.Data(sessionID: CustomerCenterEvent.SessionID(),
locale: .current,
darkMode: self.colorScheme == .dark)
viewModel.trackImpression(eventData)
}

}
Expand Down
11 changes: 5 additions & 6 deletions Sources/CustomerCenter/Events/CustomerCenterEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ extension CustomerCenterEvent {
public struct CreationData {

// swiftlint:disable missing_docs
public var id: ID
public var date: Date
var id: ID
var date: Date

public init(
id: ID = .init(),
Expand All @@ -68,11 +68,10 @@ extension CustomerCenterEvent {
public struct Data {

// swiftlint:disable missing_docs
public var sessionIdentifier: SessionID
public var localeIdentifier: String
public var darkMode: Bool
var sessionIdentifier: SessionID
var localeIdentifier: String
var darkMode: Bool

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
public init(
sessionID: SessionID,
locale: Locale,
Expand Down

0 comments on commit 6a7d6e8

Please sign in to comment.