Move SM singletons to Environment.

pull/1/head
Matthew Chen 7 years ago
parent b9d592ed15
commit 603e3bf0b6

@ -6,7 +6,9 @@
#import "OWSBackup.h" #import "OWSBackup.h"
#import "OWSContactsSyncing.h" #import "OWSContactsSyncing.h"
#import "OWSWindowManager.h" #import "OWSWindowManager.h"
#import <SignalMessaging/LockInteractionController.h>
#import <SignalMessaging/OWSPreferences.h> #import <SignalMessaging/OWSPreferences.h>
#import <SignalMessaging/OWSSounds.h>
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -21,9 +23,21 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)init - (instancetype)init
{ {
OWSPrimaryStorage *primaryStorage = SSKEnvironment.shared.primaryStorage;
OWSAssertDebug(primaryStorage);
// TODO: We should probably mock this out. // TODO: We should probably mock this out.
OWSPreferences *preferences = [OWSPreferences new]; OWSPreferences *preferences = [OWSPreferences new];
self = [super initWithPreferences:preferences]; OWSContactsSyncing *contactsSyncing = [[OWSContactsSyncing alloc] initDefault];
OWSSounds *sounds = [[OWSSounds alloc] initWithPrimaryStorage:primaryStorage];
LockInteractionController *lockInteractionController = [[LockInteractionController alloc] initDefault];
OWSWindowManager *windowManager = [[OWSWindowManager alloc] initDefault];
self = [super initWithPreferences:preferences
contactsSyncing:contactsSyncing
sounds:sounds
lockInteractionController:lockInteractionController
windowManager:windowManager];
OWSAssertDebug(self); OWSAssertDebug(self);
return self; return self;
} }

@ -1,5 +1,5 @@
// //
// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// //
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -13,6 +13,8 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
- (instancetype)initDefault NS_DESIGNATED_INITIALIZER;
+ (instancetype)sharedManager; + (instancetype)sharedManager;
@end @end

