WFCUConferenceInviteViewController.m 13 KB

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