Skip to content

Commit

Permalink
Add the client-side decision tree
Browse files Browse the repository at this point in the history
Summary:
- The SDK decision tree can be found at https://docs.google.com/document/d/10Gftcn5wqtegaEPh0LgOkarb4T0jvE_iZKEiUpVu4nU/edit
- Unit tests will be added in the next diff

Reviewed By: dreamolight

Differential Revision: D51140801

fbshipit-source-id: 0883ac19b5a35efb1a97f34cdc7cc7fb38487749
  • Loading branch information
Tao Xu authored and facebook-github-bot committed Nov 15, 2023
1 parent d18a1db commit 324571d
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 64 additions & 0 deletions FBSDKCoreKit/FBSDKCoreKit/Settings+AutoLogAppEvents.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
9 changes: 5 additions & 4 deletions FBSDKCoreKit/FBSDKCoreKit/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down Expand Up @@ -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] = [
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 3 additions & 0 deletions FBSDKCoreKit/FBSDKCoreKitTests/SettingsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -35,6 +36,7 @@ final class SettingsTests: XCTestCase {

logger = TestEventLogger()
appEventsConfigurationProvider = TestAppEventsConfigurationProvider()
serverConfigurationProvider = TestAppEventsServerConfigurationProvider()
userDefaultsSpy = UserDefaultsSpy()
bundle = TestBundle()
settings = Settings()
Expand All @@ -55,6 +57,7 @@ final class SettingsTests: XCTestCase {
settings.setDependencies(
.init(
appEventsConfigurationProvider: appEventsConfigurationProvider,
serverConfigurationProvider: serverConfigurationProvider,
dataStore: userDefaultsSpy,
eventLogger: logger,
infoDictionaryProvider: bundle
Expand Down

0 comments on commit 324571d

Please sign in to comment.