CNLiveCMineInteViewController.m 11 KB
//
//  CNLiveCMineInteViewController.m
//  CNLiveCMineModule
//
//  Created by open on 2021/12/27.
//

#import "CNLiveCMineInteViewController.h"
#import "CNLiveCMineInteCollectionViewCell.h"
#import "CNLiveCMineInteHeaderCustomView.h"
#import "CNLiveCMineInteHeaderView.h"
#import "CNLiveCMineInteViewModel.h"
#import "CNLiveCMineInteModel.h"
@interface CNLiveCMineInteViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) NSMutableArray * dataArray;//总数据源
@property (nonatomic, strong) CNLiveCMineInteScoreModel * scoreModel;  //签到数据
@property (nonatomic, strong) CNLiveCMineInteTaskListModel * taskModel;//任务列表及积分数据
@property (nonatomic, strong) CNLiveCMineInteListModel * announModel;  //公告数据
@property (nonatomic, strong) CNLiveCMineInteListModel * activModel;   //活动数据

@property (nonatomic, strong) CNLiveCMineInteHeaderView *headerView;//头视图
@property (nonatomic, strong) CNLiveNavigationBar * navigationBar;//导航栏
@property (nonatomic, strong) UICollectionView * mainCollectionView;//主界面
@property (nonatomic, strong) UIImageView * backView;
@end

@implementation CNLiveCMineInteViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    self.navigationController.navigationBarHidden = YES;
    [self reloadDataSource];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    self.navigationController.navigationBarHidden = NO;
}
-(BOOL)preferredNavigationBarHidden
{
    return YES;
}
-(void)reloadDataSource
{
    __weak typeof(self) weakSelf = self;
    [QMUITips showLoadingInView:self.view];
    [CNLiveCMineInteViewModel requestWithIntegraTaskWithSignMedthodForCompleterBlock:^(NSMutableArray * _Nonnull dataArray, NSString * _Nonnull errorMessage) {
        [QMUITips hideAllTipsInView:weakSelf.view];
        weakSelf.dataArray = dataArray;
        weakSelf.scoreModel  = ![dataArray[0] isKindOfClass:NSString.class]?dataArray[0]:nil;
        weakSelf.taskModel   = ![dataArray[1] isKindOfClass:NSString.class]?dataArray[1]:nil;
        weakSelf.announModel = ![dataArray[2] isKindOfClass:NSString.class]?dataArray[2]:nil;
        weakSelf.activModel  = ![dataArray[3] isKindOfClass:NSString.class]?dataArray[3]:nil;
        
        weakSelf.taskModel.from = weakSelf.isFrom;
        weakSelf.taskModel.feedback = weakSelf.isFeedback;
        [weakSelf.mainCollectionView reloadData];
    }];
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kCNLiveCMineInteTaskAddScoreRequestSuccessNotification object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColorHex(0xffffff);
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.backView];
    [self.view addSubview:self.mainCollectionView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(successAddScoreNotification:) name:kCNLiveCMineInteTaskAddScoreRequestSuccessNotification object:nil];
}

