2020-10-14 17:18:58 +03:00
/ * *
* @ 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 / > .
*
* /
2018-10-01 19:10:06 +03:00
# import "RoomSearchTableViewController.h"
2022-11-03 20:49:19 +03:00
@ import NextcloudKit ;
2022-07-11 20:49:11 +03:00
2018-10-01 19:10:06 +03:00
# import "NCAPIController.h"
2020-11-11 19:52:31 +03:00
# import "NCAppBranding.h"
2021-05-26 22:58:50 +03:00
# import "NCDatabaseManager.h"
2018-10-01 19:10:06 +03:00
# import "NCRoom.h"
# import "NCSettingsController.h"
# import "PlaceholderView.h"
# import "RoomTableViewCell.h"
2023-04-17 14:15:31 +03:00
# import "NextcloudTalk-Swift.h"
2022-03-03 13:49:27 +03:00
typedef enum RoomSearchSection {
RoomSearchSectionFiltered = 0 ,
2023-07-05 17:33:32 +03:00
RoomSearchSectionUsers ,
2022-07-11 20:49:11 +03:00
RoomSearchSectionListable ,
RoomSearchSectionMessages
2022-03-03 13:49:27 +03:00
} RoomSearchSection ;
2018-10-01 19:10:06 +03:00
@ interface RoomSearchTableViewController ( )
{
PlaceholderView * _roomSearchBackgroundView ;
}
@ end
@ implementation RoomSearchTableViewController
- ( void ) viewDidLoad
{
[ super viewDidLoad ] ;
[ self . tableView registerNib : [ UINib nibWithNibName : kRoomTableCellNibName bundle : nil ] forCellReuseIdentifier : kRoomCellIdentifier ] ;
self . tableView . tableFooterView = [ [ UIView alloc ] initWithFrame : CGRectZero ] ;
2023-07-12 15:33:35 +03:00
// Align header ' s title to ContactsTableViewCell ' s label
self . tableView . separatorInset = UIEdgeInsetsMake ( 0 , 52 , 0 , 0 ) ;
self . tableView . separatorInsetReference = UITableViewSeparatorInsetFromAutomaticInsets ;
2018-10-01 19:10:06 +03:00
// Contacts placeholder view
_roomSearchBackgroundView = [ [ PlaceholderView alloc ] init ] ;
2021-01-19 20:55:57 +03:00
[ _roomSearchBackgroundView setImage : [ UIImage imageNamed : @ "conversations-placeholder" ] ] ;
2021-09-03 14:05:44 +03:00
[ _roomSearchBackgroundView . placeholderTextView setText : NSLocalizedString ( @ "No results found" , nil ) ] ;
2018-10-01 19:10:06 +03:00
[ _roomSearchBackgroundView . placeholderView setHidden : YES ] ;
[ _roomSearchBackgroundView . loadingView startAnimating ] ;
self . tableView . backgroundView = _roomSearchBackgroundView ;
}
- ( void ) didReceiveMemoryWarning
{
[ super didReceiveMemoryWarning ] ;
}
- ( void ) setRooms : ( NSArray * ) rooms
{
_rooms = rooms ;
2022-07-18 11:04:42 +03:00
[ self reloadAndCheckSearchingIndicator ] ;
2018-10-01 19:10:06 +03:00
}
2023-07-05 17:33:32 +03:00
- ( void ) setUsers : ( NSArray * ) users
{
_users = users ;
[ self reloadAndCheckSearchingIndicator ] ;
}
2022-07-14 16:40:05 +03:00
- ( void ) setListableRooms : ( NSArray * ) listableRooms
{
_listableRooms = listableRooms ;
2022-07-18 11:04:42 +03:00
[ self reloadAndCheckSearchingIndicator ] ;
2022-07-14 16:40:05 +03:00
}
- ( void ) setMessages : ( NSArray * ) messages
{
_messages = messages ;
2022-07-18 11:04:42 +03:00
[ self reloadAndCheckSearchingIndicator ] ;
2022-07-14 16:40:05 +03:00
}
2022-07-15 22:13:11 +03:00
- ( void ) setSearchingMessages : ( BOOL ) searchingMessages
{
_searchingMessages = searchingMessages ;
2022-07-18 11:04:42 +03:00
[ self reloadAndCheckSearchingIndicator ] ;
2022-07-15 22:13:11 +03:00
}
2022-07-14 16:40:05 +03:00
# pragma mark - User Interface
2022-07-18 11:04:42 +03:00
- ( void ) reloadAndCheckSearchingIndicator
2022-07-14 16:40:05 +03:00
{
[ self . tableView reloadData ] ;
if ( _searchingMessages ) {
if ( [ self searchSections ] . count > 0 ) {
[ _roomSearchBackgroundView . loadingView stopAnimating ] ;
[ _roomSearchBackgroundView . loadingView setHidden : YES ] ;
[ self showSearchingFooterView ] ;
} else {
[ _roomSearchBackgroundView . loadingView startAnimating ] ;
[ _roomSearchBackgroundView . loadingView setHidden : NO ] ;
2022-07-15 22:13:11 +03:00
[ self hideSearchingFooterView ] ;
2022-07-14 16:40:05 +03:00
}
2022-07-15 22:13:11 +03:00
[ _roomSearchBackgroundView . placeholderView setHidden : YES ] ;
2022-07-14 16:40:05 +03:00
} else {
[ _roomSearchBackgroundView . loadingView stopAnimating ] ;
[ _roomSearchBackgroundView . loadingView setHidden : YES ] ;
[ _roomSearchBackgroundView . placeholderView setHidden : [ self searchSections ] . count > 0 ] ;
}
}
- ( void ) showSearchingFooterView
{
UIActivityIndicatorView * loadingMoreView = [ [ UIActivityIndicatorView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 44 , 44 ) ] ;
loadingMoreView . color = [ UIColor darkGrayColor ] ;
[ loadingMoreView startAnimating ] ;
self . tableView . tableFooterView = loadingMoreView ;
}
2022-07-15 22:13:11 +03:00
- ( void ) hideSearchingFooterView
{
self . tableView . tableFooterView = nil ;
}
- ( void ) clearSearchedResults
{
_rooms = @ [ ] ;
2023-07-05 17:33:32 +03:00
_users = @ [ ] ;
2022-07-15 22:13:11 +03:00
_listableRooms = @ [ ] ;
_messages = @ [ ] ;
2022-07-18 11:04:42 +03:00
[ self reloadAndCheckSearchingIndicator ] ;
2022-07-15 22:13:11 +03:00
}
2022-07-14 16:40:05 +03:00
2018-10-01 19:10:06 +03:00
# pragma mark - Utils
2022-07-11 20:49:11 +03:00
- ( NSArray * ) searchSections
{
NSMutableArray * sections = [ NSMutableArray new ] ;
if ( _rooms . count > 0 ) {
[ sections addObject : @ ( RoomSearchSectionFiltered ) ] ;
}
2023-07-05 17:33:32 +03:00
if ( _users . count > 0 ) {
[ sections addObject : @ ( RoomSearchSectionUsers ) ] ;
}
2022-07-11 20:49:11 +03:00
if ( _listableRooms . count > 0 ) {
[ sections addObject : @ ( RoomSearchSectionListable ) ] ;
}
if ( _messages . count > 0 ) {
[ sections addObject : @ ( RoomSearchSectionMessages ) ] ;
}
return [ NSArray arrayWithArray : sections ] ;
}
2022-03-03 13:49:27 +03:00
- ( NCRoom * ) roomForIndexPath : ( NSIndexPath * ) indexPath
{
2022-07-11 20:49:11 +03:00
NSInteger searchSection = [ [ [ self searchSections ] objectAtIndex : indexPath . section ] integerValue ] ;
if ( searchSection = = RoomSearchSectionFiltered && indexPath . row < _rooms . count ) {
2022-03-03 13:49:27 +03:00
return [ _rooms objectAtIndex : indexPath . row ] ;
2022-07-11 20:49:11 +03:00
} else if ( searchSection = = RoomSearchSectionListable && indexPath . row < _listableRooms . count ) {
2022-03-03 13:49:27 +03:00
return [ _listableRooms objectAtIndex : indexPath . row ] ;
}
return nil ;
}
2022-11-03 20:49:19 +03:00
- ( NKSearchEntry * ) messageForIndexPath : ( NSIndexPath * ) indexPath
2022-07-11 20:49:11 +03:00
{
NSInteger searchSection = [ [ [ self searchSections ] objectAtIndex : indexPath . section ] integerValue ] ;
if ( searchSection = = RoomSearchSectionMessages && indexPath . row < _messages . count ) {
return [ _messages objectAtIndex : indexPath . row ] ;;
}
return nil ;
}
- ( UITableViewCell * ) tableView : ( UITableView * ) tableView cellForMessageAtIndexPath : ( NSIndexPath * ) indexPath
2022-03-03 22:00:59 +03:00
{
2022-11-03 20:49:19 +03:00
NKSearchEntry * messageEntry = [ _messages objectAtIndex : indexPath . row ] ;
2022-07-11 20:49:11 +03:00
RoomTableViewCell * cell = [ tableView dequeueReusableCellWithIdentifier : kRoomCellIdentifier ] ;
if ( ! cell ) {
cell = [ [ RoomTableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : kRoomCellIdentifier ] ;
}
cell . titleLabel . text = messageEntry . title ;
cell . subtitleLabel . text = messageEntry . subline ;
2022-07-18 15:46:07 +03:00
// Thumbnail image
NSURL * thumbnailURL = [ [ NSURL alloc ] initWithString : messageEntry . thumbnailURL ] ;
NSString * actorId = [ messageEntry . attributes objectForKey : @ "actorId" ] ;
NSString * actorType = [ messageEntry . attributes objectForKey : @ "actorType" ] ;
if ( [ actorType isEqualToString : @ "users" ] && actorId ) {
2023-04-17 14:15:31 +03:00
[ cell . roomImage setUserAvatarFor : actorId with : self . traitCollection . userInterfaceStyle ] ;
2022-07-18 15:46:07 +03:00
} else if ( [ actorType isEqualToString : @ "guests" ] ) {
2023-03-09 18:10:26 +03:00
UIImage * image = [ NCUtils getImageWithString : @ "?" withBackgroundColor : [ UIColor clearColor ] withBounds : cell . roomImage . bounds isCircular : YES ] ;
[ cell . roomImage setImage : image ] ;
2022-07-18 15:46:07 +03:00
cell . roomImage . contentMode = UIViewContentModeScaleAspectFit ;
} else if ( thumbnailURL ) {
[ cell . roomImage setImageWithURL : thumbnailURL placeholderImage : nil ] ;
cell . roomImage . contentMode = UIViewContentModeScaleToFill ;
} else {
[ cell . roomImage setImage : [ UIImage imageNamed : @ "navigationLogo" ] ] ;
cell . roomImage . contentMode = UIViewContentModeCenter ;
}
2022-07-13 18:36:44 +03:00
2022-07-12 19:27:37 +03:00
// Clear possible content not removed by cell reuse
cell . dateLabel . text = @ "" ;
[ cell setUnreadMessages : 0 mentioned : NO groupMentioned : NO ] ;
2022-07-11 20:49:11 +03:00
2022-07-13 18:36:44 +03:00
// Add message date ( if it is included in attributes )
NSInteger timestamp = [ [ messageEntry . attributes objectForKey : @ "timestamp" ] integerValue ] ;
if ( timestamp > 0 ) {
NSDate * date = [ [ NSDate alloc ] initWithTimeIntervalSince1970 : timestamp ] ;
cell . dateLabel . text = [ NCUtils readableTimeOrDateFromDate : date ] ;
}
2022-07-11 20:49:11 +03:00
return cell ;
2022-03-03 22:00:59 +03:00
}
2023-07-05 17:33:32 +03:00
- ( NCUser * ) userForIndexPath : ( NSIndexPath * ) indexPath
{
NSInteger searchSection = [ [ [ self searchSections ] objectAtIndex : indexPath . section ] integerValue ] ;
if ( searchSection = = RoomSearchSectionUsers && indexPath . row < _users . count ) {
2023-07-10 19:09:34 +03:00
return [ _users objectAtIndex : indexPath . row ] ;
2023-07-05 17:33:32 +03:00
}
return nil ;
}
- ( UITableViewCell * ) tableView : ( UITableView * ) tableView cellForUserAtIndexPath : ( NSIndexPath * ) indexPath
{
NCUser * user = [ _users objectAtIndex : indexPath . row ] ;
RoomTableViewCell * cell = [ tableView dequeueReusableCellWithIdentifier : kRoomCellIdentifier ] ;
if ( ! cell ) {
cell = [ [ RoomTableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : kRoomCellIdentifier ] ;
}
cell . titleLabel . text = user . name ;
cell . titleOnly = YES ;
[ cell . roomImage setUserAvatarFor : user . userId with : self . traitCollection . userInterfaceStyle ] ;
return cell ;
}
2018-10-01 19:10:06 +03:00
# pragma mark - Table view data source
2022-03-03 13:49:27 +03:00
- ( NSInteger ) numberOfSectionsInTableView : ( UITableView * ) tableView
{
2022-07-11 20:49:11 +03:00
return [ self searchSections ] . count ;
2018-10-01 19:10:06 +03:00
}
2022-03-03 13:49:27 +03:00
- ( NSInteger ) tableView : ( UITableView * ) tableView numberOfRowsInSection : ( NSInteger ) section
{
2022-07-11 20:49:11 +03:00
NSInteger searchSection = [ [ [ self searchSections ] objectAtIndex : section ] integerValue ] ;
switch ( searchSection ) {
2022-03-03 13:49:27 +03:00
case RoomSearchSectionFiltered :
return _rooms . count ;
2023-07-05 17:33:32 +03:00
case RoomSearchSectionUsers :
return _users . count ;
2022-03-03 13:49:27 +03:00
case RoomSearchSectionListable :
return _listableRooms . count ;
2022-07-11 20:49:11 +03:00
case RoomSearchSectionMessages :
return _messages . count ;
2022-03-03 13:49:27 +03:00
default :
return 0 ;
}
2018-10-01 19:10:06 +03:00
}
- ( CGFloat ) tableView : ( UITableView * ) tableView heightForRowAtIndexPath : ( NSIndexPath * ) indexPath
{
2018-12-06 13:15:18 +03:00
return kRoomTableCellHeight ;
2018-10-01 19:10:06 +03:00
}
2022-03-03 13:49:27 +03:00
- ( NSString * ) tableView : ( UITableView * ) tableView titleForHeaderInSection : ( NSInteger ) section
{
2022-07-11 20:49:11 +03:00
NSInteger searchSection = [ [ [ self searchSections ] objectAtIndex : section ] integerValue ] ;
switch ( searchSection ) {
case RoomSearchSectionFiltered :
return NSLocalizedString ( @ "Conversations" , @ "" ) ;
2023-07-05 17:33:32 +03:00
case RoomSearchSectionUsers :
return NSLocalizedString ( @ "Users" , @ "" ) ;
2022-03-03 13:49:27 +03:00
case RoomSearchSectionListable :
2022-06-17 18:04:11 +03:00
return NSLocalizedString ( @ "Open conversations" , @ "TRANSLATORS 'Open conversations' as a type of conversation. 'Open conversations' are conversations that can be found by other users" ) ;
2022-07-11 20:49:11 +03:00
case RoomSearchSectionMessages :
return NSLocalizedString ( @ "Messages" , @ "" ) ;
2022-03-03 13:49:27 +03:00
default :
return nil ;
}
}
2018-10-01 19:10:06 +03:00
- ( UITableViewCell * ) tableView : ( UITableView * ) tableView cellForRowAtIndexPath : ( NSIndexPath * ) indexPath
{
2022-07-11 20:49:11 +03:00
NSInteger searchSection = [ [ [ self searchSections ] objectAtIndex : indexPath . section ] integerValue ] ;
2023-07-05 17:33:32 +03:00
// Messages
2022-07-11 20:49:11 +03:00
if ( searchSection = = RoomSearchSectionMessages ) {
return [ self tableView : tableView cellForMessageAtIndexPath : indexPath ] ;
}
2023-07-05 17:33:32 +03:00
// Contacts
if ( searchSection = = RoomSearchSectionUsers ) {
return [ self tableView : tableView cellForUserAtIndexPath : indexPath ] ;
}
2022-07-11 20:49:11 +03:00
2022-03-03 13:49:27 +03:00
NCRoom * room = [ self roomForIndexPath : indexPath ] ;
2018-10-01 19:10:06 +03:00
RoomTableViewCell * cell = [ tableView dequeueReusableCellWithIdentifier : kRoomCellIdentifier ] ;
if ( ! cell ) {
cell = [ [ RoomTableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : kRoomCellIdentifier ] ;
}
// Set room name
cell . titleLabel . text = room . displayName ;
2020-01-10 16:31:29 +03:00
// Set last activity
2020-01-14 21:12:37 +03:00
if ( room . lastMessage ) {
2020-01-10 16:31:29 +03:00
cell . titleOnly = NO ;
2020-01-15 15:44:27 +03:00
cell . subtitleLabel . text = room . lastMessageString ;
2018-10-01 19:10:06 +03:00
} else {
2020-01-10 16:31:29 +03:00
cell . titleOnly = YES ;
2018-10-01 19:10:06 +03:00
}
2020-01-10 16:31:29 +03:00
NSDate * date = [ [ NSDate alloc ] initWithTimeIntervalSince1970 : room . lastActivity ] ;
2022-06-12 02:02:27 +03:00
cell . dateLabel . text = [ NCUtils readableTimeOrDateFromDate : date ] ;
2018-10-01 19:10:06 +03:00
// Set unread messages
2021-09-24 19:14:51 +03:00
if ( [ [ NCDatabaseManager sharedInstance ] serverHasTalkCapability : kCapabilityDirectMentionFlag ] ) {
2023-02-06 18:24:55 +03:00
BOOL mentioned = room . unreadMentionDirect || room . type = = kNCRoomTypeOneToOne || room . type = = kNCRoomTypeFormerOneToOne ;
2021-09-24 19:14:51 +03:00
BOOL groupMentioned = room . unreadMention && ! room . unreadMentionDirect ;
[ cell setUnreadMessages : room . unreadMessages mentioned : mentioned groupMentioned : groupMentioned ] ;
} else {
2023-02-06 18:24:55 +03:00
BOOL mentioned = room . unreadMention || room . type = = kNCRoomTypeOneToOne || room . type = = kNCRoomTypeFormerOneToOne ;
2021-09-24 19:14:51 +03:00
[ cell setUnreadMessages : room . unreadMessages mentioned : mentioned groupMentioned : NO ] ;
}
2023-02-06 16:35:50 +03:00
2023-04-17 14:15:31 +03:00
[ cell . roomImage setAvatarFor : room with : self . traitCollection . userInterfaceStyle ] ;
2023-04-26 19:49:43 +03:00
// Set favorite or call image
if ( room . hasCall ) {
[ cell . favoriteImage setTintColor : [ UIColor systemRedColor ] ] ;
[ cell . favoriteImage setImage : [ UIImage systemImageNamed : @ "video.fill" ] ] ;
} else if ( room . isFavorite ) {
[ cell . favoriteImage setTintColor : [ UIColor systemYellowColor ] ] ;
2023-04-11 19:00:46 +03:00
[ cell . favoriteImage setImage : [ UIImage systemImageNamed : @ "star.fill" ] ] ;
2018-10-01 19:10:06 +03:00
}
return cell ;
}
@ end