// // JXNewFriendViewController.h.m // // Created by flyeagleTang on 14-4-3. // Copyright (c) 2014年 Reese. All rights reserved. // #import "JXNewFriendViewController.h" #import "JXChatViewController.h" #import "AppDelegate.h" #import "JXLabel.h" #import "JXImageView.h" #import "JXFriendCell.h" #import "JXRoomPool.h" #import "JXFriendObject.h" #import "UIFactory.h" #import "JXInputVC.h" #import "JXUserInfoVC.h" #import "JXSearchGroupVC.h" #import "JXCommonInputVC.h" #import "JXSearchUserVC.h" #import "JXNewFriendToSearVc.h" #import "JXFriendHeadView.h" #import "SaveOrReadArr.h" @interface JXNewFriendViewController () @property (nonatomic,strong) UITextField *searchfield; @property (nonatomic,strong) NSMutableArray *dataArr; @property (nonatomic,strong) NSMutableArray *dataArrTime; @end @implementation JXNewFriendViewController - (id)init { self = [super init]; if (self) { _dataArr=[NSMutableArray array]; _dataArrTime=[NSMutableArray array]; self.heightHeader = 0; self.heightFooter = 0; self.isGotoBack = YES; self.title = Localized(@"JXNewFriendVC_NewFirend"); //self.view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT); [self createHeadAndFoot]; self.isShowFooterPull = NO; self.tableView.backgroundColor = [UIColor whiteColor];// HEXCOLOR(0xF2F2F2); [self defineNavChatPush:@"新的朋友"]; self.tableView.frame=CGRectMake(0, JX_SCREEN_TOP, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-JX_SCREEN_TOP); UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 50)]; //[self.view addSubview:backView]; backView.backgroundColor = HEXCOLOR(0xF2F2F2); _searchfield = [[UITextField alloc] initWithFrame:CGRectMake(15, 10, backView.frame.size.width - 30, 35)]; _searchfield.placeholder = @"搜索(即信号或手机号)";//Localized(@"JX_Seach"); _searchfield.backgroundColor = [UIColor whiteColor];/// HEXCOLOR(0xf0f0f0); _searchfield.textColor = [UIColor blackColor]; [_searchfield setFont:SYSFONT(14)]; // _seekTextField.backgroundColor = [UIColor whiteColor]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"card_search"]]; UIView *leftView = [[UIView alloc ]initWithFrame:CGRectMake(0, 0, 30, 30)]; // imageView.center = CGPointMake(leftView.frame.size.width/2, leftView.frame.size.height/2); imageView.center = leftView.center; [leftView addSubview:imageView]; _searchfield.leftView = leftView; _searchfield.clearButtonMode = UITextFieldViewModeWhileEditing; _searchfield.leftViewMode = UITextFieldViewModeAlways; _searchfield.borderStyle = UITextBorderStyleNone; _searchfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; _searchfield.layer.masksToBounds = YES; _searchfield.layer.cornerRadius = 5; _searchfield.delegate = self; _searchfield.returnKeyType = UIReturnKeyGoogle; [backView addSubview:_searchfield]; [_searchfield addTarget:self action:@selector(onSearchRoom) forControlEvents:UIControlEventTouchUpInside]; self.tableView.tableHeaderView=backView; [g_notify addObserver:self selector:@selector(newRequest:) name:kXMPPNewRequestNotifaction object:nil]; [g_notify addObserver:self selector:@selector(newReceipt:) name:kXMPPReceiptNotifaction object:nil]; [g_notify addObserver:self selector:@selector(onSendTimeout:) name:kXMPPSendTimeOutNotifaction object:nil]; poolCell = [[NSMutableDictionary alloc]init]; current_chat_userId = FRIEND_CENTER_USERID; [_wait stop]; } return self; } /** * 跳转到 搜索页面 */ - (BOOL)textFieldShouldReturn:(UITextField *)textField{ if (textField.text.length==0) { [_dataArr removeAllObjects]; [self.tableView reloadData]; return YES; } [textField resignFirstResponder]; return YES; } //- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ // // return YES; // //} - (BOOL)textFieldShouldClear:(UITextField *)textField{ [_dataArr removeAllObjects]; [self.tableView reloadData]; return YES; } - (void)textFieldDidBeginEditing:(UITextField *)textField{ [textField resignFirstResponder]; JXSearchUserVC* vc = [JXSearchUserVC alloc]; vc.delegate = self; vc.type = JXSearchTypeUser; vc = [vc init]; [g_navigation pushViewController:vc animated:YES]; } - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { [textField resignFirstResponder]; if (textField.text.length==0) { [_dataArr removeAllObjects]; [self.tableView reloadData]; } NSString *userID=textField.text; // NSLog(@"xxxx1 %@",userID ); [_array enumerateObjectsUsingBlock:^(JXFriendObject * obj, NSUInteger idx, BOOL * _Nonnull stop) { NSString *userID2=obj.userId; //NSLog(@"xxxx %@",userID2 ); if ([userID isEqualToString:obj.userId]) { NSLog(@"xxxx %@",obj ); [_dataArr addObject:obj]; [self.tableView reloadData]; } }]; return YES; JXNewFriendToSearVc *vc = [[JXNewFriendToSearVc alloc] init]; vc.delegate = self; vc.titleStr = [NSString stringWithFormat:@"%@%@",Localized(@"JX_Seach"),Localized(@"JXNewFriendVC_NewFirend")] ; vc.subTitle =Localized(@"JXNewFriendVC_NewFirend"); vc.tip = Localized(@"JX_InputRoomName"); vc.btnTitle = Localized(@"JX_Seach"); [g_navigation pushViewController:vc animated:YES]; return YES; // JXCommonInputVC *vc = [[JXCommonInputVC alloc] init]; vc.delegate = self; vc.titleStr = Localized(@"JX_CommonGroupSearch"); vc.subTitle = Localized(@"JX_ManyPerChat"); vc.tip = Localized(@"JX_InputRoomName"); vc.btnTitle = Localized(@"JX_Seach"); [g_navigation pushViewController:vc animated:YES]; } - (void)onSearchRoom{ } - (void)commonInputVCBtnActionWithVC:(JXCommonInputVC *)commonInputVC { JXSearchGroupVC *vc = [[JXSearchGroupVC alloc] init]; vc.searchName = commonInputVC.name.text; [g_navigation pushViewController:vc animated:YES]; } - (void)dealloc { // NSLog(@"JXNewFriendViewController.dealloc"); // [super dealloc]; } -(void)free{ current_chat_userId = nil; [_array removeAllObjects]; // [_array release]; [poolCell removeAllObjects]; // [poolCell release]; [g_notify removeObserver:self name:kXMPPNewRequestNotifaction object:nil]; [g_notify removeObserver:self name:kXMPPSendTimeOutNotifaction object:nil]; [g_notify removeObserver:self name:kXMPPReceiptNotifaction object:nil]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor whiteColor]; self.tableView.backgroundColor=[UIColor whiteColor]; _array = [[NSMutableArray alloc]init]; [self refresh]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:YES]; [_wait stop]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark ---------tableView协议---------------- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if( _dataArrTime.count>0||_dataArrTime.count>0){ return 1; }else if( _array.count>0&&_dataArrTime.count>0){ return 2; }else{ return 0; } } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section==0) { return _dataArrTime.count; }else{ return _array.count; } } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ static NSString *viewIdentfier = @"headView"; JXFriendHeadView *headView=[tableView dequeueReusableHeaderFooterViewWithIdentifier:viewIdentfier]; if(!headView){ headView = [[JXFriendHeadView alloc] initWithReuseIdentifier:viewIdentfier]; } if (section==0) { [headView setStrURL:@"近三天"]; }else{ [headView setStrURL:@"三天前"]; } return headView; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 30; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.01; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { JXFriendCell *cell=nil; NSString* cellName = [NSString stringWithFormat:@"msg_%d_%ld",_refreshCount,indexPath.row]; cell = [tableView dequeueReusableCellWithIdentifier:cellName]; if (indexPath.section==0) { if(cell==nil){ JXFriendObject *user=_dataArrTime[indexPath.row]; cell = [JXFriendCell alloc]; [_table addToPool:cell]; cell.tag = indexPath.row; cell.delegate = self; cell.title = user.remarkName.length > 0 ? user.remarkName : user.userNickname; cell.subtitle = user.userId; cell.bottomTitle = [TimeUtil formatDate:user.timeCreate format:@"MM-dd HH:mm"]; cell.user = user; cell.target = self; cell = [cell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName]; user = nil; } }else{ if(cell==nil){ if (_dataArr.count>0) { _array=_dataArr; } JXFriendObject *user=_array[indexPath.row]; cell = [JXFriendCell alloc]; [_table addToPool:cell]; cell.delegate = self; cell.tag=indexPath.row; cell.title = user.remarkName.length > 0 ? user.remarkName : user.userNickname; cell.subtitle = user.userId; cell.bottomTitle = [TimeUtil formatDate:user.timeCreate format:@"MM-dd HH:mm"]; cell.user = user; cell.target = self; cell = [cell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName]; user = nil; } } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section==0) { JXFriendObject *friend=_dataArrTime[indexPath.row]; JXUserObject *user = [[JXUserObject alloc] init]; user.userNickname = friend.userNickname; user.userId = friend.userId; JXChatViewC *sendView=[JXChatViewC alloc]; sendView.isHiddenFooter = YES; sendView.title = friend.userNickname; sendView.chatPerson = user; sendView = [sendView init]; [g_navigation pushViewController:sendView animated:YES]; }else{ JXFriendObject *friend=_array[indexPath.row]; JXUserObject *user = [[JXUserObject alloc] init]; user.userNickname = friend.userNickname; user.userId = friend.userId; JXChatViewC *sendView=[JXChatViewC alloc]; sendView.isHiddenFooter = YES; sendView.title = friend.userNickname; sendView.chatPerson = user; sendView = [sendView init]; [g_navigation pushViewController:sendView animated:YES]; } } - (void)friendCell:(JXFriendCell *)friendCell headImageAction:(NSString *)userId { // [g_server getUser:userId toView:self]; JXUserInfoVC* vc = [JXUserInfoVC alloc]; vc.userId = userId; vc.fromAddType = 6; vc = [vc init]; [g_navigation pushViewController:vc animated:YES]; } -(void)refresh{ [self stopLoading]; _refreshCount++; // [_array release]; _array=[[JXFriendObject sharedInstance] fetchAllFriendsFromLocal]; NSArray *arr = [[_array reverseObjectEnumerator] allObjects]; [_dataArrTime removeAllObjects]; [arr enumerateObjectsUsingBlock:^(JXFriendObject *_Nonnull user, NSUInteger idx, BOOL * _Nonnull stop) { //status NSString *strtime = [TimeUtil formatDate:user.timeCreate format:@"yyyy-MM-dd"]; NSString *endTime= [TimeUtil formatDate:[NSDate date] format:@"yyyy-MM-dd"];; NSString *chaDay=[self intervalFromLastDate:strtime toTheDate:endTime]; if ([chaDay isEqualToString:@"0"]||[chaDay isEqualToString:@"1"]||[chaDay isEqualToString:@"2"]||[chaDay isEqualToString:@"3"]) { [_dataArrTime addObject:user]; [_array removeObjectAtIndex:0]; }else{ *stop = YES; } }]; [_wait stop]; [_table reloadData]; } - (NSString *)intervalFromLastDate: (NSString *) dateString1 toTheDate:(NSString *) dateString2 { NSDateFormatter *date=[[NSDateFormatter alloc] init]; [date setDateFormat:@"yyyy-MM-dd"]; NSDate *d1=[date dateFromString:dateString1]; NSTimeInterval late1=[d1 timeIntervalSince1970]*1; NSDate *d2=[date dateFromString:dateString2]; NSTimeInterval late2=[d2 timeIntervalSince1970]*1; NSTimeInterval cha=late2-late1; NSString *timeString=@""; NSString *house=@""; NSString *min=@""; NSString *sen=@""; sen = [NSString stringWithFormat:@"%d", (int)cha%60]; sen=[NSString stringWithFormat:@"%@", sen]; min = [NSString stringWithFormat:@"%d", (int)cha/60%60]; min=[NSString stringWithFormat:@"%@", min]; house = [NSString stringWithFormat:@"%d", (int)cha/3600]; house=[NSString stringWithFormat:@"%@", house]; NSString *dayDD=[NSString stringWithFormat:@"%d", (int)cha/3600/24]; timeString=[NSString stringWithFormat:@"差小时:%@-----分:%@------秒:%@",house,min,sen]; return dayDD; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 84; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return NO; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%ld",indexPath.row); JXFriendObject *user = _array[indexPath.row]; [_array removeObjectAtIndex:indexPath.row]; [user delete]; [_table reloadData]; } -(void)scrollToPageUp{ [self refresh]; } -(void)onSendTimeout:(NSNotification *)notifacation//超时未收到回执 { [_wait stop]; JXMessageObject *msg = (JXMessageObject *)notifacation.object; if(msg==nil) return; JXFriendCell* cell = [poolCell objectForKey:msg.messageId]; if(cell){ // [g_App showAlert:Localized(@"JXAlert_SendFilad")]; [JXMyTools showTipView:Localized(@"JXAlert_SendFilad")]; [poolCell removeObjectForKey:msg.messageId]; } } -(void)newRequest:(NSNotification *)notifacation{//新推送 // NSLog(@"newRequest"); JXFriendObject *user = (JXFriendObject *)notifacation.object; if(user == nil) return; // if(_wait.isShowing)//正在等待,就不刷新 // return; for(int i=0;i<[_array count];i++){ JXFriendObject* friend = [_array objectAtIndex:i]; if([friend.userId isEqualToString:user.userId]){ [friend loadFromObject:user]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; JXFriendCell* cell = (JXFriendCell*)[_table cellForRowAtIndexPath:indexPath]; [cell update]; cell = nil; return; } friend = nil; } NSMutableArray *arrxx=[[NSMutableArray alloc]initWithArray:[SaveOrReadArr initNSKeyedFriendUnarchiver]]; //本地存储 [arrxx addObject:user]; [SaveOrReadArr initSerializationFriendArray:arrxx]; //加好友 [_wait stop]; // [g_notify postNotificationName:@"newFriend" object:nil userInfo:@{@"use":user}]; [self refresh]; [_wait stop]; } -(void)onSayHello:(UIButton*)sender{//打招呼 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0]; _cell = (JXFriendCell*)[_table cellForRowAtIndexPath:indexPath]; JXInputVC* vc = [JXInputVC alloc]; vc.delegate = self; vc.didTouch = @selector(onInputHello:); vc.inputText = Localized(@"JXNewFriendVC_Iam"); vc = [vc init]; [g_window addSubview:vc.view]; } -(void)onInputHello:(JXInputVC*)sender{ NSString* messageId = [_cell.user doSendMsg:XMPP_TYPE_SAYHELLO content:sender.inputText]; [poolCell setObject:_cell forKey:messageId]; /// [_wait start:nil]; 修改了 } -(void)onFeedback:(UIButton*)sender{//回话 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0]; _cell = (JXFriendCell*)[_table cellForRowAtIndexPath:indexPath]; JXInputVC* vc = [JXInputVC alloc]; vc.delegate = self; vc.didTouch = @selector(onInputReply:); vc.inputText = Localized(@"JXNewFriendVC_Who"); vc = [vc init]; [g_window addSubview:vc.view]; } -(void)onInputReply:(JXInputVC*)sender{ NSString* messageId = [_cell.user doSendMsg:XMPP_TYPE_FEEDBACK content:sender.inputText]; [poolCell setObject:_cell forKey:messageId]; [_wait start:nil]; } -(void)onSeeHim:(UIButton*)sender{//关注他 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0]; JXFriendCell* cell = (JXFriendCell*)[_table cellForRowAtIndexPath:indexPath]; NSString* messageId = [cell.user doSendMsg:XMPP_TYPE_NEWSEE content:nil]; [poolCell setObject:cell forKey:messageId]; [_wait start:nil]; } /** * ti添加朋友 */ -(void)onAddFriend:(UIButton*)sender{//加好友 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0]; _cell = (JXFriendCell*)[_table cellForRowAtIndexPath:indexPath]; _user = _cell.user; [g_server addAttention:_user.userId fromAddType:0 toView:self]; } -(void)actionQuit{ [self free]; [super actionQuit]; } -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{ [_wait stop]; if([aDownload.action isEqualToString:act_AttentionAdd]){ int n = [[dict objectForKey:@"type"] intValue]; if( n==2 || n==4) _friendStatus = 2; // else // _friendStatus = 1; if(_friendStatus == 2){ NSString* messageId = [_user doSendMsg:XMPP_TYPE_PASS content:nil]; [poolCell setObject:_cell forKey:messageId]; //[_wait start:nil]; 修改了 messageId = nil; } } //点击好友头像响应 if( [aDownload.action isEqualToString:act_UserGet] ){ JXUserObject* user = [[JXUserObject alloc]init]; [user getDataFromDict:dict]; JXUserInfoVC* vc = [JXUserInfoVC alloc]; vc.user = user; vc.fromAddType = 6; // vc.isJustShow = YES; vc = [vc init]; // [g_window addSubview:vc.view]; [g_navigation pushViewController:vc animated:YES]; // [self cancelBtnAction]; } } -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{ [_wait stop]; return show_error; } -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时 [_wait stop]; return show_error; } -(void) didServerConnectStart:(JXConnection*)aDownload{ // [_wait start]; 修改了 } -(void)newReceipt:(NSNotification *)notifacation{//新回执 // NSLog(@"newReceipt"); [_wait stop]; JXMessageObject *msg = (JXMessageObject *)notifacation.object; if(msg == nil) return; if(![msg isAddFriendMsg]) return; if(![msg.toUserId isEqualToString:_cell.user.userId]) return; if([msg.type intValue] == XMPP_TYPE_PASS){//通过 _friendStatus = friend_status_friend; _user.status = [NSNumber numberWithInt:_friendStatus]; [_user update]; JXMessageObject *msg=[[JXMessageObject alloc] init]; msg.type = [NSNumber numberWithInt:kWCMessageTypeText]; msg.toUserId = _user.userId; msg.fromUserId = MY_USER_ID; msg.fromUserName = g_server.myself.userNickname; msg.content = Localized(@"JXFriendObject_StartChat"); msg.timeSend = [NSDate date]; [msg insert:nil]; [msg updateLastSend:UpdateLastSendType_None]; [msg notifyNewMsg]; } JXFriendCell* cell = [poolCell objectForKey:msg.messageId]; if(cell){ [cell.user loadFromMessageObj:msg]; [cell update]; [g_App showAlert:Localized(@"JXAlert_SendOK")]; [poolCell removeObjectForKey:msg.messageId]; } // [g_notify postNotificationName:@"newFriend" object:nil userInfo:@{@"use":_user}]; } @end