mirror of https://github.com/oxen-io/session-ios
parent
8c6bf3cba6
commit
a181d218cf
@ -0,0 +1,16 @@
|
|||||||
|
//
|
||||||
|
// OWSContactsSearcher.h
|
||||||
|
// Signal
|
||||||
|
//
|
||||||
|
// Created by Michael Kirk on 6/27/16.
|
||||||
|
// Copyright © 2016 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "Contact.h"
|
||||||
|
|
||||||
|
@interface OWSContactsSearcher : NSObject
|
||||||
|
|
||||||
|
- (instancetype)initWithContacts:(NSArray<Contact *> *)contacts;
|
||||||
|
- (NSArray<Contact *> *)filterWithString:(NSString *)string;
|
||||||
|
|
||||||
|
@end
|
@ -0,0 +1,42 @@
|
|||||||
|
//
|
||||||
|
// OWSContactsSearcher.m
|
||||||
|
// Signal
|
||||||
|
//
|
||||||
|
// Created by Michael Kirk on 6/27/16.
|
||||||
|
// Copyright © 2016 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "OWSContactsSearcher.h"
|
||||||
|
#import <SignalServiceKit/PhoneNumber.h>
|
||||||
|
|
||||||
|
@interface OWSContactsSearcher ()
|
||||||
|
|
||||||
|
@property (copy) NSArray<Contact *> *contacts;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation OWSContactsSearcher
|
||||||
|
|
||||||
|
- (instancetype)initWithContacts:(NSArray<Contact *> *)contacts {
|
||||||
|
self = [super init];
|
||||||
|
if (!self) return self;
|
||||||
|
|
||||||
|
_contacts = contacts;
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSArray<Contact *> *)filterWithString:(NSString *)string {
|
||||||
|
NSString *searchTerm = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||||
|
|
||||||
|
if ([searchTerm isEqualToString:@""]) {
|
||||||
|
return self.contacts;
|
||||||
|
}
|
||||||
|
|
||||||
|
NSString *formattedNumber = [PhoneNumber removeFormattingCharacters:searchTerm];
|
||||||
|
|
||||||
|
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fullName contains[c] %@) OR (ANY parsedPhoneNumbers.toE164 contains[c] %@)", searchTerm, formattedNumber];
|
||||||
|
|
||||||
|
return [self.contacts filteredArrayUsingPredicate:predicate];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
@ -0,0 +1,69 @@
|
|||||||
|
//
|
||||||
|
// OWSContactSearcherTest.m
|
||||||
|
// Signal
|
||||||
|
//
|
||||||
|
// Created by Michael Kirk on 6/27/16.
|
||||||
|
// Copyright © 2016 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import <XCTest/XCTest.h>
|
||||||
|
|
||||||
|
#import "OWSContactsSearcher.h"
|
||||||
|
|
||||||
|
@interface OWSContactsSearcherTest : XCTestCase
|
||||||
|
|
||||||
|
@property Contact *meow;
|
||||||
|
@property Contact *clement;
|
||||||
|
@property OWSContactsSearcher *contactsSearcher;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation OWSContactsSearcherTest
|
||||||
|
|
||||||
|
- (void)setUp {
|
||||||
|
[super setUp];
|
||||||
|
|
||||||
|
self.meow = [[Contact alloc] initWithContactWithFirstName:@"Chairman"
|
||||||
|
andLastName:@"Meow"
|
||||||
|
andUserTextPhoneNumbers:@[ @"1-323-555-1234", @"+86 10 1111 2222" ]
|
||||||
|
andImage:nil
|
||||||
|
andContactID:1];
|
||||||
|
|
||||||
|
self.clement = [[Contact alloc] initWithContactWithFirstName:@"Clément"
|
||||||
|
andLastName:@"Duval"
|
||||||
|
andUserTextPhoneNumbers:@[ @"33 123456789" ]
|
||||||
|
andImage:nil
|
||||||
|
andContactID:2];
|
||||||
|
|
||||||
|
self.contactsSearcher = [[OWSContactsSearcher alloc] initWithContacts:@[self.meow, self.clement]];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testFilterWithStringMatchAllOnEmtpy {
|
||||||
|
XCTAssertEqualObjects((@[self.meow, self.clement]), [self.contactsSearcher filterWithString:@""]);
|
||||||
|
XCTAssertEqualObjects((@[self.meow, self.clement]), [self.contactsSearcher filterWithString:@" "]);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testFilterWithStringMatchByName {
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@"Chairman"]);
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@"Chair"]);
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@"Meow"]);
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@"Chairman Meow"]);
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@" Chairman Meow "]);
|
||||||
|
XCTAssertEqualObjects((@[self.meow, self.clement]), ([self.contactsSearcher filterWithString:@"C"]));
|
||||||
|
XCTAssertEqualObjects(@[], [self.contactsSearcher filterWithString:@"Chairman Meowww"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testFilterWithStringByNumber {
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@"1-323-555-1234"]);
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@"+86 10 1111 2222"]);
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@"323-555-1234"]);
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@"323.555.1234"]);
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@"3235551234"]);
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@"323"]);
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@"323 555 1234"]);
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@"+1 323 555 1234"]);
|
||||||
|
XCTAssertEqualObjects(@[self.meow], [self.contactsSearcher filterWithString:@"+13235551234"]);
|
||||||
|
XCTAssertEqualObjects((@[self.meow, self.clement]), [self.contactsSearcher filterWithString:@"1234"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
Loading…
Reference in New Issue