PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/TeamTalk/IOSDuoduo/VC/Chatting/EditGroupViewController.m

https://gitlab.com/lisit1003/TTiOSClient
Objective C | 504 lines | 449 code | 44 blank | 11 comment | 47 complexity | e491c77db029e1a580b49af2c0f618bb MD5 | raw file
  1. //
  2. // EditGroupViewController.m
  3. // TeamTalk
  4. //
  5. // Created by Michael Scofield on 2014-09-01.
  6. // Copyright (c) 2014 dujia. All rights reserved.
  7. //
  8. #import "EditGroupViewController.h"
  9. #import "EditGroupViewCell.h"
  10. #import "DDUserEntity.h"
  11. #import "EditContactsCell.h"
  12. #import "DDAddMemberToGroupAPI.h"
  13. #import "DDCreateGroupAPI.h"
  14. #import "std.h"
  15. #import "RuntimeStatus.h"
  16. #import "DDSearch.h"
  17. #import "ChattingMainViewController.h"
  18. #import "MBProgressHUD.h"
  19. #import "UIImageView+WebCache.h"
  20. #import "DDGroupModule.h"
  21. #import "ContactsModule.h"
  22. #import "DDDeleteMemberFromGroupAPI.h"
  23. @interface EditGroupViewController ()
  24. @property(weak)IBOutlet UICollectionView *personView;
  25. @property(weak)IBOutlet UITableView *tableView;
  26. @property(weak)IBOutlet UISearchBar *searchBar;
  27. @property(strong)NSDictionary *items;
  28. @property(strong)UILabel *label;
  29. @property(strong)NSMutableArray *backupArray;
  30. @property(strong)NSMutableArray *editArray;
  31. @property(strong)NSMutableArray *selectedArray;
  32. @property(strong)NSArray *searchResult;
  33. @property(strong)UISearchDisplayController *searchController;
  34. @property(strong) ContactsModule*model;
  35. @property(strong)MBProgressHUD *hud;
  36. @end
  37. @implementation EditGroupViewController
  38. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  39. {
  40. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  41. if (self) {
  42. // Custom initialization
  43. }
  44. return self;
  45. }
  46. - (void)viewDidLoad
  47. {
  48. [super viewDidLoad];
  49. self.edgesForExtendedLayout = UIRectEdgeNone;
  50. self.extendedLayoutIncludesOpaqueBars = NO;
  51. self.title=@"编辑群联系人";
  52. if ([self.users count] == 1) {
  53. self.label = [[UILabel alloc] initWithFrame:CGRectMake(90, 50, 200, 30)];
  54. [self.label setText:@"选择联系人添加进来"];
  55. [self.label setTextColor:GRAYCOLOR];
  56. [self.personView addSubview:self.label];
  57. }
  58. self.searchResult = [NSArray new];
  59. self.selectedArray = [NSMutableArray new];
  60. [self.users removeLastObject];
  61. self.editArray = [NSMutableArray arrayWithArray:self.users];
  62. self.sessionID=self.editControll.session.sessionID;
  63. [self.personView setBackgroundColor:[UIColor whiteColor]];
  64. [self.personView.layer setBorderWidth:0.5];
  65. [self.personView.layer setBorderColor:RGB(199, 199, 196).CGColor];
  66. self.personView.delegate = self;
  67. self.personView.dataSource = self;
  68. self.items = [NSMutableDictionary new];
  69. [self.personView registerClass:[EditGroupViewCell class] forCellWithReuseIdentifier:@"EditGroupViewCell"];
  70. self.model = [ContactsModule new];
  71. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshAllContacts) name:@"refreshAllContacts" object:nil];
  72. UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveSelectItems)];
  73. super.navigationItem.rightBarButtonItem=item;
  74. self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
  75. self.searchController.delegate=self;
  76. self.searchController.searchResultsDataSource=self;
  77. self.searchController.searchResultsDelegate=self;
  78. self.hud = [[MBProgressHUD alloc] initWithView:self.view];
  79. [self.view addSubview:self.hud];
  80. self.hud.dimBackground = YES;
  81. self.hud.labelText=@"正在删除...";
  82. // Do any additional setup after loading the view from its nib.
  83. }
  84. -(void)viewWillDisappear:(BOOL)animated
  85. {
  86. [super viewWillDisappear:animated];
  87. }
  88. -(void)saveSelectItems
  89. {
  90. if (self.isCreat) {
  91. [self createGroup];
  92. }else
  93. {
  94. __block NSMutableArray *tempArray = [NSMutableArray new];
  95. [self.editArray enumerateObjectsUsingBlock:^(DDUserEntity *obj, NSUInteger idx, BOOL *stop) {
  96. if (![self.backupArray containsObject:obj]) {
  97. [tempArray addObject:obj];
  98. }
  99. }];
  100. if ([tempArray count] != 0) {
  101. [self addUsersToGroup:tempArray];
  102. }
  103. [self.backupArray removeObjectsInArray:self.editArray];
  104. if ([self.backupArray count] !=0) {
  105. [self deleteUserFromGroup];
  106. }
  107. }
  108. }
  109. -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
  110. if ([searchString isEqualToString:@""]) {
  111. return NO;
  112. }
  113. [[DDSearch instance] searchContent:searchString completion:^(NSArray *result, NSError *error) {
  114. self.searchResult=result;
  115. [self.self.searchDisplayController.searchResultsTableView reloadData];
  116. }];
  117. return YES;
  118. }
  119. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
  120. if ( [[self allKeys] count] == 0) {
  121. return NO;
  122. }
  123. [self.searchBar setShowsCancelButton:YES animated:YES];
  124. [self.searchController setActive:YES animated:YES];
  125. return YES;
  126. }
  127. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
  128. [self.searchBar resignFirstResponder];
  129. [self.searchBar setShowsCancelButton:NO animated:YES];
  130. [self.tableView reloadData];
  131. }
  132. -(void)refreshAllContacts
  133. {
  134. [self.hud removeFromSuperview];
  135. self.items = [self.model sortByContactFirstLetter];
  136. [self.model.contacts enumerateObjectsUsingBlock:^(DDUserEntity *obj1, NSUInteger idx, BOOL *stop) {
  137. [self.editArray enumerateObjectsUsingBlock:^(DDUserEntity *obj2, NSUInteger idx2, BOOL *stop) {
  138. if ([obj1.objID isEqualToString:obj2.objID]) {
  139. [self.editArray replaceObjectAtIndex:idx2 withObject:obj1];
  140. }
  141. }];
  142. }];
  143. self.backupArray =[NSMutableArray arrayWithArray:self.editArray];
  144. [self.tableView reloadData];
  145. }
  146. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  147. {
  148. return [self.editArray count];
  149. }
  150. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  151. {
  152. EditGroupViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"EditGroupViewCell" forIndexPath:indexPath];
  153. DDUserEntity *user = [self.editArray objectAtIndex:indexPath.row];
  154. [cell.name setText:user.nick];
  155. [cell.name setTextColor:GRAYCOLOR];
  156. [cell.personIcon sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:[UIImage imageNamed:@"user_placeholder"]];
  157. [cell.button setTitle:user.objID forState:UIControlStateNormal];
  158. cell.button.tag=indexPath.row;
  159. [cell.button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
  160. [cell.button addTarget:self action:@selector(deletePerson:) forControlEvents:UIControlEventTouchUpInside];
  161. return cell;
  162. }
  163. -(IBAction)deletePerson:(id)sender
  164. {
  165. UIButton *btn = (UIButton *)sender;
  166. __block BOOL isInSelectArray = NO;
  167. [self.selectedArray enumerateObjectsUsingBlock:^(DDUserEntity *obj, NSUInteger idx, BOOL *stop) {
  168. if ([obj.objID isEqualToString:btn.titleLabel.text]) {
  169. [self.selectedArray removeObjectAtIndex:idx];
  170. isInSelectArray=YES;
  171. [self.editArray enumerateObjectsUsingBlock:^(DDUserEntity *obj, NSUInteger idx, BOOL *stop) {
  172. if ([obj.objID isEqualToString:btn.titleLabel.text]) {
  173. [self.editArray removeObjectAtIndex:btn.tag];
  174. [self.personView reloadData];
  175. [self.tableView reloadData];
  176. }}];
  177. }
  178. }];
  179. if (isInSelectArray) {
  180. return;
  181. }
  182. [self.hud show:YES];
  183. [self.editArray enumerateObjectsUsingBlock:^(DDUserEntity *obj, NSUInteger idx, BOOL *stop) {
  184. if ([obj.objID isEqualToString:btn.titleLabel.text]) {
  185. if (!self.group) {
  186. [self.personView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:idx inSection:0]]];
  187. [self.editArray removeObjectAtIndex:idx];
  188. [self.tableView reloadData];
  189. }else
  190. {
  191. if (self.isGroupCreator) {
  192. DDDeleteMemberFromGroupAPI* deleteMemberAPI = [[DDDeleteMemberFromGroupAPI alloc] init];
  193. NSArray* array = @[self.sessionID,@[obj.objID]];
  194. [deleteMemberAPI requestWithObject:array Completion:^(id response, NSError *error) {
  195. if (response)
  196. {
  197. [self.hud hide:YES];
  198. [self.personView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:idx inSection:0]]];
  199. [self.editArray removeObjectAtIndex:idx];
  200. [self.tableView reloadData];
  201. }
  202. }];
  203. }else
  204. {
  205. [self showAlert:@"你不是该群的创建者,无法删除成员"];
  206. }
  207. }
  208. }
  209. }];
  210. [self.hud hide:YES];
  211. }
  212. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  213. {
  214. EditGroupViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  215. [cell showDeleteActionView];
  216. }
  217. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  218. return 5;
  219. }
  220. - (void)didReceiveMemoryWarning
  221. {
  222. [super didReceiveMemoryWarning];
  223. // Dispose of any resources that can be recreated.
  224. }
  225. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  226. {
  227. if (tableView.tag == 100) {
  228. return [[self.items allKeys] count];
  229. }
  230. return 1;
  231. }
  232. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  233. {
  234. if (tableView.tag == 100) {
  235. NSString *keyStr = [self allKeys][(NSUInteger) (section)];
  236. NSArray *arr = (self.items)[keyStr];
  237. return [arr count];
  238. }
  239. return [self.searchResult count];
  240. }
  241. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  242. return [self allKeys][section];
  243. }
  244. -(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
  245. NSMutableArray *arr = [NSMutableArray new];
  246. [[self allKeys] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  247. char firstLetter = getFirstChar((NSString *)obj);
  248. [arr addObject:[NSString stringWithFormat:@"%c",firstLetter]];
  249. }];
  250. return arr;
  251. }
  252. -(NSArray*)allKeys{
  253. return [[self.items allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  254. return [obj1 compare:obj2];
  255. }];
  256. }
  257. -(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  258. {
  259. return 55.0;
  260. }
  261. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  262. {
  263. static NSString *cellIdentifier = @"contactsCell";
  264. EditContactsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier ];
  265. cell.selectionStyle=UITableViewCellSelectionStyleNone;
  266. if (cell == nil) {
  267. cell = [[EditContactsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  268. }
  269. if (tableView.tag == 100) {
  270. NSString *keyStr = [[self allKeys] objectAtIndex:indexPath.section];
  271. NSArray *userArray =[self.items objectForKey:keyStr];
  272. DDUserEntity *user = [userArray objectAtIndex:indexPath.row];
  273. if ([self.editArray containsObject:user]) {
  274. [cell setCellToSelected:YES];
  275. }else
  276. {
  277. [cell setCellToSelected:NO];
  278. }
  279. cell.nameLabel.text=user.nick;
  280. UIImage* placeholder = [UIImage imageNamed:@"user_placeholder"];
  281. [cell.avatar sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:placeholder];
  282. return cell;
  283. }
  284. else
  285. {
  286. DDUserEntity *user =self.searchResult[indexPath.row];
  287. if ([self.editArray containsObject:user]) {
  288. [cell setCellToSelected:YES];
  289. }else
  290. {
  291. [cell setCellToSelected:NO];
  292. }
  293. cell.nameLabel.text=user.nick;
  294. UIImage* placeholder = [UIImage imageNamed:@"user_placeholder"];
  295. [cell.avatar sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:placeholder];
  296. return cell;
  297. }
  298. }
  299. -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
  300. {
  301. EditContactsCell *oneCell =(EditContactsCell *) [tableView cellForRowAtIndexPath: indexPath];
  302. NSString *keyStr = [[self allKeys] objectAtIndex:indexPath.section];
  303. NSArray *userArray =[self.items objectForKey:keyStr];
  304. DDUserEntity *user = [userArray objectAtIndex:indexPath.row];
  305. if ([self.selectedArray containsObject:user]) {
  306. [oneCell setCellToSelected:YES];
  307. }else
  308. {
  309. [oneCell setCellToSelected:NO];
  310. }
  311. }
  312. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  313. {
  314. //[tableView deselectRowAtIndexPath:indexPath animated:NO];
  315. DDUserEntity *user;
  316. if (tableView.tag == 100) {
  317. NSString *keyStr = [[self allKeys] objectAtIndex:indexPath.section];
  318. NSArray *userArray =[self.items objectForKey:keyStr];
  319. user = [userArray objectAtIndex:indexPath.row];
  320. }else
  321. {
  322. user = self.searchResult[indexPath.row];
  323. [self.searchController setActive:NO animated:NO];
  324. }
  325. EditContactsCell *oneCell =(EditContactsCell *) [tableView cellForRowAtIndexPath: indexPath];
  326. if (![self.editArray containsObject:user])
  327. {
  328. [oneCell setCellToSelected:YES];
  329. [self.editArray addObject:user];
  330. [self.selectedArray addObject:user];
  331. [self.personView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:[self.editArray count]-1 inSection:0]]];
  332. [self.personView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:[self.editArray count]-1 inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
  333. [UIView animateWithDuration:0.5 animations:^{
  334. self.label.alpha=0.0;
  335. }];
  336. }
  337. else
  338. {
  339. [oneCell setCellToSelected:NO];
  340. NSUInteger index =[self.editArray indexOfObject:user];
  341. [self.editArray removeObjectAtIndex:index];
  342. [self.selectedArray removeObject:user];
  343. [self.personView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]]];
  344. if ([self.editArray count] == 0) {
  345. [UIView animateWithDuration:0.5 animations:^{
  346. self.label.alpha=1.0;
  347. }];
  348. }
  349. }
  350. }
  351. -(void)deleteUserFromGroup
  352. {
  353. __block NSMutableArray *userIDs = [NSMutableArray new];
  354. [self.backupArray enumerateObjectsUsingBlock:^(DDUserEntity *obj, NSUInteger idx, BOOL *stop) {
  355. [userIDs addObject:obj.objID];
  356. }];
  357. DDDeleteMemberFromGroupAPI* deleteMemberAPI = [[DDDeleteMemberFromGroupAPI alloc] init];
  358. NSArray* array = @[self.sessionID,userIDs];
  359. [deleteMemberAPI requestWithObject:array Completion:^(DDGroupEntity *response, NSError *error) {
  360. if (error) {
  361. [self showAlert:error.domain?error.domain:@"未知错误"];
  362. }
  363. if (response)
  364. {
  365. [self.hud hide:YES];
  366. [self.tableView reloadData];
  367. [self.navigationController popToViewController:self.editControll animated:YES];
  368. [self.editControll refreshUsers:self.editArray];
  369. }else
  370. {
  371. [[DDGroupModule instance] getGroupInfogroupID:self.sessionID completion:^(DDGroupEntity *group) {
  372. [[DDGroupModule instance] addGroup:group];
  373. [self.hud hide:YES];
  374. [self.tableView reloadData];
  375. [self.navigationController popToViewController:self.editControll animated:YES];
  376. [self.editControll refreshUsers:self.editArray];
  377. }];
  378. }
  379. }];
  380. }
  381. -(void)addUsersToGroup:(NSMutableArray *)users
  382. {
  383. DDAddMemberToGroupAPI *addMember = [[DDAddMemberToGroupAPI alloc] init];
  384. __block NSMutableArray *userIDs = [NSMutableArray new];
  385. [users enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  386. DDUserEntity *user = (DDUserEntity *)obj;
  387. if (user.objID) {
  388. [userIDs addObject:user.objID];
  389. }
  390. }];
  391. [addMember requestWithObject:@[self.sessionID,userIDs] Completion:^(DDGroupEntity * response, NSError *error) {
  392. if (response != nil) {
  393. [self.navigationController popToViewController:self.editControll animated:YES];
  394. [self.editControll refreshUsers:self.editArray];
  395. }else
  396. {
  397. [self showAlert:error.domain?error.domain:@"未知错误"];
  398. }
  399. }];
  400. }
  401. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  402. {
  403. if (buttonIndex == 1) {
  404. UITextField *tf=[alertView textFieldAtIndex:0];
  405. if(tf.text.length !=0)
  406. {
  407. }
  408. DDCreateGroupAPI *creatGroup = [[DDCreateGroupAPI alloc] init];
  409. __block NSMutableArray *userIDs = [NSMutableArray new];
  410. [userIDs addObject:TheRuntime.user.objID];
  411. [self.editArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  412. DDUserEntity *user = (DDUserEntity *)obj;
  413. if (user.objID) {
  414. [userIDs addObject:user.objID];
  415. }
  416. }];
  417. NSString *groupName = tf.text.length !=0?tf.text:[self creatGroupName];
  418. NSArray *array =@[groupName,@"",userIDs];
  419. [creatGroup requestWithObject:array Completion:^(DDGroupEntity * response, NSError *error) {
  420. if (response !=nil) {
  421. [[DDGroupModule instance] addGroup:response];
  422. [[DDGroupModule instance] addRecentlyGroup:@[response]];
  423. [[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshRecentData" object:nil];
  424. [self.editControll refreshUsers:self.editArray];
  425. self.editControll.session.sessionID=response.objID;
  426. self.editControll.session.sessionType=SESSIONTYPE_TEMP_GROUP;
  427. DDSessionEntity *session = [[DDSessionEntity alloc] initWithSessionID:response.objID type:SESSIONTYPE_TEMP_GROUP];
  428. [ChattingMainViewController shareInstance].module.sessionEntity =session;
  429. [ChattingMainViewController shareInstance].title=response.name;
  430. [self.navigationController popToViewController:[ChattingMainViewController shareInstance] animated:YES];
  431. }else
  432. {
  433. [self showAlert:error.domain?error.domain:@"未知错误"];
  434. }
  435. }];
  436. }
  437. }
  438. -(void)createGroup
  439. {
  440. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"创建群" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
  441. alert.alertViewStyle=UIAlertViewStylePlainTextInput;
  442. [alert show];
  443. }
  444. -(void)showAlert:(NSString *)string
  445. {
  446. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:string delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  447. [alert show];
  448. }
  449. -(NSString *)creatGroupName
  450. {
  451. NSMutableString *string= [NSMutableString new];
  452. [self.selectedArray enumerateObjectsUsingBlock:^(DDUserEntity *obj, NSUInteger idx, BOOL *stop) {
  453. [string appendFormat:@"%@,",obj.name];
  454. if (idx == 3) {
  455. *stop=YES;
  456. }
  457. }];
  458. return string;
  459. }
  460. @end