IVTMyChatListViewController.m 15.6 KB
//
//  IVTMyChatListViewController.m
//  IVTMeLiveTwo
//
//  Created by XRG on 16/8/23.
//  Copyright © 2016年 QLQ. All rights reserved.
//

#import "IVTMyChatListViewController.h"
#import "IVTMyChatListTableViewCell.h"
#import "IVTMyChatContentListViewController.h"
#import "DBManager.h"

@interface IVTMyChatListViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
{
    UIView *segementView;
    UIImageView *lineImageView;
    
    UIScrollView *myScrollView;
    UITableView *followedTableView;
    UITableView *unFollowedTableView;
    
    NSMutableArray *myFollowedChatListArray;
    NSMutableArray *myUnFollowedChatListArray;

    UILabel *noDataLabel1;
    UILabel *noDataLabel2;
}
@end

@implementation IVTMyChatListViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTableView) name:kPrivateMessageDidChange object:nil];
    [self refreshTableView];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak typeof(self) wself = self;

    [self setNavigationBar];

    myFollowedChatListArray = [[NSMutableArray alloc]initWithCapacity:10];
    myUnFollowedChatListArray = [[NSMutableArray alloc]initWithCapacity:10];

    segementView = [[UIView alloc]init];
    segementView.backgroundColor = UIColorFromRGBAlphaHex(0xf6f6f6, 1.0);
    [self.view addSubview:segementView];
    [segementView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wself.view.mas_top);
        make.left.equalTo(wself.view.mas_left);
        make.right.equalTo(wself.view.mas_right);
        make.height.mas_equalTo(40);
    }];
    

    CGFloat scrollViewHeight = 0;
    if (self.isSmall) {
        scrollViewHeight = 300 - 40;
        self.view.frame = CGRectMake(0, kScreenHeight - 300, kScreenWidth, 300);
        UIButton *ignorUnRed = [UIButton buttonWithType:UIButtonTypeCustom];
        [ignorUnRed setTitle:@"忽略未读" forState:UIControlStateNormal];
        [ignorUnRed setTitleColor:UIColorFromRGBAlphaHex(0xD31863, 1.0) forState:UIControlStateNormal];
        ignorUnRed.titleLabel.font = [UIFont systemFontOfSize:11];
        [ignorUnRed addTarget:self action:@selector(doIgnoreTheUnread) forControlEvents:UIControlEventTouchUpInside];
        [segementView addSubview:ignorUnRed];
        [ignorUnRed mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(wself.view.mas_top);
            make.size.mas_equalTo(CGSizeMake(60, 40));
            make.right.equalTo(segementView.mas_right).offset(-10);
        }];
    }
    else{
        scrollViewHeight = kScreenHeight - 64 - 40;
        
    }
    
    UIButton *followedButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [followedButton setTitle:@"关注好友" forState:UIControlStateNormal];
    [followedButton setTitleColor:UIColorFromRGBAlphaHex(0xD31863, 1.0) forState:UIControlStateNormal];
    followedButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [followedButton addTarget:self action:@selector(doFollowButton) forControlEvents:UIControlEventTouchUpInside];
    [segementView addSubview:followedButton];
    [followedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wself.view.mas_top);
        make.size.mas_equalTo(CGSizeMake(60, 40));
        make.centerX.equalTo(segementView.mas_centerX).offset(-40);
    }];
    
    UIButton *unFollowButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [unFollowButton setTitle:@"未关注" forState:UIControlStateNormal];
    [unFollowButton setTitleColor:UIColorFromRGBAlphaHex(0xD31863, 1.0) forState:UIControlStateNormal];
    unFollowButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [unFollowButton addTarget:self action:@selector(doUnFollowButton) forControlEvents:UIControlEventTouchUpInside];
    [segementView addSubview:unFollowButton];
    [unFollowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wself.view.mas_top);
        make.size.mas_equalTo(CGSizeMake(50, 40));
        make.centerX.equalTo(segementView.mas_centerX).offset(40);
    }];

    lineImageView = [[UIImageView alloc]init];
    lineImageView.backgroundColor = UIColorFromRGBAlphaHex(0xD31863, 1.0);
    [segementView addSubview:lineImageView];
    [lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(segementView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(60, 2));
        make.centerX.equalTo(segementView.mas_centerX).offset(-40);
    }];
    
    myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, kScreenWidth, scrollViewHeight)];
    myScrollView.showsVerticalScrollIndicator = NO;
    myScrollView.showsHorizontalScrollIndicator = NO;
    myScrollView.alwaysBounceVertical = NO;
    myScrollView.alwaysBounceHorizontal = YES;
    myScrollView.pagingEnabled = YES;
    myScrollView.scrollEnabled = YES;
    myScrollView.contentSize = CGSizeMake(kScreenWidth*2, scrollViewHeight);
    myScrollView.delegate = self;
    [self.view addSubview:myScrollView];
    
    followedTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(myScrollView.frame), CGRectGetHeight(myScrollView.frame))];
    followedTableView.delegate = self;
    followedTableView.dataSource = self;
    followedTableView.showsHorizontalScrollIndicator = NO;
    followedTableView.showsVerticalScrollIndicator = NO;
    followedTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    followedTableView.scrollEnabled = YES;
    [myScrollView addSubview:followedTableView];
    
    unFollowedTableView = [[UITableView alloc]initWithFrame:CGRectMake(CGRectGetWidth(myScrollView.frame), 0, CGRectGetWidth(myScrollView.frame), CGRectGetHeight(myScrollView.frame))];
    unFollowedTableView.delegate = self;
    unFollowedTableView.dataSource = self;
    unFollowedTableView.showsHorizontalScrollIndicator = NO;
    unFollowedTableView.showsVerticalScrollIndicator = NO;
    unFollowedTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    unFollowedTableView.scrollEnabled = YES;
    [myScrollView addSubview:unFollowedTableView];
    
    noDataLabel1 = [[UILabel alloc]init];
    noDataLabel1.font = [UIFont systemFontOfSize:13];
    noDataLabel1.frame = CGRectMake(kScreenWidth / 2 - 100, kScreenWidth / 2 + 50, 200, 100);
    noDataLabel1.text = @"没有数据";
    noDataLabel1.hidden = YES;
    noDataLabel1.font = [UIFont systemFontOfSize:16];
    noDataLabel1.text = @"空空如也~~";
    noDataLabel1.textAlignment = 1;
    
    [followedTableView addSubview:noDataLabel1];
    
    
    
    noDataLabel2 = [[UILabel alloc]init];
    noDataLabel2.font = [UIFont systemFontOfSize:13];
    noDataLabel2.frame = CGRectMake(kScreenWidth / 2 - 100, kScreenWidth / 2 + 50, 200, 100);
    noDataLabel2.text = @"没有数据";
    noDataLabel2.hidden = YES;
    noDataLabel2.font = [UIFont systemFontOfSize:16];
    noDataLabel2.text = @"空空如也~~";
    noDataLabel2.textAlignment = 1;
    
    [unFollowedTableView addSubview:noDataLabel2];
}

