From 0c633835b932f9127198db53231ffe48a439f513 Mon Sep 17 00:00:00 2001 From: Ivan Sein Date: Tue, 27 Jun 2023 18:00:56 +0200 Subject: [PATCH 1/5] Allow to filter conversation with unread messages or mentions. Signed-off-by: Ivan Sein --- NextcloudTalk/RoomsTableViewController.m | 89 ++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 4 deletions(-) diff --git a/NextcloudTalk/RoomsTableViewController.m b/NextcloudTalk/RoomsTableViewController.m index 87014e4b4..0277d29e1 100644 --- a/NextcloudTalk/RoomsTableViewController.m +++ b/NextcloudTalk/RoomsTableViewController.m @@ -50,10 +50,17 @@ typedef void (^FetchRoomsCompletionBlock)(BOOL success); +typedef enum RoomsFilter { + kRoomsFilterAll = 0, + kRoomsFilterUnread, + kRoomsFilterMentioned +} RoomsFilter; + @interface RoomsTableViewController () { RLMNotificationToken *_rlmNotificationToken; NSMutableArray *_rooms; + NSMutableArray *_allRooms; UIRefreshControl *_refreshControl; BOOL _allowEmptyGroupRooms; UISearchController *_searchController; @@ -94,6 +101,13 @@ - (void)viewDidLoad _searchController = [[UISearchController alloc] initWithSearchResultsController:_resultTableViewController]; _searchController.searchResultsUpdater = self; [_searchController.searchBar sizeToFit]; + + if (@available(iOS 16.0, *)) { + _searchController.scopeBarActivation = UISearchControllerScopeBarActivationOnSearchActivation; + } else { + _searchController.automaticallyShowsScopeBar = YES; + } + _searchController.searchBar.scopeButtonTitles = [self getFilters]; [self setupNavigationBar]; @@ -497,10 +511,29 @@ - (void)updateSearchResultsForSearchController:(UISearchController *)searchContr } } +- (void)willDismissSearchController:(UISearchController *)searchController +{ + _searchController.searchBar.selectedScopeButtonIndex = kRoomsFilterAll; + [self filterRooms]; +} + +- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope +{ + [self filterRooms]; +} + - (void)filterRooms { + RoomsFilter filter = (RoomsFilter) _searchController.searchBar.selectedScopeButtonIndex; + NSArray *filteredRooms = [self filterRoomsWithFilter:filter]; + NSString *searchString = _searchController.searchBar.text; - _resultTableViewController.rooms = [self filterRoomsWithString:searchString]; + if ([searchString isEqualToString:@""]) { + _rooms = [[NSMutableArray alloc] initWithArray:filteredRooms]; + [self.tableView reloadData]; + } else { + _resultTableViewController.rooms = [self filterRooms:filteredRooms withString:searchString]; + } } - (void)searchListableRoomsAndMessages @@ -535,10 +568,21 @@ - (void)searchForMessagesWithCurrentSearchTerm }]; } -- (NSArray *)filterRoomsWithString:(NSString *)searchString +- (NSArray *)filterRoomsWithFilter:(RoomsFilter)filter +{ + switch (filter) { + case kRoomsFilterUnread: + return [_allRooms filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"unreadMessages > 0"]]; + case kRoomsFilterMentioned: + return [_allRooms filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"hasUnreadMention == YES"]]; + default: + return _allRooms; + } +} + +- (NSArray *)filterRooms:(NSArray *)rooms withString:(NSString *)searchString { - NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"displayName CONTAINS[c] %@", searchString]; - return [_rooms filteredArrayUsingPredicate:sPredicate]; + return [rooms filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"displayName CONTAINS[c] %@", searchString]]; } - (void)setLoadMoreButtonHidden:(BOOL)hidden @@ -563,12 +607,49 @@ - (void)loadMoreMessagesWithCurrentSearchTerm } } +#pragma mark - Rooms filter + +- (NSArray *)availableFilters +{ + NSMutableArray *filters = [[NSMutableArray alloc] init]; + [filters addObject:[NSNumber numberWithInt:kRoomsFilterAll]]; + [filters addObject:[NSNumber numberWithInt:kRoomsFilterUnread]]; + [filters addObject:[NSNumber numberWithInt:kRoomsFilterMentioned]]; + + return [NSArray arrayWithArray:filters]; +} + +- (NSString *)filterName:(RoomsFilter)filter +{ + switch (filter) { + case kRoomsFilterAll: + return NSLocalizedString(@"All", @"'All' meaning 'All conversations'"); + case kRoomsFilterUnread: + return NSLocalizedString(@"Unread", @"'Unread' meaning 'Unread conversations'"); + case kRoomsFilterMentioned: + return NSLocalizedString(@"Mentioned", @"'Mentioned' meaning 'Mentioned conversations'"); + default: + return @""; + } +} + +- (NSArray *)getFilters +{ + NSMutableArray *filters = [[NSMutableArray alloc] init]; + for (NSNumber *filter in [self availableFilters]) { + [filters addObject:[self filterName:filter.intValue]]; + } + + return [NSArray arrayWithArray:filters]; +} + #pragma mark - User Interface - (void)refreshRoomList { TalkAccount *account = [[NCDatabaseManager sharedInstance] activeAccount]; NSArray *accountRooms = [[NCRoomsManager sharedInstance] roomsForAccountId:account.accountId witRealm:nil]; + _allRooms = [[NSMutableArray alloc] initWithArray:accountRooms]; _rooms = [[NSMutableArray alloc] initWithArray:accountRooms]; // Show/Hide placeholder view From fcb3b25bb3d240483537ce92d5120c713f273ac9 Mon Sep 17 00:00:00 2001 From: Ivan Sein Date: Tue, 27 Jun 2023 18:02:03 +0200 Subject: [PATCH 2/5] Update localizable strings file. Signed-off-by: Ivan Sein --- NextcloudTalk/en.lproj/Localizable.strings | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/NextcloudTalk/en.lproj/Localizable.strings b/NextcloudTalk/en.lproj/Localizable.strings index 093fcab19..d4463ac48 100644 --- a/NextcloudTalk/en.lproj/Localizable.strings +++ b/NextcloudTalk/en.lproj/Localizable.strings @@ -103,6 +103,9 @@ /* No comment provided by engineer. */ "AirPlay button" = "AirPlay button"; +/* 'All' meaning 'All conversations' */ +"All" = "All"; + /* No comment provided by engineer. */ "All messages" = "All messages"; @@ -934,6 +937,9 @@ /* No comment provided by engineer. */ "Meeting settings" = "Meeting settings"; +/* 'Mentioned' meaning 'Mentioned conversations' */ +"Mentioned" = "Mentioned"; + /* No comment provided by engineer. */ "Message copied" = "Message copied"; @@ -1558,6 +1564,9 @@ /* No comment provided by engineer. */ "Unknown error occurred" = "Unknown error occurred"; +/* 'Unread' meaning 'Unread conversations' */ +"Unread" = "Unread"; + /* No comment provided by engineer. */ "Unread mentions" = "Unread mentions"; From 21d9ba95ebd88b4ad96d4e8f49bb06adb4f51236 Mon Sep 17 00:00:00 2001 From: Ivan Sein Date: Wed, 28 Jun 2023 17:31:26 +0200 Subject: [PATCH 3/5] Fix calculateLastRoomWithMention. Signed-off-by: Ivan Sein --- NextcloudTalk/RoomsTableViewController.m | 33 ++++++++++++------------ 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/NextcloudTalk/RoomsTableViewController.m b/NextcloudTalk/RoomsTableViewController.m index 0277d29e1..438662de1 100644 --- a/NextcloudTalk/RoomsTableViewController.m +++ b/NextcloudTalk/RoomsTableViewController.m @@ -528,8 +528,9 @@ - (void)filterRooms NSArray *filteredRooms = [self filterRoomsWithFilter:filter]; NSString *searchString = _searchController.searchBar.text; - if ([searchString isEqualToString:@""]) { + if (searchString.length == 0) { _rooms = [[NSMutableArray alloc] initWithArray:filteredRooms]; + [self calculateLastRoomWithMention]; [self.tableView reloadData]; } else { _resultTableViewController.rooms = [self filterRooms:filteredRooms withString:searchString]; @@ -656,22 +657,9 @@ - (void)refreshRoomList [_roomsBackgroundView.loadingView stopAnimating]; [_roomsBackgroundView.loadingView setHidden:YES]; [_roomsBackgroundView.placeholderView setHidden:(_rooms.count > 0)]; - - // Calculate index of last room with a mention - _lastRoomWithMentionIndexPath = nil; - for (int i = 0; i < _rooms.count; i++) { - NCRoom *room = [_rooms objectAtIndex:i]; - if (room.hasUnreadMention) { - _lastRoomWithMentionIndexPath = [NSIndexPath indexPathForRow:i inSection:0]; - } - } - - // Reload search controller if active - NSString *searchString = _searchController.searchBar.text; - if (_searchController.isActive && searchString.length > 0) { - // Filter rooms to show updated rooms - [self filterRooms]; - } + + // Filter rooms + [self filterRooms]; // Reload room list [self.tableView reloadData]; @@ -809,6 +797,17 @@ - (void)unreadMentionsBottomButtonPressed:(id)sender } } +- (void)calculateLastRoomWithMention +{ + _lastRoomWithMentionIndexPath = nil; + for (int i = 0; i < _rooms.count; i++) { + NCRoom *room = [_rooms objectAtIndex:i]; + if (room.hasUnreadMention) { + _lastRoomWithMentionIndexPath = [NSIndexPath indexPathForRow:i inSection:0]; + } + } +} + #pragma mark - User profile - (void)setProfileButton From 0b45173ee64b17ad306b21f998e4aa99194821be Mon Sep 17 00:00:00 2001 From: Ivan Sein Date: Tue, 4 Jul 2023 18:36:05 +0200 Subject: [PATCH 4/5] Reset search bar text when search controller will be dismissed to correctly filter the rooms. Signed-off-by: Ivan Sein --- NextcloudTalk/RoomsTableViewController.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NextcloudTalk/RoomsTableViewController.m b/NextcloudTalk/RoomsTableViewController.m index 438662de1..a5fb3f222 100644 --- a/NextcloudTalk/RoomsTableViewController.m +++ b/NextcloudTalk/RoomsTableViewController.m @@ -513,7 +513,9 @@ - (void)updateSearchResultsForSearchController:(UISearchController *)searchContr - (void)willDismissSearchController:(UISearchController *)searchController { + _searchController.searchBar.text = @""; _searchController.searchBar.selectedScopeButtonIndex = kRoomsFilterAll; + [self filterRooms]; } From 96befff72bf3cfa266760c4321539b15d914ae49 Mon Sep 17 00:00:00 2001 From: Ivan Sein Date: Tue, 4 Jul 2023 18:37:03 +0200 Subject: [PATCH 5/5] Always filter the rooms (even when searchString is empty) in updateSearchResultsForSearchController. Signed-off-by: Ivan Sein --- NextcloudTalk/RoomsTableViewController.m | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/NextcloudTalk/RoomsTableViewController.m b/NextcloudTalk/RoomsTableViewController.m index a5fb3f222..cbab9b657 100644 --- a/NextcloudTalk/RoomsTableViewController.m +++ b/NextcloudTalk/RoomsTableViewController.m @@ -492,15 +492,13 @@ - (void)updateSearchResultsForSearchController:(UISearchController *)searchContr // Cancel previous call to search listable rooms and messages [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchListableRoomsAndMessages) object:nil]; - // Filer rooms and search for listable rooms and messages + // Search for listable rooms and messages if (searchString.length > 0) { // Set searchingMessages flag if we are going to search for messages if ([[NCDatabaseManager sharedInstance] serverHasTalkCapability:kCapabilityUnifiedSearch]) { [self setLoadMoreButtonHidden:YES]; _resultTableViewController.searchingMessages = YES; } - // Filter rooms - [self filterRooms]; // Throttle listable rooms and messages search [self performSelector:@selector(searchListableRoomsAndMessages) withObject:nil afterDelay:1]; } else { @@ -509,6 +507,9 @@ - (void)updateSearchResultsForSearchController:(UISearchController *)searchContr _resultTableViewController.searchingMessages = NO; [_resultTableViewController clearSearchedResults]; } + + // Filter rooms + [self filterRooms]; } - (void)willDismissSearchController:(UISearchController *)searchController