session-ios/SignalServiceKit/src/SSKEnvironment.m
2018-09-19 15:11:26 -04:00

125 lines
3.1 KiB
Objective-C

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "SSKEnvironment.h"
#import "AppContext.h"
#import "OWSPrimaryStorage.h"
NS_ASSUME_NONNULL_BEGIN
static SSKEnvironment *sharedSSKEnvironment;
@implementation SSKEnvironment
@synthesize callMessageHandler = _callMessageHandler;
@synthesize notificationsManager = _notificationsManager;
@synthesize objectReadWriteConnection = _objectReadWriteConnection;
- (instancetype)initWithContactsManager:(id<ContactsManagerProtocol>)contactsManager
messageSender:(OWSMessageSender *)messageSender
profileManager:(id<ProfileManagerProtocol>)profileManager
primaryStorage:(OWSPrimaryStorage *)primaryStorage
contactsUpdater:(ContactsUpdater *)contactsUpdater
networkManager:(TSNetworkManager *)networkManager
{
self = [super init];
if (!self) {
return self;
}
OWSAssertDebug(contactsManager);
OWSAssertDebug(messageSender);
OWSAssertDebug(profileManager);
OWSAssertDebug(primaryStorage);
OWSAssertDebug(contactsUpdater);
OWSAssertDebug(networkManager);
_contactsManager = contactsManager;
_messageSender = messageSender;
_profileManager = profileManager;
_primaryStorage = primaryStorage;
_contactsUpdater = contactsUpdater;
_networkManager = networkManager;
return self;
}
+ (instancetype)shared
{
OWSAssertDebug(sharedSSKEnvironment);
return sharedSSKEnvironment;
}
+ (void)setShared:(SSKEnvironment *)env
{
OWSAssertDebug(env);
OWSAssertDebug(!sharedSSKEnvironment || CurrentAppContext().isRunningTests);
sharedSSKEnvironment = env;
}
+ (void)clearSharedForTests
{
sharedSSKEnvironment = nil;
}
#pragma mark - Mutable Accessors
- (nullable id<OWSCallMessageHandler>)callMessageHandler
{
@synchronized(self) {
OWSAssertDebug(_callMessageHandler);
return _callMessageHandler;
}
}
- (void)setCallMessageHandler:(nullable id<OWSCallMessageHandler>)callMessageHandler
{
@synchronized(self) {
OWSAssertDebug(callMessageHandler);
OWSAssertDebug(!_callMessageHandler);
_callMessageHandler = callMessageHandler;
}
}
- (nullable id<NotificationsProtocol>)notificationsManager
{
@synchronized(self) {
OWSAssertDebug(_notificationsManager);
return _notificationsManager;
}
}
- (void)setNotificationsManager:(nullable id<NotificationsProtocol>)notificationsManager
{
@synchronized(self) {
OWSAssertDebug(notificationsManager);
OWSAssertDebug(!_notificationsManager);
_notificationsManager = notificationsManager;
}
}
- (BOOL)isComplete
{
return (self.callMessageHandler != nil && self.notificationsManager != nil);
}
- (YapDatabaseConnection *)objectReadWriteConnection
{
@synchronized(self) {
if (!_objectReadWriteConnection) {
_objectReadWriteConnection = self.primaryStorage.newDatabaseConnection;
}
return _objectReadWriteConnection;
}
}
@end
NS_ASSUME_NONNULL_END