CNLiveMineController.m 13.1 KB
//
//  CNLiveMineController.m
//  CNLiveMineModule
//
//  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 "CNLiveServices.h"

#import "CNLiveMineNavigationBar.h"
#import "CNLiveMineHeaderView.h"
#import "CNLiveMineCell.h"
#import "CNLiveMineModel.h"
#import "CNLiveUserInfoController.h"

#import "CNLiveMyQrCodeController.h"
#import "CNLiveSettingController.h"

// 积分
#import "CNLiveTaskListController.h"
#import "CNLiveSignInView.h"

@interface CNLiveMineController()<UITableViewDelegate, UITableViewDataSource, CNLiveMineHeaderViewDelegate, CNLiveMineCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, weak) CNLiveMineHeaderView *headerView;
@property (nonatomic, strong) CNLiveMineNavigationBar *navigationBar;
@property (nonatomic, strong) UIButton *soccerBtn;
@property (nonatomic, strong) NSMutableArray *data;

@end

@implementation CNLiveMineController
static const NSInteger kHEIGHT = 250;

#pragma mark - 加载数据
- (void)loadData {
    NSArray *data = @[
                      @{
                          @"name":@"我的订单",
                          @"style":@"1",
                          @"margin":@"10",
                          @"titles":@[@"订单", @"待付款", @"待收货",@"待评价",@"售后"],
                          @"images":@[@"mine_order",@"mine_pending",@"mine_received",@"mine_evaluate",@"mine_refund"]
                          },
                      @{
                          @"name":@"常用工具",
                          @"style":@"2",
                          @"margin":@"10",
                          @"titles": @[@"收货地址", @"补贴余额", @"我的收藏",@"发布记录",@"邀请好友",@"设置",@"路径工具",@"小家音响"],
                          @"images":@[@"mine_address",@"mine_card",@"mine_like",@"mine_release",@"mine_invitation",@"mine_set",@"mine_tool_map",@"mine_home_box"]
                          },
                      @{
                          @"name":@"购买的商品",
                          @"pic":@"mine_buy_commodity",
                          @"style":@"3",
                          @"margin":@"10",
                          @"titles": @[@"电子票"],
                          @"images":@[@"mine_e-ticket"]
                          },
                      @{
                          @"name":@"大众体育",
                          @"pic":@"mine_china_sport",
                          @"style":@"4",
                          @"margin":@"10",
                          @"titles": @[@"我的活动", @"我的记录"],                          @"images":@[@"mine_activity",@"mine_record"]
                          
                          }
                      ];
    for(NSDictionary *dic in data){
        [self.data addObject:[CNLiveMineModel mj_objectWithKeyValues:dic]];
    }
    [self.tableView reloadData];
    
}

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

    if(self.headerView){
        self.headerView.frame = CGRectMake(0, -kHEIGHT, SCREEN_WIDTH, kHEIGHT);
    }
    if(self.soccerBtn){
        self.soccerBtn.frame = CGRectMake(SCREEN_WIDTH-67-10, SCREEN_HEIGHT-71-10-TabBarHeight, 67, 71);
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:244.0/255.0 green:244.0/255.0 blue:244.0/255.0 alpha:1.0];
    if (@available(iOS 11, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self createSubViews];
    [self loadData];
}

- (void)dealloc {
    
    
}

