diff --git a/Riot/Views/RoomList/RoomTableViewCell.h b/Riot/Views/RoomList/RoomTableViewCell.h index ac262029d..92d5cf2cb 100644 --- a/Riot/Views/RoomList/RoomTableViewCell.h +++ b/Riot/Views/RoomList/RoomTableViewCell.h @@ -33,8 +33,6 @@ */ - (void)render:(MXRoom *)room; -- (void)renderWithSummary:(MXRoomSummary *)roomSummary restClient:(MXRestClient *)restClient; - /** Get the cell height. diff --git a/Riot/Views/RoomList/RoomTableViewCell.m b/Riot/Views/RoomList/RoomTableViewCell.m index d53d5a1a8..938038c77 100644 --- a/Riot/Views/RoomList/RoomTableViewCell.m +++ b/Riot/Views/RoomList/RoomTableViewCell.m @@ -59,17 +59,6 @@ self.encryptedRoomIcon.hidden = !room.state.isEncrypted; } -- (void)renderWithSummary:(MXRoomSummary *)roomSummary restClient:(MXRestClient *)restClient -{ - [self.avatarImageView setImageURL:[restClient urlOfContentThumbnail:roomSummary.avatar toFitViewSize:self.avatarImageView.frame.size withMethod:MXThumbnailingMethodCrop] withType:nil andImageOrientation:UIImageOrientationUp previewImage:nil]; - - self.titleLabel.text = roomSummary.displayname; - - self.directRoomBorderView.hidden = !roomSummary.isDirect; - - self.encryptedRoomIcon.hidden = !roomSummary.isEncrypted; -} - - (void)prepareForReuse { [super prepareForReuse]; diff --git a/RiotShareExtension/Model/ShareDataSource.h b/RiotShareExtension/Model/ShareDataSource.h index f7a574ca9..d9b675466 100644 --- a/RiotShareExtension/Model/ShareDataSource.h +++ b/RiotShareExtension/Model/ShareDataSource.h @@ -27,6 +27,4 @@ typedef NS_ENUM(NSInteger, ShareDataSourceMode) - (instancetype)initWithMode:(ShareDataSourceMode)dataSourceMode; -- (MXRoomSummary *)getRoomSummaryAtIndexPath:(NSIndexPath *)indexPath; - @end diff --git a/RiotShareExtension/Model/ShareDataSource.m b/RiotShareExtension/Model/ShareDataSource.m index d10bebc1d..6e51321ad 100644 --- a/RiotShareExtension/Model/ShareDataSource.m +++ b/RiotShareExtension/Model/ShareDataSource.m @@ -17,13 +17,14 @@ #import "ShareDataSource.h" #import "ShareExtensionManager.h" #import "RoomTableViewCell.h" +#import "MXRoom+Riot.h" @interface ShareDataSource () @property (nonatomic, readwrite) ShareDataSourceMode dataSourceMode; -@property NSMutableArray *recentRooms; -@property NSMutableArray *recentPeople; +@property NSArray *rooms; +@property NSMutableArray *visibleRooms; @end @@ -31,11 +32,11 @@ - (instancetype)initWithMode:(ShareDataSourceMode)dataSourceMode { + self = [super init]; if (self) { self.dataSourceMode = dataSourceMode; - _recentRooms = [NSMutableArray array]; - _recentPeople = [NSMutableArray array]; + _rooms = [NSMutableArray array]; [self updateRooms]; } return self; @@ -46,50 +47,105 @@ - (void)updateRooms { - [self.recentRooms removeAllObjects]; - [self.recentPeople removeAllObjects]; - MXFileStore *fileStore = [[MXFileStore alloc] initWithCredentials:[ShareExtensionManager sharedManager].account.mxCredentials]; + MXSession *session = [[MXSession alloc] initWithMatrixRestClient:[[MXRestClient alloc] initWithCredentials:[ShareExtensionManager sharedManager].account.mxCredentials andOnUnrecognizedCertificateBlock:nil]]; + __weak MXSession *weakSession = session; + [session setStore:fileStore success:^{ + if (weakSession) + { + __strong MXSession *session = weakSession; + [self getRoomsFromStore:fileStore session:session]; + } + } failure:^(NSError *error) { + NSLog(@"[ShareExtensionManager failed to set store]"); + }]; +} + +- (void)getRoomsFromStore:(MXFileStore *)fileStore session:(MXSession *)session +{ + NSMutableArray *rooms = [NSMutableArray array]; [fileStore asyncRoomsSummaries:^(NSArray * _Nonnull roomsSummaries) { for (MXRoomSummary *roomSummary in roomsSummaries) { - if (self.dataSourceMode == DataSourceModePeople) - { - if (roomSummary.isDirect) - { - [self.recentPeople addObject:roomSummary]; - } - } - else - { - if (!roomSummary.isDirect) - { - [self.recentRooms addObject:roomSummary]; - } - } - [self.delegate dataSource:self didCellChange:nil]; + [fileStore asyncAccountDataOfRoom:roomSummary.roomId success:^(MXRoomAccountData * _Nonnull accountData) { + [fileStore asyncStateEventsOfRoom:roomSummary.roomId success:^(NSArray * _Nonnull roomStateEvents) { + + MXRoom *room = [[MXRoom alloc] initWithRoomId:roomSummary.roomId andMatrixSession:session andStateEvents:roomStateEvents andAccountData:accountData]; + + if ((self.dataSourceMode == DataSourceModeRooms) ^ roomSummary.isDirect) + { + [rooms addObject:room]; + } + + if ([roomsSummaries indexOfObject:roomSummary] == roomsSummaries.count - 1) + { + self.rooms = rooms; + dispatch_async(dispatch_get_main_queue(), ^{ + [self.delegate dataSource:self didCellChange:nil]; + }); + } + + } failure:^(NSError * _Nonnull error) { + NSLog(@"[ShareExtensionManager failed to get state events]"); + }]; + } failure:^(NSError * _Nonnull error) { + NSLog(@"[ShareExtensionManager failed to get account data]"); + }]; + } } failure:^(NSError * _Nonnull error) { NSLog(@"[ShareExtensionManager failed to get room summaries]"); }]; } +#pragma mark - MXKRecentsDataSource -- (MXRoomSummary *)getRoomSummaryAtIndexPath:(NSIndexPath *)indexPath +- (void)dataSource:(MXKDataSource *)dataSource didCellChange:(id)changes { - MXRoomSummary *room = nil; - - if (self.dataSourceMode == DataSourceModePeople) + dispatch_async(dispatch_get_main_queue(), ^{ + [self.delegate dataSource:dataSource didCellChange:nil]; + }); +} + +- (MXRoom *)getRoomAtIndexPath:(NSIndexPath *)indexPath +{ + if (self.visibleRooms) { - room = self.recentPeople[indexPath.row]; + return self.visibleRooms[indexPath.row]; + } + return self.rooms[indexPath.row]; +} + +- (void)searchWithPatterns:(NSArray *)patternsList +{ + if (self.visibleRooms) + { + [self.visibleRooms removeAllObjects]; } else { - room = self.recentRooms[indexPath.row]; + self.visibleRooms = [NSMutableArray arrayWithCapacity:self.rooms.count]; } - - return room; + if (patternsList.count) + { + for (MXRoom *room in self.rooms) + { + for (NSString* pattern in patternsList) + { + if (room.riotDisplayname && [room.riotDisplayname rangeOfString:pattern options:NSCaseInsensitiveSearch].location != NSNotFound) + { + [self.visibleRooms addObject:room]; + break; + } + } + } + } + else + { + self.visibleRooms = nil; + } + [self.delegate dataSource:self didCellChange:nil]; } #pragma mark - UITableViewDataSource @@ -101,28 +157,20 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - if (self.dataSourceMode == DataSourceModePeople) + if (self.visibleRooms) { - return self.recentPeople.count; - } - else - { - return self.recentRooms.count; + return self.visibleRooms.count; } + return self.rooms.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { RoomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[RoomTableViewCell defaultReuseIdentifier]]; - MXRoomSummary *roomSummary = [self getRoomSummaryAtIndexPath:indexPath]; + MXRoom *room = [self getRoomAtIndexPath:indexPath]; - [cell renderWithSummary:roomSummary restClient:[ShareExtensionManager sharedManager].mxRestClient]; - - if (!roomSummary.displayname.length && !cell.titleLabel.text.length) - { - cell.titleLabel.text = NSLocalizedStringFromTable(@"room_displayname_no_title", @"Vector", nil); - } + [cell render:room]; return cell; } diff --git a/RiotShareExtension/Model/ShareExtensionManager.h b/RiotShareExtension/Model/ShareExtensionManager.h index bb187fe2e..25137a5fe 100644 --- a/RiotShareExtension/Model/ShareExtensionManager.h +++ b/RiotShareExtension/Model/ShareExtensionManager.h @@ -45,9 +45,9 @@ extern NSString *const kShareExtensionManagerDidChangeMXSessionNotification; /** Called when the manager starts sending the content to a room @param extensionManager the ShareExtensionManager object that called the method - @param roomID the ID of the room + @param room the room where content will be sent */ -- (void)shareExtensionManager:(ShareExtensionManager *)extensionManager didStartSendingContentToRoom:(NSString *)roomID; +- (void)shareExtensionManager:(ShareExtensionManager *)extensionManager didStartSendingContentToRoom:(MXRoom *)room; /** Called when the progress of the uploading media changes @@ -90,11 +90,11 @@ extern NSString *const kShareExtensionManagerDidChangeMXSessionNotification; /** Send the content that the user has chosen to a room - @param roomID the ID of the room to send the content to + @param room the room to send the content to @param failureBlock the code to be executed when sharing has failed for whatever reason note: there is no "successBlock" parameter because when the sharing succeds, the extension needs to close itself */ -- (void)sendContentToRoom:(NSString *)roomID failureBlock:(void(^)())failureBlock; +- (void)sendContentToRoom:(MXRoom *)room failureBlock:(void(^)())failureBlock; /** Checks if there is an image in the user chosen content diff --git a/RiotShareExtension/Model/ShareExtensionManager.m b/RiotShareExtension/Model/ShareExtensionManager.m index 196c2e7ba..dc2561475 100644 --- a/RiotShareExtension/Model/ShareExtensionManager.m +++ b/RiotShareExtension/Model/ShareExtensionManager.m @@ -8,7 +8,8 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, + distributed under the License is distributed on + an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. @@ -33,10 +34,6 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) @interface ShareExtensionManager () @property (nonatomic, readwrite) MXKAccount *account; -@property (nonatomic, readwrite) MXRestClient *mxRestClient; - -@property (nonatomic) NSArray *rooms; -@property (nonatomic) NSArray *people; @property (nonatomic) NSMutableArray *pendingImages; @property (nonatomic) NSMutableDictionary *imageUploadProgresses; @@ -74,7 +71,6 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) // Save the first active account sharedInstance.account = [MXKAccountManager sharedManager].activeAccounts.firstObject; - sharedInstance.mxRestClient = [[MXRestClient alloc] initWithCredentials:sharedInstance.account.mxCredentials andOnUnrecognizedCertificateBlock:nil]; }); return sharedInstance; } @@ -87,7 +83,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) _shareExtensionContext = shareExtensionContext; } -- (void)sendContentToRoom:(NSString *)roomID failureBlock:(void(^)())failureBlock +- (void)sendContentToRoom:(MXRoom *)room failureBlock:(void(^)())failureBlock { NSString *UTTypeText = (__bridge NSString *)kUTTypeText; NSString *UTTypeURL = (__bridge NSString *)kUTTypeURL; @@ -114,7 +110,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) if (weakSelf) { typeof(self) self = weakSelf; - //[self sendFileWithUrl:fileUrl toRoom:room extensionItem:item failureBlock:failureBlock]; + [self sendFileWithUrl:fileUrl toRoom:room extensionItem:item failureBlock:failureBlock]; } }); @@ -131,7 +127,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) if (weakSelf) { typeof(self) self = weakSelf; - //[self sendText:text toRoom:room extensionItem:item failureBlock:failureBlock]; + [self sendText:text toRoom:room extensionItem:item failureBlock:failureBlock]; } }); @@ -148,7 +144,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) if (weakSelf) { typeof(self) self = weakSelf; - //[self sendText:url.absoluteString toRoom:room extensionItem:item failureBlock:failureBlock]; + [self sendText:url.absoluteString toRoom:room extensionItem:item failureBlock:failureBlock]; } }); @@ -169,7 +165,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) { UIImage *firstImage = [UIImage imageWithData:self.pendingImages.firstObject]; UIAlertController *compressionPrompt = [self compressionPromptForImage:firstImage shareBlock:^{ - //[self sendImages:self.pendingImages withProviders:item.attachments toRoom:room extensionItem:item failureBlock:failureBlock]; + [self sendImages:self.pendingImages withProviders:item.attachments toRoom:room extensionItem:item failureBlock:failureBlock]; }]; [self.delegate shareExtensionManager:self showImageCompressionPrompt:compressionPrompt]; @@ -187,7 +183,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) if (weakSelf) { typeof(self) self = weakSelf; - //[self sendVideo:videoLocalUrl toRoom:room extensionItem:item failureBlock:failureBlock]; + [self sendVideo:videoLocalUrl toRoom:room extensionItem:item failureBlock:failureBlock]; } }); @@ -204,7 +200,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) if (weakSelf) { typeof(self) self = weakSelf; - //[self sendVideo:videoLocalUrl toRoom:room extensionItem:item failureBlock:failureBlock]; + [self sendVideo:videoLocalUrl toRoom:room extensionItem:item failureBlock:failureBlock]; } }); @@ -232,8 +228,6 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) - (void)terminateExtensionCanceled:(BOOL)canceled { - //[self suspendSession]; - if (canceled) { [self.shareExtensionContext cancelRequestWithError:[NSError errorWithDomain:@"MXUserCancelErrorDomain" code:4201 userInfo:nil]]; @@ -407,11 +401,11 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) return compressionPrompt; } -- (void)didStartSendingToRoom:(NSString *)roomID +- (void)didStartSendingToRoom:(MXRoom *)room { if ([self.delegate respondsToSelector:@selector(shareExtensionManager:didStartSendingContentToRoom:)]) { - [self.delegate shareExtensionManager:self didStartSendingContentToRoom:roomID]; + [self.delegate shareExtensionManager:self didStartSendingContentToRoom:room]; } } @@ -452,9 +446,9 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) #pragma mark - Sharing -- (void)sendText:(NSString *)text toRoom:(NSString *)roomID extensionItem:(NSExtensionItem *)extensionItem failureBlock:(void(^)())failureBlock +- (void)sendText:(NSString *)text toRoom:(MXRoom *)room extensionItem:(NSExtensionItem *)extensionItem failureBlock:(void(^)())failureBlock { - [self didStartSendingToRoom:roomID]; + [self didStartSendingToRoom:room]; if (!text) { NSLog(@"[ShareExtensionManager] loadItemForTypeIdentifier: failed."); @@ -466,8 +460,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) } __weak typeof(self) weakSelf = self; - - [self.mxRestClient sendTextMessageToRoom:roomID text:text success:^(NSString *eventId) { + [room sendTextMessage:text success:^(NSString *eventId) { if (weakSelf) { typeof(self) self = weakSelf; @@ -482,9 +475,9 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) }]; } -- (void)sendFileWithUrl:(NSURL *)fileUrl toRoom:(NSString *)roomID extensionItem:(NSExtensionItem *)extensionItem failureBlock:(void(^)())failureBlock +- (void)sendFileWithUrl:(NSURL *)fileUrl toRoom:(MXRoom *)room extensionItem:(NSExtensionItem *)extensionItem failureBlock:(void(^)())failureBlock { - [self didStartSendingToRoom:roomID]; + [self didStartSendingToRoom:room]; if (!fileUrl) { NSLog(@"[ShareExtensionManager] loadItemForTypeIdentifier: failed."); @@ -502,11 +495,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) __weak typeof(self) weakSelf = self; - //self.mxRestClient send - - - - /*[room sendFile:fileUrl mimeType:mimeType localEcho:nil success:^(NSString *eventId) { + [room sendFile:fileUrl mimeType:mimeType localEcho:nil success:^(NSString *eventId) { if (weakSelf) { typeof(self) self = weakSelf; @@ -518,7 +507,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) { failureBlock(); } - } keepActualFilename:YES];*/ + } keepActualFilename:YES]; } @@ -596,7 +585,6 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) if (!imageDatas.count) { - //[self suspendSession]; [self.shareExtensionContext completeRequestReturningItems:@[extensionItem] completionHandler:nil]; } diff --git a/RiotShareExtension/ViewController/RoomsListViewController.m b/RiotShareExtension/ViewController/RoomsListViewController.m index 3cf2c3033..3e2eb7101 100644 --- a/RiotShareExtension/ViewController/RoomsListViewController.m +++ b/RiotShareExtension/ViewController/RoomsListViewController.m @@ -207,6 +207,16 @@ #pragma mark - UISearchBarDelegate +- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText +{ + NSArray *patterns = nil; + if (searchText.length) + { + patterns = @[searchText]; + } + [self.dataSource searchWithPatterns:patterns]; +} + - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { if (searchBar == _tableSearchBar) @@ -231,6 +241,7 @@ - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { [self.recentsSearchBar setShowsCancelButton:NO animated:NO]; + [self.dataSource searchWithPatterns:nil]; } #pragma mark - UIScrollViewDelegate