IVTMeManagerViewController.m 8.33 KB
//
//  IVTMeManagerViewController.m
//  IVTMeLiveTwo
//
//  Created by xrg on 16/9/8.
//  Copyright © 2016年 QLQ. All rights reserved.
//

#import "IVTMeManagerViewController.h"
#import "Masonry.h"
#import "UIColor+IVTMeColor.h"
#import "UIFont+IVTMeFont.h"
#import "IVTMeManageCell.h"
#import "SVProgressHUD.h"
@interface IVTMeManagerViewController ()<UITableViewDataSource,UITableViewDelegate,XMPPManagerDelegate>
@property (nonatomic, retain) UITableView      *hintTable;
@property (nonatomic, retain) NSMutableArray *dataArray;

@end

@implementation IVTMeManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [[NSMutableArray alloc] initWithCapacity:10];
    
    self.view.backgroundColor = [UIColor IVTBackgroundColor];

//    [self setNavigationBar];
    
    [self buildHintTable];
    
    
    [XMPPManager sharedInstance].delegate = self;
    
    [[XMPPManager sharedInstance] getManagersList];
    // Do any additional setup after loading the view.
}


//- (void)setNavigationBar{
//    self.navigationItem.title = @"昵称";
//    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(backToTopPage:)];
//    rightBarButton.tintColor = [UIColor whiteColor];
//    self.navigationItem.rightBarButtonItem = rightBarButton;
//}

- (void)buildHintTable
{
    if (_hintTable==nil) {
        self.hintTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-58) style:UITableViewStyleGrouped];
        _hintTable.backgroundColor = [UIColor IVTBackgroundColor];
        _hintTable.delegate = self;
        _hintTable.dataSource = self;
        _hintTable.showsHorizontalScrollIndicator = NO;
        _hintTable.showsVerticalScrollIndicator = NO;
        _hintTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        //        _hintTable.sectionHeaderHeight = 20;
        [self.view addSubview:_hintTable];
        
    }
}

- (void)didGetManagersList:(NSArray *)list
{
    if (_dataArray.count > 4) {
        [SVProgressHUD setMinimumDismissTimeInterval:2];
        [SVProgressHUD showSuccessWithStatus:@"管理员人数已达到上线"];
    }else{
    for (NSInteger i=0; i<list.count; i++) {
        DDXMLElement *element = [list objectAtIndex:i];
        DDXMLNode *node = [element attributeForName:@"jid"];
        NSString *currentJid = node.stringValue;
        NSRange range = [currentJid rangeOfString:@"@"];
        NSString *currentUserId = [currentJid substringToIndex:range.location];
        if (![currentUserId isEqualToString:[IVTAccountTool getUserId]]) {
        [_dataArray addObject:[NSDictionary dictionaryWithObject:currentUserId forKey:@"userID"]];
            [self getUserInfoForUserInCurrentRoom:currentUserId];
            }
        }
    }
}

- (void)didGetModeratorsList:(NSArray *)list{
    for (NSInteger i=0; i<list.count; i++) {
        DDXMLElement *element = [list objectAtIndex:i];
        DDXMLNode *node = [element attributeForName:@"jid"];
        NSString *currentJid = node.stringValue;
        NSRange range = [currentJid rangeOfString:@"@"];
        NSString *currentUserId = [currentJid substringToIndex:range.location];
        [_dataArray addObject:[NSDictionary dictionaryWithObject:currentUserId forKey:@"userID"]];
        [self getUserInfoForUserInCurrentRoom:currentUserId];
    }
}
- (void)didNotGetModeratorsList{
    
}
- (void)getUserInfoForUserInCurrentRoom:(NSString *)userId{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    
    NSDictionary *dicInfo = [[NSBundle mainBundle] infoDictionary];
    [dict setObject:userId forKey:@"userid"];
    [dict setObject:[IVTAccountTool getUserName] forKey:@"username"];
    [dict setObject:[IVTAccountTool getPassword] forKey:@"captcha"];
    [dict setObject:[[UIDevice currentDevice] model] forKey:@"model"];
    [dict setObject:[[UIDevice currentDevice] identifierForVendor] forKey:@"deviceID"];
    [dict setObject:[NSString stringWithFormat:@"%f",kScreenWidth] forKey:@"width"];
    [dict setObject:[NSString stringWithFormat:@"%f",kScreenHeight] forKey:@"heigth"];
    [dict setObject:[dicInfo objectForKey:@"CFBundleShortVersionString"] forKey:@"appversion"];
    [dict setObject:@"apple" forKey:@"brand"];
    [dict setObject:@"" forKey:@"msisdn"];
    [dict setObject:@"" forKey:@"otherinfo"];
    [dict setObject:@"iOS" forKey:@"platform"];
    
    [[IVTNetworkAPIClient sharedClient] getUserInfoForUserId:userId success:^(NSDictionary *successData) {
        NSDictionary *userInfo = [successData objectForKey:@"data"];
        for (NSInteger i=0; i<_dataArray.count; i++) {
            if ([[[_dataArray objectAtIndex:i] valueForKey:@"userID"] integerValue] == [[userInfo valueForKey:@"id"] integerValue]) {
                NSMutableDictionary *dict = [[_dataArray objectAtIndex:i] mutableCopy];
                [dict addEntriesFromDictionary:userInfo];
                [_dataArray replaceObjectAtIndex:i withObject:[dict copy]];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.hintTable reloadData];
        });
    } error:^(NSDictionary *successData) {
        
    } failure:^(NSError *failureError) {
        
    }];
}


#pragma mark - table datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [_dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    IVTMeManageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blackListCell"];
    if (!cell) {
        cell = [[IVTMeManageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"blackListCell"];
        cell.backgroundColor  =[UIColor IVTTitleColor];
        [cell addSubview:[self addLineView]];
    }
    cell.entity = [_dataArray objectAtIndex:indexPath.row];
    
    return cell;
}

#pragma mark - table delegate

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 59;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(canncelAsAdministrator:)]) {
            [self.delegate canncelAsAdministrator:[_dataArray objectAtIndex:indexPath.row]];
        }
        [_dataArray removeObjectAtIndex:indexPath.row];
        [_hintTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.title = [NSString stringWithFormat:@"当前管理员:(%ld / 5)",_dataArray.count];
        });
        [self.hintTable reloadData];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"取消管理";
}

- ( CGFloat )tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section

{

    return 30.0 ;

}



//-(void)tableView:(UITableView *)tableView  commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath
//{
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        [_dataArray removeObjectAtIndex:indexPath.row];
//        [_hintTable  deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//
//    }
//}

//  设置标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    self.title = [NSString stringWithFormat:@"当前管理员:(%ld / 5)",_dataArray.count];
    return self.title;
}


- (UIView*)addLineView
{
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 59.0-0.5, kScreenWidth, 0.5)];
    line.backgroundColor = [UIColor IVTSeparatorColor];
    return line;
}




//返回按钮点击
-(void)backToTopPage:(id)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


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


@end