WFCUForwardViewController.m 13 KB

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