Minor refactoring

pull/166/head
nielsandriesse 6 years ago
parent d3125c5039
commit 43866596ba

@ -1654,38 +1654,12 @@ typedef enum : NSUInteger {
#pragma mark - Updating
- (void)updateInputToolbar {
BOOL isEnabled;
BOOL isAttachmentButtonHidden;
if ([self.thread isKindOfClass:TSContactThread.class]) {
NSString *senderID = ((TSContactThread *)self.thread).contactIdentifier;
__block NSSet<TSContactThread *> *linkedDeviceThreads;
__block BOOL isNoteToSelf;
[OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
linkedDeviceThreads = [LKDatabaseUtilities getLinkedDeviceThreadsFor:senderID in:transaction];
isNoteToSelf = [LKDatabaseUtilities isUserLinkedDevice:senderID in:transaction];
}];
if ([linkedDeviceThreads contains:^BOOL(TSContactThread *thread) {
return thread.isContactFriend;
}] || isNoteToSelf) {
isEnabled = true;
isAttachmentButtonHidden = false;
} else if (![linkedDeviceThreads contains:^BOOL(TSContactThread *thread) {
return thread.hasPendingFriendRequest;
}]) {
isEnabled = true;
isAttachmentButtonHidden = true;
} else {
isEnabled = false;
isAttachmentButtonHidden = true;
}
} else {
isEnabled = true;
isAttachmentButtonHidden = false;
}
[self.inputToolbar setUserInteractionEnabled:isEnabled];
NSString *placeholderText = isEnabled ? NSLocalizedString(@"Message", "") : NSLocalizedString(@"Pending session request", "");
BOOL shouldInputBarBeEnabled = [LKFriendRequestProtocol shouldInputBarBeEnabledForThread:self.thread];
[self.inputToolbar setUserInteractionEnabled:shouldInputBarBeEnabled];
NSString *placeholderText = shouldInputBarBeEnabled ? NSLocalizedString(@"Message", "") : NSLocalizedString(@"Pending session request", "");
[self.inputToolbar setPlaceholderText:placeholderText];
[self.inputToolbar setAttachmentButtonHidden:isAttachmentButtonHidden];
BOOL shouldAttachmentButtonBeEnabled = [LKFriendRequestProtocol shouldAttachmentButtonBeEnabledForThread:self.thread];
[self.inputToolbar setAttachmentButtonHidden:!shouldAttachmentButtonBeEnabled];
}
#pragma mark - Identity

@ -15,6 +15,45 @@ public final class FriendRequestProtocol : NSObject {
internal static var storage: OWSPrimaryStorage { OWSPrimaryStorage.shared() }
// MARK: - General
@objc(shouldInputBarBeEnabledForThread:)
public static func shouldInputBarBeEnabled(for thread: TSThread) -> Bool {
// Friend requests have nothing to do with groups, so if this isn't a contact thread the input bar should be enabled
guard let thread = thread as? TSContactThread else { return true }
// If this is a note to self, the input bar should be enabled
if SessionProtocol.isMessageNoteToSelf(thread) { return true }
let contactID = thread.contactIdentifier()
var linkedDeviceThreads: Set<TSContactThread> = []
storage.dbReadConnection.read { transaction in
linkedDeviceThreads = LokiDatabaseUtilities.getLinkedDeviceThreads(for: contactID, in: transaction)
}
// If the current user is friends with any of the other user's devices, the input bar should be enabled
if linkedDeviceThreads.contains(where: { $0.isContactFriend }) { return true }
// If no friend request has been sent, the input bar should be enabled
if !linkedDeviceThreads.contains(where: { $0.hasPendingFriendRequest }) { return true }
// There must be a pending friend request
return false
}
@objc(shouldAttachmentButtonBeEnabledForThread:)
public static func shouldAttachmentButtonBeEnabled(for thread: TSThread) -> Bool {
// Friend requests have nothing to do with groups, so if this isn't a contact thread the attachment button should be enabled
guard let thread = thread as? TSContactThread else { return true }
// If this is a note to self, the attachment button should be enabled
if SessionProtocol.isMessageNoteToSelf(thread) { return true }
let contactID = thread.contactIdentifier()
var linkedDeviceThreads: Set<TSContactThread> = []
storage.dbReadConnection.read { transaction in
linkedDeviceThreads = LokiDatabaseUtilities.getLinkedDeviceThreads(for: contactID, in: transaction)
}
// If the current user is friends with any of the other user's devices, the attachment button should be enabled
if linkedDeviceThreads.contains(where: { $0.isContactFriend }) { return true }
// If no friend request has been sent, the attachment button should be disabled
if !linkedDeviceThreads.contains(where: { $0.hasPendingFriendRequest }) { return false }
// There must be a pending friend request
return false
}
// MARK: - Sending
@objc(acceptFriendRequest:in:using:)
public static func acceptFriendRequest(_ friendRequest: TSIncomingMessage, in thread: TSThread, using transaction: YapDatabaseReadWriteTransaction) {

Loading…
Cancel
Save