- (void)refreshTableView{
    dispatch_async(dispatch_get_main_queue(), ^{
        [myFollowedChatListArray removeAllObjects];
        [myUnFollowedChatListArray removeAllObjects];
        
        
        RLMResults<ChatListObject *> *chatList = [[DBManager sharedInstance] getAllChat];
        NSArray *followArray = [IVTAccountTool getMyFollowListArray];
        for (NSInteger i=0; i<chatList.count; i++) {
            ChatListObject *chatObject = [chatList objectAtIndex:i];
            BOOL isHave = NO;
            for (NSInteger j=0; j<followArray.count; j++) {
                if ([[[followArray objectAtIndex:j] valueForKey:@"id"] integerValue] == [chatObject.chatToUserId integerValue]) {
                    isHave = YES;
                    break;
                }
            }
            if (isHave) {
                [myFollowedChatListArray addObject:chatObject];
            }
            else{
                [myUnFollowedChatListArray addObject:chatObject];
            }
        }
        
        if (myFollowedChatListArray.count > 0) {
          
            noDataLabel1.hidden = YES;
            [followedTableView reloadData];
        }else{
            noDataLabel1.hidden = NO;
        }
        if (myUnFollowedChatListArray.count > 0) {
            noDataLabel2.hidden = YES;
            [unFollowedTableView reloadData];
        }else{
            noDataLabel2.hidden = NO;
        }
    });
}

- (void)setNavigationBar{
    self.navigationItem.title = @"私信";
    
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:@"忽略未读" style:UIBarButtonItemStylePlain target:self action:@selector(doIgnoreTheUnread)];
    rightBarButton.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}

- (void)doIgnoreTheUnread{
    [[DBManager sharedInstance] updataAllChatMessageToReaded];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if ([scrollView isEqual:myScrollView]) {
        if (scrollView.contentOffset.x == 0) {
            [self doFollowButton];
        }
        else{
            [self doUnFollowButton];
        }
    }
}
- (void)doFollowButton{
    [lineImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(segementView.mas_centerX).offset(-40);
    }];
    [UIView animateWithDuration:.4f animations:^{
        myScrollView.contentOffset = CGPointMake(0, 0);

        [segementView layoutIfNeeded];
    }];
}

