Skip to content

Commit

Permalink
Merge pull request #1289 from nextcloud/filter-conversation-list
Browse files Browse the repository at this point in the history
Filter conversation list for unread / mentioned conversations
  • Loading branch information
SystemKeeper authored Jul 4, 2023
2 parents 8ede92d + 96befff commit fd52768
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 23 deletions.
129 changes: 106 additions & 23 deletions NextcloudTalk/RoomsTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@

typedef void (^FetchRoomsCompletionBlock)(BOOL success);

typedef enum RoomsFilter {
kRoomsFilterAll = 0,
kRoomsFilterUnread,
kRoomsFilterMentioned
} RoomsFilter;

@interface RoomsTableViewController () <CCCertificateDelegate, UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>
{
RLMNotificationToken *_rlmNotificationToken;
NSMutableArray *_rooms;
NSMutableArray *_allRooms;
UIRefreshControl *_refreshControl;
BOOL _allowEmptyGroupRooms;
UISearchController *_searchController;
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -478,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 {
Expand All @@ -495,12 +507,37 @@ - (void)updateSearchResultsForSearchController:(UISearchController *)searchContr
_resultTableViewController.searchingMessages = NO;
[_resultTableViewController clearSearchedResults];
}

// Filter rooms
[self filterRooms];
}

- (void)willDismissSearchController:(UISearchController *)searchController
{
_searchController.searchBar.text = @"";
_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.length == 0) {
_rooms = [[NSMutableArray alloc] initWithArray:filteredRooms];
[self calculateLastRoomWithMention];
[self.tableView reloadData];
} else {
_resultTableViewController.rooms = [self filterRooms:filteredRooms withString:searchString];
}
}

- (void)searchListableRoomsAndMessages
Expand Down Expand Up @@ -535,10 +572,21 @@ - (void)searchForMessagesWithCurrentSearchTerm
}];
}

- (NSArray *)filterRoomsWithString:(NSString *)searchString
- (NSArray *)filterRoomsWithFilter:(RoomsFilter)filter
{
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"displayName CONTAINS[c] %@", searchString];
return [_rooms filteredArrayUsingPredicate:sPredicate];
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
{
return [rooms filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"displayName CONTAINS[c] %@", searchString]];
}

- (void)setLoadMoreButtonHidden:(BOOL)hidden
Expand All @@ -563,34 +611,58 @@ - (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
[_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];
Expand Down Expand Up @@ -728,6 +800,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
Expand Down
9 changes: 9 additions & 0 deletions NextcloudTalk/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -940,6 +943,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";

Expand Down Expand Up @@ -1567,6 +1573,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";

Expand Down

0 comments on commit fd52768

Please sign in to comment.