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 4fd073c
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 79 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
9 changes: 0 additions & 9 deletions Sources/Misc/StoreKit2Setting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ extension StoreKit2Setting {
: .enabledOnlyForOptimizations
}

init(version: Configuration.StoreKitVersion) {
switch version {
case .storeKit1:
self = .enabledOnlyForOptimizations
case .storeKit2:
self = .enabledForCompatibleDevices
}
}

static let `default`: Self = .enabledOnlyForOptimizations

}
Expand Down
37 changes: 9 additions & 28 deletions Sources/Purchasing/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@ import Foundation
*/
@objc(RCConfiguration) public final class Configuration: NSObject {

/// The StoreKit version that the SDK is setup with.
@objc(RCConfigurationStoreKitVersion)
public enum StoreKitVersion: Int {

// swiftlint:disable missing_docs
case storeKit1 = 1
case storeKit2 = 2
// swiftlint:enable missing_docs

}

static let storeKitRequestTimeoutDefault: TimeInterval = 30
static let networkTimeoutDefault: TimeInterval = 60

Expand All @@ -64,6 +53,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 +133,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 +293,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 @@ -15,14 +15,12 @@ func checkConfigurationAPI() {
.with(appUserID: "")
.with(appUserID: nil)
.with(observerMode: false)
.with(observerMode: false, storeKitVersion: .storeKit1)
.with(userDefaults: UserDefaults.standard)
.with(dangerousSettings: DangerousSettings())
.with(dangerousSettings: DangerousSettings(autoSyncPurchases: true))
.with(networkTimeout: 1)
.with(storeKit1Timeout: 1)
.with(platformInfo: Purchases.PlatformInfo(flavor: "", version: ""))
// Trusted Entitlements: internal until ready to be made public.
// .with(entitlementVerificationMode: .informational)
.build()

Expand Down
13 changes: 2 additions & 11 deletions Tests/APITesters/ObjCAPITester/ObjCAPITester/RCConfigurationAPI.m
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 All @@ -32,12 +31,4 @@ + (void)checkAPI {
}
}

+ (void)checkStoreKitVersion:(RCConfigurationStoreKitVersion)version {
switch (version) {
case RCConfigurationStoreKitVersionStoreKit1:
case RCConfigurationStoreKitVersionStoreKit2:
break;
}
}

@end
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 All @@ -33,14 +31,6 @@ func checkConfigurationAPI() {
}
}

func checkStoreKitVersion(_ version: Configuration.StoreKitVersion) {
switch version {
case .storeKit1: break
case .storeKit2: break
@unknown default: break
}
}

@available(*, deprecated)
func checkDeprecatedConfiguration(_ builder: Configuration.Builder) {
_ = builder
Expand Down
8 changes: 0 additions & 8 deletions Tests/UnitTests/Misc/StoreKit2SettingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ class StoreKit2SettingTests: TestCase {
expect(StoreKit2Setting(useStoreKit2IfAvailable: false).usesStoreKit2IfAvailable) == false
}

func testInitWithStoreKit1() {
expect(StoreKit2Setting(version: .storeKit1)) == .enabledOnlyForOptimizations
}

func testInitWithStoreKit2() {
expect(StoreKit2Setting(version: .storeKit2)) == .enabledForCompatibleDevices
}

func testStoreKit2NotAvailableWhenDisabled() {
expect(StoreKit2Setting.disabled.shouldOnlyUseStoreKit2) == false
}
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 4fd073c

Please sign in to comment.