diff --git a/MatrixKit/Models/Room/MXKRoomBubbleCellData.h b/MatrixKit/Models/Room/MXKRoomBubbleCellData.h index 0fc71f4f7..c703a1561 100644 --- a/MatrixKit/Models/Room/MXKRoomBubbleCellData.h +++ b/MatrixKit/Models/Room/MXKRoomBubbleCellData.h @@ -110,6 +110,8 @@ */ @property (nonatomic, readonly) BOOL containsBubbleComponentWithEncryptionBadge; +@property (nonatomic, weak) MXEvent *event; + /** Check and refresh the position of each component. */ @@ -148,4 +150,6 @@ */ - (MXKRoomBubbleComponent*)getFirstBubbleComponentWithDisplay; +- (BOOL)searchText:(NSString *)text; + @end diff --git a/MatrixKit/Models/Room/MXKRoomBubbleCellData.m b/MatrixKit/Models/Room/MXKRoomBubbleCellData.m index b26c9cc22..2be4769dc 100644 --- a/MatrixKit/Models/Room/MXKRoomBubbleCellData.m +++ b/MatrixKit/Models/Room/MXKRoomBubbleCellData.m @@ -51,7 +51,7 @@ - (instancetype)initWithEvent:(MXEvent *)event andRoomState:(MXRoomState *)roomS { bubbleComponents = [NSMutableArray array]; [bubbleComponents addObject:firstComponent]; - + _event = event; senderId = event.sender; roomId = roomDataSource.roomId; senderDisplayName = [roomDataSource.eventFormatter senderDisplayNameForEvent:event withRoomState:roomState]; @@ -90,6 +90,10 @@ - (instancetype)initWithEvent:(MXEvent *)event andRoomState:(MXRoomState *)roomS return self; } +- (BOOL)searchText:(NSString *)text { + return [self.textMessage rangeOfString:text options:NSCaseInsensitiveSearch].location != NSNotFound; +} + - (void)dealloc { // Reset any observer on publicised groups by user. diff --git a/MatrixKit/Models/Room/MXKRoomDataSource.h b/MatrixKit/Models/Room/MXKRoomDataSource.h index a89ea41d7..97cd9a1c5 100644 --- a/MatrixKit/Models/Room/MXKRoomDataSource.h +++ b/MatrixKit/Models/Room/MXKRoomDataSource.h @@ -708,4 +708,6 @@ extern NSString *const kMXKRoomDataSourceTimelineErrorErrorKey; */ - (NSString*)editableTextMessageForEvent:(MXEvent*)event; +- (MXSearchRoomEventResults *)search:(NSString* )text filter:(MXRoomEventFilter *)filter; + @end diff --git a/MatrixKit/Models/Room/MXKRoomDataSource.m b/MatrixKit/Models/Room/MXKRoomDataSource.m index d70397ddd..6ea12bd7d 100644 --- a/MatrixKit/Models/Room/MXKRoomDataSource.m +++ b/MatrixKit/Models/Room/MXKRoomDataSource.m @@ -3582,4 +3582,28 @@ - (void)replaceTextMessageForEventWithId:(NSString*)eventId } } +#pragma mark - Search from locally in encrypted room. + +- (MXSearchRoomEventResults *)search:(NSString* )text filter:(MXRoomEventFilter *)filter { + NSArray *bubbles = self->bubbles; + NSMutableArray *searchResult = [[NSMutableArray alloc] init]; + + for (MXKRoomBubbleCellData* item in bubbles) { + if([item searchText:text]) { + if ( (filter.containsURL && item.event.isMediaAttachment) || !filter.containsURL) { + MXSearchResult *result = [[MXSearchResult alloc] init]; + result.result = item.event; + [searchResult addObject:result]; + } + } + } + + MXSearchRoomEventResults *result = [[MXSearchRoomEventResults alloc] init]; + result.count = searchResult.count; + result.nextBatch = nil; + result.groups = nil; + result.results = [NSArray arrayWithArray:searchResult]; + + return result; +} @end diff --git a/MatrixKit/Models/Search/MXKSearchDataSource.h b/MatrixKit/Models/Search/MXKSearchDataSource.h index 5a89c09d1..de13ab0cd 100644 --- a/MatrixKit/Models/Search/MXKSearchDataSource.h +++ b/MatrixKit/Models/Search/MXKSearchDataSource.h @@ -16,6 +16,7 @@ #import "MXKDataSource.h" #import "MXKSearchCellDataStoring.h" +#import "MXKRoomDataSource.h" #import "MXKEventFormatter.h" @@ -71,6 +72,8 @@ extern NSString *const kMXKSearchCellDataIdentifier; */ @property (nonatomic) BOOL shouldShowRoomDisplayName; +@property (nonatomic) MXKRoomDataSource *roomDataSource; + /** Launch a message search homeserver side. diff --git a/MatrixKit/Models/Search/MXKSearchDataSource.m b/MatrixKit/Models/Search/MXKSearchDataSource.m index 7a2cffe20..ba7a249e2 100644 --- a/MatrixKit/Models/Search/MXKSearchDataSource.m +++ b/MatrixKit/Models/Search/MXKSearchDataSource.m @@ -169,8 +169,34 @@ - (void)setState:(MXKDataSourceState)newState } } +-(void)doLocalSearch { + MXSearchRoomEventResults *roomEventResults = [_roomDataSource search:_searchText filter:_roomEventFilter]; + // Process HS response to cells data + MXWeakify(self); + [self convertHomeserverResultsIntoCells:roomEventResults onComplete:^{ + MXStrongifyAndReturnIfNil(self); + + self.state = MXKDataSourceStateReady; + + // Provide changes information to the delegate + NSIndexSet *insertedIndexes; + if (roomEventResults.results.count) + { + insertedIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, roomEventResults.results.count)]; + } + + [self.delegate dataSource:self didCellChange:insertedIndexes]; + }]; +} + - (void)doSearch { + + // Handle local search + if (_roomDataSource.roomState.isEncrypted) { + [self doLocalSearch]; + return; + } // Handle one request at a time if (searchRequest) {