Home: Add search option in public rooms

This commit is contained in:
giomfo
2014-12-08 09:32:21 +01:00
parent 6bed500694
commit 60dbff4097
6 changed files with 156 additions and 14 deletions

View File

@@ -41,6 +41,8 @@
F0D3C30F1A01330F0000D49E /* SettingsTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D3C30E1A01330F0000D49E /* SettingsTableViewCell.m */; };
F0D942F61A31F3A300826CC1 /* RecentRoom.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D942F51A31F3A300826CC1 /* RecentRoom.m */; };
F0E84D401A1F9AEC005F2E42 /* RecentsTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = F0E84D3F1A1F9AEC005F2E42 /* RecentsTableViewCell.m */; };
F0F90C691A32596700455977 /* icon_search.png in Resources */ = {isa = PBXBuildFile; fileRef = F0F90C681A32596700455977 /* icon_search.png */; };
F0F90C6B1A325ABF00455977 /* icon_search@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = F0F90C6A1A325ABF00455977 /* icon_search@2x.png */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -113,6 +115,8 @@
F0D942F51A31F3A300826CC1 /* RecentRoom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RecentRoom.m; sourceTree = "<group>"; };
F0E84D3E1A1F9AEC005F2E42 /* RecentsTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RecentsTableViewCell.h; sourceTree = "<group>"; };
F0E84D3F1A1F9AEC005F2E42 /* RecentsTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RecentsTableViewCell.m; sourceTree = "<group>"; };
F0F90C681A32596700455977 /* icon_search.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = icon_search.png; sourceTree = "<group>"; };
F0F90C6A1A325ABF00455977 /* icon_search@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "icon_search@2x.png"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -156,6 +160,8 @@
F01628B519E298710071C473 /* Assets */ = {
isa = PBXGroup;
children = (
F0F90C6A1A325ABF00455977 /* icon_search@2x.png */,
F0F90C681A32596700455977 /* icon_search.png */,
F08B6FCB1A1DE7F80094A35B /* matrixConsole.jpg */,
F02BCE221A1A5A2B00543B47 /* play.png */,
F024098119E7D177006E741B /* tab_recents@2x.png */,
@@ -377,7 +383,9 @@
F0CEA5AF19E6895E00E47915 /* tab_recents.png in Resources */,
F01628C319E29C660071C473 /* logo.png in Resources */,
F01628C119E29C660071C473 /* default-profile.png in Resources */,
F0F90C6B1A325ABF00455977 /* icon_search@2x.png in Resources */,
F0CEA5AE19E6895E00E47915 /* logoHighRes.png in Resources */,
F0F90C691A32596700455977 /* icon_search.png in Resources */,
F0CEA5B119E6898800E47915 /* tab_home.ico in Resources */,
F07A80E619DD9DE700B621A1 /* Images.xcassets in Resources */,
F08B6FCC1A1DE7F80094A35B /* matrixConsole.jpg in Resources */,

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 823 B

View File

@@ -16,7 +16,7 @@
#import <UIKit/UIKit.h>
@interface HomeViewController : UITableViewController <UITextFieldDelegate>
@interface HomeViewController : UITableViewController <UITextFieldDelegate, UISearchBarDelegate>
@end

View File

@@ -19,12 +19,17 @@
#import "MatrixHandler.h"
#import "AppDelegate.h"
@interface HomeViewController () {
NSArray *publicRooms;
// List of public room names to highlight in displayed list
NSArray* highlightedPublicRooms;
// Search in public room
UISearchBar *recentsSearchBar;
NSMutableArray *filteredPublicRooms;
BOOL searchBarShouldEndEditing;
UIView *savedTableHeaderView;
}
@property (weak, nonatomic) IBOutlet UITableView *publicRoomsTable;
@@ -64,6 +69,10 @@
- (void)dealloc{
publicRooms = nil;
highlightedPublicRooms = nil;
recentsSearchBar = nil;
filteredPublicRooms = nil;
savedTableHeaderView = nil;
}
- (void)viewWillAppear:(BOOL)animated {
@@ -84,6 +93,10 @@
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Leave potential search session
if (recentsSearchBar) {
[self searchBarCancelButtonClicked:recentsSearchBar];
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}
@@ -110,6 +123,29 @@
}
- (void)search:(id)sender {
if (!recentsSearchBar) {
// Check whether there are data in which search
if (publicRooms.count) {
// Create search bar
recentsSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
recentsSearchBar.showsCancelButton = YES;
recentsSearchBar.returnKeyType = UIReturnKeyDone;
recentsSearchBar.delegate = self;
[recentsSearchBar becomeFirstResponder];
// Hide table header during search session
savedTableHeaderView = self.tableView.tableHeaderView;
self.tableView.tableHeaderView = nil;
// Reload table in order to display search bar as section header
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
[self.tableView reloadData];
}
} else {
[self searchBarCancelButtonClicked: recentsSearchBar];
}
}
- (void)dismissKeyboard {
// Hide the keyboard
[_roomNameTextField resignFirstResponder];
@@ -289,32 +325,64 @@
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (filteredPublicRooms) {
return filteredPublicRooms.count;
}
return publicRooms.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (recentsSearchBar) {
return (recentsSearchBar.frame.size.height + 40);
}
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *sectionHeader = [[UILabel alloc] initWithFrame:[tableView rectForHeaderInSection:section]];
sectionHeader.font = [UIFont boldSystemFontOfSize:16];
UIView *sectionHeader = [[UIView alloc] initWithFrame:[tableView rectForHeaderInSection:section]];
sectionHeader.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, sectionHeader.frame.size.width, 40)];
sectionLabel.font = [UIFont boldSystemFontOfSize:16];
sectionLabel.backgroundColor = [UIColor clearColor];
[sectionHeader addSubview:sectionLabel];
if (publicRooms) {
NSString *homeserver = [MatrixHandler sharedHandler].homeServerURL;
if (homeserver.length) {
sectionHeader.text = [NSString stringWithFormat:@" Public Rooms (at %@):", homeserver];
sectionLabel.text = [NSString stringWithFormat:@" Public Rooms (at %@):", homeserver];
} else {
sectionHeader.text = @" Public Rooms:";
sectionLabel.text = @" Public Rooms:";
}
UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[searchButton setImage:[UIImage imageNamed:@"icon_search"] forState:UIControlStateNormal];
[searchButton setImage:[UIImage imageNamed:@"icon_search"] forState:UIControlStateHighlighted];
[searchButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
searchButton.frame = CGRectMake(sectionLabel.frame.size.width - 45, 0, 40, 40);
[sectionHeader addSubview:searchButton];
sectionHeader.userInteractionEnabled = YES;
if (recentsSearchBar) {
CGRect frame = recentsSearchBar.frame;
frame.origin.y = 40;
recentsSearchBar.frame = frame;
[sectionHeader addSubview:recentsSearchBar];
}
} else {
sectionHeader.text = @" No Public Rooms";
sectionLabel.text = @" No Public Rooms";
}
return sectionHeader;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Cell is larger for public room with topic
MXPublicRoom *publicRoom = [publicRooms objectAtIndex:indexPath.row];
MXPublicRoom *publicRoom;
if (filteredPublicRooms) {
publicRoom = [filteredPublicRooms objectAtIndex:indexPath.row];
} else {
publicRoom = [publicRooms objectAtIndex:indexPath.row];
}
if (publicRoom.topic) {
return 60;
}
@@ -322,8 +390,13 @@
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MXPublicRoom *publicRoom = [publicRooms objectAtIndex:indexPath.row];
UITableViewCell *cell;
MXPublicRoom *publicRoom;
if (filteredPublicRooms) {
publicRoom = [filteredPublicRooms objectAtIndex:indexPath.row];
} else {
publicRoom = [publicRooms objectAtIndex:indexPath.row];
}
// Check whether this public room has topic
if (publicRoom.topic) {
@@ -351,10 +424,15 @@
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MatrixHandler *mxHandler = [MatrixHandler sharedHandler];
MXPublicRoom *publicRoom;
if (filteredPublicRooms) {
publicRoom = [filteredPublicRooms objectAtIndex:indexPath.row];
} else {
publicRoom = [publicRooms objectAtIndex:indexPath.row];
}
// Check whether the user has already joined the selected public room
MXPublicRoom *publicRoom = [publicRooms objectAtIndex:indexPath.row];
MatrixHandler *mxHandler = [MatrixHandler sharedHandler];
if ([mxHandler.mxSession roomWithRoomId:publicRoom.roomId]) {
// Open selected room
[[AppDelegate theDelegate].masterTabBarController showRoom:publicRoom.roomId];
@@ -385,4 +463,55 @@
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
searchBarShouldEndEditing = NO;
return YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
return searchBarShouldEndEditing;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
// Update filtered list
if (searchText.length) {
if (filteredPublicRooms) {
[filteredPublicRooms removeAllObjects];
} else {
filteredPublicRooms = [NSMutableArray arrayWithCapacity:publicRooms.count];
}
for (MXPublicRoom *publicRoom in publicRooms) {
if ([[publicRoom displayname] rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound) {
[filteredPublicRooms addObject:publicRoom];
}
}
} else {
filteredPublicRooms = nil;
}
// Refresh display
[self.tableView reloadData];
if (filteredPublicRooms.count) {
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// "Done" key has been pressed
searchBarShouldEndEditing = YES;
[searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
// Leave search
searchBarShouldEndEditing = YES;
[searchBar resignFirstResponder];
recentsSearchBar = nil;
filteredPublicRooms = nil;
// Restore table header and refresh table display
self.tableView.tableHeaderView = savedTableHeaderView;
[self.tableView reloadData];
}
@end

View File

@@ -444,6 +444,11 @@
#pragma mark - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
searchBarShouldEndEditing = NO;
return YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
return searchBarShouldEndEditing;
}