WFCUForwardViewController.m 14 KB

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