Skip to content

Commit a5271d8

Browse files
committed
Removed CustomerInfo, offDeviceSubscriptionsDidChange
1 parent 7fd7f16 commit a5271d8

20 files changed

+39
-127
lines changed

Examples/Advanced/Advanced/Purchase Controllers/RCPurchaseController.swift

-17
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,4 @@ final class RCPurchaseController: PurchaseController {
9393
return .failed(error)
9494
}
9595
}
96-
97-
/// An optional function to use if you're using web paywalls. This merges the entitlements from RevenueCat with
98-
/// the web entitlements and sets the ``Superwall/subscriptionStatus`` with the merged set.
99-
func offDeviceSubscriptionsDidChange(entitlements: Set<Entitlement>) async {
100-
var allEntitlements: Set<Entitlement> = entitlements
101-
102-
if let customerInfo = try? await Purchases.shared.customerInfo() {
103-
let revenueCatEntitlements = Set(customerInfo.entitlements.activeInCurrentEnvironment.keys.map {
104-
Entitlement(id: $0)
105-
})
106-
allEntitlements.formUnion(revenueCatEntitlements)
107-
}
108-
109-
await MainActor.run {
110-
Superwall.shared.subscriptionStatus = .active(allEntitlements)
111-
}
112-
}
11396
}

Examples/Advanced/Advanced/Purchase Controllers/SWPurchaseController.swift

-7
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,4 @@ final class SWPurchaseController: PurchaseController {
5858
await syncSubscriptionStatus()
5959
return result
6060
}
61-
62-
/// An optional function to use if you're using web paywalls. This sets the `subscriptionStatus`
63-
/// with the entitlements returned from `customerInfo`.
64-
/// This consists of the existing active entitlements joined with the updated set of web entitlements.
65-
func offDeviceSubscriptionsDidChange(entitlements: Set<Entitlement>) async {
66-
await syncSubscriptionStatus()
67-
}
6861
}

Sources/SuperwallKit/Delegate/SuperwallDelegate.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public protocol SuperwallDelegate: AnyObject {
109109
)
110110

111111
@MainActor
112-
func didRedeemCode(customerInfo: CustomerInfo, result: RedemptionResult)
112+
func didRedeemCode(result: RedemptionResult)
113113
}
114114

115115
extension SuperwallDelegate {
@@ -145,5 +145,5 @@ extension SuperwallDelegate {
145145
error: Swift.Error?
146146
) {}
147147

148-
public func didRedeemCode(customerInfo: CustomerInfo, result: RedemptionResult) {}
148+
public func didRedeemCode(result: RedemptionResult) {}
149149
}

Sources/SuperwallKit/Delegate/SuperwallDelegateAdapter.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,9 @@ final class SuperwallDelegateAdapter {
134134
}
135135

