Merge branch 'charlesmchen/cachePrefs'

This commit is contained in:
Matthew Chen 2019-11-04 14:26:41 -03:00
commit 658dfa7a60
6 changed files with 61 additions and 10 deletions

View File

@ -97,6 +97,7 @@ NS_ASSUME_NONNULL_BEGIN
SignalServiceAddressCache *signalServiceAddressCache = [SignalServiceAddressCache new];
AccountServiceClient *accountServiceClient = [AccountServiceClient new];
OWSStorageServiceManager *storageServiceManager = OWSStorageServiceManager.shared;
SSKPreferences *sskPreferences = [SSKPreferences new];
OWSAudioSession *audioSession = [OWSAudioSession new];
OWSSounds *sounds = [OWSSounds new];
@ -147,7 +148,8 @@ NS_ASSUME_NONNULL_BEGIN
signalServiceAddressCache:signalServiceAddressCache
accountServiceClient:accountServiceClient
storageServiceManager:storageServiceManager
storageCoordinator:storageCoordinator]];
storageCoordinator:storageCoordinator
sskPreferences:sskPreferences]];
appSpecificSingletonBlock();

View File

@ -50,6 +50,14 @@ NSString *const OWSPreferencesKey_IsAudibleErrorLoggingEnabled = @"IsAudibleErro
NSString *const OWSPreferencesKeySystemCallLogEnabled = @"OWSPreferencesKeySystemCallLogEnabled";
NSString *const OWSPreferencesKeyIsViewOnceMessagesEnabled = @"OWSPreferencesKeyIsViewOnceMessagesEnabled";
@interface OWSPreferences ()
@property (atomic, nullable) NSNumber *notificationPreviewTypeCache;
@end
#pragma mark -
@implementation OWSPreferences
#pragma mark - Dependencies
@ -503,12 +511,21 @@ NSString *const OWSPreferencesKeyIsViewOnceMessagesEnabled = @"OWSPreferencesKey
- (void)setNotificationPreviewType:(NotificationType)value
{
[self setUInt:(NSUInteger)value forKey:OWSPreferencesKeyNotificationPreviewType];
self.notificationPreviewTypeCache = @(value);
}
- (NotificationType)notificationPreviewType
{
return (NotificationType)
[self uintForKey:OWSPreferencesKeyNotificationPreviewType defaultValue:(NSUInteger)NotificationNamePreview];
NSNumber *_Nullable cachedValue = self.notificationPreviewTypeCache;
if (cachedValue != nil) {
return (NotificationType)cachedValue.unsignedIntegerValue;
}
NotificationType result = (NotificationType)[self uintForKey:OWSPreferencesKeyNotificationPreviewType
defaultValue:(NSUInteger)NotificationNamePreview];
self.notificationPreviewTypeCache = @(result);
return result;
}
- (NSString *)nameForNotificationPreviewType:(NotificationType)notificationType

View File

@ -26,6 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
@class SDSDatabaseStorage;
@class SSKMessageDecryptJobQueue;
@class SSKPreKeyStore;
@class SSKPreferences;
@class SSKSessionStore;
@class SSKSignedPreKeyStore;
@class SignalServiceAddressCache;
@ -84,7 +85,8 @@ NS_ASSUME_NONNULL_BEGIN
signalServiceAddressCache:(SignalServiceAddressCache *)signalServiceAddressCache
accountServiceClient:(AccountServiceClient *)accountServiceClient
storageServiceManager:(id<StorageServiceManagerProtocol>)storageServiceManager
storageCoordinator:(StorageCoordinator *)storageCoordinator NS_DESIGNATED_INITIALIZER;
storageCoordinator:(StorageCoordinator *)storageCoordinator
sskPreferences:(SSKPreferences *)sskPreferences NS_DESIGNATED_INITIALIZER;
@property (nonatomic, readonly, class) SSKEnvironment *shared;
@ -132,6 +134,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) StickerManager *stickerManager;
@property (nonatomic, readonly) SDSDatabaseStorage *databaseStorage;
@property (nonatomic, readonly) StorageCoordinator *storageCoordinator;
@property (nonatomic, readonly) SSKPreferences *sskPreferences;
@property (nonatomic, readonly, nullable) OWSPrimaryStorage *primaryStorage;

