WFCUConferenceInviteViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //
  2. // ForwardViewController.m
  3. // WildFireChat
  4. //
  5. // Created by heavyrain lee on 2018/9/27.
  6. // Copyright © 2018 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUConferenceInviteViewController.h"
  9. #import <SDWebImage/SDWebImage.h>
  10. #import "WFCUForwardMessageCell.h"
  11. #import "WFCUContactTableViewCell.h"
  12. #import "WFCUSearchGroupTableViewCell.h"
  13. #import "TYAlertView.h"
  14. #import "TYAlertController.h"
  15. #import "WFCUShareMessageView.h"
  16. #import "UIView+TYAlertView.h"
  17. #import "UIView+Toast.h"
  18. #import "WFCUContactListViewController.h"
  19. #import "WFCUConfigManager.h"
  20. #import "UIImage+ERCategory.h"
  21. @interface WFCUConferenceInviteViewController () <UITableViewDataSource, UISearchControllerDelegate, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating>
  22. @property (nonatomic, strong)UITableView *tableView;
  23. @property (nonatomic, strong)UISearchController *searchController;
  24. @property (nonatomic, strong)NSMutableArray<WFCCConversationInfo *> *conversations;
  25. @property (nonatomic, strong)NSArray<WFCCUserInfo *> *searchFriendList;
  26. @property (nonatomic, strong)NSArray<WFCCGroupSearchInfo *> *searchGroupList;
  27. @end
  28. @implementation WFCUConferenceInviteViewController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. CGRect frame = self.view.frame;
  32. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 54, frame.size.width, frame.size.height - 64)];
  33. self.tableView.delegate = self;
  34. self.tableView.dataSource = self;
  35. if (@available(iOS 15, *)) {
  36. self.tableView.sectionHeaderTopPadding = 0;
  37. }
  38. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  39. self.tableView.tableHeaderView = nil;
  40. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:WFCString(@"Cancel") style:UIBarButtonItemStyleDone target:self action:@selector(onLeftBarBtn:)];
  41. self.conversations = [[[WFCCIMService sharedWFCIMService] getConversationInfos:@[@(Single_Type), @(Group_Type), @(SecretChat_Type)] lines:@[@(0)]] mutableCopy];
  42. self.extendedLayoutIncludesOpaqueBars = YES;
  43. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  44. self.searchController.searchResultsUpdater = self;
  45. self.searchController.delegate = self;
  46. self.searchController.dimsBackgroundDuringPresentation = NO;
  47. if (@available(iOS 13, *)) {
  48. self.searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
  49. self.searchController.searchBar.searchTextField.backgroundColor = [WFCUConfigManager globalManager].naviBackgroudColor;
  50. UIImage* searchBarBg = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(self.view.frame.size.width - 8 * 2, 36) cornerRadius:4];
  51. [self.searchController.searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
  52. } else {
  53. [self.searchController.searchBar setValue:WFCString(@"Cancel") forKey:@"_cancelButtonText"];
  54. }
  55. if (@available(iOS 9.1, *)) {
  56. self.searchController.obscuresBackgroundDuringPresentation = NO;
  57. }
  58. [self.searchController.searchBar setPlaceholder:WFCString(@"Search")];
  59. if (@available(iOS 11.0, *)) {
  60. self.navigationItem.searchController = _searchController;
  61. _searchController.hidesNavigationBarDuringPresentation = YES;
  62. } else {
  63. self.tableView.tableHeaderView = _searchController.searchBar;
  64. }
  65. self.definesPresentationContext = YES;
  66. self.tableView.sectionIndexColor = [UIColor grayColor];
  67. [self.view addSubview:self.tableView];
  68. [self.tableView reloadData];
  69. }
  70. - (void)onLeftBarBtn:(UIBarButtonItem *)sender {
  71. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  72. }
  73. - (void)altertSend:(WFCCConversation *)conversation {
  74. WFCUShareMessageView *shareView = [WFCUShareMessageView createViewFromNib];
  75. shareView.conversation = conversation;
  76. WFCCMessage *message = [[WFCCMessage alloc] init];
  77. message.conversation = conversation;
  78. message.content = self.invite;
  79. shareView.message = message;
  80. __weak typeof(self)ws = self;
  81. shareView.forwardDone = ^(BOOL success) {
  82. if (success) {
  83. [ws.view makeToast:WFCString(@"ForwardSuccess") duration:1 position:CSToastPositionCenter];
  84. [ws.navigationController dismissViewControllerAnimated:YES completion:nil];
  85. } else {
  86. [ws.view makeToast:WFCString(@"ForwardFailure") duration:1 position:CSToastPositionCenter];
  87. }
  88. };
  89. TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:shareView preferredStyle:TYAlertControllerStyleAlert];
  90. // // blur effect
  91. // [alertController setBlurEffectWithView:self.view];
  92. //
  93. //alertController.alertViewOriginY = 60;
  94. [self.navigationController presentViewController:alertController animated:YES completion:nil];
  95. // [shareView showInWindow];
  96. }
  97. #pragma mark - UITableViewDataSource
  98. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  99. if (self.searchController.active) {
  100. int sec = 0;
  101. if (self.searchFriendList.count) {
  102. sec++;
  103. if (section == sec-1) {
  104. return self.searchFriendList.count;
  105. }
  106. }
  107. if (self.searchGroupList.count) {
  108. sec++;
  109. if (section == sec-1) {
  110. return self.searchGroupList.count;
  111. }
  112. }
  113. return 0;
  114. } else {
  115. if (section == 0) {
  116. return 1;
  117. }
  118. return self.conversations.count;
  119. }
  120. }
  121. // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
  122. // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
  123. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  124. #define REUSECONVIDENTIFY @"resueConvCell"
  125. #define REUSENEWCONVIDENTIFY @"resueNewConvCell"
  126. if (self.searchController.active) {
  127. int sec = 0;
  128. if (self.searchFriendList.count) {
  129. sec++;
  130. if (indexPath.section == sec-1) {
  131. WFCUContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friendCell"];
  132. if (cell == nil) {
  133. cell = [[WFCUContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"friendCell"];
  134. }
  135. [cell setUserId:self.searchFriendList[indexPath.row].userId groupId:nil];
  136. return cell;
  137. }
  138. }
  139. if (self.searchGroupList.count) {
  140. sec++;
  141. if (indexPath.section == sec-1) {
  142. WFCUSearchGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"groupCell"];
  143. if (cell == nil) {
  144. cell = [[WFCUSearchGroupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"groupCell"];
  145. }
  146. cell.groupSearchInfo = self.searchGroupList[indexPath.row];
  147. return cell;
  148. }
  149. }
  150. return nil;
  151. } else {
  152. if (indexPath.section == 0) {
  153. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:REUSENEWCONVIDENTIFY];
  154. if(!cell) {
  155. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:REUSENEWCONVIDENTIFY];
  156. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  157. }
  158. cell.textLabel.text = WFCString(@"CreateNewChat");
  159. return cell;
  160. } else {
  161. WFCUForwardMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:REUSECONVIDENTIFY];
  162. if(!cell) {
  163. cell = [[WFCUForwardMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:REUSECONVIDENTIFY];
  164. }
  165. WFCCConversationInfo *info = [self.conversations objectAtIndex:indexPath.row];
  166. cell.conversation = info.conversation;
  167. return cell;
  168. }
  169. }
  170. return nil;
  171. }
  172. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  173. if (self.searchController.active) {
  174. int sec = 0;
  175. if (self.searchFriendList.count) {
  176. sec++;
  177. }
  178. if (self.searchGroupList.count) {
  179. sec++;
  180. }
  181. if (sec == 0) {
  182. sec = 1;
  183. }
  184. return sec;
  185. }
  186. return 2;
  187. }
  188. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  189. if (section == 0) {
  190. if (self.searchController.isActive) {
  191. return 44;
  192. } else {
  193. return 0;
  194. }
  195. }
  196. return 21;
  197. }
  198. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  199. if (self.searchController.isActive) {
  200. if (self.searchGroupList.count + self.searchFriendList.count > 0) {
  201. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, section == 0 ? 44 : 20)];
  202. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, section == 0 ? 24 : 0, self.tableView.frame.size.width, 20)];
  203. label.font = [UIFont systemFontOfSize:13];
  204. label.textColor = [UIColor grayColor];
  205. label.textAlignment = NSTextAlignmentLeft;
  206. label.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  207. int sec = 0;
  208. if (self.searchFriendList.count) {
  209. sec++;
  210. if (section == sec-1) {
  211. label.text = WFCString(@"Contact");
  212. }
  213. }
  214. if (self.searchGroupList.count) {
  215. sec++;
  216. if (section == sec-1) {
  217. label.text = WFCString(@"Group");
  218. }
  219. }
  220. [header addSubview:label];
  221. return header;
  222. } else {
  223. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
  224. return header;
  225. }
  226. } else {
  227. NSString *title = WFCString(@"RecentChat");
  228. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 21)];
  229. label.font = [UIFont systemFontOfSize:13];
  230. label.textColor = [UIColor grayColor];
  231. label.textAlignment = NSTextAlignmentLeft;
  232. label.text = [NSString stringWithFormat:@" %@", title];
  233. label.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  234. return label;
  235. }
  236. }
  237. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  238. return 56;
  239. }
  240. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  241. WFCCConversation *selectedConv;
  242. if (self.searchController.isActive) {
  243. if (indexPath.section == 0 && self.searchFriendList.count > 0) {
  244. WFCCUserInfo *userInfo = self.searchFriendList[indexPath.row];
  245. selectedConv = [[WFCCConversation alloc] init];
  246. selectedConv.type = Single_Type;
  247. selectedConv.target = userInfo.userId;
  248. selectedConv.line = 0;
  249. } else {
  250. WFCCGroupInfo *groupInfo = self.searchGroupList[indexPath.row].groupInfo;
  251. selectedConv = [[WFCCConversation alloc] init];
  252. selectedConv.type = Group_Type;
  253. selectedConv.target = groupInfo.target;
  254. selectedConv.line = 0;
  255. }
  256. } else {
  257. if (indexPath.section == 0) { //new conversation
  258. WFCUContactListViewController *pvc = [[WFCUContactListViewController alloc] init];
  259. pvc.selectContact = YES;
  260. pvc.multiSelect = NO;
  261. pvc.isPushed = YES;
  262. __weak typeof(self)ws = self;
  263. pvc.selectResult = ^(NSArray<NSString *> *contacts) {
  264. dispatch_async(dispatch_get_main_queue(), ^{
  265. if (contacts.count == 1) {
  266. WFCCConversation *conversation = [[WFCCConversation alloc] init];
  267. conversation.type = Single_Type;
  268. conversation.target = contacts[0];
  269. conversation.line = 0;
  270. [ws altertSend:conversation];
  271. }
  272. });
  273. };
  274. [self.navigationController pushViewController:pvc animated:YES];
  275. return;
  276. } else {
  277. selectedConv = self.conversations[indexPath.row].conversation;
  278. }
  279. }
  280. if (selectedConv) {
  281. [self altertSend:selectedConv];
  282. }
  283. }
  284. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  285. if (self.searchController.active) {
  286. [self.searchController.searchBar resignFirstResponder];
  287. }
  288. }
  289. #pragma mark - UISearchControllerDelegate
  290. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  291. NSString *searchString = [self.searchController.searchBar text];
  292. if (searchString.length) {
  293. self.searchFriendList = [[WFCCIMService sharedWFCIMService] searchFriends:searchString];
  294. self.searchGroupList = [[WFCCIMService sharedWFCIMService] searchGroups:searchString];
  295. } else {
  296. self.searchFriendList = nil;
  297. self.searchGroupList = nil;
  298. }
  299. [self.tableView reloadData];
  300. }
  301. @end