-(void)successAddScoreNotification:(NSNotification *)note
{
    NSInteger addScore = [note.userInfo[@"score"] integerValue];
    NSInteger scored = [self.taskModel.integral integerValue];
    self.taskModel.integral = [NSString stringWithFormat:@"%ld",scored + addScore];
    [self.headerView setupWithScoreModel:self.scoreModel taskModel:self.taskModel announModel:self.announModel activModel:self.activModel];
}
-(UIStatusBarStyle)preferredStatusBarStyle
{
    if (@available(iOS 13.0, *)) {
        return UIStatusBarStyleDarkContent;
    } else {
        return UIStatusBarStyleDefault;
    }
}
#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.activModel.list.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CNLiveCMineInteCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([CNLiveCMineInteCollectionViewCell class]) forIndexPath:indexPath];
    CNLiveCMineInteListSubModel * taskModel = self.activModel.list[indexPath.row];
    cell.taskModel = taskModel;
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 0, 5);
}
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(kScreenWidth, self.announModel ? 310 : 260);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath {
    // 判断返回表头类型 还是表尾类型
    if([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        CNLiveCMineInteHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass(CNLiveCMineInteHeaderView.class) forIndexPath:indexPath];
        headerView.backgroundColor= [UIColor clearColor];
        [headerView setupWithScoreModel:self.scoreModel taskModel:self.taskModel announModel:self.announModel activModel:self.activModel];
        __weak typeof(self) weakSelf = self;
        [headerView setClickActionBlock:^(CNLiveCMineInteScoreModel * _Nonnull scoreModel, CNLiveCMineInteTaskListModel * _Nonnull taskModel, CNLiveCMineInteListModel * _Nonnull announModel, CNLiveCMineInteHeaderClick headerClick) {
            if (headerClick == CNLiveCMineInteHeaderClickDetail) {
                CNLiveCMineInteDetailViewController * inteView = [[CNLiveCMineInteDetailViewController alloc] initWithInte:!CNLiveStringIsEmpty(taskModel.integral)?taskModel.integral:@"0"];
                [weakSelf.navigationController pushViewController:inteView animated:YES];
            } else if (headerClick == CNLiveCMineInteHeaderClickAnnotation) {
                CNLiveCMineInteAnnouListViewController * annouView = [[CNLiveCMineInteAnnouListViewController alloc] initWithAnnouModel:announModel];
                [weakSelf.navigationController pushViewController:annouView animated:YES];
            } else {
                NSString * url = cn_API_Managemall(@"/html/select_good/1/#/pointsMall");
                [[CNLiveCMineService shareMineProtocol] pushCMineInteContentForCMainWebViewWithURL:url statue:CNLiveCMineIntePushWebViewStatueMall];
            }
        }];
        self.headerView = headerView;
        return headerView;
    }
    return nil;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CNLiveCMineInteListSubModel * model = self.activModel.list[indexPath.row];
    if ([model.contentId isEqualToString:@"21"]) {//跳转原生
        CNLiveCMineInteTaskViewController * taskView = [[CNLiveCMineInteTaskViewController alloc] initWithTaskModel:self.taskModel];
        [self.navigationController pushViewController:taskView animated:YES];
    }else{
        [[CNLiveCMineService shareMineProtocol] pushCMineInteContentForCMainWebViewWithURL:model.thirdUrl statue:CNLiveCMineIntePushWebViewStatueTask];
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    self.mainCollectionView.backgroundColor = [UIColor colorWithWhite:1 alpha:(offsetY/200.0f)];
}
#pragma mark = getter
-(UICollectionView *)mainCollectionView
{
    if (!_mainCollectionView) {
        CGFloat item_w = (KScreenWidth - 30)/3;
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(item_w, item_w * 1.25);
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, KScreenWidth, KScreenHeight - kNavigationBarHeight - kVerticalBottomSafeHeight) collectionViewLayout:layout];
        _mainCollectionView.delegate = self;
        _mainCollectionView.dataSource = self;
        _mainCollectionView.backgroundColor = [UIColor clearColor];
        _mainCollectionView.showsVerticalScrollIndicator = YES;
        _mainCollectionView.showsHorizontalScrollIndicator = NO;
        [_mainCollectionView registerClass:[CNLiveCMineInteCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass(CNLiveCMineInteCollectionViewCell.class)];
        [_mainCollectionView registerClass:[CNLiveCMineInteHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass(CNLiveCMineInteHeaderView.class)];
    }
    return _mainCollectionView;
}
- (CNLiveNavigationBar *)navigationBar {
    if (!_navigationBar) {
        _navigationBar = [CNLiveNavigationBar navigationBar];
        _navigationBar.leftImage = [CNLiveCMineService imageWithMineName:@"c_mine_skip_push_back"];
        __weak typeof(self) weakSelf = self;
        _navigationBar.lineHidden = YES;
        _navigationBar.backgroundColor = UIColorHex(0xFFE9EA);
        _navigationBar.onClickLeftButton = ^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
        _navigationBar.title.text = @"我的积分";
        _navigationBar.right.hidden = NO;
        _navigationBar.rightImage = [CNLiveCMineService imageWithMineName:@"c_mine_skip_push_more"];
        _navigationBar.right.frame = CGRectMake(KScreenWidth - 44, kStatusHeight, 44, 44);
        [_navigationBar.right setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
        [_navigationBar.right setUserInteractionEnabled:YES];
        _navigationBar.onClickRightButton = ^{
            [CNLiveCMineInteHeaderCustomView showHeaderAlertViewWithCurrentVC:weakSelf CompleterBlock:^(CNLiveCMineInteHeaderClick headerClick) {
                if (headerClick == CNLiveCMineInteHeaderClickRule) {
                    [[CNLiveCMineService shareMineProtocol] pushCMineInteContentForCMainWebViewWithURL:@"" statue:CNLiveCMineIntePushWebViewStatueRule];
                }
                if (headerClick == CNLiveCMineInteHeaderClickService) {
                    [[CNLiveCMineService shareMineProtocol] pushCMineInteContentForCMainPlatFromCurrentVC:weakSelf];
                }
            }];
        };
    }
    return _navigationBar;
}
-(UIImageView *)backView
{
    if (!_backView) {
        _backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, kScreenWidth, 200)];
        _backView.contentMode = UIViewContentModeScaleAspectFill;
        _backView.clipsToBounds = YES;
        _backView.backgroundColor = [UIColor whiteColor];
        _backView.image = [CNLiveCMineService imageWithMineName:@"c_mine_content_header_back_groud"];
    }
    return _backView;
}

@end