talk-ios/NextcloudTalk/RoomSearchTableViewControll...

180 строки
6.2 KiB
Mathematica
Исходник Обычный вид История

/**
* @copyright Copyright (c) 2020 Ivan Sein <ivan@nextcloud.com>
*
* @author Ivan Sein <ivan@nextcloud.com>
*
* @license GNU GPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#import "RoomSearchTableViewController.h"
#import "NSDate+DateTools.h"
#import "UIImageView+AFNetworking.h"
#import "NCAPIController.h"
#import "NCAppBranding.h"
#import "NCDatabaseManager.h"
#import "NCRoom.h"
#import "NCSettingsController.h"
#import "PlaceholderView.h"
#import "RoomTableViewCell.h"
@interface RoomSearchTableViewController ()
{
PlaceholderView *_roomSearchBackgroundView;
}
@end
@implementation RoomSearchTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:kRoomTableCellNibName bundle:nil] forCellReuseIdentifier:kRoomCellIdentifier];
// Align header's title to ContactsTableViewCell's label
self.tableView.separatorInset = UIEdgeInsetsMake(0, 72, 0, 0);
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
// Contacts placeholder view
_roomSearchBackgroundView = [[PlaceholderView alloc] init];
[_roomSearchBackgroundView setImage:[UIImage imageNamed:@"conversations-placeholder"]];
[_roomSearchBackgroundView.placeholderTextView setText:NSLocalizedString(@"No results found", nil)];
[_roomSearchBackgroundView.placeholderView setHidden:YES];
[_roomSearchBackgroundView.loadingView startAnimating];
self.tableView.backgroundView = _roomSearchBackgroundView;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)setRooms:(NSArray *)rooms
{
_rooms = rooms;
[_roomSearchBackgroundView.loadingView stopAnimating];
[_roomSearchBackgroundView.loadingView setHidden:YES];
[_roomSearchBackgroundView.placeholderView setHidden:(rooms.count > 0)];
}
#pragma mark - Utils
- (NSString *)getDateLabelStringForDate:(NSDate *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
if ([date isToday]) {
[formatter setDateFormat:@"HH:mm"];
} else if ([date isYesterday]) {
return NSLocalizedString(@"Yesterday", nil);
} else {
[formatter setDateFormat:@"dd/MM/yy"];
}
return [formatter stringFromDate:date];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _rooms.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return kRoomTableCellHeight;
}
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
// return [_indexes objectAtIndex:section];
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NCRoom *room = [_rooms objectAtIndex:indexPath.row];
RoomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kRoomCellIdentifier];
if (!cell) {
cell = [[RoomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kRoomCellIdentifier];
}
// Set room name
cell.titleLabel.text = room.displayName;
// Set last activity
if (room.lastMessage) {
cell.titleOnly = NO;
cell.subtitleLabel.text = room.lastMessageString;
} else {
cell.titleOnly = YES;
}
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:room.lastActivity];
cell.dateLabel.text = [self getDateLabelStringForDate:date];
// Set unread messages
if ([[NCDatabaseManager sharedInstance] serverHasTalkCapability:kCapabilityDirectMentionFlag]) {
BOOL mentioned = room.unreadMentionDirect || room.type == kNCRoomTypeOneToOne;
BOOL groupMentioned = room.unreadMention && !room.unreadMentionDirect;
[cell setUnreadMessages:room.unreadMessages mentioned:mentioned groupMentioned:groupMentioned];
} else {
BOOL mentioned = room.unreadMention || room.type == kNCRoomTypeOneToOne;
[cell setUnreadMessages:room.unreadMessages mentioned:mentioned groupMentioned:NO];
}
// Set room image
switch (room.type) {
case kNCRoomTypeOneToOne:
[cell.roomImage setImageWithURLRequest:[[NCAPIController sharedInstance] createAvatarRequestForUser:room.name andSize:96 usingAccount:[[NCDatabaseManager sharedInstance] activeAccount]]
placeholderImage:nil success:nil failure:nil];
[cell.roomImage setContentMode:UIViewContentModeScaleToFill];
break;
case kNCRoomTypeGroup:
[cell.roomImage setImage:[UIImage imageNamed:@"group"]];
break;
case kNCRoomTypePublic:
[cell.roomImage setImage:[UIImage imageNamed:@"public"]];
break;
case kNCRoomTypeChangelog:
[cell.roomImage setImage:[UIImage imageNamed:@"changelog"]];
[cell.roomImage setContentMode:UIViewContentModeScaleToFill];
break;
default:
break;
}
// Set objectType image
if ([room.objectType isEqualToString:NCRoomObjectTypeFile]) {
[cell.roomImage setImage:[UIImage imageNamed:@"file"]];
} else if ([room.objectType isEqualToString:NCRoomObjectTypeSharePassword]) {
[cell.roomImage setImage:[UIImage imageNamed:@"password"]];
}
// Set favorite image
if (room.isFavorite) {
[cell.favoriteImage setImage:[UIImage imageNamed:@"favorite-room"]];
}
return cell;
}
@end