PageRenderTime 70ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/TeamTalk/IOSDuoduo/VC/ContactsViewController.m

https://gitlab.com/lisit1003/TTiOSClient
Objective C | 443 lines | 389 code | 36 blank | 18 comment | 50 complexity | 7913a9767acbe12521958733b7543608 MD5 | raw file
  1. //
  2. // ContactsViewController.m
  3. // IOSDuoduo
  4. //
  5. // Created by Michael Scofield on 2014-07-15.
  6. // Copyright (c) 2014 dujia. All rights reserved.
  7. //
  8. #import "ContactsViewController.h"
  9. #import "std.h"
  10. #import "PublicProfileViewControll.h"
  11. #import "ContactsModule.h"
  12. #import "DDGroupEntity.h"
  13. #import "DDSearch.h"
  14. #import "ContactAvatarTools.h"
  15. #import "DDContactsCell.h"
  16. #import "DDUserDetailInfoAPI.h"
  17. #import "DDGroupModule.h"
  18. #import "ChattingMainViewController.h"
  19. #import "SearchContentViewController.h"
  20. #import "MBProgressHUD.h"
  21. #import "DDFixedGroupAPI.h"
  22. @interface ContactsViewController ()
  23. @property(strong)UISegmentedControl *seg;
  24. @property(strong)NSMutableDictionary *items;
  25. @property(strong)NSMutableDictionary *keys;
  26. @property(strong)ContactsModule *model;
  27. @property(strong)NSMutableArray *groups;
  28. @property(strong)NSArray *searchResult;
  29. @property(strong)UITableView *tableView;
  30. @property(strong)UISearchBar *searchBar;
  31. @property(strong)ContactAvatarTools *tools;
  32. @property(strong)UISearchDisplayController *searchController;
  33. @property(strong)MBProgressHUD *hud;
  34. @property(assign)int selectIndex;
  35. @end
  36. @implementation ContactsViewController
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. self.edgesForExtendedLayout = UIRectEdgeNone;
  41. self.extendedLayoutIncludesOpaqueBars = NO;
  42. self.hud = [[MBProgressHUD alloc] initWithView:self.view];
  43. [self.view addSubview:self.hud];
  44. self.hud.dimBackground = YES;
  45. self.hud.labelText=@"正在加载...";
  46. [self.hud show:YES];
  47. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshAllContacts) name:@"refreshAllContacts" object:nil];
  48. self.groups = [NSMutableArray new];
  49. self.title=@"联系人";
  50. self.model = [ContactsModule new];
  51. self.searchResult = [NSArray new];
  52. self.seg = [[UISegmentedControl alloc] initWithItems:@[@"全部",@"部门"]];
  53. self.seg.selectedSegmentIndex=0;
  54. self.seg.frame=CGRectMake(80.0f, 8.0f, 200.0f, 30.0f);
  55. [self.seg addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
  56. self.navigationItem.titleView=self.seg;
  57. self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
  58. [self.searchBar setPlaceholder:@"搜索"];
  59. [self.searchBar setBarTintColor:RGB(242, 242, 244)];
  60. self.searchBar.delegate=self;
  61. [self.searchBar setBarStyle:UIBarStyleDefault];
  62. [self.view addSubview:self.searchBar];
  63. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 320,(self.tabBarController.tabBar.isHidden?self.view.frame.size.height:self.view.frame.size.height-152))];
  64. self.tableView.delegate=self;
  65. self.tableView.tag=100;
  66. self.tableView.dataSource=self;
  67. [self.view addSubview:self.tableView];
  68. DDFixedGroupAPI *fixedGroupApi = [DDFixedGroupAPI new];
  69. [fixedGroupApi requestWithObject:nil Completion:^(NSArray *response, NSError *error) {
  70. [self.groups addObjectsFromArray:response];
  71. [self.tableView reloadData];
  72. }];
  73. // NSArray *array = [[DDGroupModule instance] getAllGroups];
  74. // [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  75. // DDGroupEntity *group = (DDGroupEntity *)obj;
  76. // if (group.groupType == GROUP_TYPE_FIXED) {
  77. // [self.groups addObject:group];
  78. // }
  79. // }];
  80. }
  81. -(void)scrollToTitle:(NSNotification *)notification
  82. {
  83. NSString *string = [notification object];
  84. self.sectionTitle=string;
  85. }
  86. -(void)viewWillDisappear:(BOOL)animated
  87. {
  88. [super viewWillDisappear:animated];
  89. self.sectionTitle=nil;
  90. }
  91. -(void)viewWillAppear:(BOOL)animated
  92. {
  93. [super viewWillAppear:animated];
  94. if (self.isSearchResult) {
  95. [self.tabBarController.tabBar setHidden:YES];
  96. }else
  97. {
  98. [self.tabBarController.tabBar setHidden:NO];
  99. }
  100. if (self.sectionTitle) {
  101. [self.seg setSelectedSegmentIndex:1];
  102. self.selectIndex=1;
  103. [self swichToShowDepartment];
  104. if ([self.allKeys count]) {
  105. int location = [self.allKeys indexOfObject:self.sectionTitle];
  106. [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:location] atScrollPosition:UITableViewScrollPositionTop animated:YES];
  107. }
  108. return;
  109. }
  110. }
  111. -(void)refreshAllContacts
  112. {
  113. if (self.sectionTitle) {
  114. [self.seg setSelectedSegmentIndex:1];
  115. self.selectIndex=1;
  116. [self swichToShowDepartment];
  117. int location = [self.allKeys indexOfObject:self.sectionTitle];
  118. [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:location] atScrollPosition:UITableViewScrollPositionTop animated:YES];
  119. return;
  120. }
  121. switch (self.selectIndex) {
  122. case 0:
  123. [self swichContactsToALl];
  124. break;
  125. case 1:
  126. [self swichToShowDepartment];
  127. default:
  128. break;
  129. }
  130. }
  131. -(void)viewDidAppear:(BOOL)animated
  132. {
  133. [super viewDidAppear:animated];
  134. }
  135. -(void)swichContactsToALl
  136. {
  137. //[self.items removeAllObjects];
  138. self.items = [self.model sortByContactFirstLetter];
  139. [self.tableView reloadData];
  140. }
  141. -(void)swichToShowDepartment
  142. {
  143. // [self.items removeAllObjects];
  144. self.items = [self.model sortByDepartment];
  145. [self.tableView reloadData];
  146. }
  147. -(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
  148. NSMutableArray* array = [[NSMutableArray alloc] init];
  149. if (self.selectIndex == 1) {
  150. [[self allKeys] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  151. char firstLetter = getFirstChar((NSString *)obj);
  152. [array addObject:[[NSString stringWithFormat:@"%c",firstLetter] uppercaseString]];
  153. }];
  154. }
  155. else
  156. {
  157. NSArray* allKeys = [self allKeys];
  158. [allKeys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  159. [array addObject:[obj uppercaseString]];
  160. }];
  161. }
  162. return array;
  163. }
  164. -(IBAction)segmentAction:(UISegmentedControl *)sender
  165. {
  166. int index = sender.selectedSegmentIndex;
  167. switch (index) {
  168. case 0:
  169. self.selectIndex=0;
  170. [self swichContactsToALl];
  171. break;
  172. case 1:
  173. self.selectIndex=1;
  174. [self swichToShowDepartment];
  175. default:
  176. break;
  177. }
  178. }
  179. - (void)didReceiveMemoryWarning
  180. {
  181. [super didReceiveMemoryWarning];
  182. // Dispose of any resources that can be recreated.
  183. }
  184. #pragma mark - Table view data source
  185. -(NSArray*)allKeys{
  186. return [[self.items allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  187. return [obj1 compare:obj2];
  188. }];
  189. }
  190. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  191. {
  192. if (self.selectIndex == 0) {
  193. return [[self.items allKeys] count]+2;
  194. }
  195. return [[self.items allKeys] count];
  196. }
  197. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  198. {
  199. if (self.selectIndex == 0) {
  200. if (section == 0) {
  201. return [self.groups count];
  202. }else if (section == 1)
  203. {
  204. NSLog(@"%d........",[[ContactsModule getFavContact] count]);
  205. return [[ContactsModule getFavContact] count];
  206. }
  207. else
  208. {
  209. NSString *keyStr = [self allKeys][(NSUInteger) (section - 2)];
  210. NSArray *arr = (self.items)[keyStr];
  211. return [arr count];
  212. }
  213. }
  214. NSString *keyStr = [self allKeys][(NSUInteger) (section)];
  215. NSArray *arr = (self.items)[keyStr];
  216. return [arr count];
  217. }
  218. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  219. if (self.selectIndex == 0) {
  220. if (section == 0) {
  221. return @"群";
  222. }else if (section == 1)
  223. {
  224. return @"收藏";
  225. }
  226. return [self.allKeys[section - 2] uppercaseString];
  227. }else
  228. {
  229. return [self.allKeys[section] uppercaseString];
  230. }
  231. }
  232. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  233. {
  234. static NSString *cellIdentifier = @"contactsCell";
  235. DDContactsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier ];
  236. if (cell == nil) {
  237. cell = [[DDContactsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  238. }
  239. if (self.selectIndex == 0) {
  240. if (indexPath.section == 0) {
  241. DDGroupEntity *group = [self.groups objectAtIndex:indexPath.row];
  242. cell.nameLabel.text=group.name;
  243. UIImage* placeholder = [UIImage imageNamed:@"user_placeholder"];
  244. [cell setGroupAvatar:group];
  245. }else if (indexPath.section == 1)
  246. {
  247. NSArray *arr = [ContactsModule getFavContact];
  248. DDUserEntity *user = [arr objectAtIndex:indexPath.row];
  249. NSString *name =user.nick?user.nick:user.name;
  250. [cell setCellContent:user.avatar Name:name];
  251. }else
  252. {
  253. NSString *keyStr = [[self allKeys] objectAtIndex:indexPath.section-2];
  254. NSArray *userArray =[self.items objectForKey:keyStr];
  255. DDUserEntity *user = [userArray objectAtIndex:indexPath.row];
  256. [cell setCellContent:user.avatar Name:user.nick];
  257. cell.button.tag=indexPath.row;
  258. [cell.button setTitle:keyStr forState:UIControlStateNormal];
  259. [cell.button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
  260. [cell.button addTarget:self action:@selector(showActions:) forControlEvents:UIControlEventTouchUpInside];
  261. }
  262. }else
  263. {
  264. NSString *keyStr = [[self allKeys] objectAtIndex:indexPath.section];
  265. NSArray *userArray =[self.items objectForKey:keyStr];
  266. DDUserEntity *user = [userArray objectAtIndex:indexPath.row];
  267. [cell setCellContent:user.avatar Name:user.nick];
  268. cell.button.tag=indexPath.row;
  269. [cell.button setTitle:keyStr forState:UIControlStateNormal];
  270. [cell.button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
  271. [cell.button addTarget:self action:@selector(showActions:) forControlEvents:UIControlEventTouchUpInside];
  272. }
  273. return cell;
  274. }
  275. -(IBAction)showActions:(id)sender
  276. {
  277. if (self.tools.isShow) {
  278. [self.tools hiddenSelf];
  279. }
  280. UIButton *btn = (UIButton *)sender;
  281. NSArray *userArray =[self.items objectForKey:btn.titleLabel.text];
  282. DDBaseEntity *user = [userArray objectAtIndex:btn.tag];
  283. CGRect rect = [self.tableView convertRect:self.tableView.frame fromView:btn];
  284. self.tools = [[ContactAvatarTools alloc] initWithFrame:CGRectMake(rect.origin.x+btn.frame.size.width+5, rect.origin.y-70, 100, 100)];
  285. __weak ContactsViewController *weakSelf = self;
  286. if ([user isKindOfClass:[DDUserEntity class]]) {
  287. __block DDUserEntity *newUser;
  288. DDUserDetailInfoAPI* detailInfoAPI = [[DDUserDetailInfoAPI alloc] init];
  289. [detailInfoAPI requestWithObject:@[((DDUserEntity *)user).objID] Completion:^(id response, NSError *error) {
  290. if ([response count] > 0)
  291. {
  292. NSDictionary* userInfo = response[0];
  293. newUser = [DDUserEntity dicToUserEntity:userInfo];
  294. }
  295. else
  296. {
  297. }
  298. }];
  299. self.tools.block=^(int index){
  300. switch (index) {
  301. case 1:
  302. [weakSelf callNum:newUser];
  303. break;
  304. case 2:
  305. [weakSelf sendEmail:newUser];
  306. break;
  307. case 3:
  308. [weakSelf chatTo:newUser];
  309. default:
  310. break;
  311. }
  312. };
  313. }
  314. [self.tableView addSubview:self.tools];
  315. }
  316. -(void)callNum:(DDUserEntity *)user
  317. {
  318. if (user == nil) {
  319. return;
  320. }
  321. NSString *string = [NSString stringWithFormat:@"tel:%@",user.telphone];
  322. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
  323. }
  324. -(void)sendEmail:(DDUserEntity *)user
  325. {
  326. if (user == nil) {
  327. return;
  328. }
  329. NSString *string = [NSString stringWithFormat:@"mailto:%@",user.email];
  330. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
  331. }
  332. -(void)chatTo:(DDUserEntity *)user
  333. {
  334. if (user == nil) {
  335. return;
  336. }
  337. DDSessionEntity* session = [[DDSessionEntity alloc] initWithSessionID:user.objID type:SESSIONTYPE_SINGLE];
  338. [session setSessionName:user.nick];
  339. [[ChattingMainViewController shareInstance] showChattingContentForSession:session];
  340. [self.navigationController pushViewController:[ChattingMainViewController shareInstance] animated:YES];
  341. }
  342. -(void)scrollViewDidScroll:(UIScrollView *)scrollView
  343. {
  344. if (self.tools.isShow) {
  345. [self.tools hiddenSelf];
  346. }
  347. }
  348. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  349. {
  350. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  351. if (self.tools.isShow) {
  352. [self.tools hiddenSelf];
  353. return;
  354. }
  355. if (self.selectIndex == 0) {
  356. if (indexPath.section == 0) {
  357. DDGroupEntity *group = [self.groups objectAtIndex:indexPath.row];
  358. DDSessionEntity *session = [[DDSessionEntity alloc] initWithSessionID:group.objID type:SESSIONTYPE_GROUP];
  359. [session setSessionName:group.name];
  360. ChattingMainViewController *main = [ChattingMainViewController shareInstance];
  361. [main showChattingContentForSession:session];
  362. [self.navigationController pushViewController:main animated:YES];
  363. return;
  364. }
  365. if (indexPath.section == 1) {
  366. DDUserEntity *user;
  367. user = [ContactsModule getFavContact][indexPath.row];
  368. PublicProfileViewControll *public = [PublicProfileViewControll new];
  369. public.user=user;
  370. [self.navigationController pushViewController:public animated:YES];
  371. return;
  372. }
  373. NSString *keyStr = [[self allKeys] objectAtIndex:indexPath.section-2];
  374. NSArray *userArray =[self.items objectForKey:keyStr];
  375. DDUserEntity *user;
  376. user = [userArray objectAtIndex:indexPath.row];
  377. PublicProfileViewControll *public = [PublicProfileViewControll new];
  378. public.user=user;
  379. [self.navigationController pushViewController:public animated:YES];
  380. }else
  381. {
  382. NSString *keyStr = [[self allKeys] objectAtIndex:indexPath.section];
  383. NSArray *userArray =[self.items objectForKey:keyStr];
  384. DDUserEntity *user;
  385. user = [userArray objectAtIndex:indexPath.row];
  386. PublicProfileViewControll *public = [PublicProfileViewControll new];
  387. public.user=user;
  388. [self.navigationController pushViewController:public animated:YES];
  389. }
  390. }
  391. -(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  392. {
  393. return 50;
  394. }
  395. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
  396. {
  397. [self.navigationController pushViewController:[SearchContentViewController new] animated:YES];
  398. return NO;
  399. }
  400. -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  401. {
  402. if (self == [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  403. // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollToTitle:) name:@"SearchDerpartment" object:nil];
  404. }
  405. return self;
  406. }
  407. @end