session-ios/SessionMessagingKit/Utilities/AppReadiness.m
Morgan Pretty e768bebe6d Merge remote-tracking branch 'upstream/dev' into feature/job-runner-unit-tests
# Conflicts:
#	Session.xcodeproj/project.pbxproj
#	Session/Meta/Session-Prefix.pch
#	Session/Notifications/SyncPushTokensJob.swift
#	Session/Utilities/BackgroundPoller.swift
#	SessionMessagingKit/Configuration.swift
#	SessionMessagingKit/Database/Models/Profile.swift
#	SessionMessagingKit/Jobs/Types/AttachmentDownloadJob.swift
#	SessionMessagingKit/Jobs/Types/AttachmentUploadJob.swift
#	SessionMessagingKit/Jobs/Types/DisappearingMessagesJob.swift
#	SessionMessagingKit/Jobs/Types/FailedAttachmentDownloadsJob.swift
#	SessionMessagingKit/Jobs/Types/FailedMessageSendsJob.swift
#	SessionMessagingKit/Jobs/Types/GroupLeavingJob.swift
#	SessionMessagingKit/Jobs/Types/MessageReceiveJob.swift
#	SessionMessagingKit/Jobs/Types/MessageSendJob.swift
#	SessionMessagingKit/Jobs/Types/NotifyPushServerJob.swift
#	SessionMessagingKit/Jobs/Types/RetrieveDefaultOpenGroupRoomsJob.swift
#	SessionMessagingKit/Jobs/Types/SendReadReceiptsJob.swift
#	SessionMessagingKit/Jobs/Types/UpdateProfilePictureJob.swift
#	SessionMessagingKit/Sending & Receiving/MessageSender.swift
#	SessionMessagingKit/Sending & Receiving/Pollers/ClosedGroupPoller.swift
#	SessionMessagingKit/Utilities/AppReadiness.m
#	SessionMessagingKitTests/Open Groups/OpenGroupManagerSpec.swift
#	SessionMessagingKitTests/_TestUtilities/TestOnionRequestAPI.swift
#	SessionShareExtension/ShareNavController.swift
#	SessionSnodeKit/Jobs/GetSnodePoolJob.swift
#	SessionUtilitiesKit/Configuration.swift
#	SessionUtilitiesKit/Database/Utilities/Database+Utilities.swift
#	SessionUtilitiesKit/JobRunner/JobRunner.swift
#	SignalUtilitiesKit/Meta/SignalUtilitiesKit.h
#	SignalUtilitiesKit/Utilities/SSKAsserts.h
2023-07-18 10:02:51 +10:00

150 lines
3.2 KiB
Objective-C
Executable file

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "AppReadiness.h"
#import "AppContext.h"
#import <SignalCoreKit/SignalCoreKit.h>
#import <SignalCoreKit/Threading.h>
#import <SessionUtilitiesKit/SessionUtilitiesKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface AppReadiness ()
@property (atomic) BOOL isAppReady;
@property (nonatomic) NSMutableArray<AppReadyBlock> *appWillBecomeReadyBlocks;
@property (nonatomic) NSMutableArray<AppReadyBlock> *appDidBecomeReadyBlocks;
@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;
}
self.appWillBecomeReadyBlocks = [NSMutableArray new];
self.appDidBecomeReadyBlocks = [NSMutableArray new];
return self;
}
+ (BOOL)isAppReady
{
return [self.sharedManager isAppReady];
}
+ (void)runNowOrWhenAppWillBecomeReady:(AppReadyBlock)block
{
if ([NSThread isMainThread]) {
[self.sharedManager runNowOrWhenAppWillBecomeReady:block];
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self.sharedManager runNowOrWhenAppWillBecomeReady:block];
});
}
}
- (void)runNowOrWhenAppWillBecomeReady:(AppReadyBlock)block
{
if ([SNUtilitiesKitConfiguration isRunningTests]) {
// We don't need to do any "on app ready" work in the tests.
return;
}
if (self.isAppReady) {
block();
return;
}
[self.appWillBecomeReadyBlocks addObject:block];
}
+ (void)runNowOrWhenAppDidBecomeReady:(AppReadyBlock)block
{
if ([NSThread isMainThread]) {
[self.sharedManager runNowOrWhenAppDidBecomeReady:block];
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self.sharedManager runNowOrWhenAppDidBecomeReady:block];
});
}
}
- (void)runNowOrWhenAppDidBecomeReady:(AppReadyBlock)block
{
if ([SNUtilitiesKitConfiguration isRunningTests]) {
// We don't need to do any "on app ready" work in the tests.
return;
}
if (self.isAppReady) {
block();
return;
}
[self.appDidBecomeReadyBlocks addObject:block];
}
+ (void)invalidate
{
[self.sharedManager invalidate];
}
- (void)invalidate
{
self.isAppReady = NO;
}
+ (void)setAppIsReady
{
[self.sharedManager setAppIsReady];
}
- (void)setAppIsReady
{
self.isAppReady = YES;
[self runAppReadyBlocks];
}
- (void)runAppReadyBlocks
{
NSArray<AppReadyBlock> *appWillBecomeReadyBlocks = [self.appWillBecomeReadyBlocks copy];
[self.appWillBecomeReadyBlocks removeAllObjects];
NSArray<AppReadyBlock> *appDidBecomeReadyBlocks = [self.appDidBecomeReadyBlocks copy];
[self.appDidBecomeReadyBlocks removeAllObjects];
// We invoke the _will become_ blocks before the _did become_ blocks.
for (AppReadyBlock block in appWillBecomeReadyBlocks) {
block();
}
for (AppReadyBlock block in appDidBecomeReadyBlocks) {
block();
}
}
@end
NS_ASSUME_NONNULL_END