- (void)createSubViews {
    [self.view addSubview:self.tableView];
    CNLiveMineHeaderView *headerView = [[CNLiveMineHeaderView alloc]initWithFrame:CGRectMake(0, -kHEIGHT, SCREEN_WIDTH, kHEIGHT)];
    headerView.delegate = self;
    headerView.tag = 666;
    [self.tableView addSubview:headerView];
    self.headerView = headerView;
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.soccerBtn];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CNLiveMineModel *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 {
    CNLiveMineCell *cell = [tableView dequeueReusableCellWithIdentifier:kCNLiveMineCell];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    CNLiveMineModel *model = self.data[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
#pragma mark - CNLiveMineHeaderViewDelegate
- (void)headerView:(CNLiveMineHeaderView *)headerView pushToController:(CNLiveMineHeaderViewType)type{
    switch (type) {
        case CNLiveMineHeaderViewTypeUerInfo:
        {
            CNLiveUserInfoController *vc = [[CNLiveUserInfoController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CNLiveMineHeaderViewTypeCode:
        {
            CNLiveMyQrCodeController *vc = [[CNLiveMyQrCodeController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CNLiveMineHeaderViewTypeSign:
        {
            id<CNLiveIntegralServiceProtocol> service = [[BeeHive shareInstance] createService:@protocol(CNLiveIntegralServiceProtocol)];
            [service showSignInViewWithScore:@"asd" scores:@"312" day:2 url:@"http://www.baidu.com"];
//            [CNLiveSignInView showWithScore:@"asd" scores:@"312" day:1 url:@"http://www.baidu.com"];
        }
            break;
        case CNLiveMineHeaderViewTypeWintess:
        {

        }
            break;
    }
}

- (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;
        if(point.y < -320){
//            XWWitnessController *vc = [[XWWitnessController alloc]init];
//            CATransition *transition = [CATransition animation];
//            transition.duration = 0.5;
//            transition.timingFunction = [CAMediaTimingFunction
//                                         functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//            transition.type = kCATransitionPush;
//            transition.subtype = kCATransitionFromBottom;
//            [self.view.window.layer addAnimation:transition forKey:nil];
//            [self.navigationController pushViewController:vc animated:NO];

        }
    }
   
    if (point.y > -200) {
        [UIView animateWithDuration:0.25 animations:^{
            self.navigationBar.frame = CGRectMake(0, 0, SCREEN_WIDTH, NavigationContentTop);
        }];
  
    } else {
        [UIView animateWithDuration:0.25 animations:^{
            self.navigationBar.frame = CGRectMake(0, -NavigationContentTop, SCREEN_WIDTH, NavigationContentTop);
        }];
   
    }

}

#pragma mark - CNLiveMineCellDelegate
- (void)cell:(CNLiveMineCell *)cell clickedButtonWithType:(CNLiveMineCellType)type tag:(NSInteger)tag{
    switch (type) {
        case CNLiveMineCellTypeOrder:
        {
            if(tag-10000 == 4){
                //售后
                
            }else{
                id<CNLiveOrderServiceProtocol> service = [[BeeHive shareInstance] createService:@protocol(CNLiveOrderServiceProtocol)];
                [service pushToOrderViewController:tag-10000];
            }
        }
            break;
        case CNLiveMineCellTypeTool:
        {
            if(tag == 10000){
                // 收货地址
//                XWAddressController *vc = [[XWAddressController alloc]init];
//                [self.navigationController pushViewController:vc animated:YES];
            }
            else if(tag == 10001){
                // 余额
//                XWBalanceController *vc = [[XWBalanceController alloc]init];
//                [self.navigationController pushViewController:vc animated:YES];
            }
            else if(tag == 10002){
                // 收藏
//                XWCollectionController *vc = [[XWCollectionController alloc]init];
//                [self.navigationController pushViewController:vc animated:YES];
            }
            else if(tag == 10003){
                // 发布记录
//                XWOrdersController *vc = [[XWOrdersController alloc]init];
//                [self.navigationController pushViewController:vc animated:YES];
            }
            else if(tag == 10004){
                // 邀请好友
//                XWOrdersController *vc = [[XWOrdersController alloc]init];
//                [self.navigationController pushViewController:vc animated:YES];
            }
            else if(tag == 10005){
                // 设置
                CNLiveSettingController *vc = [[CNLiveSettingController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            else if(tag == 10006){
                //路径工具
//                XWOrdersController *vc = [[XWOrdersController alloc]init];
//                [self.navigationController pushViewController:vc animated:YES];
            }
        }
            break;
        case CNLiveMineCellTypeGoods:
        {
            
        }
            break;
        case CNLiveMineCellTypeSports:
        {
            
        }
            break;
            
        default:
            break;
    }
    
}

#pragma mark - 状态栏
/**
 * 状态栏的颜色
 *
 * @return UIStatusBarStyle
 *
 */
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
    
}

#pragma mark - 懒加载
- (NSMutableArray *)data {
    if (!_data) {
        _data = [NSMutableArray array];
    }
    return _data;
}
- (CNLiveMineNavigationBar *)navigationBar {
    if (!_navigationBar) {
        _navigationBar = [[CNLiveMineNavigationBar alloc]initWithFrame:CGRectMake(0, -NavigationContentTop, SCREEN_WIDTH, NavigationContentTop)];
        _navigationBar.backgroundColor = [UIColor colorWithRed:32/255.0 green:31/255.0 blue:36/255.0 alpha:1.0];
    }
    return _navigationBar;
}

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-TabBarHeight) style:UITableViewStylePlain];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithRed:244.0/255.0 green:244.0/255.0 blue:244.0/255.0 alpha:1.0];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[CNLiveMineCell class] forCellReuseIdentifier:kCNLiveMineCell];
        _tableView.contentInset = UIEdgeInsetsMake(kHEIGHT, 0, 0, 0);
    }
    return _tableView;
}

- (UIButton *)soccerBtn{
    if(!_soccerBtn){
        _soccerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _soccerBtn.adjustsImageWhenHighlighted = NO;
        _soccerBtn.frame = CGRectMake(SCREEN_WIDTH-67-10, SCREEN_HEIGHT-71-10-TabBarHeight, 67, 71);
        UIImage *xw_image = [self xw_getImageName:@"mine_soccer_task" bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineController class]];
        [_soccerBtn setImage:xw_image forState:UIControlStateNormal];
        [_soccerBtn addTarget:self action:@selector(soccerDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _soccerBtn;
}

#pragma mark - 响应方法
- (void)soccerDidClicked:(UIButton *)btn{
    CNLiveTaskListController *vc = [[CNLiveTaskListController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

/**
 *  创建图片
 *
 *  @param imageName 图片名字
 *  @param bundleName 图片所在的bundle名字
 *  @param targetClass 类和bundle的同级目录
 *  @return 返回UIImage
 */
- (UIImage *)xw_getImageName:(NSString *)imageName bundleName:(NSString *)bundleName targetClass:(Class)targetClass{
    NSBundle *bundle = [NSBundle bundleForClass:targetClass];
    NSURL *url = [bundle URLForResource:bundleName withExtension:@"bundle"];
    NSBundle *targetBundle = [NSBundle bundleWithURL:url];
    UIImage *image = [UIImage imageNamed:imageName inBundle:targetBundle compatibleWithTraitCollection:nil];
    return image?image:[UIImage imageNamed:imageName inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
}

@end