Files
yinmeng-ios/xplan-ios/Main/Tabbar/View/XPNewUserRechargeViewController.m
2022-07-28 11:56:59 +08:00

329 lines
13 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"
///Model
#import "FirstRechargeModel.h"
///View
#import "XPFirstRechargeCollectionViewCell.h"
///P
#import "MainPresenter.h"
#import "MainProtocol.h"
///VC
#import "XPMineRechargeViewController.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) UICollectionView *collectionView;
///立即充值
@property (nonatomic, strong) UIButton *rechargeButton;
///数据源
@property (nonatomic,strong) NSArray<FirstRechargeRewardModel *> *rewardArray;
///初始数据源
@property (nonatomic,strong) NSArray<FirstRechargeModel *> *originArray;
///当前的导航栏
@property (nonatomic,weak) UINavigationController * currentNav;
@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];
}
#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.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(21);
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(47);
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(128);
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(50);
}];
[self.extraButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(245, 17));
make.top.mas_equalTo(self.segmentView.mas_bottom).offset(12);
make.centerX.mas_equalTo(self.rewardBackView);
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.extraButton.mas_bottom).offset(12);
make.left.right.mas_equalTo(self.rewardBackView);
make.bottom.mas_equalTo(self.rewardBackView);
}];
}
- (void)createExtraRewardAttrebute:(FirstRechargeModel *)model {
NSString * title = model.chargeProdTitle;
NSString * giveMoneyStr = [NSString stringWithFormat:@"%ld", model.giveMoney];
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:UIColorFromRGB(0x845DFF)}];
[attribute addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xFFA027) range:[title rangeOfString:giveMoneyStr]];
[attribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:[title rangeOfString:giveMoneyStr]];
[self.extraButton setAttributedTitle:attribute forState:UIControlStateNormal];
}
#pragma mark - XPFirstRechargeProtocol
- (void)getFirstRechargeListSuccess:(NSArray *)array {
if (array.count <= 0) return;
self.originArray = array;
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 < array.count; i++) {
FirstRechargeModel * modelInfo = [array objectAtIndex:i];
NSString * title = [NSString stringWithFormat:@"%ld元", modelInfo.chargeMoney];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitleColor:[ThemeColor rewardSegmentTextColor] forState:UIControlStateSelected];
[button setTitleColor:[ThemeColor rewardSegmentNormalTextColor] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:18];
[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) {
button.selected = YES;
[self createExtraRewardAttrebute:modelInfo];
self.rewardArray = modelInfo.firstChargeRewardList;
[self.collectionView reloadData];
}
[self.segmentView addArrangedSubview:button];
}
}
#pragma mark - UICollectionViewDelegate And UICollectionViewDatasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.rewardArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPFirstRechargeCollectionViewCell * cell= [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPFirstRechargeCollectionViewCell class]) forIndexPath:indexPath];
cell.rewardInfo = [self.rewardArray objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Event Response
- (void)dismissFirstRechargeVC {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)rechargeButtonAction:(UIButton *)sender {
[self dismissViewControllerAnimated:YES completion:nil];
XPMineRechargeViewController * rechargeVC = [[XPMineRechargeViewController alloc] init];
[self.currentNav pushViewController:rechargeVC animated:YES];
}
- (void)buttonAction:(UIButton *)sender {
if (sender.tag <= self.originArray.count) {
FirstRechargeModel * model = [self.originArray objectAtIndex:sender.tag];
[self createExtraRewardAttrebute:model];
self.rewardArray = model.firstChargeRewardList;
[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;
}
#pragma mark - Getters And Setters
- (UICollectionView *)collectionView{
if (!_collectionView) {
XPFirstRechargeFlowLayout *layout = [[XPFirstRechargeFlowLayout alloc] init];
layout.itemSize = CGSizeMake(60, 60+ 16);
layout.minimumLineSpacing = 12;
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor clearColor];
[_collectionView registerClass:[XPFirstRechargeCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPFirstRechargeCollectionViewCell 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:@""] forState:UIControlStateNormal];
}
return _closeBtn;
}
- (UILabel *)countDownLabel {
if (!_countDownLabel) {
_countDownLabel = [[UILabel alloc] init];
_countDownLabel.textColor = [UIColor whiteColor];
_countDownLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightMedium];
}
return _countDownLabel;
}
- (UIImageView *)rewardBackView {
if (!_rewardBackView) {
_rewardBackView = [[UIImageView alloc] init];
_rewardBackView.userInteractionEnabled = YES;
_rewardBackView.backgroundColor = UIColorFromRGB(0xFCE9FC);
}
return _rewardBackView;
}
- (UIButton *)extraButton {
if (!_extraButton) {
_extraButton = [UIButton buttonWithType:UIButtonTypeCustom];
}
return _extraButton;
}
- (UIButton *)rechargeButton {
if (!_rechargeButton) {
_rechargeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_rechargeButton setImage:[UIImage imageNamed:@"NewUserRecharge_goto"] forState:UIControlStateNormal];
}
return _rechargeButton;
}
- (UIStackView *)segmentView {
if (!_segmentView) {
_segmentView = [[UIStackView alloc] init];
_segmentView.axis = UILayoutConstraintAxisHorizontal;
_segmentView.distribution = UIStackViewDistributionFillEqually;
_segmentView.alignment = UIStackViewAlignmentFill;
_segmentView.spacing = 4;
_segmentView.backgroundColor = UIColorFromRGB(0xE0B4FF);
}
return _segmentView;
}
@end