session-ios/SignalServiceKit/src/SSKEnvironment.m

125 lines
3.1 KiB
Mathematica
Raw Normal View History

//
2018-07-18 03:32:44 +02:00
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
2015-12-07 03:31:43 +01:00
#import "SSKEnvironment.h"
2017-12-06 21:53:19 +01:00
#import "AppContext.h"
2018-09-17 15:27:58 +02:00
#import "OWSPrimaryStorage.h"
2015-12-07 03:31:43 +01:00
NS_ASSUME_NONNULL_BEGIN
static SSKEnvironment *sharedSSKEnvironment;
@implementation SSKEnvironment
2018-09-17 15:27:58 +02:00
@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);
2018-09-17 15:27:58 +02:00
OWSAssertDebug(primaryStorage);
OWSAssertDebug(contactsUpdater);
OWSAssertDebug(networkManager);
2017-12-06 21:53:19 +01:00
_contactsManager = contactsManager;
_messageSender = messageSender;
_profileManager = profileManager;
2018-09-17 15:27:58 +02:00
_primaryStorage = primaryStorage;
_contactsUpdater = contactsUpdater;
_networkManager = networkManager;
return self;
}
+ (instancetype)shared
{
OWSAssertDebug(sharedSSKEnvironment);
2015-12-07 03:31:43 +01:00
return sharedSSKEnvironment;
}
+ (void)setShared:(SSKEnvironment *)env
{
OWSAssertDebug(env);
OWSAssertDebug(!sharedSSKEnvironment || CurrentAppContext().isRunningTests);
sharedSSKEnvironment = env;
}
2018-09-17 15:27:58 +02:00
+ (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;
}
}
2015-12-07 03:31:43 +01:00
@end
NS_ASSUME_NONNULL_END