IVTMeManageVC.m 8.66 KB
//
//  IVTMeManageVC.m
//  IVTMeLive
//
//  Created by XRG on 16/5/13.
//  Copyright © 2016年 Joky. All rights reserved.
//

#import "IVTMeManageVC.h"
#import "Masonry.h"
#import "UIColor+IVTMeColor.h"
#import "UIFont+IVTMeFont.h"
#import "IVTMeManageCell.h"
#import "SVProgressHUD.h"
#define userInfoUrl [NSString stringWithFormat:@"%@%@",BaseURL,@"user/requestUserInfo.json"]

@interface IVTMeManageVC ()<UITableViewDataSource,UITableViewDelegate,XMPPManagerDelegate>
@property (nonatomic, retain) UITableView      *hintTable;
@property (nonatomic, retain) NSMutableArray *dataArray;




@end



@implementation IVTMeManageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArray = [[NSMutableArray alloc] initWithCapacity:10];

    self.view.backgroundColor = [UIColor IVTBackgroundColor];
    [self initNav];
    [self buildHintTable];

    [XMPPManager sharedInstance].delegate = self;
    
    [[XMPPManager sharedInstance] getManagersList];

}
-(void)initNav{
    
    
    //整体背景nav view
    UIView* navBgView = [[UIView alloc] init];
    [self.view addSubview:navBgView];
    navBgView.backgroundColor  = [UIColor IVTLightRedColor];
    __unsafe_unretained typeof (self) wself = self;
    [navBgView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(wself.view.mas_left);
        make.right.equalTo(wself.view.mas_right);
        make.top.equalTo(@0);
        make.height.equalTo(@58);
    }];
    
    UILabel *setting = [[UILabel alloc]init];
    [navBgView addSubview:setting];
    setting.text = @"管理员列表";
    setting.textColor = [UIColor YXColorWithHexCode:@"#FFFFFF"];
    setting.font = [UIFont YXFontOfSize:19];
    setting.textAlignment = NSTextAlignmentCenter;
    [setting mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(navBgView.mas_centerX);
        make.top.equalTo(wself.view.mas_top).with.offset(29);
        make.size.mas_equalTo(CGSizeMake(100.0f, 19.0f));
    }];
    
    //背景nav 返回上级页面按钮
    UIButton* backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [navBgView addSubview:backButton];
    UIImage* navImage = [UIImage imageNamed:@"返回按钮点击"];
    [backButton mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(wself.view);
        make.top.equalTo(wself.view.mas_top).with.offset(18);
        make.size.mas_equalTo(CGSizeMake(40, 40));
        
    }];
    [backButton setImage:navImage forState:UIControlStateNormal];
    backButton.imageEdgeInsets = UIEdgeInsetsMake(5,5,5,5);
    [backButton addTarget:self action:@selector(backToTopPage:) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)buildHintTable
{
    if (_hintTable==nil) {
        self.hintTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 58, 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)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:[IVTAccountTool get] forKey:@"deviceID"];
    [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:^(ErrorCode errorCode) {
        
    } 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
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    return 59;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [[XMPPManager sharedInstance] setManagerForUserId:[_dataArray objectAtIndex:indexPath.row][@"id"] block:^(BOOL isSuccess) {
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [_dataArray removeObjectAtIndex:indexPath.row];

            if (self.delegate && [self.delegate respondsToSelector:@selector(cancelAdmin:)]) {
                [self.delegate cancelAdmin:[_dataArray objectAtIndex:indexPath.row]];
            }
        }];
    }
}

- (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 = @"当前管理员:(2/5)";
//    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