WFCUForwardViewController.m 12 KB

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