diff --git a/FBSDKCoreKit/FBSDKCoreKit/Internal/Configuration/CoreKitConfigurator.swift b/FBSDKCoreKit/FBSDKCoreKit/Internal/Configuration/CoreKitConfigurator.swift index 179c84833..d371c3dcb 100644 --- a/FBSDKCoreKit/FBSDKCoreKit/Internal/Configuration/CoreKitConfigurator.swift +++ b/FBSDKCoreKit/FBSDKCoreKit/Internal/Configuration/CoreKitConfigurator.swift @@ -335,6 +335,7 @@ private extension CoreKitConfigurator { Settings.shared.setDependencies( .init( appEventsConfigurationProvider: components.appEventsConfigurationProvider, + serverConfigurationProvider: components.serverConfigurationProvider, dataStore: components.defaultDataStore, eventLogger: components.eventLogger, infoDictionaryProvider: components.infoDictionaryProvider diff --git a/FBSDKCoreKit/FBSDKCoreKit/Settings+AutoLogAppEvents.swift b/FBSDKCoreKit/FBSDKCoreKit/Settings+AutoLogAppEvents.swift new file mode 100644 index 000000000..ca252eda1 --- /dev/null +++ b/FBSDKCoreKit/FBSDKCoreKit/Settings+AutoLogAppEvents.swift @@ -0,0 +1,64 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * All rights reserved. + * + * This source code is licensed under the license found in the + * LICENSE file in the root directory of this source tree. + */ + +extension Settings { + enum AutoLogAppEventServerFlags: String { + case DEFAUT = "auto_log_app_events_default" + case ENABLE = "auto_log_app_events_enabled" + } + + func checkAutoLogAppEventsEnabled() -> Bool { + guard let dependencies = try? getDependencies() else { + return true + } + guard let migratedAutoLogValues = dependencies + .serverConfigurationProvider.cachedServerConfiguration().migratedAutoLogValues else { + return isAutoLogAppEventsEnabledLocally + } + if let migratedAutoLogEnabled = migratedAutoLogValues[AutoLogAppEventServerFlags.ENABLE.rawValue] as? NSNumber { + return migratedAutoLogEnabled.boolValue + } + if let locallyEnabled = checkClientSideConfiguration(dependencies) { + return locallyEnabled + } + if let migratedDefault = migratedAutoLogValues[AutoLogAppEventServerFlags.DEFAUT.rawValue] as? NSNumber { + return migratedDefault.boolValue + } + return true + } + + // swiftlint:disable:next discouraged_optional_boolean + private func checkClientSideConfiguration(_ dependencies: ObjectDependencies) -> Bool? { + if let isAutoLogAppEventsEnabledInUserDefault = checkUserDefault() { + return isAutoLogAppEventsEnabledInUserDefault + } + if let isAutoLogAppEventsEnabledInPlist = checkInfoPlist(dependencies) { + return isAutoLogAppEventsEnabledInPlist + } + return nil + } + + // swiftlint:disable:next discouraged_optional_boolean + private func checkInfoPlist(_ dependencies: ObjectDependencies) -> Bool? { + if let infoPlistValue = dependencies.infoDictionaryProvider + .fb_object(forInfoDictionaryKey: PersistenceKey.isAutoLogAppEventsEnabledLocally.rawValue) as? NSNumber { + return infoPlistValue.boolValue + } + return nil + } + + // swiftlint:disable:next discouraged_optional_boolean + private func checkUserDefault() -> Bool? { + // swiftformat:disable:this redundantSelf + if let userDefaultValue = self.dataStore? + .fb_object(forKey: Settings.PersistenceKey.isAutoLogAppEventsEnabledLocally.rawValue) as? NSNumber { + return userDefaultValue.boolValue + } + return nil + } +} diff --git a/FBSDKCoreKit/FBSDKCoreKit/Settings.swift b/FBSDKCoreKit/FBSDKCoreKit/Settings.swift index 3b33808ed..6103f1a73 100644 --- a/FBSDKCoreKit/FBSDKCoreKit/Settings.swift +++ b/FBSDKCoreKit/FBSDKCoreKit/Settings.swift @@ -80,7 +80,7 @@ public final class Settings: NSObject, SettingsProtocol, SettingsLogging, _Clien The default value is `true`. */ public var isAutoLogAppEventsEnabled: Bool { - get { isAutoLogAppEventsEnabledLocally } + get { checkAutoLogAppEventsEnabled() } set { isAutoLogAppEventsEnabledLocally = newValue } } @@ -446,9 +446,9 @@ public final class Settings: NSObject, SettingsProtocol, SettingsLogging, _Clien Sets the data processing options. - Parameters: - - options The list of the options. - - country The code for the country. - - state The code for the state. + - options The list of the options. + - country The code for the country. + - state The code for the state. */ public func setDataProcessingOptions(_ options: [String]?, country: Int32, state: Int32) { let values: [DataProcessingOptionKey.RawValue: Any] = [ @@ -642,6 +642,7 @@ public final class Settings: NSObject, SettingsProtocol, SettingsLogging, _Clien extension Settings: DependentAsObject { struct ObjectDependencies { var appEventsConfigurationProvider: _AppEventsConfigurationProviding + var serverConfigurationProvider: _ServerConfigurationProviding var dataStore: DataPersisting var eventLogger: EventLogging var infoDictionaryProvider: InfoDictionaryProviding diff --git a/FBSDKCoreKit/FBSDKCoreKitTests/Internal/Helpers/TestAppEventsServerConfigurationProvider.swift b/FBSDKCoreKit/FBSDKCoreKitTests/Internal/Helpers/TestAppEventsServerConfigurationProvider.swift new file mode 100644 index 000000000..e863b92f2 --- /dev/null +++ b/FBSDKCoreKit/FBSDKCoreKitTests/Internal/Helpers/TestAppEventsServerConfigurationProvider.swift @@ -0,0 +1,34 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * All rights reserved. + * + * This source code is licensed under the license found in the + * LICENSE file in the root directory of this source tree. + */ + +import Foundation + +final class TestAppEventsServerConfigurationProvider: NSObject, _ServerConfigurationProviding { + var configs: [String: Any]? + + func cachedServerConfiguration() -> _ServerConfiguration { + if let configs = configs { + return ServerConfigurationFixtures.configuration(withDictionary: configs) + } else { + return ServerConfigurationFixtures.defaultConfiguration + } + } + + func loadServerConfiguration(completionBlock: _ServerConfigurationBlock? = nil) { + // no-op + } + + func processLoadRequestResponse(_ result: Any, error: Error?, appID: String) { + // no-op + } + + func request(toLoadServerConfiguration appID: String) -> GraphRequest? { + // no-op + return nil + } +} diff --git a/FBSDKCoreKit/FBSDKCoreKitTests/SettingsTests.swift b/FBSDKCoreKit/FBSDKCoreKitTests/SettingsTests.swift index a25f6f1ec..285f76832 100644 --- a/FBSDKCoreKit/FBSDKCoreKitTests/SettingsTests.swift +++ b/FBSDKCoreKit/FBSDKCoreKitTests/SettingsTests.swift @@ -16,6 +16,7 @@ final class SettingsTests: XCTestCase { // swiftlint:disable implicitly_unwrapped_optional var logger: TestEventLogger! var appEventsConfigurationProvider: TestAppEventsConfigurationProvider! + var serverConfigurationProvider: TestAppEventsServerConfigurationProvider! var userDefaultsSpy: UserDefaultsSpy! var settings: Settings! var bundle: InfoDictionaryProviding! @@ -35,6 +36,7 @@ final class SettingsTests: XCTestCase { logger = TestEventLogger() appEventsConfigurationProvider = TestAppEventsConfigurationProvider() + serverConfigurationProvider = TestAppEventsServerConfigurationProvider() userDefaultsSpy = UserDefaultsSpy() bundle = TestBundle() settings = Settings() @@ -55,6 +57,7 @@ final class SettingsTests: XCTestCase { settings.setDependencies( .init( appEventsConfigurationProvider: appEventsConfigurationProvider, + serverConfigurationProvider: serverConfigurationProvider, dataStore: userDefaultsSpy, eventLogger: logger, infoDictionaryProvider: bundle