Files
peko-ios/YuMi/Modules/YMMine/View/IncomeRecord/XPExchangeDiamondsView.m

338 lines
14 KiB
Mathematica
Raw Normal View History

2023-07-14 18:50:55 +08:00
//
// 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.myGoldView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(kGetScaleWidth(15));
make.left.mas_equalTo(kGetScaleWidth(15));
make.right.mas_equalTo(-kGetScaleWidth(15));
make.height.mas_equalTo(kGetScaleWidth(116));
}];
[self.myDiamondsView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.height.equalTo(self.myGoldView);
make.top.equalTo(self.myGoldView.mas_bottom);
}];
[self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(kGetScaleWidth(296));
make.width.mas_equalTo(kGetScaleWidth(345));
make.height.mas_equalTo(kGetScaleWidth(46));
make.centerX.equalTo(self);
}];
}
-(void)setModel:(XPExchangeDiamondsModel *)model{
_model = model;
_myGoldView.model = _model;
_myDiamondsView.model = _model;
}
-(void)confirmAction{
if( self.model.golds < self.goldNum.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:currency:)]){
[self.delegate confirmExchangeDiamondsWithDiamond:self.diamondNum gold:self.goldNum currency:self.chooseType == IncomeRecord_Diamond ? @"1" : @"3"];
}
}
#pragma mark -
- (XPExchangeDiamondsItemView *)myGoldView{
if (!_myGoldView){
_myGoldView = [[XPExchangeDiamondsItemView alloc]initWithFrame:CGRectZero type:IncomeRecord_Gold];
[_myGoldView setCornerWithLeftTopCorner:kGetScaleWidth(14) rightTopCorner:kGetScaleWidth(14) bottomLeftCorner:0 bottomRightCorner:0 size:CGSizeMake(kGetScaleWidth(345), kGetScaleWidth(116))];
}
return _myGoldView;
}
-(XPExchangeDiamondsItemView *)myDiamondsView{
if (!_myDiamondsView){
_myDiamondsView = [[XPExchangeDiamondsItemView alloc]initWithFrame:CGRectZero type:IncomeRecord_Diamond];
[_myDiamondsView setCornerWithLeftTopCorner:0 rightTopCorner:0 bottomLeftCorner:kGetScaleWidth(14) bottomRightCorner:kGetScaleWidth(14) size:CGSizeMake(kGetScaleWidth(345), kGetScaleWidth(116))];
}
return _myDiamondsView;
}
-(UIButton *)confirmBtn{
if (!_confirmBtn){
UIImage *image = [UIImage gradientColorImageFromColors:@[[DJDKMIMOMColor confirmButtonGradientStartColor],[DJDKMIMOMColor confirmButtonGradientMiddleColor],[DJDKMIMOMColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(kGetScaleWidth(345), kGetScaleWidth(46))];
_confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_confirmBtn setTitle:YMLocalizedString(@"XPExchangeDiamondsView1") forState:UIControlStateNormal];
_confirmBtn.titleLabel.font = [UIFont systemFontOfSize:16];
[_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_confirmBtn.layer.cornerRadius = kGetScaleWidth(23);
_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,strong) UIButton *arrowView;
@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 whiteColor];
[self addSubview:self.titleNumView];
[self addSubview:self.bgImageView];
[self addSubview:self.arrowView];
[self.bgImageView addSubview:self.iconView];
[self.bgImageView addSubview:self.numberView];
}
- (void)initSubViewConstraints {
[self.titleNumView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(kGetScaleWidth(15));
make.top.mas_equalTo(kGetScaleWidth(16));
make.height.mas_equalTo(kGetScaleWidth(20));
make.right.mas_equalTo(-kGetScaleWidth(73));
}];
[self.arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(kGetScaleWidth(30));
make.height.mas_equalTo(kGetScaleWidth(30));
make.right.mas_equalTo(-kGetScaleWidth(38));
make.centerY.equalTo(self.titleNumView);
}];
[self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(kGetScaleWidth(14));
make.right.mas_equalTo(-kGetScaleWidth(14));
make.top.mas_equalTo(kGetScaleWidth(48));
make.height.mas_equalTo(kGetScaleWidth(52));
}];
[self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(kGetScaleWidth(40));
make.height.mas_equalTo(kGetScaleWidth(40));
make.left.mas_equalTo(kGetScaleWidth(10));
make.centerY.equalTo(self.bgImageView);
}];
[self.numberView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.equalTo(self.bgImageView);
make.right.mas_equalTo(-kGetScaleWidth(12));
make.left.equalTo(self.iconView.mas_right).mas_offset(5);
}];
}
#pragma mark -
-(void)setModel:(XPExchangeDiamondsModel *)model{
_model = model;
if(self.type == IncomeRecord_Diamond){
_titleNumView.text = [NSString stringWithFormat:YMLocalizedString(@"XPExchangeDiamondsItemView0"),@(_model.diamonds)];
NSString *diamondNum = [NSString stringWithFormat:@"%ld",_model.minDiamonds];;
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:diamondNum attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium],NSForegroundColorAttributeName:UIColorFromRGB(0xACB8D9)}];
_numberView.attributedPlaceholder = attStr;
}else{
NSString *goldNum = [NSString stringWithFormat:@"%.0f",ceilf(_model.minDiamonds / _model.rate)];
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:goldNum attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium],NSForegroundColorAttributeName:UIColorFromRGB(0xACB8D9)}];
_numberView.attributedPlaceholder = attStr;
_titleNumView.text = [NSString stringWithFormat:YMLocalizedString(@"XPExchangeDiamondsItemView1"),_model.golds];
}
}
-(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_Diamond){
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 = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
_titleNumView.textColor =[DJDKMIMOMColor inputTextColor];
NSString *title = self.type == IncomeRecord_Diamond ? 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 = [UIImage gradientColorImageFromColors:@[ UIColorFromRGB(0xDCE6FD),UIColorFromRGB(0xD8E3FE)] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(kGetScaleWidth(317), kGetScaleWidth(52))];
}else{
_bgImageView.image = [UIImage gradientColorImageFromColors:@[ UIColorFromRGB(0xFEECD2),UIColorFromRGB(0xFFDE90)] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(kGetScaleWidth(317), kGetScaleWidth(52))];
}
[_bgImageView setCornerWithLeftTopCorner:kGetScaleWidth(8) rightTopCorner:kGetScaleWidth(8) bottomLeftCorner:kGetScaleWidth(8) bottomRightCorner:kGetScaleWidth(8) size:CGSizeMake(kGetScaleWidth(317), kGetScaleWidth(52))];
_bgImageView.userInteractionEnabled = YES;
}
return _bgImageView;
}
-(UIButton *)iconView{
if (!_iconView){
UIImage *image = self.type == IncomeRecord_Diamond ? [UIImage imageNamed:@"exchange_diamonds_icon"] : [UIImage imageNamed:@"exchange_diamonds_icon"];
_iconView = [UIButton buttonWithType:UIButtonTypeCustom];
_iconView.backgroundColor = [UIColor whiteColor];
_iconView.layer.cornerRadius = kGetScaleWidth(20);
_iconView.layer.masksToBounds = YES;
[_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:@"0" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium],NSForegroundColorAttributeName:UIColorFromRGB(0xACB8D9)}];
_numberView.attributedPlaceholder = attStr;
_numberView.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
_numberView.textColor = [DJDKMIMOMColor inputTextColor];
_numberView.textAlignment = NSTextAlignmentRight;
[_numberView addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
return _numberView;
}
-(UIButton *)arrowView{
if (!_arrowView){
_arrowView = [UIButton new];
[_arrowView setImage:[UIImage imageNamed:@"exchange_diamonds_arrow"] forState:UIControlStateNormal];
_arrowView.hidden = self.type == IncomeRecord_Gold;
}
return _arrowView;
}
@end