CNLiveMineController.m 13.7 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 <CNLiveRequestBastKit/CNLiveNetworking.h>
#import "CNUserInfoManager.h"
#import "CNLiveEnvironment.h"
#import "CNLiveCategory.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;
static const CGFloat timeValue = 1.5;

#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_home_box",@"mine_gift_box",@"mine_tool_map"]
                          },
                      @{
                          @"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:
        {
            id<CNLiveQrCodeServiceProtocol> service = [[BeeHive shareInstance] createService:@protocol(CNLiveQrCodeServiceProtocol)];
            [service pushToMyQrCodeController:NO];
        }
            break;
        case CNLiveMineHeaderViewTypeSign:
        {
            [self requestSignIn];
        }
            break;
        case CNLiveMineHeaderViewTypeWintess:
        {

        }
            break;
    }
}

- (void)requestSignIn{
    NSDictionary *params = @{@"sid":CNUserShareModel.uid};
    [CNLiveNetworking setAllowRequestDefaultArgument:YES];
    [CNLiveNetworking requestNetworkWithMethod:CNLiveRequestMethodPOST URLString:CNIntegralSignInUrl Param:params CacheType:CNLiveNetworkCacheTypeNetworkOnly CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {

        if (error.code == -1001 || error.code == -1009) {
            [QMUITips showError:@"网络连接已断开" inView:self.view hideAfterDelay:timeValue];
            return ;
        }
        if (!error) {
            if(responseObject[@"data"] && [responseObject[@"data"] isKindOfClass:[NSDictionary class]]){
                id<CNLiveIntegralServiceProtocol> service = [[BeeHive shareInstance] createService:@protocol(CNLiveIntegralServiceProtocol)];
                NSDictionary *dic = responseObject[@"data"];
                NSInteger status = [dic[@"status"] integerValue];
                if (status == 1) {
                    [service showGoldWhereaboutsViewWithDic:dic];
                    
                }else{
                    [service showSignInViewWithDic:dic];
                }
                
            }

        }else{
            [QMUITips showError:@"签到失败" inView:self.view hideAfterDelay:timeValue];
            return ;

        }

    }];
}

- (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];

        }
    }
   
    CGFloat offsetToShow = 50.0;//滑动多少就完全显示
    
    CGFloat offsetY = scrollView.contentOffset.y+kHEIGHT;
    if (offsetY <= 0) {
        _navigationBar.alpha = 0.0;
    }
    else if (offsetY > offsetToShow) {
        _navigationBar.alpha = 1.0;
    }
    else {
        CGFloat alpha = MIN((offsetY / offsetToShow), 1);
        _navigationBar.alpha = alpha;
    }

}

#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){
                // 设置
                id<CNLiveSettingServiceProtocol> service = [[BeeHive shareInstance] createService:@protocol(CNLiveSettingServiceProtocol)];
                [service pushToSettingViewController];
            }
            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, 0, SCREEN_WIDTH, NavigationContentTop)];
        _navigationBar.backgroundColor = [UIColor colorWithRed:32/255.0 green:31/255.0 blue:36/255.0 alpha:1.0];
        _navigationBar.alpha = 0.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 getImageWithImageName:@"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{
    id<CNLiveIntegralServiceProtocol> service = [[BeeHive shareInstance] createService:@protocol(CNLiveIntegralServiceProtocol)];
    [service pushToTaskListViewController];
}

@end