Share picker searches by profile name

Consolidate some of the share logic

// FREEBIE
This commit is contained in:
Michael Kirk 2017-12-04 12:24:04 -05:00
parent 3ed52b6d5a
commit 766e579961
4 changed files with 86 additions and 27 deletions

View file

@ -60,7 +60,7 @@ NS_ASSUME_NONNULL_BEGIN
[self updateContacts];
self.shouldNotifyDelegateOfUpdatedContacts = NO;
_signalAccountSearcher = [self buildAccountSearcher];
_signalAccountSearcher = [self buildSignalAccountSearcher];
[self observeNotifications];
@ -102,7 +102,7 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - Contacts
- (AnySearcher *)buildAccountSearcher
- (AnySearcher *)buildSignalAccountSearcher
{
return [[AnySearcher alloc] initWithIndexer:^NSString *_Nonnull(id _Nonnull obj) {
if (![obj isKindOfClass:[SignalAccount class]]) {

View file

@ -236,28 +236,8 @@ NS_ASSUME_NONNULL_BEGIN
- (NSArray<TSThread *> *)filteredThreadsWithSearchText
{
NSArray<TSThread *> *threads = self.threadViewHelper.threads;
NSString *searchTerm = [[self.searchBar text] ows_stripped];
if ([searchTerm isEqualToString:@""]) {
return threads;
}
NSString *formattedNumber = [PhoneNumber removeFormattingCharacters:searchTerm];
NSMutableArray *result = [NSMutableArray new];
for (TSThread *thread in threads) {
if ([thread.name containsString:searchTerm]) {
[result addObject:thread];
} else if ([thread isKindOfClass:[TSContactThread class]]) {
TSContactThread *contactThread = (TSContactThread *)thread;
if (formattedNumber.length > 0 && [contactThread.contactIdentifier containsString:formattedNumber]) {
[result addObject:thread];
}
}
}
return result;
return [self.threadViewHelper threadsMatchingSearchString:searchTerm];
}
- (NSArray<SignalAccount *> *)filteredSignalAccountsWithSearchText
@ -273,10 +253,11 @@ NS_ASSUME_NONNULL_BEGIN
}
}
NSString *searchString = [self.searchBar text];
NSString *searchString = self.searchBar.text;
NSArray<SignalAccount *> *matchingAccounts =
[self.contactsViewHelper signalAccountsMatchingSearchString:searchString];
ContactsViewHelper *helper = self.contactsViewHelper;
return [[helper signalAccountsMatchingSearchString:searchString]
return [matchingAccounts
filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(SignalAccount *signalAccount,
NSDictionary<NSString *, id> *_Nullable bindings) {
return ![contactIdsToIgnore containsObject:signalAccount.recipientId];

View file

@ -4,6 +4,8 @@
NS_ASSUME_NONNULL_BEGIN
@class AnySearcher;
@protocol ThreadViewHelperDelegate <NSObject>
- (void)threadListDidChange;
@ -22,8 +24,11 @@ NS_ASSUME_NONNULL_BEGIN
@interface ThreadViewHelper : NSObject
@property (nonatomic, weak) id<ThreadViewHelperDelegate> delegate;
@property (nonatomic, readonly) NSMutableArray<TSThread *> *threads;
@property (nonatomic, readonly) AnySearcher *groupThreadSearcher;
@property (nonatomic, readonly) AnySearcher *contactThreadSearcher;
- (NSArray<TSThread *> *)threadsMatchingSearchString:(NSString *)searchString;
@end

View file

@ -3,6 +3,7 @@
//
#import "ThreadViewHelper.h"
#import "Signal-Swift.h"
#import <SignalServiceKit/TSDatabaseView.h>
#import <SignalServiceKit/TSStorageManager.h>
#import <SignalServiceKit/TSThread.h>
@ -29,6 +30,8 @@ NS_ASSUME_NONNULL_BEGIN
}
[self initializeMapping];
_groupThreadSearcher = [self buildGroupThreadSearcher];
_contactThreadSearcher = [self buildContactThreadSearcher];
return self;
}
@ -123,6 +126,76 @@ NS_ASSUME_NONNULL_BEGIN
_threads = [threads copy];
}
#pragma mark - Searching
- (OWSContactsManager *)contactsManager
{
return [Environment getCurrent].contactsManager;
}
- (NSString *)searchIndexStringForRecipientId:(NSString *)recipientId
{
NSString *contactName = [self.contactsManager displayNameForPhoneIdentifier:recipientId];
NSString *profileName = [self.contactsManager profileNameForRecipientId:recipientId];
return [NSString stringWithFormat:@"%@ %@ %@", recipientId, contactName, profileName];
}
- (AnySearcher *)buildContactThreadSearcher
{
AnySearcher *searcher = [[AnySearcher alloc] initWithIndexer:^NSString *_Nonnull(id _Nonnull obj) {
if (![obj isKindOfClass:[TSContactThread class]]) {
OWSFail(@"unexpected item in searcher");
return @"";
}
TSContactThread *contactThread = (TSContactThread *)obj;
NSString *recipientId = contactThread.contactIdentifier;
return [self searchIndexStringForRecipientId:recipientId];
}];
return searcher;
}
- (AnySearcher *)buildGroupThreadSearcher
{
AnySearcher *searcher = [[AnySearcher alloc] initWithIndexer:^NSString *_Nonnull(id _Nonnull obj) {
if (![obj isKindOfClass:[TSGroupThread class]]) {
OWSFail(@"unexpected item in searcher");
return @"";
}
TSGroupThread *groupThread = (TSGroupThread *)obj;
NSString *groupName = groupThread.groupModel.groupName;
NSMutableString *groupMemberStrings = [NSMutableString new];
for (NSString *recipientId in groupThread.groupModel.groupMemberIds) {
NSString *recipientString = [self searchIndexStringForRecipientId:recipientId];
[groupMemberStrings appendFormat:@" %@", recipientString];
}
return [NSString stringWithFormat:@"%@ %@", groupName, groupMemberStrings];
}];
return searcher;
}
- (NSArray<TSThread *> *)threadsMatchingSearchString:(NSString *)searchString
{
if (searchString.length == 0) {
return self.threads;
}
NSMutableArray *result = [NSMutableArray new];
for (TSThread *thread in self.threads) {
AnySearcher *searcher =
[thread isKindOfClass:[TSContactThread class]] ? self.contactThreadSearcher : self.groupThreadSearcher;
if ([searcher item:thread doesMatchQuery:searchString]) {
[result addObject:thread];
}
}
return result;
}
@end
NS_ASSUME_NONNULL_END