CNLiveBindingManageController.m 7.72 KB
//
//  CNLiveBindingManageController.m
//  CNLiveNetAdd
//
//  Created by CNLive-zxw on 2019/9/10.
//  Copyright © 2019 cnlive. All rights reserved.
//

#import "CNLiveBindingManageController.h"
#import <Masonry/Masonry.h>
#import "CNLiveBaseKit.h"

#import "CNLiveBindingManageHeaderView.h"
#import "CNLiveBindingManageCell.h"
#import "CNLiveBindingManageModel.h"
#import "CNLiveBindingManageRequest.h"

@interface CNLiveBindingManageController ()<UITableViewDelegate, UITableViewDataSource,CNLiveBindingManageCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *bindingData;
@property (nonatomic, strong) NSArray *unBindingData;

@end

@implementation CNLiveBindingManageController
#pragma mark - CNLiveBindingManageCellDelegate
- (void)clickedBindingButton:(CNLiveBindingManageCell *)cell isBinding:(BOOL)isBinding{
    if(isBinding){
        //绑定操作
        [self requestBindingManage:cell.model];

    }else{
        //解除绑定操作
        UIAlertController *actionAlert1 = [UIAlertController alertControllerWithTitle:@"确定解除绑定?" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:NULL];
        __weak typeof(self) weakSelf = self;
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if(!strongSelf) return;
            [strongSelf requestBindingManage:cell.model];
            
        }];
        [action1 setValue:RGBOF(0x23D41E) forKey:@"_titleTextColor"];
        [action2 setValue:RGBOF(0x23D41E) forKey:@"_titleTextColor"];
        
        [actionAlert1 addAction:action1];
        [actionAlert1 addAction:action2];
        [self presentViewController:actionAlert1 animated:YES completion:nil];
    }
    
}
- (void)requestBindingManage:(CNLiveBindingManageModel *)model{
//    switch (model.type) {
//        case CNLiveSendAuthRespWX:
//        {
//            if(![WXApi isWXAppInstalled]){
//                [QMUITips showWithText:@"未安装微信" inView:self.view hideAfterDelay:1.5];
//                return;
//            }
//        }
//            break;
//        case CNLiveSendAuthRespQQ:
//        {
//            if(![TencentOAuth iphoneQQInstalled]){
//                [QMUITips showWithText:@"未安装QQ" inView:self.view hideAfterDelay:1.5];
//                return;
//            }
//        }
//            break;
//        case CNLiveSendAuthRespWB:
//        {
//            if(![WeiboSDK isWeiboAppInstalled]){
//                [QMUITips showWithText:@"未安装微博" inView:self.view hideAfterDelay:1.5];
//                return;
//            }
//        }
//            break;
//        case CNLiveSendAuthRespApple:
//        {
//        }
//            break;
//    }
    __weak typeof(self) weakSelf = self;
    [CNLiveBindingManageRequest requestModel:model block:^(BOOL isSuccess, CNLiveBindingManageModel * _Nullable model, BOOL isBinding) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if(!strongSelf) return;
        if(isSuccess){
            [strongSelf loadData];
            
        }
        
    }];
}
#pragma mark - Data
- (void)loadData{
    __weak typeof(self) weakSelf = self;
    [CNLiveBindingManageRequest requestModels:^(NSArray * _Nullable bindingData, NSArray * _Nullable unBindingData) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if(!strongSelf) return;
        strongSelf.bindingData = bindingData;
        strongSelf.unBindingData = unBindingData;
        [strongSelf.tableView reloadData];
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGBOF(0xf4f4f4);
    self.title = @"绑定管理";
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self loadData];
    [self createUI];
    
}

- (void)createUI{
    [self.view addSubview:self.tableView];
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(kVerticalStatusHeight+44);
        make.left.equalTo(self.view.mas_left).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(-0);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-0);
        
    }];
    
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0){
        return self.bindingData.count;

    }else if(section == 1){
        return self.unBindingData.count;

    }else{
        return 0;
    }
    
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CNLiveBindingManageCell *cell = [tableView dequeueReusableCellWithIdentifier:kCNLiveBindingManageCell forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    if(indexPath.section == 0){
        if(self.bindingData.count > 0){
            cell.model = self.bindingData[indexPath.row];
        }
        
    }else if(indexPath.section == 1){
        if(self.unBindingData.count > 0){
            cell.model = self.unBindingData[indexPath.row];
        }
    }
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0000001f;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSArray *array = @[@"您已绑定网家家手机号账号", @"其他绑定方式"];
    CNLiveBindingManageHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kCNLiveBindingManageHeaderView];
    header.text = array[section];
    return header;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
}

/*
 #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.
 }
 */

#pragma mark - Lazy loading
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = RGBOF(0xf4f4f4);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        [_tableView registerClass:[CNLiveBindingManageCell class] forCellReuseIdentifier:kCNLiveBindingManageCell];
        [_tableView registerClass:[CNLiveBindingManageHeaderView class] forHeaderFooterViewReuseIdentifier:kCNLiveBindingManageHeaderView];

    }
    return _tableView;
}

@end