element-ios/matrixConsole/ViewController/RecentsViewController.m

753 lines
31 KiB
Mathematica
Raw Normal View History

/*
Copyright 2014 OpenMarket Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
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,
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.
*/
#import "RecentsViewController.h"
#import "RoomViewController.h"
2014-12-05 16:48:06 +00:00
#import "RecentRoom.h"
#import "RecentsTableViewCell.h"
#import "AppDelegate.h"
#import "MatrixSDKHandler.h"
#import "MediaManager.h"
@interface RecentsViewController () {
2014-12-05 16:48:06 +00:00
// Array of RecentRooms
NSMutableArray *recents;
id recentsListener;
NSUInteger unreadCount;
2014-12-02 14:25:00 +00:00
// Search
UISearchBar *recentsSearchBar;
NSMutableArray *filteredRecents;
BOOL searchBarShouldEndEditing;
// Date formatter
NSDateFormatter *dateFormatter;
// Keep reference on the current room view controller to release it correctly
RoomViewController *currentRoomViewController;
// Keep the selected cell index to handle correctly split view controller display in landscape mode
NSInteger currentSelectedCellIndexPathRow;
// The activity indicator is displayed on main screen in order to ignore potential table scrolling
// In some case this activity indicator shoud be hidden (For example when the recents view controller is not visible).
BOOL shouldHideActivityIndicator;
}
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@end
@implementation RecentsViewController
- (void)awakeFromNib {
[super awakeFromNib];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.preferredContentSize = CGSizeMake(320.0, 600.0);
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
2014-12-02 14:25:00 +00:00
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search:)];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(createNewRoom:)];
2014-12-02 14:25:00 +00:00
self.navigationItem.rightBarButtonItems = @[searchButton, addButton];
// Add background to activity indicator
CGRect frame = _activityIndicator.frame;
frame.size.width += 30;
frame.size.height += 30;
_activityIndicator.bounds = frame;
_activityIndicator.backgroundColor = [UIColor darkGrayColor];
[_activityIndicator.layer setCornerRadius:5];
// Initialisation
recents = nil;
2014-12-02 14:25:00 +00:00
filteredRecents = nil;
unreadCount = 0;
currentSelectedCellIndexPathRow = -1;
2014-12-05 16:48:06 +00:00
NSString *dateFormat = @"MMM dd HH:mm";
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0]]];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:dateFormat];
[[MatrixSDKHandler sharedHandler] addObserver:self forKeyPath:@"status" options:0 context:nil];
}
- (void)dealloc {
if (currentRoomViewController) {
currentRoomViewController.roomId = nil;
currentRoomViewController = nil;
}
if (recentsListener) {
[[MatrixSDKHandler sharedHandler].mxSession removeListener:recentsListener];
recentsListener = nil;
}
recents = nil;
_preSelectedRoomId = nil;
2014-12-02 14:25:00 +00:00
recentsSearchBar = nil;
filteredRecents = nil;
if (dateFormatter) {
dateFormatter = nil;
}
[[MatrixSDKHandler sharedHandler] removeObserver:self forKeyPath:@"status"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
MatrixSDKHandler *mxHandler = [MatrixSDKHandler sharedHandler];
[mxHandler addObserver:self forKeyPath:@"isActivityInProgress" options:0 context:nil];
2014-12-05 16:48:06 +00:00
// Refresh display
shouldHideActivityIndicator = NO;
if (mxHandler.isActivityInProgress) {
[self startActivityIndicator];
}
[self configureView];
if (self.splitViewController) {
// Deselect the current selected row, it will be restored on viewDidAppear (if any)
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
if (indexPath) {
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[MatrixSDKHandler sharedHandler] removeObserver:self forKeyPath:@"isActivityInProgress"];
// Leave potential editing mode
[self setEditing:NO];
2014-12-02 14:25:00 +00:00
// Leave potential search session
if (recentsSearchBar) {
[self searchBarCancelButtonClicked:recentsSearchBar];
}
// Hide activity indicator
[self stopActivityIndicator];
shouldHideActivityIndicator = YES;
_preSelectedRoomId = nil;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Release the current selected room (if any) except if the Room ViewController is still visible (see splitViewController.isCollapsed condition)
if (!self.splitViewController || self.splitViewController.isCollapsed) {
if (currentRoomViewController) {
currentRoomViewController.roomId = nil;
currentRoomViewController = nil;
// Reset selected row index
currentSelectedCellIndexPathRow = -1;
}
} else {
// In case of split view controller where the primary and secondary view controllers are displayed side-by-side onscreen,
// the selected room (if any) is highlighted.
[self refreshCurrentSelectedCell:YES];
}
}
#pragma mark -
- (void)setPreSelectedRoomId:(NSString *)roomId {
_preSelectedRoomId = nil;
if (roomId) {
// Check whether recents update is in progress
if ([_activityIndicator isAnimating]) {
// Postpone room details display
_preSelectedRoomId = roomId;
return;
}
// Look for the room index in recents list
NSIndexPath *indexPath = nil;
for (NSUInteger index = 0; index < recents.count; index++) {
2014-12-05 16:48:06 +00:00
RecentRoom *recentRoom = [recents objectAtIndex:index];
if ([roomId isEqualToString:recentRoom.roomId]) {
indexPath = [NSIndexPath indexPathForRow:index inSection:0];
break;
}
}
if (indexPath) {
// Open details view
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
UITableViewCell *recentCell = [self.tableView cellForRowAtIndexPath:indexPath];
[self performSegueWithIdentifier:@"showDetail" sender:recentCell];
} else {
NSLog(@"We are not able to open room (%@) because it does not appear in recents yet", roomId);
// Postpone room details display. We run activity indicator until recents are updated (thanks to recents listener)
_preSelectedRoomId = roomId;
// Start activity indicator
[self startActivityIndicator];
}
} else if (currentRoomViewController) {
// Release the current selected room
currentRoomViewController.roomId = nil;
currentRoomViewController = nil;
// Force table refresh to deselect related cell
[self refreshRecentsDisplay];
}
}
#pragma mark - Internal methods
- (void)refreshRecentsDisplay {
// Check whether the current selected room has not been left
if (currentRoomViewController.roomId) {
MXRoom *mxRoom = [[MatrixSDKHandler sharedHandler].mxSession roomWithRoomId:currentRoomViewController.roomId];
if (mxRoom == nil || mxRoom.state.membership == MXMembershipLeave || mxRoom.state.membership == MXMembershipBan) {
// release the room viewController
currentRoomViewController.roomId = nil;
currentRoomViewController = nil;
}
}
[self.tableView reloadData];
// In case of split view controller, update the selected row (if any) and make it visible
if (self.splitViewController) {
[self refreshCurrentSelectedCell:YES];
}
}
- (void)configureView {
MatrixSDKHandler *mxHandler = [MatrixSDKHandler sharedHandler];
[[NSNotificationCenter defaultCenter] removeObserver:self name:kRecentRoomUpdatedByBackPagination object:nil];
if (mxHandler.mxSession) {
// Check matrix handler status
if (mxHandler.status == MatrixSDKHandlerStatusStoreDataReady || mxHandler.status == MatrixSDKHandlerStatusInitialServerSyncInProgress) {
// Server sync is not complete yet
2014-12-05 16:48:06 +00:00
if (!recents) {
// Retrieve recents from local storage (some data may not be up-to-date)
NSArray *recentEvents = [NSMutableArray arrayWithArray:[mxHandler.mxSession recentsWithTypeIn:mxHandler.eventsFilterForMessages]];
recents = [NSMutableArray arrayWithCapacity:recentEvents.count];
for (MXEvent *mxEvent in recentEvents) {
MXRoom *mxRoom = [mxHandler.mxSession roomWithRoomId:mxEvent.roomId];
RecentRoom *recentRoom = [[RecentRoom alloc] initWithLastEvent:mxEvent andRoomState:mxRoom.state markAsUnread:NO];
if (recentRoom) {
[recents addObject:recentRoom];
}
}
unreadCount = 0;
}
} else if (mxHandler.status == MatrixSDKHandlerStatusServerSyncDone) {
// Force recents refresh and add listener to update them (if it is not already done)
if (!recentsListener) {
2014-12-05 16:48:06 +00:00
NSArray *recentEvents = [NSMutableArray arrayWithArray:[mxHandler.mxSession recentsWithTypeIn:mxHandler.eventsFilterForMessages]];
recents = [NSMutableArray arrayWithCapacity:recentEvents.count];
for (MXEvent *mxEvent in recentEvents) {
MXRoom *mxRoom = [mxHandler.mxSession roomWithRoomId:mxEvent.roomId];
RecentRoom *recentRoom = [[RecentRoom alloc] initWithLastEvent:mxEvent andRoomState:mxRoom.state markAsUnread:NO];
2014-12-05 16:48:06 +00:00
if (recentRoom) {
[recents addObject:recentRoom];
}
}
unreadCount = 0;
2014-12-05 16:48:06 +00:00
// Check whether redaction event belongs to the listened events list
NSArray *listenedEventTypes = mxHandler.eventsFilterForMessages;
BOOL hideRedactionEvent = ([listenedEventTypes indexOfObject:kMXEventTypeStringRoomRedaction] == NSNotFound);
if (hideRedactionEvent) {
// Add redaction event to the listened events list in order to take into account redaction of the last event in recents.
// (See [RecentRoom updateWithLastEvent:...] for more details)
listenedEventTypes = [listenedEventTypes arrayByAddingObject:kMXEventTypeStringRoomRedaction];
}
2014-12-05 16:48:06 +00:00
// Register recent listener
recentsListener = [mxHandler.mxSession listenToEventsOfTypes:listenedEventTypes onEvent:^(MXEvent *event, MXEventDirection direction, MXRoomState *roomState) {
// Consider first live event
2014-12-05 16:48:06 +00:00
if (direction == MXEventDirectionForwards) {
// Check user's membership in live room state (We will remove left rooms from recents)
MXRoom *mxRoom = [mxHandler.mxSession roomWithRoomId:event.roomId];
BOOL isLeft = (mxRoom == nil || mxRoom.state.membership == MXMembershipLeave || mxRoom.state.membership == MXMembershipBan);
2014-12-05 16:48:06 +00:00
// Consider this new event as unread only if the sender is not the user and if the room is not visible
BOOL isUnread = (![event.userId isEqualToString:mxHandler.userId]
&& ![[AppDelegate theDelegate].masterTabBarController.visibleRoomId isEqualToString:event.roomId]);
2014-12-05 16:48:06 +00:00
// Look for the room
BOOL isFound = NO;
for (NSUInteger index = 0; index < recents.count; index++) {
RecentRoom *recentRoom = [recents objectAtIndex:index];
if ([event.roomId isEqualToString:recentRoom.roomId]) {
isFound = YES;
// Decrement here unreads count for this recent (we will add later the refreshed count)
unreadCount -= recentRoom.unreadCount;
if (isLeft) {
// Remove left room
[recents removeObjectAtIndex:index];
if (filteredRecents) {
NSUInteger filteredIndex = [filteredRecents indexOfObject:recentRoom];
if (filteredIndex != NSNotFound) {
[filteredRecents removeObjectAtIndex:filteredIndex];
}
}
} else {
if ([recentRoom updateWithLastEvent:event andRoomState:roomState markAsUnread:isUnread]) {
if (index) {
// Move this room at first position
[recents removeObjectAtIndex:index];
[recents insertObject:recentRoom atIndex:0];
}
// Update filtered recents (if any)
if (filteredRecents) {
NSUInteger filteredIndex = [filteredRecents indexOfObject:recentRoom];
if (filteredIndex && filteredIndex != NSNotFound) {
[filteredRecents removeObjectAtIndex:filteredIndex];
[filteredRecents insertObject:recentRoom atIndex:0];
}
}
}
// Refresh global unreads count
unreadCount += recentRoom.unreadCount;
}
// Refresh title
[self updateTitleView];
2014-12-05 16:48:06 +00:00
break;
}
}
if (!isFound && !isLeft) {
2014-12-05 16:48:06 +00:00
// Insert in first position this new room
RecentRoom *recentRoom = [[RecentRoom alloc] initWithLastEvent:event andRoomState:roomState markAsUnread:isUnread];
2014-12-05 16:48:06 +00:00
if (recentRoom) {
[recents insertObject:recentRoom atIndex:0];
if (isUnread) {
unreadCount++;
[self updateTitleView];
}
// Check whether we were waiting for this room
if (_preSelectedRoomId) {
if ([recentRoom.roomId isEqualToString:_preSelectedRoomId]) {
[self stopActivityIndicator];
self.preSelectedRoomId = _preSelectedRoomId;
}
}
2014-12-05 16:48:06 +00:00
}
}
// Reload table
[self refreshRecentsDisplay];
2014-12-05 16:48:06 +00:00
}
}];
}
// else nothing to do
} else if (mxHandler.status != MatrixSDKHandlerStatusPaused) {
// Here status is MatrixSDKHandlerStatusLoggedOut or MatrixSDKHandlerStatusLogged - Reset recents
recents = nil;
}
// Reload table
[self refreshRecentsDisplay];
// Check whether a room is preselected
if (_preSelectedRoomId) {
self.preSelectedRoomId = _preSelectedRoomId;
}
2014-11-28 14:06:53 +00:00
} else {
if (mxHandler.status == MatrixSDKHandlerStatusLoggedOut) {
// Update title
unreadCount = 0;
[self updateTitleView];
}
2014-11-28 14:06:53 +00:00
recents = nil;
[self refreshRecentsDisplay];
}
2014-12-05 16:48:06 +00:00
if (recents) {
// Add observer to force refresh when a recent last description is updated thanks to back pagination
// (This happens when the current last event description is blank, a back pagination is triggered to display non empty description)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRecentRoomUpdatedByBackPagination:) name:kRecentRoomUpdatedByBackPagination object:nil];
} else {
2014-12-05 16:48:06 +00:00
// Remove potential listener
if (recentsListener && mxHandler.mxSession) {
[mxHandler.mxSession removeListener:recentsListener];
recentsListener = nil;
}
}
[self updateTitleView];
}
- (void)onRecentRoomUpdatedByBackPagination:(NSNotification *)notif{
[self refreshRecentsDisplay];
[self updateTitleView];
if ([notif.object isKindOfClass:[NSString class]]) {
NSString* roomId = notif.object;
// Check whether this room is currently displayed in RoomViewController
if ([[AppDelegate theDelegate].masterTabBarController.visibleRoomId isEqualToString:roomId]) {
// For sanity reason, we have to force a full refresh in order to restore back state of the room
dispatch_async(dispatch_get_main_queue(), ^{
[currentRoomViewController forceRefresh];
});
}
}
}
- (void)updateTitleView {
NSString *title = @"Recents";
if (unreadCount) {
title = [NSString stringWithFormat:@"Recents (%tu)", unreadCount];
}
self.navigationItem.title = title;
}
- (void)createNewRoom:(id)sender {
[[AppDelegate theDelegate].masterTabBarController showRoomCreationForm];
}
2014-12-02 14:25:00 +00:00
- (void)search:(id)sender {
if (!recentsSearchBar) {
// Check whether there are data in which search
if (recents.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;
searchBarShouldEndEditing = NO;
// add it to the tableHeaderView
// do not create a header view
// the header view is refreshed every time there is a [tableView reloaddata]
// i.e. there is a removeFromSuperView call, the view is added to the tableview..
// with a first respondable view, IOS seems lost to find the first responder
// so, the keyboard is always displayed and can not be dismissed
// tableHeaderView is never removed from superview so the first responder is not lost
self.tableView.tableHeaderView = recentsSearchBar;
2014-12-02 14:25:00 +00:00
[recentsSearchBar becomeFirstResponder];
[self scrollToTop];
2014-12-02 14:25:00 +00:00
}
} else {
[self searchBarCancelButtonClicked: recentsSearchBar];
}
}
- (void)startActivityIndicator {
// Add the spinner on main screen in order to ignore potential table scrolling
2014-12-05 16:48:06 +00:00
_activityIndicator.center = CGPointMake(self.view.center.x, self.view.center.x);
[[AppDelegate theDelegate].window addSubview:_activityIndicator];
[_activityIndicator startAnimating];
}
- (void)stopActivityIndicator {
[_activityIndicator stopAnimating];
[_activityIndicator removeFromSuperview];
}
- (void)scrollToTop {
// stop any scrolling effect
[UIView setAnimationsEnabled:NO];
// before scrolling to the tableview top
self.tableView.contentOffset = CGPointMake(-self.tableView.contentInset.left, -self.tableView.contentInset.top);
[UIView setAnimationsEnabled:YES];
}
- (void)refreshCurrentSelectedCell:(BOOL)forceVisible {
// Update here the index of the current selected cell (if any) - Useful in landscape mode with split view controller.
currentSelectedCellIndexPathRow = -1;
if (currentRoomViewController) {
// Look for the rank of this selected room in displayed recents
NSArray *displayedRecents = filteredRecents ? filteredRecents : recents;
for (NSInteger index = 0; index < displayedRecents.count; index ++) {
RecentRoom *recentRoom = [displayedRecents objectAtIndex:index];
if ([currentRoomViewController.roomId isEqualToString:recentRoom.roomId]) {
currentSelectedCellIndexPathRow = index;
break;
}
}
}
if (currentSelectedCellIndexPathRow != -1) {
// Select the right row
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:currentSelectedCellIndexPathRow inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
if (forceVisible) {
// Scroll table view to make the selected row appear at second position
NSInteger topCellIndexPathRow = currentSelectedCellIndexPathRow ? currentSelectedCellIndexPathRow - 1: currentSelectedCellIndexPathRow;
indexPath = [NSIndexPath indexPathForRow:topCellIndexPathRow inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
} else {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
if (indexPath) {
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
}
}
#pragma mark - KVO
2014-11-28 14:06:53 +00:00
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([@"status" isEqualToString:keyPath]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self configureView];
});
} else if ([@"isActivityInProgress" isEqualToString:keyPath]) {
if (!shouldHideActivityIndicator && [MatrixSDKHandler sharedHandler].isActivityInProgress) {
[self startActivityIndicator];
} else {
[self stopActivityIndicator];
}
}
}
#pragma mark - Segues
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
2014-12-05 16:48:06 +00:00
RecentRoom *recentRoom;
2014-12-02 14:25:00 +00:00
if (filteredRecents) {
2014-12-05 16:48:06 +00:00
recentRoom = filteredRecents[indexPath.row];
2014-12-02 14:25:00 +00:00
} else {
2014-12-05 16:48:06 +00:00
recentRoom = recents[indexPath.row];
2014-12-02 14:25:00 +00:00
}
UIViewController *controller;
if ([[segue destinationViewController] isKindOfClass:[UINavigationController class]]) {
controller = [[segue destinationViewController] topViewController];
} else {
controller = [segue destinationViewController];
}
if ([controller isKindOfClass:[RoomViewController class]]) {
// Release potential Room ViewController
if (currentRoomViewController) {
currentRoomViewController.roomId = nil;
currentRoomViewController = nil;
}
currentRoomViewController = (RoomViewController *)controller;
2014-12-05 16:48:06 +00:00
currentRoomViewController.roomId = recentRoom.roomId;
}
2014-12-05 16:48:06 +00:00
// Reset unread count for this room
unreadCount -= recentRoom.unreadCount;
2014-12-05 16:48:06 +00:00
[recentRoom resetUnreadCount];
[self updateTitleView];
2014-12-05 16:48:06 +00:00
if (self.splitViewController) {
// Refresh selected cell without scrolling the selected cell (We suppose it's visible here)
[self refreshCurrentSelectedCell:NO];
2014-12-16 17:30:49 +00:00
// IOS >= 8
if ([self.splitViewController respondsToSelector:@selector(displayModeButtonItem)]) {
controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
}
// hide the keyboard when opening a new controller
// do not hide the searchBar until the RecentsViewController is dismissed
// on tablets / iphone 6+, the user could expect to search again while looking at a room
if ([recentsSearchBar isFirstResponder]) {
searchBarShouldEndEditing = YES;
[recentsSearchBar resignFirstResponder];
}
2014-12-16 17:30:49 +00:00
//
2014-12-05 16:48:06 +00:00
controller.navigationItem.leftItemsSupplementBackButton = YES;
}
// Hide back button title
self.navigationItem.backBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
2014-12-02 14:25:00 +00:00
if (filteredRecents) {
return filteredRecents.count;
}
return recents.count;
}
2014-12-02 14:25:00 +00:00
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RecentsTableViewCell *cell = (RecentsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"RecentsCell" forIndexPath:indexPath];
2014-12-05 16:48:06 +00:00
RecentRoom *recentRoom;
2014-12-02 14:25:00 +00:00
if (filteredRecents) {
2014-12-05 16:48:06 +00:00
recentRoom = filteredRecents[indexPath.row];
2014-12-02 14:25:00 +00:00
} else {
2014-12-05 16:48:06 +00:00
recentRoom = recents[indexPath.row];
2014-12-02 14:25:00 +00:00
}
MatrixSDKHandler *mxHandler = [MatrixSDKHandler sharedHandler];
2014-12-05 16:48:06 +00:00
MXRoom *mxRoom = [mxHandler.mxSession roomWithRoomId:recentRoom.roomId];
cell.roomTitle.text = [mxRoom.state displayname];
cell.lastEventDescription.text = recentRoom.lastEventDescription;
// Set in bold public room name
if (mxRoom.state.isPublic) {
cell.roomTitle.font = [UIFont boldSystemFontOfSize:20];
} else {
cell.roomTitle.font = [UIFont systemFontOfSize:19];
}
if (recentRoom.lastEventOriginServerTs != kMXUndefinedTimestamp) {
NSDate *date = [NSDate dateWithTimeIntervalSince1970:recentRoom.lastEventOriginServerTs/1000];
cell.recentDate.text = [dateFormatter stringFromDate:date];
} else {
cell.recentDate.text = nil;
2014-12-05 16:48:06 +00:00
}
// Set background color
2014-12-05 16:48:06 +00:00
if (recentRoom.unreadCount) {
cell.backgroundColor = [UIColor colorWithRed:1 green:0.9 blue:0.9 alpha:1.0];
cell.roomTitle.text = [NSString stringWithFormat:@"%@ (%tu)", cell.roomTitle.text, recentRoom.unreadCount];
2014-12-05 16:48:06 +00:00
} else {
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Leave the selected room
RecentRoom *selectedRoom;
2014-12-02 14:25:00 +00:00
if (filteredRecents) {
selectedRoom = filteredRecents[indexPath.row];
2014-12-02 14:25:00 +00:00
} else {
selectedRoom = recents[indexPath.row];
2014-12-02 14:25:00 +00:00
}
MXRoom *mxRoom = [[MatrixSDKHandler sharedHandler].mxSession roomWithRoomId:selectedRoom.roomId];
// cancel pending uploads/downloads
// they are useless by now
[MediaManager cancelDownloadsInFolder:selectedRoom.roomId];
[MediaManager cancelUploadsInFolder:selectedRoom.roomId];
[mxRoom leave:^{
// Remove the selected room (if it is not already done by recents listener)
for (NSUInteger index = 0; index < recents.count; index++) {
RecentRoom *recentRoom = [recents objectAtIndex:index];
if ([recentRoom.roomId isEqualToString:selectedRoom.roomId]) {
[recents removeObjectAtIndex:index];
if (filteredRecents) {
NSUInteger filteredIndex = [filteredRecents indexOfObject:selectedRoom];
if (filteredIndex != NSNotFound) {
[filteredRecents removeObjectAtIndex:filteredIndex];
}
}
break;
}
2014-12-02 14:25:00 +00:00
}
// Refresh table display
[self refreshRecentsDisplay];
} failure:^(NSError *error) {
NSLog(@"Failed to leave room (%@) failed: %@", selectedRoom.roomId, error);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
}
}
2014-12-02 14:25:00 +00:00
#pragma mark - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
searchBarShouldEndEditing = NO;
return YES;
}
2014-12-02 14:25:00 +00:00
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
return searchBarShouldEndEditing;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
// Update filtered list
if (searchText.length) {
if (filteredRecents) {
[filteredRecents removeAllObjects];
} else {
filteredRecents = [NSMutableArray arrayWithCapacity:recents.count];
}
MatrixSDKHandler *mxHandler = [MatrixSDKHandler sharedHandler];
2014-12-05 16:48:06 +00:00
for (RecentRoom *recentRoom in recents) {
MXRoom *mxRoom = [mxHandler.mxSession roomWithRoomId:recentRoom.roomId];
2014-12-02 14:25:00 +00:00
if ([[mxRoom.state displayname] rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound) {
2014-12-05 16:48:06 +00:00
[filteredRecents addObject:recentRoom];
2014-12-02 14:25:00 +00:00
}
}
} else {
filteredRecents = nil;
}
// Refresh display
[self refreshRecentsDisplay];
[self scrollToTop];
2014-12-02 14:25:00 +00:00
}
- (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;
filteredRecents = nil;
self.tableView.tableHeaderView = nil;
[self refreshRecentsDisplay];
[self scrollToTop];
2014-12-02 14:25:00 +00:00
}
@end