Skip to content

Commit

Permalink
Changed approach to discourage with a warning log
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto committed Sep 14, 2023
1 parent 3b4a59f commit 75063bd
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 33 deletions.
4 changes: 4 additions & 0 deletions Sources/Logging/Strings/ConfigureStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ enum ConfigureStrings {

case observer_mode_enabled

case observer_mode_with_storekit2

case response_verification_mode(Signing.ResponseVerificationMode)

case delegate_set
Expand Down Expand Up @@ -98,6 +100,8 @@ extension ConfigureStrings: LogMessage {
return "StoreKit 2 support enabled"
case .observer_mode_enabled:
return "Purchases is configured in observer mode"
case .observer_mode_with_storekit2:
return "Observer mode is not currently compatible with StoreKit 2."
case let .response_verification_mode(mode):
switch mode {
case .disabled:
Expand Down
26 changes: 9 additions & 17 deletions Sources/Purchasing/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ import Foundation

private init(with builder: Builder) {
Self.verify(apiKey: builder.apiKey)
Self.verify(observerMode: builder.observerMode,
storeKit2Setting: builder.storeKit2Setting)

self.apiKey = builder.apiKey
self.appUserID = builder.appUserID
Expand Down Expand Up @@ -142,28 +144,12 @@ import Foundation
* RevenueCat's backend. Default is `false`.
*
* - Warning: This assumes your IAP implementation uses StoreKit 1.
* If you use StoreKit 2, use ``with(observerMode:storeKitVersion:)`` instead.
* Observer mode is not compatible with StoreKit 2.
*/
@objc public func with(observerMode: Bool) -> Configuration.Builder {
return self.with(observerMode: observerMode, storeKitVersion: .storeKit1)
}

/**
* Set `observerMode` with a corresponding `StoreKit` implementation.
* - Parameter observerMode: Set this to `true` if you have your own IAP implementation and want to use only
* RevenueCat's backend. Default is `false`.
* - Parameter storeKitVersion: Set this to the StoreKit implementation your app uses for purchases.
* I.e.: if your app uses `StoreKit 1`, set it to ``Configuration/StoreKitVersion/storeKit1``
* and if your app uses `StoreKit 2`, set it to ``Configuration/StoreKitVersion/storeKit2``.
* Apps using `StoreKit 1` use `SKPaymentQueue` for transactions,
* whereas `StoreKit 2` uses `StoreKit.Product.Purchase`.
*/
@objc public func with(observerMode: Bool, storeKitVersion: StoreKitVersion) -> Builder {
self.observerMode = observerMode
self.storeKit2Setting = .init(version: storeKitVersion)
return self
}

/**
* Set `userDefaults`.
* - Parameter userDefaults: Custom `UserDefaults` to use
Expand Down Expand Up @@ -318,6 +304,12 @@ extension Configuration {
}
}

fileprivate static func verify(observerMode: Bool, storeKit2Setting: StoreKit2Setting) {
if observerMode, storeKit2Setting.usesStoreKit2IfAvailable {
Logger.warn(Strings.configure.observer_mode_with_storekit2)
}
}

private static let applePlatformKeyPrefix: String = "appl_"

}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Purchasing/Purchases/Purchases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1139,8 +1139,8 @@ public extension Purchases {
*
* - Returns: An instantiated ``Purchases`` object that has been set as a singleton.
*
* - Note: This assumes your IAP implementation uses StoreKit 1.
* If you use StoreKit 2, use ``Configuration/Builder/with(observerMode:storeKitVersion:)`` instead.
* - Warning: This assumes your IAP implementation uses StoreKit 1.
* Observer mode is not compatible with StoreKit 2.
*/
@objc(configureWithAPIKey:appUserID:observerMode:)
@discardableResult static func configure(withAPIKey apiKey: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ @implementation RCConfigurationAPI

+ (void)checkAPI {
RCConfigurationBuilder *builder = [RCConfiguration builderWithAPIKey:@""];
RCConfiguration *config __unused = [[[[[[[[[[[[builder withApiKey:@""]
withObserverMode:false]
withObserverMode:true storeKitVersion:RCConfigurationStoreKitVersionStoreKit1]
RCConfiguration *config __unused = [[[[[[[[[[[builder withApiKey:@""]
withObserverMode:false]
withUserDefaults:NSUserDefaults.standardUserDefaults]
withAppUserID:@""]
withAppUserID:nil]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ func checkConfigurationAPI() {
.with(appUserID: "")
.with(appUserID: nil)
.with(observerMode: true)
.with(observerMode: false, storeKitVersion: .storeKit1)
.with(observerMode: true, storeKitVersion: .storeKit2)
.with(userDefaults: UserDefaults.standard)
.with(dangerousSettings: DangerousSettings())
.with(dangerousSettings: DangerousSettings(autoSyncPurchases: true))
Expand Down
32 changes: 24 additions & 8 deletions Tests/UnitTests/Purchasing/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,50 @@ class ConfigurationTests: TestCase {
expect(Configuration.validate(apiKey: "swRTCezdEzjnJSxdexDNJfcfiFrMXwqZ")) == .legacy
}

@available(*, deprecated)
func testObserverModeDefaultsToStoreKit1() {
let observerMode = Bool.random()
func testNoObserverModeWithStoreKit1() {
let configuration = Configuration.Builder(withAPIKey: "test").build()

expect(configuration.observerMode) == false
expect(configuration.storeKit2Setting) == .enabledOnlyForOptimizations

self.logger.verifyMessageWasNotLogged(Strings.configure.observer_mode_with_storekit2)
}

@available(*, deprecated)
func testNoObserverModeWithStoreKit2() {
let configuration = Configuration.Builder(withAPIKey: "test")
.with(observerMode: observerMode)
.with(usesStoreKit2IfAvailable: true)
.build()

expect(configuration.observerMode) == observerMode
expect(configuration.storeKit2Setting) == .enabledOnlyForOptimizations
expect(configuration.observerMode) == false
expect(configuration.storeKit2Setting) == .enabledForCompatibleDevices

self.logger.verifyMessageWasNotLogged(Strings.configure.observer_mode_with_storekit2)
}

func testObserverModeWithStoreKit1() {
let configuration = Configuration.Builder(withAPIKey: "test")
.with(observerMode: true, storeKitVersion: .storeKit1)
.with(observerMode: true)
.build()

expect(configuration.observerMode) == true
expect(configuration.storeKit2Setting) == .enabledOnlyForOptimizations

self.logger.verifyMessageWasNotLogged(Strings.configure.observer_mode_with_storekit2)
}

@available(*, deprecated)
func testObserverModeWithStoreKit2() {
let configuration = Configuration.Builder(withAPIKey: "test")
.with(observerMode: true, storeKitVersion: .storeKit2)
.with(observerMode: true)
.with(usesStoreKit2IfAvailable: true)
.build()

expect(configuration.observerMode) == true
expect(configuration.storeKit2Setting) == .enabledForCompatibleDevices

self.logger.verifyMessageWasLogged(Strings.configure.observer_mode_with_storekit2,
level: .warn)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ class PurchasesConfiguringTests: BasePurchasesTests {
private static func create(observerMode: Bool) -> Purchases {
return Purchases.configure(
with: .init(withAPIKey: "")
.with(observerMode: observerMode, storeKitVersion: .storeKit1)
.with(observerMode: observerMode)
)
}

Expand Down

0 comments on commit 75063bd

Please sign in to comment.