JXBlackFriendVC.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. //
  2. // JXBlackFriendVC.m
  3. // shiku_im
  4. //
  5. // Created by p on 2018/6/4.
  6. // Copyright © 2018年 Reese. All rights reserved.
  7. //
  8. #import "JXBlackFriendVC.h"
  9. #import "BMChineseSort.h"
  10. #import "JXCell.h"
  11. #import "JXUserInfoVC.h"
  12. #import "JXChatViewController.h"
  13. #import "JXRoomPool.h"
  14. #import "JXDevice.h"
  15. @interface JXBlackFriendVC ()<UITextFieldDelegate>
  16. //排序后的出现过的拼音首字母数组
  17. @property(nonatomic,strong)NSMutableArray *indexArray;
  18. //排序好的结果数组
  19. @property(nonatomic,strong)NSMutableArray *letterResultArr;
  20. @property (nonatomic, strong) NSMutableArray *array;
  21. @property (nonatomic, strong) UITextField *seekTextField;
  22. @property (nonatomic, strong) NSMutableArray *searchArray;
  23. @property (nonatomic, strong) JXUserObject * currentUser;
  24. @end
  25. @implementation JXBlackFriendVC
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. // Do any additional setup after loading the view.
  29. self.heightHeader = JX_SCREEN_TOP;
  30. self.heightFooter = 0;
  31. self.isGotoBack = YES;
  32. self.isShowFooterPull = NO;
  33. _array = [NSMutableArray array];
  34. [self createHeadAndFoot];
  35. [g_notify addObserver:self selector:@selector(newReceipt:) name:kXMPPReceiptNotifaction object:nil];
  36. if (self.isDevice) {
  37. self.isShowHeaderPull = NO;
  38. [g_notify addObserver:self selector:@selector(updateIsOnLineMultipointLogin) name:kUpdateIsOnLineMultipointLogin object:nil];// 多点登录在线离线状态更新
  39. }
  40. if (!self.isDevice) {
  41. [self customView];
  42. }
  43. [self getArrayData];
  44. }
  45. - (void)scrollToPageUp {
  46. if (!self.isDevice) {
  47. [g_server listBlacklist:0 toView:self];
  48. }
  49. }
  50. - (void)customView {
  51. //搜索输入框
  52. UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_TOP, JX_SCREEN_WIDTH, 55)];
  53. // backView.backgroundColor = HEXCOLOR(0xf0f0f0);
  54. [self.view addSubview:backView];
  55. // [seekImgView release];
  56. // UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(backView.frame.size.width-5-45, 5, 45, 30)];
  57. // [cancelBtn setTitle:Localized(@"JX_Cencal") forState:UIControlStateNormal];
  58. // [cancelBtn setTitleColor:THEMECOLOR forState:UIControlStateNormal];
  59. // [cancelBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside];
  60. // cancelBtn.titleLabel.font = SYSFONT(14.0);
  61. // [backView addSubview:cancelBtn];
  62. _seekTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 10, backView.frame.size.width - 30, 35)];
  63. _seekTextField.placeholder = [NSString stringWithFormat:@"%@",Localized(@"JX_EnterKeyword")];
  64. _seekTextField.textColor = [UIColor blackColor];
  65. [_seekTextField setFont:SYSFONT(14)];
  66. _seekTextField.backgroundColor = HEXCOLOR(0xf0f0f0);
  67. UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"card_search"]];
  68. UIView *leftView = [[UIView alloc ]initWithFrame:CGRectMake(0, 0, 30, 30)];
  69. // imageView.center = CGPointMake(leftView.frame.size.width/2, leftView.frame.size.height/2);
  70. imageView.center = leftView.center;
  71. [leftView addSubview:imageView];
  72. _seekTextField.leftView = leftView;
  73. _seekTextField.leftViewMode = UITextFieldViewModeAlways;
  74. _seekTextField.borderStyle = UITextBorderStyleNone;
  75. _seekTextField.layer.masksToBounds = YES;
  76. _seekTextField.layer.cornerRadius = 3.f;
  77. _seekTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  78. _seekTextField.delegate = self;
  79. _seekTextField.returnKeyType = UIReturnKeyGoogle;
  80. [backView addSubview:_seekTextField];
  81. [_seekTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  82. self.tableView.tableHeaderView = backView;
  83. }
  84. - (void)getArrayData {
  85. [self.array removeAllObjects];
  86. //获取黑名單列表
  87. if (self.isDevice) {
  88. _array = [[JXDevice sharedInstance] fetchAllDeviceFromLocal];
  89. self.isShowHeaderPull = NO;
  90. self.isShowFooterPull = NO;
  91. }else {
  92. //从数据库获取好友staus为-1的
  93. _array=[[JXUserObject sharedInstance] fetchAllBlackFromLocal];
  94. if (_array.count <= 0) {
  95. [self scrollToPageUp];
  96. }
  97. }
  98. //选择拼音 转换的 方法
  99. BMChineseSortSetting.share.sortMode = 2; // 1或2
  100. //排序 Person对象
  101. [BMChineseSort sortAndGroup:_array key:@"userNickname" finish:^(bool isSuccess, NSMutableArray *unGroupArr, NSMutableArray *sectionTitleArr, NSMutableArray<NSMutableArray *> *sortedObjArr) {
  102. if (isSuccess) {
  103. self.indexArray = sectionTitleArr;
  104. self.letterResultArr = sortedObjArr;
  105. [self.tableView reloadData];
  106. }
  107. }];
  108. // //根据Person对象的 name 属性 按中文 对 Person数组 排序
  109. // self.indexArray = [BMChineseSort IndexWithArray:_array Key:@"userNickname"];
  110. // self.letterResultArr = [BMChineseSort sortObjectArray:_array Key:@"userNickname"];
  111. //
  112. // [self.tableView reloadData];
  113. }
  114. - (void)updateIsOnLineMultipointLogin {
  115. [self getArrayData];
  116. [_table reloadData];
  117. }
  118. - (void) textFieldDidChange:(UITextField *)textField {
  119. if (textField.text.length <= 0) {
  120. [self getArrayData];
  121. [self.tableView reloadData];
  122. return;
  123. }
  124. [_searchArray removeAllObjects];
  125. _searchArray = [[JXUserObject sharedInstance] fetchBlackFromLocalWhereLike:textField.text];
  126. [self.tableView reloadData];
  127. }
  128. - (void) cancelBtnAction {
  129. if (_seekTextField.text.length > 0) {
  130. _seekTextField.text = nil;
  131. [self getArrayData];
  132. }
  133. [_seekTextField resignFirstResponder];
  134. [self.tableView reloadData];
  135. }
  136. #pragma mark ---------tableView协议----------------
  137. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  138. {
  139. if (_seekTextField.text.length > 0) {
  140. return 1;
  141. }
  142. return [self.indexArray count];
  143. }
  144. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  145. if (self.isDevice) {
  146. return nil;
  147. }
  148. if (_seekTextField.text.length > 0) {
  149. return Localized(@"JXFriend_searchTitle");
  150. }
  151. return [self.indexArray objectAtIndex:section];
  152. }
  153. -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
  154. UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
  155. header.tintColor = HEXCOLOR(0xF2F2F2);
  156. [header.textLabel setTextColor:HEXCOLOR(0x999999)];
  157. [header.textLabel setFont:SYSFONT(15)];
  158. }
  159. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  160. if (_seekTextField.text.length > 0) {
  161. return _searchArray.count;
  162. }
  163. return [(NSArray *)[self.letterResultArr objectAtIndex:section] count];
  164. }
  165. -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
  166. if (_seekTextField.text.length > 0 || self.isDevice) {
  167. return nil;
  168. }
  169. return self.indexArray;
  170. }
  171. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
  172. return index;
  173. }
  174. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  175. {
  176. JXUserObject *user;
  177. if (_seekTextField.text.length > 0) {
  178. user = _searchArray[indexPath.row];
  179. }else{
  180. user = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  181. }
  182. JXCell *cell=nil;
  183. NSString* cellName = @"JXCell";
  184. cell = [tableView dequeueReusableCellWithIdentifier:cellName];
  185. if(cell==nil){
  186. cell = [[JXCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
  187. [_table addToPool:cell];
  188. // cell.headImage = user.userHead;
  189. // user = nil;
  190. }
  191. if (self.isDevice) {
  192. cell.title = [self multipleLoginIsOnlineTitle:user];
  193. }else {
  194. cell.title = user.remarkName.length > 0 ? user.remarkName : user.userNickname;
  195. }
  196. // cell.subtitle = user.userId;
  197. cell.index = (int)indexPath.row;
  198. cell.delegate = self;
  199. if (!self.isDevice) {
  200. cell.didTouch = @selector(onHeadImage:);
  201. }
  202. // cell.bottomTitle = [TimeUtil formatDate:user.timeCreate format:@"MM-dd HH:mm"];
  203. [cell setForTimeLabel:[TimeUtil formatDate:user.timeCreate format:@"MM-dd HH:mm"]];
  204. cell.timeLabel.frame = CGRectMake(JX_SCREEN_WIDTH - 115-15, 19.5, 115, 20);
  205. cell.userId = user.userId;
  206. [cell.lbTitle setText:cell.title];
  207. cell.dataObj = user;
  208. // cell.headImageView.tag = (int)indexPath.row;
  209. // cell.headImageView.delegate = cell.delegate;
  210. // cell.headImageView.didTouch = cell.didTouch;
  211. cell.isSmall = YES;
  212. [cell headImageViewImageWithUserId:nil roomId:nil];
  213. if (self.isDevice) {
  214. cell.headImageView.frame = CGRectMake(6, 8, 40, 40);
  215. cell.lbTitle.frame = CGRectMake(CGRectGetMaxX(cell.headImageView.frame)+4, 19.5, 200, 20);
  216. cell.lineView.frame = CGRectMake(CGRectGetMaxX(cell.headImageView.frame)+4, 58.5, JX_SCREEN_WIDTH-CGRectGetMaxX(cell.headImageView.frame)-4, LINE_WH);
  217. }
  218. if (indexPath.row == [(NSArray *)[self.letterResultArr objectAtIndex:indexPath.section] count]-1) {
  219. cell.lineView.frame = CGRectMake(cell.lineView.frame.origin.x, cell.lineView.frame.origin.y, cell.lineView.frame.size.width,0);
  220. }else {
  221. cell.lineView.frame = CGRectMake(cell.lineView.frame.origin.x, cell.lineView.frame.origin.y, cell.lineView.frame.size.width,LINE_WH);
  222. }
  223. return cell;
  224. }
  225. - (NSString *)multipleLoginIsOnlineTitle:(JXUserObject *)user {
  226. NSString *isOnline;
  227. if ([user.isOnLine intValue] == 1) {
  228. isOnline = [NSString stringWithFormat:@"(%@)", Localized(@"JX_OnLine")];
  229. }else {
  230. isOnline = [NSString stringWithFormat:@"(%@)", Localized(@"JX_OffLine")];
  231. }
  232. NSString *title = user.userNickname;
  233. title = [title stringByAppendingString:isOnline];
  234. return title;
  235. }
  236. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  237. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  238. JXUserObject *user;
  239. if (_seekTextField.text.length > 0) {
  240. user = _searchArray[indexPath.row];
  241. }else{
  242. user = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  243. }
  244. if (self.isDevice) {
  245. JXCell * cell = [_table cellForRowAtIndexPath:indexPath];
  246. cell.selected = NO;
  247. JXChatViewController *sendView=[JXChatViewController alloc];
  248. if([user.roomFlag intValue] > 0 || user.roomId.length > 0){
  249. sendView.roomJid = user.userId;
  250. sendView.roomId = user.roomId;
  251. [[JXXMPP sharedInstance].roomPool joinRoom:user.userId title:user.userNickname lastDate:nil isNew:NO];
  252. }
  253. sendView.title = user.userNickname;
  254. sendView.chatPerson = user;
  255. sendView = [sendView init];
  256. // [g_App.window addSubview:sendView.view];
  257. [g_navigation pushViewController:sendView animated:YES];
  258. }else {
  259. _currentUser = user;
  260. JXUserInfoVC *userVC = [JXUserInfoVC alloc];
  261. userVC.userId = user.userId;
  262. userVC.fromAddType = 6;
  263. userVC = [userVC init];
  264. [g_navigation pushViewController:userVC animated:YES];
  265. }
  266. }
  267. -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
  268. if (self.isDevice) {
  269. return NO;
  270. }
  271. if (_seekTextField.text.length <= 0){
  272. return YES;
  273. }else{
  274. return NO;
  275. }
  276. }
  277. //-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
  278. // if (_selMenu == 0) {
  279. // return UITableViewCellEditingStyleDelete;
  280. // }else{
  281. // return UITableViewCellEditingStyleNone;
  282. // }
  283. //}
  284. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  285. UITableViewRowAction *cancelBlackBtn = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:Localized(@"REMOVE") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  286. JXUserObject *user = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  287. _currentUser = user;
  288. [g_server delBlacklist:user.userId toView:self];
  289. }];
  290. return @[cancelBlackBtn];
  291. }
  292. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  293. {
  294. return 59;
  295. }
  296. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  297. [self.view endEditing:YES];
  298. }
  299. //服务器返回数据
  300. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  301. [_wait stop];
  302. [self stopLoading];
  303. // //更新本地好友
  304. // if ([aDownload.action isEqualToString:act_AttentionList]) {
  305. // [_wait stop];
  306. // JXProgressVC * pv = [JXProgressVC alloc];
  307. // // 服务端不会返回新朋友 , 减去新朋友
  308. // pv.dbFriends = (long)[_array count] - 1;
  309. // pv.dataArray = array1;
  310. // pv = [pv init];
  311. // // [g_window addSubview:pv.view];
  312. // }
  313. if ([aDownload.action isEqualToString:act_FriendDel]) {
  314. [_currentUser doSendMsg:XMPP_TYPE_DELALL content:nil];
  315. }
  316. if([aDownload.action isEqualToString:act_BlacklistDel]){
  317. [_currentUser doSendMsg:XMPP_TYPE_NOBLACK content:nil];
  318. }
  319. if( [aDownload.action isEqualToString:act_UserGet] ){
  320. [_wait stop];
  321. JXUserObject* user = [[JXUserObject alloc]init];
  322. [user getDataFromDict:dict];
  323. JXUserInfoVC* vc = [JXUserInfoVC alloc];
  324. vc.user = user;
  325. vc.fromAddType = 6;
  326. vc = [vc init];
  327. // [g_window addSubview:vc.view];
  328. [g_navigation pushViewController:vc animated:YES];
  329. }
  330. if ([aDownload.action isEqualToString:act_BlacklistList]) {
  331. [[JXUserObject sharedInstance] deleteAllBlackUser];
  332. for (int i = 0; i< [array1 count]; i++) {
  333. NSDictionary * dict = array1[i];
  334. JXUserObject * user = [[JXUserObject alloc]init];
  335. //数据转为一个好友对象
  336. [user getDataFromDictSmall:dict];
  337. //访问数据库是否存在改好友,没有则写入数据库
  338. if (user.userId.length > 5) {
  339. [user insertFriend];
  340. }
  341. }
  342. if (array1.count > 0) {
  343. [self getArrayData];
  344. }
  345. }
  346. }
  347. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  348. [_wait hide];
  349. [self stopLoading];
  350. return show_error;
  351. }
  352. -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时
  353. [_wait hide];
  354. [self stopLoading];
  355. return show_error;
  356. }
  357. -(void) didServerConnectStart:(JXConnection*)aDownload{
  358. [_wait start];
  359. }
  360. -(void)newFriend:(NSObject*)sender{
  361. [self getArrayData];
  362. }
  363. -(void)onHeadImage:(id)dataObj{
  364. JXUserObject *p = (JXUserObject *)dataObj;
  365. if([p.userId isEqualToString:FRIEND_CENTER_USERID] || [p.userId isEqualToString:CALL_CENTER_USERID])
  366. return;
  367. _currentUser = p;
  368. // [g_server getUser:p.userId toView:self];
  369. JXUserInfoVC* vc = [JXUserInfoVC alloc];
  370. vc.userId = p.userId;
  371. vc.fromAddType = 6;
  372. vc = [vc init];
  373. [g_navigation pushViewController:vc animated:YES];
  374. p = nil;
  375. }
  376. -(void)onSendTimeout:(NSNotification *)notifacation//超时未收到回执
  377. {
  378. // NSLog(@"onSendTimeout");
  379. [_wait stop];
  380. // [g_App showAlert:Localized(@"JXAlert_SendFilad")];
  381. [JXMyTools showTipView:Localized(@"JXAlert_SendFilad")];
  382. }
  383. -(void)newReceipt:(NSNotification *)notifacation{//新回执
  384. // NSLog(@"newReceipt");
  385. JXMessageObject *msg = (JXMessageObject *)notifacation.object;
  386. if(msg == nil)
  387. return;
  388. if(![msg isAddFriendMsg])
  389. return;
  390. [_wait stop];
  391. if([msg.type intValue] == XMPP_TYPE_DELALL){
  392. if([msg.toUserId isEqualToString:_currentUser.userId] || [msg.fromUserId isEqualToString:_currentUser.userId]){
  393. [_array removeObject:_currentUser];
  394. _currentUser = nil;
  395. [self getArrayData];
  396. [_table reloadData];
  397. [g_App showAlert:Localized(@"JXAlert_DeleteFirend")];
  398. }
  399. }
  400. if([msg.type intValue] == XMPP_TYPE_BLACK){//拉黑
  401. [_array removeObject:_currentUser];
  402. //选择拼音 转换的 方法
  403. BMChineseSortSetting.share.sortMode = 2; // 1或2
  404. //排序 Person对象
  405. [BMChineseSort sortAndGroup:_array key:@"userNickname" finish:^(bool isSuccess, NSMutableArray *unGroupArr, NSMutableArray *sectionTitleArr, NSMutableArray<NSMutableArray *> *sortedObjArr) {
  406. if (isSuccess) {
  407. self.indexArray = sectionTitleArr;
  408. self.letterResultArr = sortedObjArr;
  409. [self.tableView reloadData];
  410. }
  411. }];
  412. // //根据Person对象的 name 属性 按中文 对 Person数组 排序
  413. // self.indexArray = [BMChineseSort IndexWithArray:_array Key:@"userNickname"];
  414. // self.letterResultArr = [BMChineseSort sortObjectArray:_array Key:@"userNickname"];
  415. // [self.tableView reloadData];
  416. }
  417. if([msg.type intValue] == XMPP_TYPE_NOBLACK){
  418. // _currentUser.status = [NSNumber numberWithInt:friend_status_friend];
  419. // int status = [_currentUser.status intValue];
  420. // [_currentUser update];
  421. if ([[JXXMPP sharedInstance].blackList containsObject:_currentUser.userId]) {
  422. [[JXXMPP sharedInstance].blackList removeObject:_currentUser.userId];
  423. }
  424. [JXMessageObject msgWithFriendStatus:_currentUser.userId status:friend_status_friend];
  425. for (JXUserObject *obj in _array) {
  426. if ([obj.userId isEqualToString:_currentUser.userId]) {
  427. [_array removeObject:obj];
  428. break;
  429. }
  430. }
  431. [self getArrayData];
  432. [self.tableView reloadData];
  433. // [g_App showAlert:Localized(@"JXAlert_MoveBlackList")];
  434. }
  435. }
  436. - (void)friendRemarkNotif:(NSNotification *)notif {
  437. JXUserObject *user = notif.object;
  438. for (int i = 0; i < _array.count; i ++) {
  439. JXUserObject *user1 = _array[i];
  440. if ([user.userId isEqualToString:user1.userId]) {
  441. user1.userNickname = user.userNickname;
  442. [_table reloadData];
  443. break;
  444. }
  445. }
  446. }
  447. - (void)dealloc {
  448. [g_notify removeObserver:self];
  449. // [_table release];
  450. [_array removeAllObjects];
  451. // [_array release];
  452. // [super dealloc];
  453. }
  454. - (void)didReceiveMemoryWarning {
  455. [super didReceiveMemoryWarning];
  456. // Dispose of any resources that can be recreated.
  457. }
  458. /*
  459. #pragma mark - Navigation
  460. // In a storyboard-based application, you will often want to do a little preparation before navigation
  461. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  462. // Get the new view controller using [segue destinationViewController].
  463. // Pass the selected object to the new view controller.
  464. }
  465. */
  466. @end