136136
@MainActor
137-
func didRedeemCode(customerInfo: CustomerInfo, result: RedemptionResult) {
137+
func didRedeemCode(result: RedemptionResult) {
138138
if let swiftDelegate = swiftDelegate {
139-
swiftDelegate.didRedeemCode(
140-
customerInfo: customerInfo,
141-
result: result
142-
)
139+
swiftDelegate.didRedeemCode(result: result)
143140
}
144141
// TODO: Objective C version
145142
}

Sources/SuperwallKit/Dependencies/DependencyContainer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ extension DependencyContainer: TriggerFactory {
471471
}
472472

473473
// MARK: - Purchase Controller Factory
474-
extension DependencyContainer: ExternalPurchaseControllerFactory {
474+
extension DependencyContainer: HasExternalPurchaseControllerFactory {
475475
func makeHasExternalPurchaseController() -> Bool {
476476
return purchaseController.isInternal == false
477477
}

Sources/SuperwallKit/Dependencies/FactoryProtocols.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ protocol DeviceHelperFactory: AnyObject {
9898
func makeSessionDeviceAttributes() async -> [String: Any]
9999
}
100100

101-
protocol ExternalPurchaseControllerFactory: AnyObject {
101+
protocol HasExternalPurchaseControllerFactory: AnyObject {
102102
func makeHasExternalPurchaseController() -> Bool
103-
func makeExternalPurchaseController() -> PurchaseController
104103
}
105104

106105
struct DummyDecodable: Decodable {}

Sources/SuperwallKit/Models/Web2App/CustomerInfo.swift

-18
This file was deleted.

Sources/SuperwallKit/Storage/Cache/Cache.swift

+5-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class Cache {
2121
private let memCache = NSCache<AnyObject, AnyObject>()
2222
private let ioQueue: DispatchQueue
2323
private let fileManager: FileManager
24-
private unowned let factory: ExternalPurchaseControllerFactory
2524

2625
/// Life time of disk cache, in second. Default is a week
2726
private var maxCachePeriodInSecond = Cache.defaultMaxCachePeriodInSecond
@@ -32,10 +31,8 @@ class Cache {
3231
/// Specify distinct name param, it represents folder name for disk cache
3332
init(
3433
fileManager: FileManager = FileManager(),
35-
ioQueue: DispatchQueue = DispatchQueue(label: Cache.ioQueuePrefix),
36-
factory: ExternalPurchaseControllerFactory
34+
ioQueue: DispatchQueue = DispatchQueue(label: Cache.ioQueuePrefix)
3735
) {
38-
self.factory = factory
3936
self.fileManager = fileManager
4037
cacheUrl = fileManager
4138
.urls(for: .cachesDirectory, in: .userDomainMask)
@@ -377,14 +374,12 @@ extension Cache {
377374
if let redeemResponse = read(LatestRedeemResponse.self) {
378375
let existingWebEntitlements = redeemResponse.entitlements
379376
var deviceResults: [RedemptionResult] = []
380-
var webEntitlementsToKeep: Set<Entitlement> = []
381377

382378
for result in redeemResponse.results {
383379
switch result {
384380
case .success(_, let redemptionInfo):
385381
if case .device = redemptionInfo.ownership {
386382
deviceResults.append(result)
387-
webEntitlementsToKeep.formUnion(redemptionInfo.entitlements)
388383
}
389384
case .expiredSubscription(_, let redemptionInfo):
390385
if case .device = redemptionInfo.ownership {
@@ -397,16 +392,14 @@ extension Cache {
397392

398393
let newRedeemResponse = RedeemResponse(
399394
results: deviceResults,
400-
entitlements: webEntitlementsToKeep
395+
entitlements: []
401396
)
402397

403398
write(newRedeemResponse, forType: LatestRedeemResponse.self)
404399

405-
if existingWebEntitlements != webEntitlementsToKeep {
406-
Task {
407-
let purchaseController = factory.makeExternalPurchaseController()
408-
await purchaseController.offDeviceSubscriptionsDidChange(entitlements: webEntitlementsToKeep)
409-
}
400+
if !existingWebEntitlements.isEmpty {
401+
let deviceEntitlements = Superwall.shared.entitlements.activeDeviceEntitlements
402+
Superwall.shared.internallySetSubscriptionStatus(to: .active(deviceEntitlements))
410403
}
411404
}
412405
}

Sources/SuperwallKit/Storage/Storage.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ class Storage {
7373
/// The disk cache.
7474
private let cache: Cache
7575

76-
private unowned let factory: DeviceHelperFactory & ExternalPurchaseControllerFactory
76+
private unowned let factory: DeviceHelperFactory & HasExternalPurchaseControllerFactory
7777

7878
// MARK: - Configuration
7979

8080
init(
81-
factory: DeviceHelperFactory & ExternalPurchaseControllerFactory,
82-
cache: Cache? = nil,
81+
factory: DeviceHelperFactory & HasExternalPurchaseControllerFactory,
82+
cache: Cache = Cache(),
8383
coreDataManager: CoreDataManager = CoreDataManager()
8484
) {
85-
self.cache = cache ?? Cache(factory: factory)
85+
self.cache = cache
8686
self.coreDataManager = coreDataManager
8787
self._didTrackFirstSeen = self.cache.read(DidTrackFirstSeen.self) == true
8888

Sources/SuperwallKit/StoreKit/Purchase Controller/AutomaticPurchaseController.swift

-8
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@ extension AutomaticPurchaseController: PurchaseController {
6060

6161
return result
6262
}
63-
64-
@MainActor
65-
func offDeviceSubscriptionsDidChange(entitlements: Set<Entitlement>) async {
66-
let deviceEntitlements = entitlementsInfo.activeDeviceEntitlements
67-
let allEntitlements = deviceEntitlements.union(entitlements)
68-
69-
Superwall.shared.internallySetSubscriptionStatus(to: .active(allEntitlements))
70-
}
7163
}
7264

7365
// MARK: - InternalPurchaseController

Sources/SuperwallKit/StoreKit/Purchase Controller/PurchaseController.swift

-13
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,4 @@ public protocol PurchaseController: AnyObject {
4040
/// **Note**: `restored` does not imply the user has an active subscription, it just mean the restore had no errors.
4141
@MainActor
4242
func restorePurchases() async -> RestorationResult
43-
44-
/// Called when web entitlements changed.
45-
///
46-
/// If you are using web paywalls, make sure to add this method.
47-
/// You must merge the entitlements returned here with your local entitlements and set the ``Superwall/subscriptionStatus``
48-
/// with the merged set.
49-
/// - Parameter entitlements: A `Set` of web ``Entitlement``s.
50-
@MainActor
51-
func offDeviceSubscriptionsDidChange(entitlements: Set<Entitlement>) async
52-
}
53-
54-
extension PurchaseController {
55-
public func offDeviceSubscriptionsDidChange(entitlements: Set<Entitlement>) async {}
5643
}

Sources/SuperwallKit/StoreKit/Transactions/Purchasing/PurchaseManager.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class PurchaseManager: Purchasing {
1616
let coordinator: PurchasingCoordinator
1717
let purchaser: Purchasing
1818
private unowned let factory: Factory
19-
typealias Factory = ExternalPurchaseControllerFactory
19+
typealias Factory = HasExternalPurchaseControllerFactory
2020
& StoreTransactionFactory
2121
& OptionsFactory
2222
& TransactionManagerFactory

Sources/SuperwallKit/StoreKit/Transactions/Purchasing/StoreKit 1/ProductPurchaserSK1.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class ProductPurchaserSK1: NSObject, Purchasing {
3232
private let identityManager: IdentityManager
3333
private let storage: Storage
3434
private let factory:
35-
ExternalPurchaseControllerFactory
35+
HasExternalPurchaseControllerFactory
3636
& StoreTransactionFactory
3737
& OptionsFactory
3838
& TransactionManagerFactory
@@ -48,7 +48,7 @@ final class ProductPurchaserSK1: NSObject, Purchasing {
4848
identityManager: IdentityManager,
4949
coordinator: PurchasingCoordinator,
5050
storage: Storage,
51-
factory: ExternalPurchaseControllerFactory
51+
factory: HasExternalPurchaseControllerFactory
5252
& StoreTransactionFactory
5353
& OptionsFactory
5454
& TransactionManagerFactory

Sources/SuperwallKit/StoreKit/Transactions/Purchasing/StoreKit 2/ProductPurchaserSK2.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class ProductPurchaserSK2: Purchasing {
1414
private unowned let receiptManager: ReceiptManager
1515
private let coordinator: PurchasingCoordinator
1616
private unowned let factory: Factory
17-
typealias Factory = ExternalPurchaseControllerFactory
17+
typealias Factory = HasExternalPurchaseControllerFactory
1818
& OptionsFactory
1919
& TransactionManagerFactory
2020
& PurchasedTransactionsFactory

Sources/SuperwallKit/StoreKit/Transactions/Purchasing/StoreKit 2/SK2TransactionListener.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import StoreKit
1212
actor SK2TransactionListener {
1313
private(set) var taskHandle: Task<Void, Never>?
1414
private let shouldFinishTransactions: Bool
15-
private let factory: ExternalPurchaseControllerFactory
15+
private let factory: HasExternalPurchaseControllerFactory
1616
private unowned let receiptManager: ReceiptManager
1717

1818
deinit {
@@ -23,7 +23,7 @@ actor SK2TransactionListener {
2323
init(
2424
shouldFinishTransactions: Bool,
2525
receiptManager: ReceiptManager,
26-
factory: ExternalPurchaseControllerFactory
26+
factory: HasExternalPurchaseControllerFactory
2727
) {
2828
self.shouldFinishTransactions = shouldFinishTransactions
2929
self.receiptManager = receiptManager

Sources/SuperwallKit/StoreKit/Transactions/TransactionManager.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class TransactionManager {
2323
& PurchasedTransactionsFactory
2424
& StoreTransactionFactory
2525
& DeviceHelperFactory
26-
& ExternalPurchaseControllerFactory
26+
& HasExternalPurchaseControllerFactory
2727
& RestoreAccessFactory
2828
enum State {
2929
case observing

Sources/SuperwallKit/Superwall.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,11 @@ public final class Superwall: NSObject, ObservableObject {
150150
}
151151

152152
/// Gets web entitlements and merges them with device entitlements before
153-
/// setting the status.
153+
/// setting the status if no external purchase controller.
154154
func internallySetSubscriptionStatus(to status: SubscriptionStatus) {
155+
if dependencyContainer.makeHasExternalPurchaseController() {
156+
return
157+
}
155158
let webEntitlements = dependencyContainer.entitlementsInfo.web
156159

157160
switch status {

Sources/SuperwallKit/Web/WebEntitlementRedeemer.swift

+12-24
Original file line numberDiff line numberDiff line change
@@ -123,24 +123,16 @@ actor WebEntitlementRedeemer {
123123

124124
storage.save(response, forType: LatestRedeemResponse.self)
125125

126-
// Either sets the subscription status internally using
127-
// automatic purchase controller or calls the external
128-
// purchase controller.
129-
await purchaseController.offDeviceSubscriptionsDidChange(entitlements: response.entitlements)
126+
// Sets the subscription status internally if no external PurchaseController
127+
let deviceEntitlements = entitlementsInfo.activeDeviceEntitlements
128+
let allEntitlements = deviceEntitlements.union(response.entitlements)
129+
130+
Superwall.shared.internallySetSubscriptionStatus(to: .active(allEntitlements))
130131

131132
// Call the delegate if user try to redeem a code
132133
if case let .code(code) = type {
133134
if let codeResult = response.results.first(where: { $0.code == code }) {
134-
let allEntitlements = Superwall.shared.entitlements.activeDeviceEntitlements.union(response.entitlements)
135-
let customerInfo = CustomerInfo(
136-
entitlements: allEntitlements,
137-
redemptions: response.results
138-
)
139-
140-
await delegate.didRedeemCode(
141-
customerInfo: customerInfo,
142-
result: codeResult
143-
)
135+
await delegate.didRedeemCode(result: codeResult)
144136
}
145137
}
146138
} catch {
@@ -160,15 +152,7 @@ actor WebEntitlementRedeemer {
160152
)
161153
redemptions.append(errorResult)
162154

163-
let customerInfo = CustomerInfo(
164-
entitlements: entitlements,
165-
redemptions: redemptions
166-
)
167-
168-
await delegate.didRedeemCode(
169-
customerInfo: customerInfo,
170-
result: errorResult
171-
)
155+
await delegate.didRedeemCode(result: errorResult)
172156
}
173157

174158
Logger.debug(
@@ -216,7 +200,11 @@ actor WebEntitlementRedeemer {
216200
storage.save(Date(), forType: LastWebEntitlementsFetchDate.self)
217201

218202
if existingWebEntitlements != entitlements {
219-
await purchaseController.offDeviceSubscriptionsDidChange(entitlements: entitlements)
203+
// Sets the subscription status internally if no external PurchaseController
204+
let deviceEntitlements = entitlementsInfo.activeDeviceEntitlements
205+
let allEntitlements = deviceEntitlements.union(entitlements)
206+
207+
Superwall.shared.internallySetSubscriptionStatus(to: .active(allEntitlements))
220208
}
221209
} catch {
222210
Logger.debug(

Tests/SuperwallKitTests/Storage/Cache/MockExternalPurchaseControllerFactory.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Foundation
99
@testable import SuperwallKit
1010

11-
final class MockExternalPurchaseControllerFactory: ExternalPurchaseControllerFactory {
11+
final class MockExternalPurchaseControllerFactory: HasExternalPurchaseControllerFactory {
1212
let purchaseController: PurchaseController
1313

1414
init(purchaseController: PurchaseController) {

Tests/SuperwallKitTests/Storage/Cache/MockPurchaseController.swift

-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Foundation
99
@testable import SuperwallKit
1010

1111
final class MockPurchaseController: PurchaseController {
12-
var didCallOffDeviceSubscriptionsDidChange = false
1312

1413
func purchase(product: SuperwallKit.StoreProduct) async -> SuperwallKit.PurchaseResult {
1514
return .purchased
@@ -18,8 +17,4 @@ final class MockPurchaseController: PurchaseController {
1817
func restorePurchases() async -> SuperwallKit.RestorationResult {
1918
return .restored
2019
}
21-
22-
func offDeviceSubscriptionsDidChange(entitlements: Set<Entitlement>) async {
23-
didCallOffDeviceSubscriptionsDidChange = true
24-
}
2520
}

0 commit comments

Comments
 (0)