CNLiveUserInfoController.m 6.73 KB
//
//  CNLiveBMineController.m
//  CNLiveBMineModule
//
//  Created by 153993236@qq.com on 11/12/2019.
//  Copyright (c) 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveUserInfoController.h"
#import "MJExtension.h"
#import "QMUIKit.h"

#import "CNUserInfoManager.h"
#import "CNLiveServices.h"
#import "CNLiveNavigationBar.h"

#import "CNLiveUserInfoCell.h"
#import "CNLiveUserInfoModel.h"
#import "CNLiveInfoUpdateController.h"
#import "CNLiveUserInfoNotification.h"

@interface CNLiveUserInfoController ()<UITableViewDelegate, UITableViewDataSource>{
    UIImage *_image;
}
@property (nonatomic, strong) CNLiveNavigationBar *navigationBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *data;

@end

@implementation CNLiveUserInfoController
#pragma mark - 加载数据
- (void)loadData {
    NSArray *data = @[
                      @{
                          @"layout":@"2",
                          @"data":@"0",
                          @"title":@"头像",
                          @"info":CNUserShareModel.faceUrl?CNUserShareModel.faceUrl:@"",
                          @"height":@"81",
                          @"lineHeight":@"0.5"
                          },
                      @{
                          @"layout":@"1",
                          @"data":@"1",
                          @"title":@"昵称",
                          @"info":CNUserShareModel.nickname?CNUserShareModel.nickname:@"",
                          @"height":@"51",
                          @"lineHeight":@"0.5"
                          },
                      @{
                          @"layout":@"1",
                          @"data":@"2",
                          @"title":@"性别",
                          @"info":CNUserShareModel.gender?CNUserShareModel.gender:@"未知",
                          @"height":@"60",
                          @"lineHeight":@"10"
                          },
                      @{
                          @"layout":@"1",
                          @"data":@"3",
                          @"title":@"地区",
                          @"info":CNUserShareModel.location?CNUserShareModel.location:@"",
                          @"height":@"60",
                          @"lineHeight":@"10"
                          }
                      
                      ];
    for(NSDictionary *dic in data){
        [self.data addObject:[CNLiveUserInfoModel mj_objectWithKeyValues:dic]];
    }
    [self.tableView reloadData];
    
}

#pragma mark - 生命周期
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if (@available(iOS 11, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [CNLiveUserInfoNotification addUserInfoUpdateObserver:self selector:@selector(userInfoUpdateNotification:)];

    [self createSubViews];
    [self loadData];
    
}

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

- (void)createSubViews {
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.tableView];

}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CNLiveUserInfoModel *model = self.data[indexPath.row];
    return model.height;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CNLiveUserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kCNLiveUserInfoCell];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CNLiveUserInfoModel *model = self.data[indexPath.row];
    cell.model = model;
    if(_image){
        cell.image = _image;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CNLiveUserInfoModel *model = self.data[indexPath.row];
    switch (model.dataType) {
        case CNLiveUserInfoDataTypeCode:
        {
            id<CNLiveQrCodeServiceProtocol> service = [[BeeHive shareInstance] createService:@protocol(CNLiveQrCodeServiceProtocol)];
            [service pushToMyQrCodeController:NO];
        }
            break;
        default:
        {
            CNLiveInfoUpdateController *vc = [[CNLiveInfoUpdateController alloc]initWithModel:model];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
    }
}

#pragma mark - 监听
// 更新用户信息
- (void)userInfoUpdateNotification:(NSNotification *)noti{
    //使用userInfo处理消息
    NSDictionary *dic = [noti userInfo];
    CNLiveUserInfoDataType dataType = [[dic objectForKey:@"dataType"] integerValue];
    NSString *info = [dic objectForKey:@"info"];
    
    // 更新cell展示信息
    for(CNLiveUserInfoModel *model in self.data){
        if(dataType == model.dataType){
            model.info = info;
            break;
        }
    }
    [self.tableView reloadData];

}

#pragma mark - 懒加载
- (CNLiveNavigationBar *)navigationBar {
    if (!_navigationBar) {
        _navigationBar = [CNLiveNavigationBar navigationBar];
        _navigationBar.title.text = @"个人资料";
        __weak typeof(self) weakSelf = self;
        _navigationBar.onClickLeftButton = ^{
            [weakSelf goBack];
        };
    }
    return _navigationBar;
}

- (NSMutableArray *)data {
    if (!_data) {
        _data = [NSMutableArray array];
    }
    return _data;
}

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[CNLiveUserInfoCell class] forCellReuseIdentifier:kCNLiveUserInfoCell];
    }
    return _tableView;
}

#pragma mark - 响应方法
- (void)goBack {
    if (self.presentingViewController != nil) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end