Files
peko-ios/YuMi/Modules/YMMine/View/IncomeRecord/XPExchangeDiamondsView.m
2024-06-07 17:10:07 +08:00

329 lines
12 KiB
Objective-C

//
// XPExchangeDiamondsView.m
// YuMi
//
// Created by YuMi on 2022/11/17.
//
#import "XPExchangeDiamondsView.h"
///Third
#import <Masonry/Masonry.h>
#import "XPTextField.h"
#import "XNDJTDDLoadingTool.h"
#import "UIView+Corner.h"
@interface XPExchangeDiamondsView()
@property (nonatomic,strong) XPExchangeDiamondsItemView *myGoldView;
@property (nonatomic,strong) XPExchangeDiamondsItemView *myDiamondsView;
@property (nonatomic,strong) UIButton *confirmBtn;
///金币数
@property (nonatomic,copy) NSString *goldNum;
///钻石数
@property (nonatomic,copy) NSString *diamondNum;
@property (nonatomic,assign) IncomeRecordViewType chooseType;
@end
@implementation XPExchangeDiamondsView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.myGoldView];
[self addSubview:self.myDiamondsView];
[self addSubview:self.confirmBtn];
@kWeakify(self)
self.myGoldView.inputCounthandle = ^(NSString *_Nonnull goldNum,NSString *_Nonnull diamondNum) {
@kStrongify(self)
if(goldNum.integerValue == 0 && diamondNum.integerValue == 0){
self.confirmBtn.enabled = NO;
self.myGoldView.text = @"";
self.myDiamondsView.text = @"";
return;
}
self.chooseType = IncomeRecord_Gold;
self.confirmBtn.enabled = YES;
self.goldNum = goldNum;
self.diamondNum = diamondNum;
self.myDiamondsView.count = diamondNum;
};
self.myDiamondsView.inputCounthandle = ^(NSString *_Nonnull goldNum,NSString *_Nonnull diamondNum) {
@kStrongify(self)
if(goldNum.integerValue == 0 && diamondNum.integerValue == 0){
self.confirmBtn.enabled = NO;
self.myGoldView.text = @"";
self.myDiamondsView.text = @"";
return;
}
self.confirmBtn.enabled = YES;
self.goldNum = goldNum;
self.diamondNum = diamondNum;
self.myGoldView.count = goldNum;
self.chooseType = IncomeRecord_Diamond;
};
}
- (void)initSubViewConstraints {
[self.myDiamondsView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(kGetScaleWidth(0));
make.leading.mas_equalTo(kGetScaleWidth(15));
make.trailing.mas_equalTo(-kGetScaleWidth(15));
make.height.mas_equalTo(kGetScaleWidth(72));
}];
[self.myGoldView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.width.height.equalTo(self.myDiamondsView);
make.top.equalTo(self.myDiamondsView.mas_bottom).mas_offset(kGetScaleWidth(28));
}];
[self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.myGoldView.mas_bottom).mas_offset(kGetScaleWidth(33));
make.width.mas_equalTo(kGetScaleWidth(303));
make.height.mas_equalTo(kGetScaleWidth(48));
make.centerX.equalTo(self);
}];
}
/// 返回
- (void)backBtnAction {
}
-(void)setModel:(XPExchangeDiamondsModel *)model{
_model = model;
_myGoldView.model = _model;
_myDiamondsView.model = _model;
}
-(void)confirmAction{
if( self.model.diamonds < self.diamondNum.doubleValue){
[XNDJTDDLoadingTool showErrorWithMessage:YMLocalizedString(@"XPExchangeDiamondsView2")];
return;
}
if(self.diamondNum.doubleValue < self.model.minDiamonds){
[XNDJTDDLoadingTool showErrorWithMessage:[NSString stringWithFormat:YMLocalizedString(@"XPExchangeDiamondsView3"),self.model.minDiamonds]];
return;
}
if(self.diamondNum.doubleValue > self.model.maxDiamonds){
[XNDJTDDLoadingTool showErrorWithMessage:[NSString stringWithFormat:YMLocalizedString(@"XPExchangeDiamondsView4"),self.model.maxDiamonds]];
return;
}
[self.myGoldView resignResponder];
[self.myDiamondsView resignResponder];
if(self.delegate && [self.delegate respondsToSelector:@selector(confirmExchangeDiamondsWithDiamond:gold:)]){
[self.delegate confirmExchangeDiamondsWithDiamond:self.diamondNum gold:self.goldNum];
}
}
#pragma mark -懒加载
- (XPExchangeDiamondsItemView *)myGoldView{
if (!_myGoldView){
_myGoldView = [[XPExchangeDiamondsItemView alloc]initWithFrame:CGRectZero type:IncomeRecord_Gold];
}
return _myGoldView;
}
-(XPExchangeDiamondsItemView *)myDiamondsView{
if (!_myDiamondsView){
_myDiamondsView = [[XPExchangeDiamondsItemView alloc]initWithFrame:CGRectZero type:IncomeRecord_Diamond];
}
return _myDiamondsView;
}
-(UIButton *)confirmBtn{
if (!_confirmBtn){
UIImage *image = [UIImage gradientColorImageFromColors:@[[DJDKMIMOMColor confirmButtonGradientStartColor],[DJDKMIMOMColor confirmButtonGradientMiddleColor],[DJDKMIMOMColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(kGetScaleWidth(345), kGetScaleWidth(48))];
_confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_confirmBtn setTitle:YMLocalizedString(@"XPExchangeDiamondsView1") forState:UIControlStateNormal];
_confirmBtn.titleLabel.font = kFontHeavy(16);
[_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_confirmBtn.layer.cornerRadius = kGetScaleWidth(24);
_confirmBtn.layer.masksToBounds = YES;
_confirmBtn.enabled = NO;
[_confirmBtn setBackgroundImage:image forState:UIControlStateNormal];
[_confirmBtn addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmBtn;
}
@end
/*******************************************************华丽分割线***************************************************************/
@interface XPExchangeDiamondsItemView()
///我的数量
@property (nonatomic,strong) UILabel *titleNumView;
///背景
@property (nonatomic,strong) UIImageView *bgImageView;
///图标
@property (nonatomic,strong) UIButton *iconView;
///兑换数量
@property (nonatomic,strong) XPTextField *numberView;
@property (nonatomic,assign) IncomeRecordViewType type;
@end
@implementation XPExchangeDiamondsItemView
-(instancetype)initWithFrame:(CGRect)frame type:(IncomeRecordViewType)type{
self = [super initWithFrame:frame];
if(self){
self.type = type;
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.titleNumView];
[self addSubview:self.bgImageView];
[self.bgImageView addSubview:self.iconView];
[self.bgImageView addSubview:self.numberView];
}
- (void)initSubViewConstraints {
[self.titleNumView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(kGetScaleWidth(15));
make.top.mas_equalTo(kGetScaleWidth(0));
make.height.mas_equalTo(kGetScaleWidth(14));
make.trailing.mas_equalTo(-kGetScaleWidth(15));
}];
[self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(kGetScaleWidth(15));
make.trailing.mas_equalTo(-kGetScaleWidth(15));
make.top.equalTo(self.titleNumView.mas_bottom).mas_offset(kGetScaleWidth(10));
make.height.mas_equalTo(kGetScaleWidth(48));
}];
[self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(kGetScaleWidth(28));
make.height.mas_equalTo(kGetScaleWidth(28));
make.leading.mas_equalTo(kGetScaleWidth(9));
make.centerY.equalTo(self.bgImageView);
}];
[self.numberView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.equalTo(self.bgImageView);
make.trailing.mas_equalTo(-kGetScaleWidth(12));
make.leading.equalTo(self.iconView.mas_trailing).mas_offset(8);
}];
}
#pragma mark - 赋值
-(void)setModel:(XPExchangeDiamondsModel *)model{
_model = model;
if(self.type == IncomeRecord_Gold){
_titleNumView.text = [NSString stringWithFormat:YMLocalizedString(@"XPExchangeDiamondsItemView0"),@(_model.diamonds)];
UIImage *image = [UIImage imageNamed:@"ms_exchange_coin_icon"];
[_iconView setImage:image forState:UIControlStateNormal];
}else{
_titleNumView.text = [NSString stringWithFormat:YMLocalizedString(@"XPExchangeDiamondsItemView1"),_model.golds];
UIImage *image = [UIImage imageNamed:@"exchange_new_diamonds_icon"];
[_iconView setImage:image forState:UIControlStateNormal];
}
}
-(void)resignResponder{
[_numberView resignFirstResponder];
}
-(void)setText:(NSString *)text{
_text = text;
_numberView.text = _text;
}
-(void)setCount:(NSString *)count{
_count = count;
_numberView.text = _count;
}
-(void)textFieldDidChange:(UITextField *)textField{
if (textField.text.length == 0){
if(self.inputCounthandle){
self.inputCounthandle(@"0", @"0");
}
return;
}
NSInteger count = textField.text.integerValue;
if(self.type == IncomeRecord_Gold){
NSString *goldNum = [NSString stringWithFormat:@"%.0f",ceilf(count / _model.rate)];
if(self.inputCounthandle){
self.inputCounthandle(goldNum, textField.text);
}
return;
}
NSString *diamondNum = [NSString stringWithFormat:@"%.0f",floorf(count * _model.rate)];
if(self.inputCounthandle){
self.inputCounthandle(textField.text,diamondNum);
}
}
#pragma mark -懒加载
- (UILabel *)titleNumView {
if (!_titleNumView) {
_titleNumView = [[UILabel alloc] init];
_titleNumView.font = kFontBold(15);
_titleNumView.textColor = UIColorFromRGB(0x00223D);
NSString *title = self.type == IncomeRecord_Gold ? YMLocalizedString(@"XPExchangeDiamondsItemView0"): YMLocalizedString(@"XPExchangeDiamondsItemView1");
_titleNumView.text = [NSString stringWithFormat:title,@"0"];
}
return _titleNumView;
}
- (UIImageView *)bgImageView{
if (!_bgImageView){
_bgImageView = [UIImageView new];
if(self.type == IncomeRecord_Gold){
_bgImageView.image = kImage(@"ms_exchange_diamonds_coin_bg");
}else{
_bgImageView.image = kImage(@"ms_exchange_diamonds_diamonds_bg");
}
_bgImageView.userInteractionEnabled = YES;
}
return _bgImageView;
}
-(UIButton *)iconView{
if (!_iconView){
UIImage *image = self.type == IncomeRecord_Gold ? [UIImage imageNamed:@"ms_exchange_coin_icon"] : [UIImage imageNamed:@"exchange_new_diamonds_icon"];
_iconView = [UIButton buttonWithType:UIButtonTypeCustom];
[_iconView setImage:image forState:UIControlStateNormal];
}
return _iconView;
}
- (XPTextField *)numberView{
if (!_numberView){
_numberView = [[XPTextField alloc]initWithFrame:CGRectZero];
_numberView.keyboardType = UIKeyboardTypeNumberPad;
_numberView.isValidation = YES;
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:YMLocalizedString(@"XPExchangeDiamondsView6") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium],NSForegroundColorAttributeName:[UIColor colorWithWhite:1 alpha:0.6]}];
_numberView.attributedPlaceholder = attStr;
_numberView.font = kFontMedium(15);
_numberView.textColor = [UIColor whiteColor];
// _numberView.textAlignment = NSTextAlignmentRight;
[_numberView addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
return _numberView;
}
@end