From 131c27d089fbcf6e7972012651ae38b08532780d Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Tue, 15 Oct 2019 09:43:58 +1100 Subject: [PATCH] Fix duplication --- .../src/Loki/Onboarding/DisplayNameVC.swift | 4 +-- .../Group Chat/LokiPublicChatManager.swift | 6 ++--- SignalServiceKit/src/Loki/API/LokiAPI.swift | 2 +- .../Loki/Database/LokiDatabaseUtilities.swift | 18 ++++++++++--- .../Database/OWSPrimaryStorage+Loki.swift | 27 ------------------- .../src/Messages/OWSMessageSender.m | 2 +- 6 files changed, 21 insertions(+), 38 deletions(-) diff --git a/Signal/src/Loki/Onboarding/DisplayNameVC.swift b/Signal/src/Loki/Onboarding/DisplayNameVC.swift index 65eaed844..58c5bf7e3 100644 --- a/Signal/src/Loki/Onboarding/DisplayNameVC.swift +++ b/Signal/src/Loki/Onboarding/DisplayNameVC.swift @@ -4,12 +4,12 @@ final class DisplayNameVC : OnboardingBaseViewController { private lazy var userNameTextField: UITextField = { let result = UITextField() result.textColor = Theme.primaryColor - result.font = UIFont.ows_dynamicTypeBodyClamped + result.font = .ows_dynamicTypeBodyClamped result.textAlignment = .center let placeholder = NSMutableAttributedString(string: NSLocalizedString("Display Name", comment: "")) placeholder.addAttribute(.foregroundColor, value: Theme.placeholderColor, range: NSRange(location: 0, length: placeholder.length)) result.attributedPlaceholder = placeholder - result.tintColor = UIColor.lokiGreen() + result.tintColor = .lokiGreen() result.accessibilityIdentifier = "onboarding.accountDetailsStep.userNameTextField" result.keyboardAppearance = .dark return result diff --git a/SignalServiceKit/src/Loki/API/Group Chat/LokiPublicChatManager.swift b/SignalServiceKit/src/Loki/API/Group Chat/LokiPublicChatManager.swift index 5ec16ecdc..772b0ad61 100644 --- a/SignalServiceKit/src/Loki/API/Group Chat/LokiPublicChatManager.swift +++ b/SignalServiceKit/src/Loki/API/Group Chat/LokiPublicChatManager.swift @@ -85,7 +85,7 @@ public final class LokiPublicChatManager: NSObject { } // Save the group chat - self.storage.setGroupChat(chat, for: thread.uniqueId!, in: transaction) + LokiDatabaseUtilities.setGroupChat(chat, for: thread.uniqueId!, in: transaction) } // Update chats and pollers @@ -101,7 +101,7 @@ public final class LokiPublicChatManager: NSObject { private func refreshChatsAndPollers() { storage.dbReadConnection.read { transaction in - let newChats = self.storage.getAllGroupChats(in: transaction) + let newChats = LokiDatabaseUtilities.getAllGroupChats(in: transaction) // Remove any chats that don't exist in the database let removedChatThreadIds = self.chats.keys.filter { !newChats.keys.contains($0) } @@ -129,7 +129,7 @@ public final class LokiPublicChatManager: NSObject { // Remove the chat from the db storage.dbReadWriteConnection.readWrite { transaction in - self.storage.removeGroupChat(for: threadId, in: transaction) + LokiDatabaseUtilities.removeGroupChat(for: threadId, in: transaction) } refreshChatsAndPollers() diff --git a/SignalServiceKit/src/Loki/API/LokiAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI.swift index d2b28897e..cb654cfd8 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI.swift @@ -310,7 +310,7 @@ public final class LokiAPI : NSObject { var candidates: [Mention] = [] // Gather candidates var groupChat: LokiGroupChat? - OWSPrimaryStorage.shared().dbReadConnection.read { transaction in + storage.dbReadConnection.read { transaction in groupChat = LokiDatabaseUtilities.getGroupChat(for: threadID, in: transaction) } storage.dbReadConnection.read { transaction in diff --git a/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift b/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift index 3f80b4392..3e8796289 100644 --- a/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift +++ b/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift @@ -4,6 +4,7 @@ public final class LokiDatabaseUtilities : NSObject { private override init() { } + // MARK: Quotes @objc(getServerIDForQuoteWithID:quoteeHexEncodedPublicKey:threadID:transaction:) public static func getServerID(quoteID: UInt64, quoteeHexEncodedPublicKey: String, threadID: String, transaction: YapDatabaseReadTransaction) -> UInt64 { guard let message = TSInteraction.interactions(withTimestamp: quoteID, filter: { interaction in @@ -20,28 +21,37 @@ public final class LokiDatabaseUtilities : NSObject { return message.groupChatServerID } + // MARK: Device Links @objc(getMasterHexEncodedPublicKeyFor:in:) public static func objc_getMasterHexEncodedPublicKey(for slaveHexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> String? { return OWSPrimaryStorage.shared().getMasterHexEncodedPublicKey(for: slaveHexEncodedPublicKey, in: transaction) } + // MARK: Group Chats + private static let groupChatCollection = "LokiGroupChatCollection" + @objc(getAllGroupChats:) public static func getAllGroupChats(in transaction: YapDatabaseReadTransaction) -> [String:LokiGroupChat] { - return OWSPrimaryStorage.shared().getAllGroupChats(in: transaction) + var result = [String:LokiGroupChat]() + transaction.enumerateKeysAndObjects(inCollection: groupChatCollection) { threadID, object, _ in + guard let groupChat = object as? LokiGroupChat else { return } + result[threadID] = groupChat + } + return result } @objc(getGroupChatForThreadID:transaction:) public static func getGroupChat(for threadID: String, in transaction: YapDatabaseReadTransaction) -> LokiGroupChat? { - return OWSPrimaryStorage.shared().getGroupChat(for: threadID, in: transaction) + return transaction.object(forKey: threadID, inCollection: groupChatCollection) as? LokiGroupChat } @objc(setGroupChat:threadID:transaction:) public static func setGroupChat(_ groupChat: LokiGroupChat, for threadID: String, in transaction: YapDatabaseReadWriteTransaction) { - return OWSPrimaryStorage.shared().setGroupChat(groupChat, for: threadID, in: transaction) + transaction.setObject(groupChat, forKey: threadID, inCollection: groupChatCollection) } @objc(removeGroupChatForThreadID:transaction:) public static func removeGroupChat(for threadID: String, in transaction: YapDatabaseReadWriteTransaction) { - return OWSPrimaryStorage.shared().removeGroupChat(for: threadID, in: transaction) + transaction.removeObject(forKey: threadID, inCollection: groupChatCollection) } } diff --git a/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.swift b/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.swift index 478720c85..71ba0eedc 100644 --- a/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.swift +++ b/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.swift @@ -1,7 +1,6 @@ public extension OWSPrimaryStorage { - // MARK: Device Links private func getDeviceLinkCollection(for primaryDevice: String) -> String { return "LokiDeviceLinkCollection-\(primaryDevice)" } @@ -52,30 +51,4 @@ public extension OWSPrimaryStorage { public func getMasterHexEncodedPublicKey(for slaveHexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> String? { return getDeviceLink(for: slaveHexEncodedPublicKey, in: transaction)?.master.hexEncodedPublicKey } - - // MARK: Group Chats - private var groupChatCollection: String { - return "LokiGroupChatCollection" - } - - public func getAllGroupChats(in transaction: YapDatabaseReadTransaction) -> [String:LokiGroupChat] { - var result = [String:LokiGroupChat]() - transaction.enumerateKeysAndObjects(inCollection: groupChatCollection) { threadID, object, _ in - guard let groupChat = object as? LokiGroupChat else { return } - result[threadID] = groupChat - } - return result - } - - public func getGroupChat(for threadID: String, in transaction: YapDatabaseReadTransaction) -> LokiGroupChat? { - return transaction.object(forKey: threadID, inCollection: groupChatCollection) as? LokiGroupChat - } - - public func setGroupChat(_ groupChat: LokiGroupChat, for threadID: String, in transaction: YapDatabaseReadWriteTransaction) { - transaction.setObject(groupChat, forKey: threadID, inCollection: groupChatCollection) - } - - public func removeGroupChat(for threadID: String, in transaction: YapDatabaseReadWriteTransaction) { - transaction.removeObject(forKey: threadID, inCollection: groupChatCollection) - } } diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index d3f07b217..4b796818f 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -503,7 +503,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; if ([message isKindOfClass:[OWSOutgoingSyncMessage class]]) { [recipientIds addObject:self.tsAccountManager.localNumber]; } else if (thread.isGroupThread) { - [self.primaryStorage.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction * _Nonnull transaction) { + [self.primaryStorage.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { LKGroupChat *groupChat = [LKDatabaseUtilities getGroupChatForThreadID:thread.uniqueId transaction:transaction]; [recipientIds addObject:groupChat.server]; }];