Skip to content

Commit

Permalink
ApplicationDelegate Refactoring
Browse files Browse the repository at this point in the history
Summary: Moving towards more instance methods in ApplicationDelegate since most of the existing class methods already relied on the singleton `shared`.

Reviewed By: linmx0130

Differential Revision: D27222159

fbshipit-source-id: ec48548965a202f2ed7c9755d58627466314af59
  • Loading branch information
joesus authored and facebook-github-bot committed Mar 23, 2021
1 parent 895c1b3 commit 813fa58
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 103 deletions.
3 changes: 2 additions & 1 deletion FBSDKCoreKit/FBSDKCoreKit/FBSDKApplicationDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ didFinishLaunchingWithOptions:(nullable NSDictionary<UIApplicationLaunchOptionsK
@param launchOptions The launchOptions as passed to [UIApplicationDelegate application:didFinishLaunchingWithOptions:].
Could be nil if you don't call this function from [UIApplicationDelegate application:didFinishLaunchingWithOptions:].
*/
+ (void)initializeSDK:(nullable NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions;
+ (void)initializeSDK:(nullable NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions
DEPRECATED_MSG_ATTRIBUTE("This method is called automatically from `application:didFinishLaunchingWithOptions:`. Please use that method instead. This one will be removed in a future release.");

@end

Expand Down
120 changes: 67 additions & 53 deletions FBSDKCoreKit/FBSDKCoreKit/FBSDKApplicationDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

static NSString *const FBSDKAppLinkInboundEvent = @"fb_al_inbound";
static NSString *const FBSDKKitsBitmaskKey = @"com.facebook.sdk.kits.bitmask";
static BOOL g_isSDKInitialized = NO;
static BOOL hasInitializeBeenCalled = NO;
static UIApplicationState _applicationState;

@implementation FBSDKApplicationDelegate
Expand All @@ -83,39 +83,67 @@ @implementation FBSDKApplicationDelegate

+ (void)initializeSDK:(NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions
{
[self initializeSDKWithApplicationDelegate:self.sharedInstance
launchOptions:launchOptions];
[self.sharedInstance initializeSDKWithLaunchOptions:launchOptions];
}

+ (void)initializeSDKWithApplicationDelegate:(FBSDKApplicationDelegate *)delegate
launchOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions
+ (FBSDKApplicationDelegate *)sharedInstance
{
static FBSDKApplicationDelegate *_sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [self new];
});
return _sharedInstance;
}

#pragma mark - Object Lifecycle

- (instancetype)init
{
return [self initWithNotificationObserver:NSNotificationCenter.defaultCenter
tokenWallet:FBSDKAccessToken.class
settings:FBSDKSettings.class];
}

- (instancetype)initWithNotificationObserver:(id<FBSDKNotificationObserving>)observer
tokenWallet:(Class<FBSDKAccessTokenProviding, FBSDKAccessTokenSetting>)tokenWallet
settings:(Class<FBSDKSettingsLogging>)settings
{
if ((self = [super init]) != nil) {
_applicationObservers = [[NSHashTable alloc] init];
_notificationObserver = observer;
_tokenWallet = tokenWallet;
_settings = settings;
}
return self;
}

- (void)initializeSDKWithLaunchOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions
{
if (g_isSDKInitialized) {
if (hasInitializeBeenCalled) {
// Do nothing if initialized already
return;
} else {
hasInitializeBeenCalled = YES;
}
[self configureDependencies];

[self setIsSdkInitialized];

Class<FBSDKSettingsLogging> settingsLogger = [delegate settings];
id<FBSDKSettingsLogging> const settingsLogger = self.settings;
[settingsLogger logWarnings];
[settingsLogger logIfSDKSettingsChanged];
[settingsLogger recordInstall];

id<FBSDKNotificationObserving> defaultCenter = [delegate notificationObserver];
[defaultCenter addObserver:delegate selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[defaultCenter addObserver:delegate selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
[defaultCenter addObserver:delegate selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
[self addObservers];

[[FBSDKAppEvents singleton] registerNotifications];

[delegate application:[UIApplication sharedApplication] didFinishLaunchingWithOptions:launchOptions];
[self application:[UIApplication sharedApplication] didFinishLaunchingWithOptions:launchOptions];

// In case of sdk autoInit enabled sdk expects one appDidBecomeActive notification after app launch and has some logic to ignore it.
// if sdk autoInit disabled app won't receive appDidBecomeActive on app launch and will ignore the first one it gets instead of handling it.
// Send first applicationDidBecomeActive notification manually
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
[delegate applicationDidBecomeActive:nil];
[self applicationDidBecomeActive:nil];
}

[FBSDKFeatureManager checkFeature:FBSDKFeatureInstrument completionBlock:^(BOOL enabled) {
Expand All @@ -127,7 +155,7 @@ + (void)initializeSDKWithApplicationDelegate:(FBSDKApplicationDelegate *)delegat
#if !TARGET_OS_TV
// Register Listener for App Link measurement events
[FBSDKMeasurementEventListener defaultListener];
[delegate _logIfAutoAppLinkEnabled];
[self _logIfAutoAppLinkEnabled];
#endif
// Set the SourceApplication for time spent data. This is not going to update the value if the app has already launched.
[FBSDKTimeSpentData setSourceApplication:launchOptions[UIApplicationLaunchOptionsSourceApplicationKey]
Expand All @@ -138,36 +166,24 @@ + (void)initializeSDKWithApplicationDelegate:(FBSDKApplicationDelegate *)delegat
[FBSDKInternalUtility validateFacebookReservedURLSchemes];
}

+ (FBSDKApplicationDelegate *)sharedInstance
{
static FBSDKApplicationDelegate *_sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}

#pragma mark - Object Lifecycle

- (instancetype)init
- (void)addObservers
{
return [self initWithNotificationObserver:NSNotificationCenter.defaultCenter
tokenWallet:FBSDKAccessToken.class
settings:FBSDKSettings.class];
}

- (instancetype)initWithNotificationObserver:(id<FBSDKNotificationObserving>)observer
tokenWallet:(Class<FBSDKAccessTokenProviding, FBSDKAccessTokenSetting>)tokenWallet
settings:(Class<FBSDKSettingsLogging>)settings
{
if ((self = [super init]) != nil) {
_applicationObservers = [[NSHashTable alloc] init];
_notificationObserver = observer;
_tokenWallet = tokenWallet;
_settings = settings;
}
return self;
id<FBSDKNotificationObserving> const observer = self.notificationObserver;
[observer addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[observer addObserver:self
selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
[observer addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:nil];
#if !TARGET_OS_TV
[self addObserver:FBSDKBridgeAPI.sharedInstance];
#endif
}

- (void)dealloc
Expand Down Expand Up @@ -247,8 +263,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
return NO;
}

if (!g_isSDKInitialized) {
[FBSDKApplicationDelegate initializeSDK:launchOptions];
if (!hasInitializeBeenCalled) {
[self initializeSDKWithLaunchOptions:launchOptions];
}

_isAppLaunched = YES;
Expand Down Expand Up @@ -448,12 +464,11 @@ - (void)_logIfAutoAppLinkEnabled

+ (BOOL)isSDKInitialized
{
return g_isSDKInitialized;
return hasInitializeBeenCalled;
}

+ (void)setIsSdkInitialized
- (void)configureDependencies
{
g_isSDKInitialized = YES;
id<FBSDKGraphRequestProviding> graphRequestProvider = [FBSDKGraphRequestFactory new];
[FBSDKGraphRequestConnection setCanMakeRequests];
[FBSDKAppEvents configureWithGateKeeperManager:[FBSDKGateKeeperManager class]
Expand All @@ -479,7 +494,6 @@ + (void)setIsSdkInitialized
[FBSDKSKAdNetworkReporter configureWithRequestProvider:[FBSDKGraphRequestFactory new]];
[FBSDKProfile configureWithStore:NSUserDefaults.standardUserDefaults
accessTokenProvider:FBSDKAccessToken.class];
[self.sharedInstance addObserver:FBSDKBridgeAPI.sharedInstance];
#endif
}

Expand All @@ -488,9 +502,9 @@ + (void)setIsSdkInitialized
#if DEBUG
#if FBSDKTEST

+ (void)resetIsSdkInitialized
+ (void)resetHasInitializeBeenCalled
{
g_isSDKInitialized = NO;
hasInitializeBeenCalled = NO;
}

- (BOOL)isAppLaunched
Expand Down
64 changes: 32 additions & 32 deletions FBSDKCoreKit/FBSDKCoreKitTests/FBSDKApplicationDelegateTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ - (void)_logSDKInitialize;
- (void)applicationDidBecomeActive:(NSNotification *)notification;
- (void)applicationWillResignActive:(NSNotification *)notification;
- (void)setApplicationState:(UIApplicationState)state;
- (void)initializeSDKWithLaunchOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions;

@end

Expand Down Expand Up @@ -202,10 +203,10 @@ - (void)testRemovingMissingObserver

- (void)testInitializingSdkEnablesGraphRequests
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[FBSDKGraphRequestConnection resetCanMakeRequests];

[FBSDKApplicationDelegate initializeSDK:@{}];
[_delegate initializeSDKWithLaunchOptions:@{}];

XCTAssertTrue(
[FBSDKGraphRequestConnection canMakeRequests],
Expand All @@ -215,10 +216,10 @@ - (void)testInitializingSdkEnablesGraphRequests

- (void)testInitializingSdkEnablesLogEvents
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[FBSDKAppEvents resetCanLogEvents];

[FBSDKApplicationDelegate initializeSDK:@{}];
[_delegate initializeSDKWithLaunchOptions:@{}];

XCTAssertTrue(
[FBSDKAppEvents canLogEvents],
Expand All @@ -240,10 +241,10 @@ - (void)testInitializingSdkEnablesLogEvents

- (void)testInitializingSdkConfiguresGateKeeperManager
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[FBSDKGateKeeperManager reset];

[FBSDKApplicationDelegate initializeSDK:@{}];
[_delegate initializeSDKWithLaunchOptions:@{}];

NSObject *requestProvider = (NSObject *)FBSDKGateKeeperManager.requestProvider;
NSObject *connectionProvider = (NSObject *)FBSDKGateKeeperManager.connectionProvider;
Expand Down Expand Up @@ -278,8 +279,8 @@ - (void)testInitializingSdkConfiguresGateKeeperManager

- (void)testConfiguringCodelessIndexer
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate initializeSDK:@{}];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[_delegate initializeSDKWithLaunchOptions:@{}];
NSObject *requestProvider = (NSObject *)[FBSDKCodelessIndexer requestProvider];
XCTAssertEqualObjects(
requestProvider.class,
Expand All @@ -290,8 +291,8 @@ - (void)testConfiguringCodelessIndexer

- (void)testInitializingSdkConfiguresAppLinkUtility
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate initializeSDK:@{}];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[_delegate initializeSDKWithLaunchOptions:@{}];
NSObject *requestProvider = (NSObject *)[FBSDKAppLinkUtility requestProvider];
NSObject *infoDictionaryProvider = (NSObject *)[FBSDKAppLinkUtility infoDictionaryProvider];
XCTAssertEqualObjects(
Expand All @@ -308,8 +309,8 @@ - (void)testInitializingSdkConfiguresAppLinkUtility

- (void)testConfiguringFBSDKSKAdNetworkReporter
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate initializeSDK:@{}];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[_delegate initializeSDKWithLaunchOptions:@{}];
NSObject *requestProvider = (NSObject *)[FBSDKSKAdNetworkReporter requestProvider];
XCTAssertEqualObjects(
requestProvider.class,
Expand All @@ -320,18 +321,18 @@ - (void)testConfiguringFBSDKSKAdNetworkReporter

- (void)testInitializingSdkConfiguresAccessTokenCache
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[FBSDKAccessToken setTokenCache:nil];
[FBSDKApplicationDelegate initializeSDK:@{}];
[_delegate initializeSDKWithLaunchOptions:@{}];

NSObject *tokenCache = (NSObject *) FBSDKAccessToken.tokenCache;
XCTAssertEqualObjects(tokenCache.class, FBSDKTokenCache.class, "Should be configured with expected concrete token cache");
}

- (void)testInitializingSdkConfiguresProfile
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate initializeSDK:@{}];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[_delegate initializeSDKWithLaunchOptions:@{}];
NSObject *store = (NSObject *)[FBSDKProfile store];
NSObject *tokenProvider = (NSObject *)[FBSDKProfile accessTokenProvider];
XCTAssertEqualObjects(
Expand All @@ -348,19 +349,19 @@ - (void)testInitializingSdkConfiguresProfile

- (void)testInitializingSdkConfiguresAuthenticationTokenCache
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[FBSDKAuthenticationToken setTokenCache:nil];
[FBSDKApplicationDelegate initializeSDK:@{}];
[_delegate initializeSDKWithLaunchOptions:@{}];

NSObject *tokenCache = (NSObject *) FBSDKAuthenticationToken.tokenCache;
XCTAssertEqualObjects(tokenCache.class, FBSDKTokenCache.class, "Should be configured with expected concrete token cache");
}

- (void)testInitializingSdkConfiguresAccessTokenConnectionFactory
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
FBSDKAccessToken.connectionFactory = nil;
[FBSDKApplicationDelegate initializeSDK:@{}];
[_delegate initializeSDKWithLaunchOptions:@{}];

NSObject *connectionFactory = (NSObject *) FBSDKAccessToken.connectionFactory;
XCTAssertEqualObjects(
Expand All @@ -372,9 +373,9 @@ - (void)testInitializingSdkConfiguresAccessTokenConnectionFactory

- (void)testInitializingSdkConfiguresSettings
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[FBSDKSettings reset];
[FBSDKApplicationDelegate initializeSDK:@{}];
[_delegate initializeSDKWithLaunchOptions:@{}];

NSObject *store = (NSObject *) FBSDKSettings.store;
NSObject *appEventsConfigProvider = (NSObject *) FBSDKSettings.appEventsConfigurationProvider;
Expand Down Expand Up @@ -404,8 +405,8 @@ - (void)testInitializingSdkConfiguresSettings

- (void)testInitializingSdkConfiguresInternalUtility
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate initializeSDK:@{}];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[_delegate initializeSDKWithLaunchOptions:@{}];
NSObject *infoDictionaryProvider = (NSObject *)[FBSDKInternalUtility infoDictionaryProvider];
XCTAssertEqualObjects(
infoDictionaryProvider,
Expand All @@ -416,8 +417,8 @@ - (void)testInitializingSdkConfiguresInternalUtility

- (void)testInitializingSdkConfiguredGraphRequestPiggybackManager
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate initializeSDK:@{}];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[_delegate initializeSDKWithLaunchOptions:@{}];
NSObject *tokenWallet = (NSObject *) FBSDKGraphRequestPiggybackManager.tokenWallet;
XCTAssertEqualObjects(
tokenWallet,
Expand All @@ -428,20 +429,19 @@ - (void)testInitializingSdkConfiguredGraphRequestPiggybackManager

- (void)testInitializingSdkAddsBridgeApiObserver
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate initializeSDK:@{}];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[_delegate initializeSDKWithLaunchOptions:@{}];

XCTAssertTrue(
[FBSDKApplicationDelegate.sharedInstance.applicationObservers containsObject:FBSDKBridgeAPI.sharedInstance],
[_delegate.applicationObservers containsObject:FBSDKBridgeAPI.sharedInstance],
"Should add the shared bridge api instance to the application observers"
);
}

- (void)testInitializingSdkPerformsSettingsLogging
{
[FBSDKApplicationDelegate resetIsSdkInitialized];
[FBSDKApplicationDelegate initializeSDKWithApplicationDelegate:_delegate
launchOptions:@{}];
[FBSDKApplicationDelegate resetHasInitializeBeenCalled];
[_delegate initializeSDKWithLaunchOptions:@{}];
XCTAssertEqual(
TestSettings.logWarningsCallCount,
1,
Expand Down
Loading

0 comments on commit 813fa58

Please sign in to comment.