- (void)doUnFollowButton{
    [lineImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(segementView.mas_centerX).offset(40);
    }];
    [UIView animateWithDuration:.4f animations:^{
        myScrollView.contentOffset = CGPointMake(CGRectGetWidth(myScrollView.frame), 0);
        
        [segementView layoutIfNeeded];
    }];
}

#pragma --mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == followedTableView) {
        return myFollowedChatListArray.count;
    }else if(tableView == unFollowedTableView){
        return myUnFollowedChatListArray.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    IVTMyChatListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeFriendListCell"];
    if (!cell) {
        cell = [[IVTMyChatListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MeFriendListCell"];
        cell.backgroundColor = [UIColor whiteColor];
    }
    if ([tableView isEqual:followedTableView]) {
        ChatListObject *followed = [myFollowedChatListArray objectAtIndex:indexPath.row];
        if (followed.chatToUserId != nil) {
            cell.toChatUserId = followed.chatToUserId;
            cell.headIconUrl = followed.chatToUserAvatarUrl;
            cell.userLevel = followed.chatToUserLevel;
            cell.userSex = followed.chatToUserSex;
            cell.userName = followed.chatToUserNickName;
            cell.userSignature = followed.chatToUserSignature;
            cell.numberOfMessageNoRead = followed.numberOfMessageNoRead;
        }
    }
    else if ([tableView isEqual:unFollowedTableView]){
        ChatListObject *unFollowed = [myUnFollowedChatListArray objectAtIndex:indexPath.row];
        if (unFollowed.chatToUserId != nil) {
            cell.toChatUserId = unFollowed.chatToUserId;
            cell.headIconUrl = unFollowed.chatToUserAvatarUrl;
            cell.userLevel = unFollowed.chatToUserLevel;
            cell.userSex = unFollowed.chatToUserSex;
            cell.userName = unFollowed.chatToUserNickName;
            cell.userSignature = unFollowed.chatToUserSignature;
            cell.numberOfMessageNoRead = unFollowed.numberOfMessageNoRead;
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if ([tableView isEqual:followedTableView]) {
            ChatListObject *followed = [myFollowedChatListArray objectAtIndex:indexPath.row];
            [[DBManager sharedInstance] removeAllChatMessageToUserId:followed.chatToUserId];
            [myFollowedChatListArray removeObjectAtIndex:indexPath.row];
            [followedTableView reloadData];
            [followedTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            
        }else{
            ChatListObject *unFollowed = [myUnFollowedChatListArray objectAtIndex:indexPath.row];
            [[DBManager sharedInstance] removeAllChatMessageToUserId:unFollowed.chatToUserId];
            [myUnFollowedChatListArray removeObjectAtIndex:indexPath.row];
            [unFollowedTableView reloadData];
            [unFollowedTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    IVTMyChatContentListViewController *chatContentListVC = [[IVTMyChatContentListViewController alloc]init];
    if ([tableView isEqual:followedTableView]) {
        ChatListObject *followed = [myFollowedChatListArray objectAtIndex:indexPath.row];
        chatContentListVC.chatToUserId = followed.chatToUserId;
        chatContentListVC.chatToUserNickName = followed.chatToUserNickName;
        chatContentListVC.chatToUserAvatarUrl = followed.chatToUserAvatarUrl;
        chatContentListVC.chatToUserSex = followed.chatToUserSex;
        chatContentListVC.chatToUserLevel = followed.chatToUserLevel;
        chatContentListVC.chatToUserSignature = followed.chatToUserSignature;
    }else{
        ChatListObject *unFollowed = [myUnFollowedChatListArray objectAtIndex:indexPath.row];
        chatContentListVC.chatToUserId = unFollowed.chatToUserId;
        chatContentListVC.chatToUserNickName = unFollowed.chatToUserNickName;
        chatContentListVC.chatToUserAvatarUrl = unFollowed.chatToUserAvatarUrl;
        chatContentListVC.chatToUserSex = unFollowed.chatToUserSex;
        chatContentListVC.chatToUserLevel = unFollowed.chatToUserLevel;
        chatContentListVC.chatToUserSignature = unFollowed.chatToUserSignature;
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kPrivateMessageDidChange object:nil];
    [self.navigationController pushViewController:chatContentListVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end