Files
yinmeng-ios/xplan-ios/Main/Tabbar/View/NewUserRecharge/XPNewUserRechargeViewController.m
2023-04-14 11:53:15 +08:00

440 lines
18 KiB
Objective-C

//
// XPNewUserRechargeViewController.m
// xplan-ios
//
// Created by GreenLand on 2022/7/27.
//
#import "XPNewUserRechargeViewController.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "XPMacro.h"
#import "ThemeColor+FirstRecharge.h"
#import "UIImage+Utils.h"
#import "XPFirstRechargeFlowLayout.h"
#import "Timestamp.h"
#import "StatisticsServiceHelper.h"
#import "NSArray+Safe.h"
#import "TTPopup.h"
///Model
#import "NewUserRechargeModel.h"
///View
#import "XPNewUserRechargeCollectionViewCell.h"
#import "XPEnterpriseFirstRechargePayView.h"
///P
#import "MainPresenter.h"
#import "MainProtocol.h"
///VC
#import "XPMineNewRechargeViewController.h"
#import "XPNewUserRechargePresentTrasition.h"
@interface XPNewUserRechargeViewController ()<MainProtocol, UICollectionViewDelegate, UICollectionViewDataSource>
///顶部的View
@property (nonatomic,strong) UIView * topView;
///底部的View
@property (nonatomic,strong) UIView * bottomView;
///背景图
@property (nonatomic,strong) UIImageView *backImageView;
///关闭按钮
@property (nonatomic, strong) UIButton *closeBtn;
///优惠倒计时
@property (nonatomic, strong) UILabel *countDownLabel;
///奖励的容器 segment 额外奖励的 礼物的
@property (nonatomic,strong) UIImageView * rewardBackView;
///tab 切换
@property (nonatomic,strong) UIStackView * segmentView;
///额外的奖励说明
@property (nonatomic,strong) UIButton *extraButton;
///解锁说明
@property (nonatomic, strong) UIImageView *unlockImageView;
///列表
@property (nonatomic,strong) UICollectionView *collectionView;
///立即充值
@property (nonatomic, strong) UIButton *rechargeButton;
///总数据
@property (nonatomic, strong) NewUserRechargeModel *rechargeInfo;
///数据源
@property (nonatomic,strong) NSArray<FirstRechargeRewardModel *> *rewardArray;
///初始数据源
@property (nonatomic,strong) NSArray<FirstRechargeModel *> *originArray;
///当前的导航栏
@property (nonatomic,weak) UINavigationController * currentNav;
///转场动画
@property (nonatomic, strong) XPNewUserRechargePresentTrasition *animatedTransition;
///选中的充值信息
@property (nonatomic,strong) FirstRechargeModel *selectRechargeInfo;
@end
@implementation XPNewUserRechargeViewController
- (instancetype)initWithNavigation:(UINavigationController *)nav {
if (self = [super init]) {
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
self.currentNav = nav;
}
return self;
}
- (MainPresenter *)createPresenter {
return [[MainPresenter alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.presenter getNewUserRechargeList];
[self initSubViews];
[self initSubViewConstraints];
if (self.hadAnimate) {
self.transitioningDelegate = self;
}
}
#pragma mark - Private Method
- (void)initSubViews {
self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
[self.view addSubview:self.topView];
[self.view addSubview:self.backImageView];
[self.view addSubview:self.bottomView];
[self.view addSubview:self.closeBtn];
[self.view addSubview:self.countDownLabel];
[self.backImageView addSubview:self.rechargeButton];
[self.backImageView addSubview:self.rewardBackView];
[self.rewardBackView addSubview:self.segmentView];
[self.rewardBackView addSubview:self.extraButton];
[self.rewardBackView addSubview:self.unlockImageView];
[self.rewardBackView addSubview:self.collectionView];
}
- (void)initSubViewConstraints {
[self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.mas_equalTo(self.view);
make.bottom.mas_equalTo(self.backImageView.mas_top);
}];
[self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.mas_equalTo(self.view);
make.height.mas_equalTo(405);
make.width.mas_equalTo(315);
}];
[self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.mas_equalTo(self.view);
make.top.mas_equalTo(self.backImageView.mas_bottom);
}];
[self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.backImageView.mas_top);
make.width.height.mas_equalTo(32);
make.right.mas_equalTo(self.backImageView).mas_offset(-8);
}];
[self.countDownLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.backImageView).mas_offset(87);
make.left.mas_equalTo(self.backImageView).mas_offset(20);
make.width.mas_equalTo(140);
make.height.mas_equalTo(14);
}];
[self.rechargeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(244, 44));
make.centerX.mas_equalTo(self.backImageView);
make.bottom.mas_equalTo(self.backImageView.mas_bottom).offset(-28);
}];
[self.rewardBackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.backImageView).offset(123);
make.height.mas_equalTo(205);
make.width.mas_equalTo(266);
make.centerX.mas_equalTo(self.backImageView);
}];
[self.segmentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(266);
make.centerX.top.mas_equalTo(self.rewardBackView);
make.height.mas_equalTo(38);
}];
[self.extraButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(266, 22));
make.top.mas_equalTo(self.segmentView.mas_bottom).offset(8);
make.centerX.mas_equalTo(self.rewardBackView);
}];
[self.unlockImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(84, 11));
make.top.mas_equalTo(self.extraButton.mas_bottom).offset(13);
make.centerX.mas_equalTo(self.rewardBackView);
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.unlockImageView.mas_bottom).offset(12);
make.left.mas_equalTo(self.rewardBackView);
make.centerX.mas_equalTo(self.rewardBackView);
make.height.mas_equalTo(94);
}];
}
#pragma mark - MainProtocol
///获取新用户充值列表成功
- (void)getNewUserRechargeInfoSuccess:(NewUserRechargeModel *)rechargeInfo {
_rechargeInfo = rechargeInfo;
[Timestamp getInternetDateWithSuccess:^(NSTimeInterval timeInterval) {
timeInterval = timeInterval * 1000;
long aTime = (rechargeInfo.limitEndTime - timeInterval) / 1000;
self.countDownLabel.text = [NSString stringWithFormat:@"限时%@", [self countTimeWithTime:aTime]];
} failure:^(NSError * _Nonnull error) {
NSDate *datenow = [NSDate date];//现在时间
long time2 = (long)([datenow timeIntervalSince1970]*1000);
long aTime = (rechargeInfo.limitEndTime - time2) / 1000;
self.countDownLabel.text = [NSString stringWithFormat:@"限时%@", [self countTimeWithTime:aTime]];
}];
self.originArray = rechargeInfo.limitFirstChargeTaskList;
if (self.segmentView.arrangedSubviews.count > 0) {
[[self.segmentView arrangedSubviews] enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj removeFromSuperview];
}];
}
for (int i = 0 ; i < self.originArray.count; i++) {
FirstRechargeModel * modelInfo = [self.originArray safeObjectAtIndex1:i];
NSString * title = [NSString stringWithFormat:@"%ld元", modelInfo.chargeMoney];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[button setTitleColor:[ThemeColor textThirdColor] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
[button setTitle:title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateSelected];
button.adjustsImageWhenHighlighted = NO;
button.tag = i;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:@"NewUserRecharge_segment_normal_bg"] forState:UIControlStateNormal];
[button setBackgroundImage:[[UIImage imageNamed:@"NewUserRecharge_segment_selected_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 30) resizingMode:UIImageResizingModeStretch] forState:UIControlStateSelected];
if (i == 0) {
if (modelInfo.finishCharge) {
self.rechargeButton.enabled = NO;
}
button.selected = YES;
self.selectRechargeInfo = modelInfo;
[self.extraButton setTitle:modelInfo.chargeProdTitle forState:UIControlStateNormal];
self.rewardArray = modelInfo.firstChargeRewardList;
if (self.rewardArray.count < 4) {
[self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.unlockImageView.mas_bottom).offset(12);
make.width.mas_equalTo(60*self.rewardArray.count + (self.rewardArray.count - 1) * 8);
make.centerX.mas_equalTo(self.rewardBackView);
make.height.mas_equalTo(94);
}];
}
[self.collectionView reloadData];
}
[self.segmentView addArrangedSubview:button];
}
}
- (NSString *)countTimeWithTime:(long)time {
NSInteger minute;
NSInteger hour;
NSInteger day = time / 24 / 3600;
time = time - day * 24 * 3600;
hour = time / 3600;
time = time - hour * 3600;
minute = time / 60;
if (day > 0) {
if (hour > 0) {
return [NSString stringWithFormat:@"%zd天%zd小时%zd分", day, hour, minute];
} else {
return [NSString stringWithFormat:@"%zd天%zd分", day, minute];
}
} else {
if (hour > 0) {
return [NSString stringWithFormat:@"%zd小时%zd分", hour, minute];
} else {
return [NSString stringWithFormat:@"%zd分", minute];
}
}
}
#pragma mark - UICollectionViewDelegate And UICollectionViewDatasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.rewardArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPNewUserRechargeCollectionViewCell * cell= [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPNewUserRechargeCollectionViewCell class]) forIndexPath:indexPath];
cell.rewardInfo = [self.rewardArray safeObjectAtIndex1:indexPath.row];
return cell;
}
#pragma mark - Event Response
- (void)dismissFirstRechargeVC {
[self dismissViewControllerAnimated:YES completion:nil];
[StatisticsServiceHelper trackEventWithKey:StatisticsServiceEventNugiftPopCloseClick];
}
- (void)rechargeButtonAction:(UIButton *)sender {
[self dismissViewControllerAnimated:YES completion:nil];
XPEnterpriseFirstRechargePayView * rechargetView =[[XPEnterpriseFirstRechargePayView alloc] init];
rechargetView.chargeMoney = [NSString stringWithFormat:@"%ld", self.selectRechargeInfo.chargeMoney];
rechargetView.chargeProdId = self.selectRechargeInfo.chargeProdId;
rechargetView.firstrechargeVC = self;
[TTPopup popupView:rechargetView style:TTPopupStyleActionSheet];
[StatisticsServiceHelper trackEventWithKey:StatisticsServiceEventNugiftPopClick eventAttributes:@{@"buy_level" : [NSString stringWithFormat:@"%ld", self.selectRechargeInfo.chargeMoney]}];
}
- (void)buttonAction:(UIButton *)sender {
if (sender.tag <= self.originArray.count) {
FirstRechargeModel * model = [self.originArray safeObjectAtIndex1:sender.tag];
self.rechargeButton.enabled = !model.finishCharge;
self.selectRechargeInfo = model;
[self.extraButton setTitle:model.chargeProdTitle forState:UIControlStateNormal];
self.rewardArray = model.firstChargeRewardList;
if (self.rewardArray.count < 4) {
[self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.unlockImageView.mas_bottom).offset(12);
make.width.mas_equalTo(60*self.rewardArray.count + (self.rewardArray.count - 1) * 8);
make.centerX.mas_equalTo(self.rewardBackView);
make.height.mas_equalTo(94);
}];
} else {
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.unlockImageView.mas_bottom).offset(12);
make.left.mas_equalTo(self.rewardBackView);
make.centerX.mas_equalTo(self.rewardBackView);
make.height.mas_equalTo(94);
}];
}
[self.collectionView reloadData];
}
[[self.segmentView arrangedSubviews] enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIButton * button = obj;
button.selected = NO;
}];
sender.selected = !sender.selected;
}
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
return self.animatedTransition;
}
#pragma mark - Getters And Setters
- (UICollectionView *)collectionView{
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(60, 94);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.minimumLineSpacing = 8;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor whiteColor];
[_collectionView registerClass:[XPNewUserRechargeCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPNewUserRechargeCollectionViewCell class])];
}
return _collectionView;
}
- (UIView *)bottomView {
if (!_bottomView) {
_bottomView = [[UIView alloc] init];
_bottomView.backgroundColor = [UIColor clearColor];
UITapGestureRecognizer * tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissFirstRechargeVC)];
[_bottomView addGestureRecognizer:tap];
}
return _bottomView;
}
- (UIView *)topView {
if (!_topView) {
_topView = [[UIView alloc] init];
_topView.backgroundColor = [UIColor clearColor];
UITapGestureRecognizer * tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissFirstRechargeVC)];
[_topView addGestureRecognizer:tap];
}
return _topView;
}
- (UIImageView *)backImageView {
if (!_backImageView) {
_backImageView = [[UIImageView alloc] init];
_backImageView.userInteractionEnabled = YES;
_backImageView.image = [UIImage imageNamed:@"NewUserRecharge_bg"];
}
return _backImageView;
}
- (UIButton *)closeBtn {
if (!_closeBtn) {
_closeBtn = [[UIButton alloc] init];
[_closeBtn setImage:[UIImage imageNamed:@"new_user_recharge_close"] forState:UIControlStateNormal];
_closeBtn.userInteractionEnabled = NO;
}
return _closeBtn;
}
- (UILabel *)countDownLabel {
if (!_countDownLabel) {
_countDownLabel = [[UILabel alloc] init];
_countDownLabel.textColor = [UIColor whiteColor];
_countDownLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightMedium];
_countDownLabel.textAlignment = NSTextAlignmentCenter;
}
return _countDownLabel;
}
- (UIImageView *)rewardBackView {
if (!_rewardBackView) {
_rewardBackView = [[UIImageView alloc] init];
_rewardBackView.userInteractionEnabled = YES;
}
return _rewardBackView;
}
- (UIButton *)extraButton {
if (!_extraButton) {
_extraButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_extraButton setTitleColor:UIColorFromRGB(0xFF3987) forState:UIControlStateNormal];
_extraButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
}
return _extraButton;
}
- (UIImageView *)unlockImageView {
if (!_unlockImageView) {
_unlockImageView = [[UIImageView alloc] init];
_unlockImageView.image = [UIImage imageNamed:@"NewUserRecharge_unlock"];
}
return _unlockImageView;
}
- (UIButton *)rechargeButton {
if (!_rechargeButton) {
_rechargeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_rechargeButton setImage:[UIImage imageNamed:@"NewUserRecharge_goto"] forState:UIControlStateNormal];
[_rechargeButton setImage:[UIImage imageNamed:@"NewUserRecharge_had_rewcharge"] forState:UIControlStateDisabled];
[_rechargeButton addTarget:self action:@selector(rechargeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _rechargeButton;
}
- (UIStackView *)segmentView {
if (!_segmentView) {
_segmentView = [[UIStackView alloc] init];
_segmentView.axis = UILayoutConstraintAxisHorizontal;
_segmentView.distribution = UIStackViewDistributionFillEqually;
_segmentView.alignment = UIStackViewAlignmentFill;
_segmentView.spacing = 4;
}
return _segmentView;
}
- (XPNewUserRechargePresentTrasition *)animatedTransition {
if (!_animatedTransition) {
_animatedTransition = [[XPNewUserRechargePresentTrasition alloc] init];
}
return _animatedTransition;
}
@end