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

#import "CNLiveMineController.h"

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

#import "CNLiveBServices.h"
#import <CNLiveRequestBastKit/CNLiveNetworking.h>
#import "CNUserInfoManager.h"
#import "CNLiveEnvironment.h"
#import "CNLiveCategory.h"

#import "CNLiveMineHeaderView.h"
#import "CNLiveMineCell.h"
#import "CNLiveMineModel.h"
#import "CNLiveMineRequest.h"

#import "CNLiveUserInfoController.h"
#import "CNLiveSettingController.h"
#import "CNLiveHomePageProtocol.h"

@interface CNLiveMineController()<UITableViewDelegate, UITableViewDataSource, CNLiveMineHeaderViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) CNLiveMineHeaderView *headerView;
@property (nonatomic, strong) NSMutableArray *data;

@end

@implementation CNLiveMineController
static const NSInteger kHEIGHT = 253;
static const CGFloat timeValue = 1.5;

#pragma mark - 加载数据
- (void)loadData {
    NSArray *data = @[
//                      @{
//                          @"name":@"直播中心",
//                          @"image":@"CNLiveBMineModule.bundle/mine_zbzx",
//                      },
//                      @{
//                          @"name":@"钱包",
//                          @"image":@"CNLiveBMineModule.bundle/mine_qb",
//                      },
                      @{
                          @"name":@"设置",
                          @"image":@"CNLiveBMineModule.bundle/mine_sz",
                      }
                      ];
    for(NSDictionary *dic in data){
        CNLiveMineModel *model = [CNLiveMineModel mj_objectWithKeyValues:dic];
        [self.data addObject:model];
    }
    [self.tableView reloadData];
    
}

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

//    [self requestCertification];
    if(self.headerView){
        self.headerView.frame = CGRectMake(0, -kHEIGHT, SCREEN_WIDTH, kHEIGHT);
    }
    self.headerView.isRefresh = YES;
    [self.tableView reloadData];
}

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

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

#pragma mark - UIScrollViewDelegate
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    CGPoint point = scrollView.contentOffset;
//    if (point.y < -kHEIGHT) {
//        CGRect rect = [self.tableView viewWithTag:666].frame;
//        rect.origin.y = point.y;
//        rect.size.height = -point.y;
//        [self.tableView viewWithTag:666].frame = rect;
//    }
//}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CNLiveMineCell *cell = [tableView dequeueReusableCellWithIdentifier:kCNLiveMineCell];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.data[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CNLiveSettingController *vc = [[CNLiveSettingController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - CNLiveBMineHeaderViewDelegate
- (void)headerView:(CNLiveMineHeaderView *)headerView pushToController:(CNLiveMineHeaderViewType)type{
    switch (type) {
        case CNLiveMineHeaderViewTypeUerInfo:
        {
            CNLiveUserInfoController *vc = [[CNLiveUserInfoController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CNLiveMineHeaderViewTypeHome:
        {
            id<CNLiveHomePageProtocol> service = [[BeeHive shareInstance] createService:@protocol(CNLiveHomePageProtocol)];
            UIViewController *vc = [service pushLiveHomePageViewController];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
    }
}

#pragma mark - 请求方法
// 请求用户认证
- (void)requestCertification{
    [CNLiveMineRequest certification:^(BOOL isSuccess, NSDictionary * _Nonnull data, NSError * _Nonnull error) {
        if (error.code == -1001 || error.code == -1009) {
            [QMUITips showError:@"网络连接已断开" inView:self.view hideAfterDelay:timeValue];
            return ;
        }
        if (isSuccess) {
            NSString *darenIdentity = [NSString stringWithFormat:@"%@",[data objectForKey:@"darenIdentity"]];
            if ([darenIdentity isEqualToString:@"1"]) { //普通用户
                CNUserShareModel.isDaren = 0;
                self.headerView.certificationImages = @[@"mine_wjj_certification",@"mine_common",@"mine_overbadge"];
          
            } else if ([darenIdentity isEqualToString:@"0"]) { //达人用户
                CNUserShareModel.isDaren = 1;
                self.headerView.certificationImages = @[@"mine_wjj_certification",@"mine_doyen", @"mine_overbadge"];

            }
        }
    }];
}

#pragma mark - 状态栏
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
    
}

#pragma mark - 响应方法

#pragma mark - 懒加载
- (NSMutableArray *)data {
    if (!_data) {
        _data = [NSMutableArray array];
    }
    return _data;
}
- (CNLiveMineHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[CNLiveMineHeaderView alloc]initWithFrame:CGRectMake(0, -kHEIGHT, SCREEN_WIDTH, kHEIGHT)];
        _headerView.delegate = self;
        _headerView.tag = 666;
    }
    return _headerView;
}
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-TabBarHeight) style:UITableViewStylePlain];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[CNLiveMineCell class] forCellReuseIdentifier:kCNLiveMineCell];
        _tableView.contentInset = UIEdgeInsetsMake(kHEIGHT, 0, 0, 0);
    }
    return _tableView;
}

@end