View File

@ -42,6 +42,7 @@ static SSKEnvironment *sharedSSKEnvironment;
@property (nonatomic) StickerManager *stickerManager;
@property (nonatomic) SDSDatabaseStorage *databaseStorage;
@property (nonatomic) StorageCoordinator *storageCoordinator;
@property (nonatomic) SSKPreferences *sskPreferences;
@end
@ -88,6 +89,7 @@ static SSKEnvironment *sharedSSKEnvironment;
accountServiceClient:(AccountServiceClient *)accountServiceClient
storageServiceManager:(id<StorageServiceManagerProtocol>)storageServiceManager
storageCoordinator:(StorageCoordinator *)storageCoordinator
sskPreferences:(SSKPreferences *)sskPreferences
{
self = [super init];
if (!self) {
@ -128,6 +130,7 @@ static SSKEnvironment *sharedSSKEnvironment;
OWSAssertDebug(accountServiceClient);
OWSAssertDebug(storageServiceManager);
OWSAssertDebug(storageCoordinator);
OWSAssertDebug(sskPreferences);
_contactsManager = contactsManager;
_linkPreviewManager = linkPreviewManager;
@ -164,6 +167,7 @@ static SSKEnvironment *sharedSSKEnvironment;
_accountServiceClient = accountServiceClient;
_storageServiceManager = storageServiceManager;
_storageCoordinator = storageCoordinator;
_sskPreferences = sskPreferences;
return self;
}

View File

@ -97,6 +97,7 @@ NS_ASSUME_NONNULL_BEGIN
SignalServiceAddressCache *signalServiceAddressCache = [SignalServiceAddressCache new];
AccountServiceClient *accountServiceClient = [FakeAccountServiceClient new];
OWSFakeStorageServiceManager *storageServiceManager = [OWSFakeStorageServiceManager new];
SSKPreferences *sskPreferences = [SSKPreferences new];
self = [super initWithContactsManager:contactsManager
linkPreviewManager:linkPreviewManager
@ -132,7 +133,8 @@ NS_ASSUME_NONNULL_BEGIN
signalServiceAddressCache:signalServiceAddressCache
accountServiceClient:accountServiceClient
storageServiceManager:storageServiceManager
storageCoordinator:storageCoordinator];
storageCoordinator:storageCoordinator
sskPreferences:sskPreferences];
if (!self) {
return nil;

View File

@ -6,11 +6,16 @@ import Foundation
@objc
public class SSKPreferences: NSObject {
// Never instantiate this class.
private override init() {}
private static var shared: SSKPreferences {
return SSKEnvironment.shared.sskPreferences
}
public static let store = SDSKeyValueStore(collection: "SSKPreferences")
private var store: SDSKeyValueStore {
return SSKPreferences.store
}
// MARK: -
private static let areLinkPreviewsEnabledKey = "areLinkPreviewsEnabled"
@ -33,16 +38,34 @@ public class SSKPreferences: NSObject {
// MARK: -
private static let hasSavedThreadKey = "hasSavedThread"
@objc
public static func hasSavedThread(transaction: SDSAnyReadTransaction) -> Bool {
return store.getBool(hasSavedThreadKey, defaultValue: false, transaction: transaction)
return shared.hasSavedThread(transaction: transaction)
}
@objc
public static func setHasSavedThread(_ newValue: Bool, transaction: SDSAnyWriteTransaction) {
shared.setHasSavedThread(newValue, transaction: transaction)
}
private let hasSavedThreadKey = "hasSavedThread"
// Only access this queue within db transactions.
private var hasSavedThreadCache: Bool?
@objc
public func hasSavedThread(transaction: SDSAnyReadTransaction) -> Bool {
if let value = hasSavedThreadCache {
return value
}
let value = store.getBool(hasSavedThreadKey, defaultValue: false, transaction: transaction)
hasSavedThreadCache = value
return value
}
@objc
public func setHasSavedThread(_ newValue: Bool, transaction: SDSAnyWriteTransaction) {
store.setBool(newValue, key: hasSavedThreadKey, transaction: transaction)
hasSavedThreadCache = newValue
}
// MARK: -