mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
1.9 KiB
Objective-C
111 lines
1.9 KiB
Objective-C
//
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
#import "AppReadiness.h"
|
|
#import <SignalCoreKit/Threading.h>
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
@interface AppReadiness ()
|
|
|
|
@property (atomic) BOOL isAppReady;
|
|
|
|
@property (nonatomic) NSMutableArray<AppReadyBlock> *appReadyBlocks;
|
|
|
|
@end
|
|
|
|
#pragma mark -
|
|
|
|
@implementation AppReadiness
|
|
|
|
+ (instancetype)sharedManager
|
|
{
|
|
static AppReadiness *sharedMyManager = nil;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
sharedMyManager = [[self alloc] initDefault];
|
|
});
|
|
return sharedMyManager;
|
|
}
|
|
|
|
- (instancetype)initDefault
|
|
{
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
return self;
|
|
}
|
|
|
|
OWSSingletonAssert();
|
|
|
|
self.appReadyBlocks = [NSMutableArray new];
|
|
|
|
return self;
|
|
}
|
|
|
|
+ (BOOL)isAppReady
|
|
{
|
|
return [self.sharedManager isAppReady];
|
|
}
|
|
|
|
+ (void)runNowOrWhenAppIsReady:(AppReadyBlock)block
|
|
{
|
|
DispatchMainThreadSafe(^{
|
|
[self.sharedManager runNowOrWhenAppIsReady:block];
|
|
});
|
|
}
|
|
|
|
- (void)runNowOrWhenAppIsReady:(AppReadyBlock)block
|
|
{
|
|
OWSAssertIsOnMainThread();
|
|
OWSAssertDebug(block);
|
|
|
|
if (CurrentAppContext().isRunningTests) {
|
|
// We don't need to an any "on app ready" work
|
|
// in the tests.
|
|
return;
|
|
}
|
|
|
|
if (self.isAppReady) {
|
|
block();
|
|
return;
|
|
}
|
|
|
|
[self.appReadyBlocks addObject:block];
|
|
}
|
|
|
|
+ (void)setAppIsReady
|
|
{
|
|
[self.sharedManager setAppIsReady];
|
|
}
|
|
|
|
- (void)setAppIsReady
|
|
{
|
|
OWSAssertIsOnMainThread();
|
|
OWSAssertDebug(!self.isAppReady);
|
|
|
|
OWSLogInfo(@"");
|
|
|
|
self.isAppReady = YES;
|
|
|
|
[self runAppReadyBlocks];
|
|
}
|
|
|
|
- (void)runAppReadyBlocks
|
|
{
|
|
OWSAssertIsOnMainThread();
|
|
OWSAssertDebug(self.isAppReady);
|
|
|
|
NSArray<AppReadyBlock> *appReadyBlocks = [self.appReadyBlocks copy];
|
|
[self.appReadyBlocks removeAllObjects];
|
|
|
|
for (AppReadyBlock block in appReadyBlocks) {
|
|
block();
|
|
}
|
|
}
|
|
|
|
@end
|
|
|
|
NS_ASSUME_NONNULL_END
|