JXAddressBookVC.m 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. //
  2. // JXAddressBookVC.m
  3. // shiku_im
  4. //
  5. // Created by p on 2018/8/30.
  6. // Copyright © 2018年 Reese. All rights reserved.
  7. //
  8. #import "JXAddressBookVC.h"
  9. #import "BMChineseSort.h"
  10. #import "JXAddressBookCell.h"
  11. #import "JXUserInfoVC.h"
  12. #import "JXNewRoomVC.h"
  13. #import "JXSelectFriendsVC.h"
  14. #import "JXRoomPool.h"
  15. #import "JXInviteAddressBookVC.h"
  16. @interface JXAddressBookVC ()<JXAddressBookCellDelegate, QCheckBoxDelegate, UIAlertViewDelegate>
  17. @property (nonatomic, assign) BOOL isShowSelect;
  18. @property (nonatomic, assign) BOOL isAllSelect;
  19. @property (nonatomic, strong) NSMutableArray *selectABs;
  20. @property (nonatomic, strong) UIView *doneBtn;
  21. @property (nonatomic, strong) UIButton *selectBtn;
  22. @property (nonatomic, strong) NSDictionary *phoneNumDict;
  23. @property (nonatomic, strong) NSMutableArray *headerCheckBoxs;
  24. @property (nonatomic, strong) NSMutableArray *allAbArr;
  25. @property (nonatomic, copy) NSString *selectUserIds;
  26. @property (nonatomic, copy) NSString *selectUserNames;
  27. @property (nonatomic, strong) JXUserObject *addressBookUser;
  28. @end
  29. @implementation JXAddressBookVC
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. // Do any additional setup after loading the view.
  33. self.heightHeader = JX_SCREEN_TOP;
  34. self.heightFooter = 0;
  35. self.isGotoBack = YES;
  36. //self.view.frame = g_window.bounds;
  37. self.isShowFooterPull = NO;
  38. [self createHeadAndFoot];
  39. _phoneNumDict = [[JXAddressBook sharedInstance] getMyAddressBook];
  40. _headerCheckBoxs = [NSMutableArray array];
  41. self.title = Localized(@"JX_MobilePhoneContacts");
  42. _array = [NSMutableArray array];
  43. _indexArray = [NSMutableArray array];
  44. _letterResultArr = [NSMutableArray array];
  45. _selectABs = [NSMutableArray array];
  46. _allAbArr = [NSMutableArray array];
  47. self.isShowSelect = NO;
  48. _selectBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  49. [_selectBtn setTitle:Localized(@"JX_BatchAddition") forState:UIControlStateNormal];
  50. [_selectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  51. _selectBtn.tintColor = [UIColor clearColor];
  52. _selectBtn.frame = CGRectMake(JX_SCREEN_WIDTH - 62-15, JX_SCREEN_TOP - 30, 62, 15);
  53. _selectBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  54. [_selectBtn addTarget:self action:@selector(selectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  55. [self.tableHeader addSubview:_selectBtn];
  56. _doneBtn = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - JX_SCREEN_BOTTOM, JX_SCREEN_WIDTH, JX_SCREEN_BOTTOM)];
  57. _doneBtn.backgroundColor = HEXCOLOR(0xf0f0f0);
  58. _doneBtn.hidden = YES;
  59. [self.view addSubview:_doneBtn];
  60. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 49)];
  61. [btn setTitle:Localized(@"JX_Confirm") forState:UIControlStateNormal];
  62. [btn setTitleColor:HEXCOLOR(0x55BEB8) forState:UIControlStateNormal];
  63. [btn addTarget:self action:@selector(doneBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  64. btn.backgroundColor = HEXCOLOR(0xf0f0f0);
  65. [_doneBtn addSubview:btn];
  66. [self getServerData];
  67. [self createTableHeadView];
  68. if (self.abUreadArr.count > 0) {
  69. [self createHeaderView:self.abUreadArr];
  70. NSMutableArray *arr = [[JXUserObject sharedInstance] fetchRoomsFromLocalWithCategory:[NSNumber numberWithInteger:510]];
  71. for (NSInteger i = 0; i < arr.count; i ++) {
  72. JXUserObject *user = arr[i];
  73. if ([user.createUserId isEqualToString:MY_USER_ID]) {
  74. self.addressBookUser = user;
  75. break;
  76. }
  77. }
  78. if (self.addressBookUser) {
  79. [g_App showAlert:Localized(@"JX_InviteJoinMobileContactGroup") delegate:self tag:2458 onlyConfirm:NO];
  80. }else {
  81. [g_App showAlert:Localized(@"JX_CreateMobileContactGroup") delegate:self tag:2457 onlyConfirm:NO];
  82. }
  83. }
  84. }
  85. - (void)createTableHeadView {
  86. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 65)];
  87. [btn addTarget:self action:@selector(inviteFriend) forControlEvents:UIControlEventTouchUpInside];
  88. self.tableView.tableHeaderView = btn;
  89. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(23, 23, 20, 20)];
  90. imageView.image = [UIImage imageNamed:@"ic_ct_msg_invite"];
  91. [btn addSubview:imageView];
  92. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 10, 0, 200, btn.frame.size.height)];
  93. label.text = Localized(@"JX_InviteFriends");
  94. [btn addSubview:label];
  95. }
  96. - (void)inviteFriend {
  97. JXInviteAddressBookVC *vc = [[JXInviteAddressBookVC alloc] init];
  98. [g_navigation pushViewController:vc animated:YES];
  99. }
  100. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  101. if (buttonIndex == 1) {
  102. if (alertView.tag == 2457) {
  103. if ([g_config.isCommonCreateGroup intValue] == 1) {
  104. [g_App showAlert:Localized(@"JX_NotCreateNewRoom")];
  105. return;
  106. }
  107. JXNewRoomVC *vc = [[JXNewRoomVC alloc] init];
  108. vc.isAddressBook = YES;
  109. vc.addressBookArr = [_allAbArr mutableCopy];
  110. vc.roomName.text = [NSString stringWithFormat:@"%@%@",g_server.myself.userNickname,Localized(@"JX_MyCellPhoneContactGroup")];
  111. [g_navigation pushViewController:vc animated:YES];
  112. }
  113. if (alertView.tag == 2458) {
  114. JXSelectFriendsVC* vc = [JXSelectFriendsVC alloc];
  115. vc.isNewRoom = NO;
  116. vc.chatRoom = [[JXXMPP sharedInstance].roomPool joinRoom:self.addressBookUser.userId title:self.addressBookUser.userNickname lastDate:nil isNew:NO];
  117. NSDictionary * groupDict = [self.addressBookUser toDictionary];
  118. roomData * roomdata = [[roomData alloc] init];
  119. [roomdata getDataFromDict:groupDict];
  120. vc.room = roomdata;
  121. vc.delegate = self;
  122. vc.didSelect = @selector(onAfterAddMember:);
  123. NSMutableArray *arr = [NSMutableArray array];
  124. NSMutableSet *existSet = [NSMutableSet set];
  125. for (NSInteger i = 0; i < self.abUreadArr.count; i ++) {
  126. JXAddressBook *ab = self.abUreadArr[i];
  127. JXUserObject *user = [[JXUserObject alloc] init];
  128. user.userId = ab.toUserId;
  129. user.userNickname = ab.toUserName;
  130. [arr addObject:user];
  131. [existSet addObject:ab.toUserId];
  132. }
  133. vc.existSet = [existSet copy];
  134. vc.addressBookArr = arr;
  135. vc = [vc init];
  136. [g_navigation pushViewController:vc animated:YES];
  137. }
  138. }
  139. }
  140. -(void)onAfterAddMember:(JXSelectFriendsVC*)vc{
  141. [JXMyTools showTipView:Localized(@"JX_InvitingSuccess")];
  142. }
  143. - (void)createHeaderView:(NSMutableArray *)abUread {
  144. [_headerCheckBoxs removeAllObjects];
  145. UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, (abUread.count + 1) * 64 + 30)];
  146. headerView.backgroundColor = HEXCOLOR(0xF2F2F2);
  147. self.tableView.tableHeaderView = headerView;
  148. UIButton *inviteBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 65)];
  149. [inviteBtn addTarget:self action:@selector(inviteFriend) forControlEvents:UIControlEventTouchUpInside];
  150. inviteBtn.backgroundColor = [UIColor whiteColor];
  151. [headerView addSubview:inviteBtn];
  152. UIImageView *inviteImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 25, 25)];
  153. inviteImageView.image = [UIImage imageNamed:@"ic_ct_msg_invite"];
  154. [inviteBtn addSubview:inviteImageView];
  155. UILabel *inviteLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(inviteImageView.frame) + 10, 0, 200, inviteBtn.frame.size.height)];
  156. inviteLabel.text = Localized(@"JX_InviteFriends");
  157. [inviteBtn addSubview:inviteLabel];
  158. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(inviteBtn.frame), JX_SCREEN_WIDTH, 30)];
  159. view.backgroundColor = HEXCOLOR(0xF2F2F2);
  160. [headerView addSubview:view];
  161. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, JX_SCREEN_WIDTH, 30)];
  162. // label.backgroundColor = HEXCOLOR(0xf0eff4);
  163. label.text = Localized(@"JX_LatestContacts");
  164. label.font = [UIFont systemFontOfSize:16.0];
  165. [view addSubview:label];
  166. CGFloat y = CGRectGetMaxY(view.frame);
  167. for (NSInteger i = 0; i < abUread.count; i ++) {
  168. JXAddressBook *ab = abUread[i];
  169. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, y, JX_SCREEN_WIDTH, 64)];
  170. btn.backgroundColor = [UIColor whiteColor];
  171. btn.tag = i;
  172. [btn addTarget:self action:@selector(headerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  173. [headerView addSubview:btn];
  174. QCheckBox *checkBox ;
  175. if (self.isShowSelect) {
  176. checkBox = [[QCheckBox alloc] initWithDelegate:self];
  177. checkBox.frame = CGRectMake(15, 22, 20, 20);
  178. checkBox.tag = i;
  179. checkBox.selected = self.isAllSelect;
  180. checkBox.delegate = self;
  181. [btn addSubview:checkBox];
  182. [_headerCheckBoxs addObject:checkBox];
  183. }
  184. [self addressBookCell:nil checkBoxSelectIndexNum:btn.tag isSelect:checkBox.selected];
  185. CGFloat headImageX;
  186. if (checkBox) {
  187. headImageX = CGRectGetMaxX(checkBox.frame) + 10;
  188. }else {
  189. headImageX = 15;
  190. }
  191. JXImageView *headImage = [[JXImageView alloc]init];
  192. headImage.userInteractionEnabled = NO;
  193. headImage.tag = i;
  194. // headImage.delegate = self;
  195. // headImage.didTouch = @selector(headImageDidTouch);
  196. headImage.frame = CGRectMake(headImageX,5,52,52);
  197. headImage.layer.cornerRadius = 26;
  198. headImage.layer.masksToBounds = YES;
  199. headImage.layer.borderColor = [UIColor darkGrayColor].CGColor;
  200. [g_server getHeadImageLarge:ab.toUserId userName:ab.toUserName imageView:headImage];
  201. [btn addSubview:headImage];
  202. UILabel *nickName = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(headImage.frame) + 10, 10, 300, 20)];
  203. nickName.textColor = HEXCOLOR(0x323232);
  204. nickName.userInteractionEnabled = NO;
  205. nickName.text = ab.addressBookName;
  206. nickName.backgroundColor = [UIColor clearColor];
  207. nickName.font = [UIFont systemFontOfSize:16];
  208. nickName.tag = i;
  209. [btn addSubview:nickName];
  210. UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(headImage.frame) + 10, CGRectGetMaxY(nickName.frame) + 5, 300, 20)];
  211. name.textColor = [UIColor lightGrayColor];
  212. name.userInteractionEnabled = NO;
  213. name.text = [NSString stringWithFormat:@"%@:%@",APP_NAME,ab.toUserName];
  214. name.backgroundColor = [UIColor clearColor];
  215. name.font = [UIFont systemFontOfSize:14];
  216. name.tag = i;
  217. [btn addSubview:name];
  218. UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH-70, 20, 50, 24)];
  219. [addBtn setBackgroundColor:HEXCOLOR(0x4FC557)];
  220. [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  221. [addBtn setTitleColor:HEXCOLOR(0xdcdcdc) forState:UIControlStateDisabled];
  222. addBtn.titleLabel.textColor = [UIColor whiteColor];
  223. addBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
  224. [addBtn setTitle:Localized(@"JX_Add") forState:UIControlStateNormal];
  225. [addBtn setTitle:Localized(@"JX_AlreadyAdded") forState:UIControlStateDisabled];
  226. [addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  227. addBtn.layer.cornerRadius = 3.0;
  228. addBtn.layer.masksToBounds = YES;
  229. addBtn.tag = i;
  230. [btn addSubview:addBtn];
  231. JXUserObject *user = [[JXUserObject sharedInstance] getUserById:ab.toUserId];
  232. if ([user.status intValue] == 2) {
  233. addBtn.enabled = NO;
  234. checkBox.hidden = YES;
  235. addBtn.backgroundColor = [UIColor clearColor];
  236. }else {
  237. addBtn.enabled = YES;
  238. checkBox.hidden = NO;
  239. addBtn.backgroundColor = HEXCOLOR(0x4FC557);
  240. }
  241. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 63.5, JX_SCREEN_WIDTH, LINE_WH)];
  242. line.backgroundColor = THE_LINE_COLOR;
  243. [btn addSubview:line];
  244. y += 64;
  245. }
  246. }
  247. - (void)headerBtnAction:(UIButton *)btn {
  248. JXAddressBook *ad = self.abUreadArr[btn.tag];
  249. if (self.isShowSelect) {
  250. JXUserObject *user = [[JXUserObject sharedInstance] getUserById:ad.toUserId];
  251. if ([user.status intValue] != 2) {
  252. QCheckBox *selCheckBox;
  253. for (NSInteger i = 0; i < _headerCheckBoxs.count; i ++) {
  254. QCheckBox *checkBox = _headerCheckBoxs[i];
  255. if (checkBox.tag == btn.tag) {
  256. selCheckBox = checkBox;
  257. break;
  258. }
  259. }
  260. selCheckBox.selected = !selCheckBox.selected;
  261. [self addressBookCell:nil checkBoxSelectIndexNum:btn.tag isSelect:selCheckBox.selected];
  262. }
  263. }else {
  264. JXUserInfoVC* vc = [JXUserInfoVC alloc];
  265. vc.userId = ad.toUserId;
  266. vc.fromAddType = 6;
  267. vc = [vc init];
  268. [g_navigation pushViewController:vc animated:YES];
  269. }
  270. }
  271. - (void)addBtnAction:(UIButton *)btn {
  272. JXAddressBook *ab = self.abUreadArr[btn.tag];
  273. [self addressBookCell:nil addBtnAction:ab];
  274. }
  275. - (void)didSelectedCheckBox:(QCheckBox *)checkbox checked:(BOOL)checked{
  276. [self addressBookCell:nil checkBoxSelectIndexNum:checkbox.tag isSelect:checked];
  277. }
  278. - (void)selectBtnAction:(UIButton *)btn {
  279. self.isShowSelect = YES;
  280. if (self.doneBtn.hidden == YES) {
  281. self.doneBtn.hidden = NO;
  282. self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.tableView.frame.size.height - JX_SCREEN_BOTTOM);
  283. // [self.gotoBackBtn setFrame:CGRectMake(self.gotoBackBtn.frame.origin.x, self.gotoBackBtn.frame.origin.y, self.gotoBackBtn.frame.size.width+20, self.gotoBackBtn.frame.size.height)];
  284. [self.gotoBackBtn setBackgroundImage:nil forState:UIControlStateNormal];
  285. [self.gotoBackBtn setTitle:Localized(@"JX_Cencal") forState:UIControlStateNormal];
  286. [btn setTitle:Localized(@"JX_CheckAll") forState:UIControlStateNormal];
  287. btn.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
  288. }else {
  289. self.isAllSelect = !self.isAllSelect;
  290. if (self.isAllSelect) {
  291. for (int i = 0 ; i < self.letterResultArr.count; i++) {
  292. NSArray *arr = [self.letterResultArr objectAtIndex:i];
  293. if (arr.count > 1) {
  294. for (JXAddressBook *ad in arr) {
  295. JXUserObject *user = [[JXUserObject sharedInstance] getUserById:ad.toUserId];
  296. if ([user.status intValue] != 2 && [user.status intValue] != -1) {
  297. [_selectABs addObject:ad];
  298. }
  299. }
  300. }else {
  301. JXAddressBook *ad = [arr firstObject];
  302. JXUserObject *user = [[JXUserObject sharedInstance] getUserById:ad.toUserId];
  303. if ([user.status intValue] != 2 && [user.status intValue] != -1) {
  304. [_selectABs addObject:ad];
  305. }
  306. }
  307. }
  308. }else {
  309. [_selectABs removeAllObjects];
  310. }
  311. }
  312. [self.tableView reloadData];
  313. // self.isShowSelect = !self.isShowSelect;
  314. // [self.tableView reloadData];
  315. // if (self.isShowSelect) {
  316. // [btn setTitle:Localized(@"JX_Cencal") forState:UIControlStateNormal];
  317. // self.doneBtn.hidden = NO;
  318. // self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.tableView.frame.size.height - JX_SCREEN_BOTTOM);
  319. // }else {
  320. // [btn setTitle:Localized(@"JX_BatchAddition") forState:UIControlStateNormal];
  321. // [_selectABs removeAllObjects];
  322. // self.doneBtn.hidden = YES;
  323. // self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self_height-self.heightHeader-self.heightFooter);
  324. // }
  325. if (self.abUreadArr.count > 0) {
  326. [self createHeaderView:self.abUreadArr];
  327. }
  328. }
  329. - (void)actionQuit {
  330. if (self.isShowSelect) {
  331. [self resetViwe];
  332. return;
  333. }
  334. [super actionQuit];
  335. }
  336. - (void)resetViwe {
  337. self.isShowSelect = NO;
  338. self.isAllSelect = NO;
  339. [_selectABs removeAllObjects];
  340. [_selectBtn setTitle:Localized(@"JX_BatchInvite") forState:UIControlStateNormal];
  341. _selectBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  342. [self.gotoBackBtn setTitle:nil forState:UIControlStateNormal];
  343. // [self.gotoBackBtn setFrame:CGRectMake(self.gotoBackBtn.frame.origin.x, self.gotoBackBtn.frame.origin.y, self.gotoBackBtn.frame.size.width-20, self.gotoBackBtn.frame.size.height)];
  344. [self.gotoBackBtn setBackgroundImage:[UIImage imageNamed:@"title_back_black_big"] forState:UIControlStateNormal];
  345. [_selectABs removeAllObjects];
  346. self.doneBtn.hidden = YES;
  347. self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self_height-self.heightHeader-self.heightFooter);
  348. [self.tableView reloadData];
  349. }
  350. - (void)doneBtnAction:(UIButton *)btn {
  351. NSMutableString *userIds = [NSMutableString string];
  352. NSMutableString *userNames = [NSMutableString string];
  353. if (_selectABs.count <= 0) {
  354. [g_App showAlert:Localized(@"JX_PleaseSelectContacts")];
  355. return;
  356. }
  357. for (NSInteger i = 0; i < _selectABs.count; i ++) {
  358. JXAddressBook *ab = _selectABs[i];
  359. JXUserObject *user = [[JXUserObject alloc] init];
  360. user.userId = ab.toUserId;
  361. user.userNickname = ab.toUserName;
  362. user.status = [NSNumber numberWithInt:2];
  363. user.userType = [NSNumber numberWithInt:0];
  364. user.roomFlag = [NSNumber numberWithInt:0];
  365. user.content = nil;
  366. user.timeSend = [NSDate date];
  367. user.chatRecordTimeOut = g_myself.chatRecordTimeOut;
  368. [user insert];
  369. if (i == 0) {
  370. [userIds appendString:user.userId];
  371. [userNames appendString:ab.addressBookName];
  372. }else {
  373. [userIds appendFormat:@",%@",user.userId];
  374. [userNames appendFormat:@",%@",ab.addressBookName];
  375. }
  376. }
  377. _selectUserIds = [userIds copy];
  378. _selectUserNames = [userNames copy];
  379. [g_server friendsAttentionBatchAddToUserIds:userIds toView:self];
  380. }
  381. - (void)getServerData {
  382. [_array removeAllObjects];
  383. [_allAbArr removeAllObjects];
  384. NSMutableArray *arr = [[JXAddressBook sharedInstance] fetchAllAddressBook];
  385. for (NSInteger i = 0; i < arr.count; i ++) {
  386. JXAddressBook *ab = arr[i];
  387. if (_phoneNumDict[ab.toTelephone]) {
  388. [_allAbArr addObject:ab];
  389. BOOL flag = NO;
  390. for (NSInteger j = 0; j < self.abUreadArr.count; j ++) {
  391. JXAddressBook *abUread = self.abUreadArr[j];
  392. if ([abUread.toUserId isEqualToString:ab.toUserId]) {
  393. flag = YES;
  394. break;
  395. }
  396. }
  397. if (!flag) {
  398. [_array addObject:ab];
  399. }
  400. }
  401. }
  402. //选择拼音 转换的 方法
  403. BMChineseSortSetting.share.sortMode = 2; // 1或2
  404. //排序 Person对象
  405. [BMChineseSort sortAndGroup:_array key:@"addressBookName" finish:^(bool isSuccess, NSMutableArray *unGroupArr, NSMutableArray *sectionTitleArr, NSMutableArray<NSMutableArray *> *sortedObjArr) {
  406. if (isSuccess) {
  407. self.indexArray = sectionTitleArr;
  408. self.letterResultArr = sortedObjArr;
  409. [_table reloadData];
  410. }
  411. }];
  412. // //根据Person对象的 name 属性 按中文 对 Person数组 排序
  413. // self.indexArray = [BMChineseSort IndexWithArray:_array Key:@"addressBookName"];
  414. // self.letterResultArr = [BMChineseSort sortObjectArray:_array Key:@"addressBookName"];
  415. }
  416. #pragma mark ---------tableView协议----------------
  417. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  418. {
  419. return [self.indexArray count];
  420. }
  421. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  422. return [self.indexArray objectAtIndex:section];
  423. }
  424. -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
  425. UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
  426. header.tintColor = HEXCOLOR(0xF2F2F2);
  427. [header.textLabel setTextColor:HEXCOLOR(0x999999)];
  428. [header.textLabel setFont:SYSFONT(15)];
  429. }
  430. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  431. {
  432. return [(NSArray *)[self.letterResultArr objectAtIndex:section] count];
  433. }
  434. -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
  435. return self.indexArray;
  436. }
  437. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
  438. return index;
  439. }
  440. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  441. {
  442. NSString *identifier = @"JXAddressBookCell";
  443. JXAddressBookCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  444. JXAddressBook *addressBook = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  445. if (!cell) {
  446. cell = [[JXAddressBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  447. }
  448. cell.delegate = self;
  449. cell.index = indexPath.section * 1000 + indexPath.row;
  450. // cell.isShowSelect = self.isShowSelect;
  451. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  452. if (self.isAllSelect) {
  453. cell.isShowSelect = YES;
  454. cell.checkBox.selected = YES;
  455. }else {
  456. cell.isShowSelect = self.isShowSelect;
  457. if ([_selectABs containsObject:addressBook]) {
  458. cell.checkBox.selected = YES;
  459. }else {
  460. cell.checkBox.selected = NO;
  461. }
  462. }
  463. cell.addressBook = addressBook;
  464. cell.headImage.userInteractionEnabled = NO;
  465. if (indexPath.row == [(NSArray *)[self.letterResultArr objectAtIndex:indexPath.section] count]-1) {
  466. cell.lineView.frame = CGRectMake(cell.lineView.frame.origin.x, cell.lineView.frame.origin.y, cell.lineView.frame.size.width,0);
  467. }else {
  468. cell.lineView.frame = CGRectMake(cell.lineView.frame.origin.x, cell.lineView.frame.origin.y, cell.lineView.frame.size.width,LINE_WH);
  469. }
  470. return cell;
  471. }
  472. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  473. return 64;
  474. }
  475. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  476. {
  477. JXAddressBookCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  478. if (self.isShowSelect) {
  479. JXUserObject *user = [[JXUserObject sharedInstance] getUserById:cell.addressBook.toUserId];
  480. if ([user.status intValue] != 2) {
  481. cell.checkBox.selected = !cell.checkBox.selected;
  482. [self addressBookCell:cell checkBoxSelectIndexNum:cell.index isSelect:cell.checkBox.selected];
  483. }
  484. }else {
  485. JXUserInfoVC* vc = [JXUserInfoVC alloc];
  486. vc.userId = cell.addressBook.toUserId;
  487. vc.fromAddType = 6;
  488. vc = [vc init];
  489. [g_navigation pushViewController:vc animated:YES];
  490. }
  491. }
  492. - (void)addressBookCell:(JXAddressBookCell *)abCell checkBoxSelectIndexNum:(NSInteger)indexNum isSelect:(BOOL)isSelect {
  493. JXAddressBook *ab;
  494. if (abCell) {
  495. ab = [[self.letterResultArr objectAtIndex:abCell.index / 1000] objectAtIndex:abCell.index % 1000];
  496. }else {
  497. ab = self.abUreadArr[indexNum];
  498. }
  499. if (isSelect) {
  500. [_selectABs addObject:ab];
  501. }else {
  502. [_selectABs removeObject:ab];
  503. }
  504. }
  505. - (void)addressBookCell:(JXAddressBookCell *)abCell addBtnAction:(JXAddressBook *)addressBook {
  506. JXMessageObject* p = [[JXMessageObject alloc] init];
  507. p.fromUserId = MY_USER_ID;
  508. p.toUserId = addressBook.toUserId;
  509. p.isMySend = YES;
  510. p.content = Localized(@"JX_AddYouByPhone");
  511. p.type = [NSNumber numberWithInt:kWCMessageTypeText];
  512. p.isSend = [NSNumber numberWithInt:transfer_status_yes];
  513. p.isRead = [NSNumber numberWithInt:1];
  514. p.timeSend = [NSDate date];
  515. [p insert:nil];
  516. [p notifyNewMsg];
  517. JXUserObject *user = [[JXUserObject alloc] init];
  518. user.userId = addressBook.toUserId;
  519. user.userNickname = addressBook.toUserName;
  520. user.status = [NSNumber numberWithInt:2];
  521. user.userType = [NSNumber numberWithInt:0];
  522. user.roomFlag = [NSNumber numberWithInt:0];
  523. user.content = p.content;
  524. user.timeSend = [NSDate date];
  525. user.chatRecordTimeOut = g_myself.chatRecordTimeOut;
  526. [user insert];
  527. _selectUserIds = [addressBook.toUserId copy];
  528. _selectUserNames = [addressBook.addressBookName copy];
  529. [g_server friendsAttentionBatchAddToUserIds:addressBook.toUserId toView:self];
  530. }
  531. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  532. [_wait stop];
  533. if( [aDownload.action isEqualToString:act_FriendsAttentionBatchAdd] ){
  534. [g_notify postNotificationName:kFriendListRefresh object:nil];
  535. // self.isShowSelect = NO;
  536. // [_selectBtn setTitle:Localized(@"JX_BatchAddition") forState:UIControlStateNormal];
  537. // [_selectABs removeAllObjects];
  538. // self.doneBtn.hidden = YES;
  539. // self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self_height-self.heightHeader-self.heightFooter);
  540. [self resetViwe];
  541. [self getServerData];
  542. if (self.abUreadArr.count > 0) {
  543. [self createHeaderView:self.abUreadArr];
  544. }
  545. [g_App showAlert:Localized(@"JX_AddSuccess")];
  546. [self updateNewFriend];
  547. }
  548. }
  549. - (void)updateNewFriend {
  550. NSArray *arr = [_selectUserIds componentsSeparatedByString:@","];
  551. NSArray *nameArr = [_selectUserNames componentsSeparatedByString:@","];
  552. for (NSInteger i = 0; i < arr.count; i ++) {
  553. NSString *userId = arr[i];
  554. NSString *userName = nameArr[i];
  555. JXMessageObject* p = [[JXMessageObject alloc] init];
  556. p.fromUserId = MY_USER_ID;
  557. p.fromUserName = MY_USER_NAME;
  558. p.toUserId = userId;
  559. p.toUserName = userName;
  560. p.type = [NSNumber numberWithInt:XMPP_TYPE_CONTACTFRIEND];
  561. p.timeSend = [NSDate date];
  562. p.isSend = [NSNumber numberWithInt:transfer_status_ing];
  563. [p setMsgId];
  564. JXFriendObject* friend = [[JXFriendObject alloc]init];
  565. [friend loadFromMessageObj:p];
  566. [friend doWriteDb];
  567. [friend notifyNewRequest];
  568. }
  569. }
  570. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  571. [_wait stop];
  572. return show_error;
  573. }
  574. -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时
  575. [_wait stop];
  576. return show_error;
  577. }
  578. -(void) didServerConnectStart:(JXConnection*)aDownload{
  579. [_wait start];
  580. }
  581. - (void)didReceiveMemoryWarning {
  582. [super didReceiveMemoryWarning];
  583. // Dispose of any resources that can be recreated.
  584. }
  585. /*
  586. #pragma mark - Navigation
  587. // In a storyboard-based application, you will often want to do a little preparation before navigation
  588. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  589. // Get the new view controller using [segue destinationViewController].
  590. // Pass the selected object to the new view controller.
  591. }
  592. */
  593. @end