Add application context class.

This commit is contained in:
Matthew Chen 2017-11-29 11:01:30 -05:00
parent 594544b264
commit ffa69b3502
3 changed files with 63 additions and 1 deletions

View File

@ -25,6 +25,7 @@
#import "ViewControllerUtils.h"
#import <AxolotlKit/SessionCipher.h>
#import <SignalMessaging/SignalMessaging.h>
#import <SignalServiceKit/AppContext.h>
#import <SignalServiceKit/NSUserDefaults+OWS.h>
#import <SignalServiceKit/OWSBatchMessageProcessor.h>
#import <SignalServiceKit/OWSDisappearingMessagesJob.h>
@ -50,7 +51,7 @@ static NSString *const kInitialViewControllerIdentifier = @"UserInitialViewContr
static NSString *const kURLSchemeSGNLKey = @"sgnl";
static NSString *const kURLHostVerifyPrefix = @"verify";
@interface AppDelegate ()
@interface AppDelegate () <AppContext>
@property (nonatomic) UIWindow *screenProtectionWindow;
@property (nonatomic) OWSContactsSyncing *contactsSyncing;
@ -89,6 +90,10 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// This should be the first thing we do.
SetCurrentAppContext(self);
BOOL loggingIsEnabled;
#ifdef DEBUG
// Specified at Product -> Scheme -> Edit Scheme -> Test -> Arguments -> Environment to avoid things like
@ -968,4 +973,22 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
[AppUpdateNag.sharedInstance showAppUpgradeNagIfNecessary];
}
#pragma mark - AppContext
- (BOOL)isMainApp
{
return YES;
}
- (BOOL)isMainAppAndActive
{
return [UIApplication sharedApplication].applicationState == UIApplicationStateActive;
}
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:
(BackgroundTaskExpirationHandler)expirationHandler
{
return [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:expirationHandler];
}
@end

View File

@ -0,0 +1,18 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
typedef void (^BackgroundTaskExpirationHandler)(void);
@protocol AppContext <NSObject>
- (BOOL)isMainApp;
- (BOOL)isMainAppAndActive;
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:
(BackgroundTaskExpirationHandler)expirationHandler;
@end
id<AppContext> CurrentAppContext();
void SetCurrentAppContext(id<AppContext> appContext);

View File

@ -0,0 +1,21 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "AppContext.h"
static id<AppContext> currentAppContext = nil;
id<AppContext> CurrentAppContext()
{
OWSCAssert(currentAppContext);
return currentAppContext;
}
void SetCurrentAppContext(id<AppContext> appContext)
{
OWSCAssert(!currentAppContext);
currentAppContext = appContext;
}