From 46f47274564259a05b09259e4533e310452fc956 Mon Sep 17 00:00:00 2001 From: NachoSoto Date: Thu, 17 Nov 2022 09:34:00 -0800 Subject: [PATCH 1/2] Exposed `SystemInfo.observerMode` to simplify code --- Sources/Misc/SystemInfo.swift | 2 ++ Sources/Networking/HTTPClient/HTTPClient.swift | 4 +--- Sources/Purchasing/Purchases/PurchasesOrchestrator.swift | 9 +++++---- Tests/UnitTests/Misc/SystemInfoTests.swift | 2 ++ 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Sources/Misc/SystemInfo.swift b/Sources/Misc/SystemInfo.swift index de1a967370..ae3923f9b7 100644 --- a/Sources/Misc/SystemInfo.swift +++ b/Sources/Misc/SystemInfo.swift @@ -40,6 +40,8 @@ class SystemInfo { set { self._finishTransactions.value = newValue } } + var observerMode: Bool { return !self.finishTransactions } + private let sandboxEnvironmentDetector: SandboxEnvironmentDetector private let _finishTransactions: Atomic diff --git a/Sources/Networking/HTTPClient/HTTPClient.swift b/Sources/Networking/HTTPClient/HTTPClient.swift index 463288edf9..adb6bf1be8 100644 --- a/Sources/Networking/HTTPClient/HTTPClient.swift +++ b/Sources/Networking/HTTPClient/HTTPClient.swift @@ -138,8 +138,6 @@ private extension HTTPClient { private extension HTTPClient { var defaultHeaders: [String: String] { - let observerMode = !self.systemInfo.finishTransactions - var headers: [String: String] = [ "content-type": "application/json", "X-Version": SystemInfo.frameworkVersion, @@ -150,7 +148,7 @@ private extension HTTPClient { "X-Client-Build-Version": SystemInfo.buildVersion, "X-Client-Bundle-ID": SystemInfo.bundleIdentifier, "X-StoreKit2-Setting": "\(self.systemInfo.storeKit2Setting.debugDescription)", - "X-Observer-Mode-Enabled": "\(observerMode)", + "X-Observer-Mode-Enabled": "\(self.systemInfo.observerMode)", "X-Is-Sandbox": "\(self.systemInfo.isSandbox)" ] diff --git a/Sources/Purchasing/Purchases/PurchasesOrchestrator.swift b/Sources/Purchasing/Purchases/PurchasesOrchestrator.swift index 7301db02e9..f14be950e8 100644 --- a/Sources/Purchasing/Purchases/PurchasesOrchestrator.swift +++ b/Sources/Purchasing/Purchases/PurchasesOrchestrator.swift @@ -31,6 +31,8 @@ import StoreKit final class PurchasesOrchestrator { var finishTransactions: Bool { self.systemInfo.finishTransactions } + var observerMode: Bool { self.systemInfo.observerMode } + var allowSharingAppStoreAccount: Bool { get { self._allowSharingAppStoreAccount.value ?? self.currentUserProvider.currentUserIsAnonymous } set { self._allowSharingAppStoreAccount.value = newValue } @@ -779,8 +781,7 @@ extension PurchasesOrchestrator: StoreKit2TransactionListenerDelegate { } } - // Need to restore if using observer mode (which is inverse of finishTransactions) - let isRestore = !self.systemInfo.finishTransactions + let isRestore = self.systemInfo.observerMode _ = try await self.syncPurchases(receiptRefreshPolicy: .always, isRestore: isRestore, @@ -889,7 +890,7 @@ private extension PurchasesOrchestrator { isRestore: allowSharingAppStoreAccount, productData: productData, presentedOfferingIdentifier: presentedOfferingID, - observerMode: !self.finishTransactions, + observerMode: self.observerMode, initiationSource: initiationSource, subscriberAttributes: unsyncedAttributes) { result in self.handleReceiptPost(withTransaction: transaction, @@ -1013,7 +1014,7 @@ private extension PurchasesOrchestrator { isRestore: isRestore, productData: nil, presentedOfferingIdentifier: nil, - observerMode: !self.finishTransactions, + observerMode: self.observerMode, initiationSource: initiationSource, subscriberAttributes: unsyncedAttributes) { result in self.handleReceiptPost(result: result, diff --git a/Tests/UnitTests/Misc/SystemInfoTests.swift b/Tests/UnitTests/Misc/SystemInfoTests.swift index d92fa6b00e..d29f629568 100644 --- a/Tests/UnitTests/Misc/SystemInfoTests.swift +++ b/Tests/UnitTests/Misc/SystemInfoTests.swift @@ -47,12 +47,14 @@ class SystemInfoTests: TestCase { var systemInfo = try SystemInfo(platformInfo: nil, finishTransactions: finishTransactions) expect(systemInfo.finishTransactions) == finishTransactions + expect(systemInfo.observerMode) == !finishTransactions finishTransactions = true systemInfo = try SystemInfo(platformInfo: nil, finishTransactions: finishTransactions) expect(systemInfo.finishTransactions) == finishTransactions + expect(systemInfo.observerMode) == !finishTransactions } func testIsSandbox() throws { From d95b0af4b8e67a84a61c2e9ffee5e4ba4d97f798 Mon Sep 17 00:00:00 2001 From: NachoSoto Date: Thu, 17 Nov 2022 10:36:48 -0800 Subject: [PATCH 2/2] `Logging`: added log when configuring SDK in observer mode --- Sources/Logging/Strings/ConfigureStrings.swift | 4 ++++ Sources/Purchasing/Purchases/Purchases.swift | 3 +++ 2 files changed, 7 insertions(+) diff --git a/Sources/Logging/Strings/ConfigureStrings.swift b/Sources/Logging/Strings/ConfigureStrings.swift index f3124af3b8..05e9cd6703 100644 --- a/Sources/Logging/Strings/ConfigureStrings.swift +++ b/Sources/Logging/Strings/ConfigureStrings.swift @@ -27,6 +27,8 @@ enum ConfigureStrings { case store_kit_2_enabled + case observer_mode_enabled + case delegate_set case purchase_instance_already_set @@ -62,6 +64,8 @@ extension ConfigureStrings: CustomStringConvertible { return "Debug logging enabled" case .store_kit_2_enabled: return "StoreKit 2 support enabled" + case .observer_mode_enabled: + return "Purchases is configured in observer mode" case .delegate_set: return "Delegate set" case .purchase_instance_already_set: diff --git a/Sources/Purchasing/Purchases/Purchases.swift b/Sources/Purchasing/Purchases/Purchases.swift index 0e5fcce041..6706c629e7 100644 --- a/Sources/Purchasing/Purchases/Purchases.swift +++ b/Sources/Purchasing/Purchases/Purchases.swift @@ -412,6 +412,9 @@ public typealias StartPurchaseBlock = (@escaping PurchaseCompletedBlock) -> Void if systemInfo.storeKit2Setting == .enabledForCompatibleDevices { Logger.info(Strings.configure.store_kit_2_enabled, fileName: nil) } + if systemInfo.observerMode { + Logger.debug(Strings.configure.observer_mode_enabled, fileName: nil) + } Logger.debug(Strings.configure.sdk_version(Self.frameworkVersion), fileName: nil) Logger.debug(Strings.configure.bundle_id(SystemInfo.bundleIdentifier), fileName: nil) Logger.user(Strings.configure.initial_app_user_id(isSet: appUserID != nil), fileName: nil)