@ -24,11 +24,6 @@ NSString *const kOWSPrimaryStorageOWSContactsSyncingLastMessageKey
@property (nonatomic, readonly) dispatch_queue_t serialQueue; @property (nonatomic, readonly) dispatch_queue_t serialQueue;
@property (nonatomic, readonly) OWSContactsManager *contactsManager;
@property (nonatomic, readonly) OWSIdentityManager *identityManager;
@property (nonatomic, readonly) OWSMessageSender *messageSender;
@property (nonatomic, readonly) OWSProfileManager *profileManager;
@property (nonatomic) BOOL isRequestInFlight; @property (nonatomic) BOOL isRequestInFlight;
@end @end
@ -37,26 +32,12 @@ NSString *const kOWSPrimaryStorageOWSContactsSyncingLastMessageKey
+ (instancetype)sharedManager + (instancetype)sharedManager
{ {
static OWSContactsSyncing *instance = nil; OWSAssertDebug(Environment.shared.contactsSyncing);
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] initDefault];
});
return instance;
}
- (instancetype)initDefault return Environment.shared.contactsSyncing;
{
return [self initWithContactsManager:Environment.shared.contactsManager
identityManager:OWSIdentityManager.sharedManager
messageSender:SSKEnvironment.shared.messageSender
profileManager:OWSProfileManager.sharedManager];
} }
- (instancetype)initWithContactsManager:(OWSContactsManager *)contactsManager - (instancetype)initDefault
identityManager:(OWSIdentityManager *)identityManager
messageSender:(OWSMessageSender *)messageSender
profileManager:(OWSProfileManager *)profileManager
{ {
self = [super init]; self = [super init];
@ -64,15 +45,6 @@ NSString *const kOWSPrimaryStorageOWSContactsSyncingLastMessageKey
return self; return self;
} }
OWSAssertDebug(contactsManager);
OWSAssertDebug(messageSender);
OWSAssertDebug(identityManager);
_contactsManager = contactsManager;
_identityManager = identityManager;
_messageSender = messageSender;
_profileManager = profileManager;
OWSSingletonAssert(); OWSSingletonAssert();
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
@ -92,6 +64,34 @@ NSString *const kOWSPrimaryStorageOWSContactsSyncingLastMessageKey
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
} }
#pragma mark - Dependencies
- (OWSContactsManager *)contactsManager {
OWSAssertDebug(Environment.shared.contactsManager);
return Environment.shared.contactsManager;
}
- (OWSIdentityManager *)identityManager {
OWSAssertDebug(SSKEnvironment.shared.identityManager);
return SSKEnvironment.shared.identityManager;
}
- (OWSMessageSender *)messageSender {
OWSAssertDebug(SSKEnvironment.shared.messageSender);
return SSKEnvironment.shared.messageSender;
}
- (OWSProfileManager *)profileManager {
OWSAssertDebug(SSKEnvironment.shared.profileManager);
return SSKEnvironment.shared.profileManager;
}
#pragma mark -
- (void)signalAccountsDidChange:(id)notification - (void)signalAccountsDidChange:(id)notification
{ {
OWSAssertIsOnMainThread(); OWSAssertIsOnMainThread();

@ -82,7 +82,16 @@ NS_ASSUME_NONNULL_BEGIN
OWSOutgoingReceiptManager *outgoingReceiptManager = OWSOutgoingReceiptManager *outgoingReceiptManager =
[[OWSOutgoingReceiptManager alloc] initWithPrimaryStorage:primaryStorage]; [[OWSOutgoingReceiptManager alloc] initWithPrimaryStorage:primaryStorage];
[Environment setShared:[[Environment alloc] initWithPreferences:preferences]]; OWSContactsSyncing *contactsSyncing = [[OWSContactsSyncing alloc] initDefault];
OWSSounds *sounds = [[OWSSounds alloc] initWithPrimaryStorage:primaryStorage];
LockInteractionController *lockInteractionController = [[LockInteractionController alloc] initDefault];
OWSWindowManager *windowManager = [[OWSWindowManager alloc] initDefault];
[Environment setShared:[[Environment alloc] initWithPreferences:preferences
contactsSyncing:contactsSyncing
sounds:sounds
lockInteractionController:lockInteractionController
windowManager:windowManager]];
[SSKEnvironment setShared:[[SSKEnvironment alloc] initWithContactsManager:contactsManager [SSKEnvironment setShared:[[SSKEnvironment alloc] initWithContactsManager:contactsManager
messageSender:messageSender messageSender:messageSender

@ -4,8 +4,12 @@
#import <SignalServiceKit/SSKEnvironment.h> #import <SignalServiceKit/SSKEnvironment.h>
@class LockInteractionController;
@class OWSContactsManager; @class OWSContactsManager;
@class OWSContactsSyncing;
@class OWSPreferences; @class OWSPreferences;
@class OWSSounds;
@class OWSWindowManager;
/** /**
* *
@ -19,10 +23,18 @@
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithPreferences:(OWSPreferences *)preferences; - (instancetype)initWithPreferences:(OWSPreferences *)preferences
contactsSyncing:(OWSContactsSyncing *)contactsSyncing
sounds:(OWSSounds *)sounds
lockInteractionController:(LockInteractionController *)lockInteractionController
windowManager:(OWSWindowManager *)windowManager;
@property (nonatomic, readonly) OWSContactsManager *contactsManager; @property (nonatomic, readonly) OWSContactsManager *contactsManager;
@property (nonatomic, readonly) OWSPreferences *preferences; @property (nonatomic, readonly) OWSPreferences *preferences;
@property (nonatomic, readonly) OWSContactsSyncing *contactsSyncing;
@property (nonatomic, readonly) OWSSounds *sounds;
@property (nonatomic, readonly) LockInteractionController *lockInteractionController;
@property (nonatomic, readonly) OWSWindowManager *windowManager;
@property (class, nonatomic) Environment *shared; @property (class, nonatomic) Environment *shared;

@ -36,15 +36,26 @@ static Environment *sharedEnvironment = nil;
} }
- (instancetype)initWithPreferences:(OWSPreferences *)preferences - (instancetype)initWithPreferences:(OWSPreferences *)preferences
{ contactsSyncing:(OWSContactsSyncing *)contactsSyncing
sounds:(OWSSounds *)sounds
lockInteractionController:(LockInteractionController *)lockInteractionController
windowManager:(OWSWindowManager *)windowManager {
self = [super init]; self = [super init];
if (!self) { if (!self) {
return self; return self;
} }
OWSAssertDebug(preferences); OWSAssertDebug(preferences);
OWSAssertDebug(contactsSyncing);
OWSAssertDebug(sounds);
OWSAssertDebug(lockInteractionController);
OWSAssertDebug(windowManager);
_preferences = preferences; _preferences = preferences;
_contactsSyncing = contactsSyncing;
_sounds = sounds;
_lockInteractionController = lockInteractionController;
_windowManager = windowManager;
OWSSingletonAssert(); OWSSingletonAssert();

@ -40,6 +40,7 @@ typedef NS_ENUM(NSUInteger, OWSSound) {
}; };
@class OWSAudioPlayer; @class OWSAudioPlayer;
@class OWSPrimaryStorage;
@class TSThread; @class TSThread;
@class YapDatabaseReadWriteTransaction; @class YapDatabaseReadWriteTransaction;
@ -47,6 +48,8 @@ typedef NS_ENUM(NSUInteger, OWSSound) {
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithPrimaryStorage:(OWSPrimaryStorage *)primaryStorage NS_DESIGNATED_INITIALIZER;
+ (NSString *)displayNameForSound:(OWSSound)sound; + (NSString *)displayNameForSound:(OWSSound)sound;
+ (nullable NSString *)filenameForSound:(OWSSound)sound; + (nullable NSString *)filenameForSound:(OWSSound)sound;

@ -3,6 +3,7 @@
// //
#import "OWSSounds.h" #import "OWSSounds.h"
#import "Environment.h"
#import "OWSAudioPlayer.h" #import "OWSAudioPlayer.h"
#import <SignalMessaging/SignalMessaging-Swift.h> #import <SignalMessaging/SignalMessaging-Swift.h>
#import <SignalServiceKit/OWSFileSystem.h> #import <SignalServiceKit/OWSFileSystem.h>
@ -68,19 +69,9 @@ NSString *const kOWSSoundsStorageGlobalNotificationKey = @"kOWSSoundsStorageGlob
+ (instancetype)sharedManager + (instancetype)sharedManager
{ {
static OWSSounds *instance = nil; OWSAssertDebug(Environment.shared.sounds);
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] initDefault];
});
return instance;
}
- (instancetype)initDefault
{
OWSPrimaryStorage *primaryStorage = [OWSPrimaryStorage sharedManager];
return [self initWithPrimaryStorage:primaryStorage]; return Environment.shared.sounds;
} }
- (instancetype)initWithPrimaryStorage:(OWSPrimaryStorage *)primaryStorage - (instancetype)initWithPrimaryStorage:(OWSPrimaryStorage *)primaryStorage

@ -20,6 +20,7 @@ extern const UIWindowLevel UIWindowLevel_Background;
@interface OWSWindowManager : NSObject @interface OWSWindowManager : NSObject
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
- (instancetype)initDefault NS_DESIGNATED_INITIALIZER;
+ (instancetype)sharedManager; + (instancetype)sharedManager;

@ -3,6 +3,7 @@
// //
#import "OWSWindowManager.h" #import "OWSWindowManager.h"
#import "Environment.h"
#import "UIColor+OWS.h" #import "UIColor+OWS.h"
#import "UIFont+OWS.h" #import "UIFont+OWS.h"
#import "UIView+OWS.h" #import "UIView+OWS.h"
@ -129,12 +130,9 @@ const UIWindowLevel UIWindowLevel_MessageActions(void)
+ (instancetype)sharedManager + (instancetype)sharedManager
{ {
static OWSWindowManager *instance = nil; OWSAssertDebug(Environment.shared.windowManager);
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ return Environment.shared.windowManager;
instance = [[self alloc] initDefault];
});
return instance;
} }
- (instancetype)initDefault - (instancetype)initDefault

@ -1,9 +1,12 @@
// //
// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// //
@interface LockInteractionController : NSObject @interface LockInteractionController : NSObject
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initDefault NS_DESIGNATED_INITIALIZER;
typedef void (^LIControllerCompletionBlock)(void); typedef void (^LIControllerCompletionBlock)(void);
typedef BOOL (^LIControllerBlockingOperation)(void); typedef BOOL (^LIControllerBlockingOperation)(void);
typedef void (^LIControllerRetryBlock)( typedef void (^LIControllerRetryBlock)(

@ -1,8 +1,9 @@
// //
// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// //
#import "LockInteractionController.h" #import "LockInteractionController.h"
#import "Environment.h"
#import <SignalServiceKit/AppContext.h> #import <SignalServiceKit/AppContext.h>
@interface LockInteractionController () @interface LockInteractionController ()
@ -17,12 +18,22 @@
+ (instancetype)sharedController + (instancetype)sharedController
{ {
static LockInteractionController *sharedController = nil; OWSAssertDebug(Environment.shared.lockInteractionController);
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ return Environment.shared.lockInteractionController;
sharedController = [self new]; }
});
return sharedController; - (instancetype)initDefault {
self = [super init];
if (!self) {
return self;
}
OWSAssertIsOnMainThread();
OWSSingletonAssert();
return self;
} }
+ (void)performBlock:(LIControllerBlockingOperation)blockingOperation + (void)performBlock:(LIControllerBlockingOperation)blockingOperation

Loading…
Cancel
Save