diff --git a/SignalMessaging/environment/AppSetup.m b/SignalMessaging/environment/AppSetup.m index c4864b10d1..ac165cba9f 100644 --- a/SignalMessaging/environment/AppSetup.m +++ b/SignalMessaging/environment/AppSetup.m @@ -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(); diff --git a/SignalMessaging/utils/OWSPreferences.m b/SignalMessaging/utils/OWSPreferences.m index ab5502ba1e..858e6879ba 100644 --- a/SignalMessaging/utils/OWSPreferences.m +++ b/SignalMessaging/utils/OWSPreferences.m @@ -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 diff --git a/SignalServiceKit/src/SSKEnvironment.h b/SignalServiceKit/src/SSKEnvironment.h index 84a431d86b..2fb4233c62 100644 --- a/SignalServiceKit/src/SSKEnvironment.h +++ b/SignalServiceKit/src/SSKEnvironment.h @@ -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)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; diff --git a/SignalServiceKit/src/SSKEnvironment.m b/SignalServiceKit/src/SSKEnvironment.m index 5fb451151e..1a610d534c 100644 --- a/SignalServiceKit/src/SSKEnvironment.m +++ b/SignalServiceKit/src/SSKEnvironment.m @@ -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)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; } diff --git a/SignalServiceKit/src/TestUtils/MockSSKEnvironment.m b/SignalServiceKit/src/TestUtils/MockSSKEnvironment.m index 8999167ffb..8e5430f9d7 100644 --- a/SignalServiceKit/src/TestUtils/MockSSKEnvironment.m +++ b/SignalServiceKit/src/TestUtils/MockSSKEnvironment.m @@ -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; diff --git a/SignalServiceKit/src/Util/SSKPreferences.swift b/SignalServiceKit/src/Util/SSKPreferences.swift index 746797bcb9..2449542fe6 100644 --- a/SignalServiceKit/src/Util/SSKPreferences.swift +++ b/SignalServiceKit/src/Util/SSKPreferences.swift @@ -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: -