WFCUForwardViewController.m 13 KB

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