507 lines
16 KiB
Objective-C
507 lines
16 KiB
Objective-C
//
|
|
// XPWishGiftCreateItemViewController.m
|
|
// xplan-ios
|
|
//
|
|
// Created by 冯硕 on 2022/10/18.
|
|
//
|
|
|
|
#import "XPWishGiftCreateItemViewController.h"
|
|
///Third
|
|
#import <Masonry/Masonry.h>
|
|
///Tool
|
|
#import "ThemeColor.h"
|
|
#import "UIImage+Utils.h"
|
|
///View
|
|
#import "XPWishGiftListView.h"
|
|
#import "WishGiftInfoModel.h"
|
|
///P
|
|
#import "XPWishGiftPresenter.h"
|
|
#import "XPWishGiftProtocol.h"
|
|
|
|
@interface XPWishGiftCreateItemViewController ()<XPWishGiftProtocol, XPWishGiftListViewDelegate>
|
|
///背景
|
|
@property (nonatomic,strong) UIView *topView;
|
|
///背景
|
|
@property (nonatomic,strong) UIView * bottomView;
|
|
///背景
|
|
@property (nonatomic,strong) UIView *backView;
|
|
///等级
|
|
@property (nonatomic,strong) XPWishGiftListView *levelView;
|
|
///礼物
|
|
@property (nonatomic,strong) XPWishGiftListView *giftView;
|
|
///数量
|
|
@property (nonatomic,strong) UIStackView *numberStackView;
|
|
///减
|
|
@property (nonatomic,strong) UIButton *subtractButton;
|
|
///输入框
|
|
@property (nonatomic,strong) UITextField *textField;
|
|
///添加
|
|
@property (nonatomic,strong) UIButton *addButton;
|
|
///关闭按钮
|
|
@property (nonatomic,strong) UIButton *sureButton;
|
|
///个数
|
|
@property (nonatomic,assign) int number;
|
|
///礼物等级
|
|
@property (nonatomic,strong) NSArray *levelLists;
|
|
///礼物等级
|
|
@property (nonatomic,strong) WishGiftInfoModel *levleInfo;
|
|
///礼物信息
|
|
@property (nonatomic,strong) WishGiftInfoModel *giftInfo;
|
|
@end
|
|
|
|
@implementation XPWishGiftCreateItemViewController
|
|
|
|
- (__kindof id)createPresenter {
|
|
return [[XPWishGiftPresenter alloc] init];
|
|
}
|
|
|
|
- (void)viewDidLoad {
|
|
[super viewDidLoad];
|
|
self.view.backgroundColor = UIColorRGBAlpha(0x000000, 0.5);
|
|
[self initWishGiftCongifList];
|
|
[self initSubViews];
|
|
[self initSubViewConstraints];
|
|
}
|
|
|
|
|
|
#pragma mark - Private Method
|
|
- (void)initWishGiftCongifList {
|
|
if (self.modifiGiftInfo == nil) {
|
|
self.number = 1;
|
|
self.levleInfo = self.levelLists.firstObject;
|
|
[self handleLevleDatasource:self.levleInfo.level];
|
|
[self.levelView configDefaultInfo:self.levleInfo];
|
|
if (self.levelOneGiftList.count > 0) {
|
|
self.giftInfo = self.levelOneGiftList.firstObject;
|
|
[self.giftView configDefaultInfo:self.giftInfo];
|
|
[self handleGiftDatasource:self.levelOneGiftList giftId:self.giftInfo.giftId];;
|
|
}
|
|
}
|
|
[self loadWishGiftLevelList];
|
|
}
|
|
|
|
- (void)loadWishGiftLevelList {
|
|
if (self.levelOneGiftList.count <= 0) {
|
|
[self.presenter wishGiftConfigList:self.roomUid level:@"1"];
|
|
} else if(self.levelTwoGiftList.count <= 0) {
|
|
[self.presenter wishGiftConfigList:self.roomUid level:@"2"];
|
|
} else if(self.levelThirdGiftList.count <= 0) {
|
|
[self.presenter wishGiftConfigList:self.roomUid level:@"3"];
|
|
}
|
|
}
|
|
|
|
- (void)initSubViews {
|
|
[self.view addSubview:self.topView];
|
|
[self.view addSubview:self.backView];
|
|
[self.view addSubview:self.bottomView];
|
|
|
|
[self.backView addSubview:self.sureButton];
|
|
[self.backView addSubview:self.numberStackView];
|
|
[self.backView addSubview:self.giftView];
|
|
[self.backView addSubview:self.levelView];
|
|
|
|
[self.numberStackView addArrangedSubview:self.subtractButton];
|
|
[self.numberStackView addArrangedSubview:self.textField];
|
|
[self.numberStackView addArrangedSubview:self.addButton];
|
|
}
|
|
|
|
- (void)initSubViewConstraints {
|
|
[self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.right.top.left.mas_equalTo(self.view);
|
|
make.bottom.mas_equalTo(self.backView.mas_top);
|
|
}];
|
|
|
|
[self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.right.mas_equalTo(self.view).inset(38);
|
|
make.height.mas_equalTo(245);
|
|
make.centerY.mas_equalTo(self.view);
|
|
}];
|
|
|
|
[self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.right.bottom.mas_equalTo(self.view);
|
|
make.top.mas_equalTo(self.backView.mas_bottom);
|
|
}];
|
|
|
|
[self.levelView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.right.mas_equalTo(self.backView).inset(30);
|
|
make.top.mas_equalTo(self.backView).offset(36);
|
|
}];
|
|
|
|
|
|
[self.giftView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.right.mas_equalTo(self.backView).inset(30);
|
|
make.top.mas_equalTo(self.backView).offset(84);
|
|
}];
|
|
|
|
[self.numberStackView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.right.mas_equalTo(self.levelView);
|
|
make.height.mas_equalTo(36);
|
|
make.bottom.mas_equalTo(self.sureButton.mas_top).offset(-20);
|
|
}];
|
|
|
|
[self.subtractButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.width.mas_equalTo(36);
|
|
}];
|
|
|
|
[self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.width.mas_equalTo(36);
|
|
}];
|
|
|
|
[self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.size.mas_equalTo(CGSizeMake(129, 37));
|
|
make.centerX.mas_equalTo(self.backView);
|
|
make.bottom.mas_equalTo(self.backView.mas_bottom).offset(-20);
|
|
}];
|
|
}
|
|
|
|
- (int)addMaxCount {
|
|
if ([self.levleInfo.levelName isEqualToString:@"普通礼物"]) {
|
|
return 999;
|
|
} else if ([self.levleInfo.levelName isEqualToString:@"中级礼物"]) {
|
|
return 99;
|
|
} else {
|
|
return 30;
|
|
}
|
|
}
|
|
|
|
- (BOOL)canAddMaxCount:(int)count {
|
|
if ([self.levleInfo.levelName isEqualToString:@"普通礼物"]) {
|
|
return count < 999;
|
|
} else if ([self.levleInfo.levelName isEqualToString:@"中级礼物"]) {
|
|
return count < 99;
|
|
} else {
|
|
return count < 1;
|
|
}
|
|
}
|
|
|
|
- (BOOL)canSubtractMinCount:(int)count {
|
|
if ([self.levleInfo.levelName isEqualToString:@"普通礼物"]) {
|
|
return count > 10;
|
|
} else if ([self.levleInfo.levelName isEqualToString:@"中级礼物"]) {
|
|
return count > 10;
|
|
} else {
|
|
return count > 1;
|
|
}
|
|
}
|
|
|
|
- (int)subtractMinCount {
|
|
if ([self.levleInfo.levelName isEqualToString:@"普通礼物"]) {
|
|
return 10;
|
|
} else if ([self.levleInfo.levelName isEqualToString:@"中级礼物"]) {
|
|
return 10;
|
|
} else {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
- (void)handleLevleDatasource:(WishGiftLevel)levele {
|
|
NSMutableArray * array = [self.levelLists mutableCopy];
|
|
for (int i = 0;i < self.levelLists.count;i++) {
|
|
WishGiftInfoModel * infor = [self.levelLists objectAtIndex:i];
|
|
if (infor.level == levele) {
|
|
[array removeObject:infor];
|
|
}
|
|
}
|
|
self.levelView.datasource = array.copy;
|
|
}
|
|
|
|
- (void)handleGiftDatasource:(NSArray *)levels giftId:(NSInteger)giftId {
|
|
NSMutableArray * array = [levels mutableCopy];
|
|
for (int i = 0;i < levels.count;i++) {
|
|
WishGiftInfoModel * infor = [levels objectAtIndex:i];
|
|
if (infor.giftId == giftId) {
|
|
[array removeObject:infor];
|
|
}
|
|
}
|
|
self.giftView.datasource = array.copy;
|
|
}
|
|
|
|
#pragma mark - XPWishGiftListViewDelegate
|
|
- (void)xPWishGiftListView:(XPWishGiftListView *)view didSelectItem:(WishGiftInfoModel *)info {
|
|
if (view == self.levelView) {
|
|
self.levleInfo = info;
|
|
///处理等级
|
|
[self handleLevleDatasource:info.level];
|
|
WishGiftInfoModel * giftInfo;
|
|
if (info.level == WishGiftLevel_Normal) {
|
|
///处理等级对应的礼物
|
|
if (self.levelOneGiftList.count > 0) {
|
|
giftInfo = self.levelOneGiftList.firstObject;
|
|
}
|
|
self.textField.text = @"10";
|
|
[self handleGiftDatasource:self.levelOneGiftList giftId:giftInfo.giftId];
|
|
} else if (info.level == WishGiftLevel_Middle) {
|
|
if (self.levelTwoGiftList.count > 0) {
|
|
giftInfo = self.levelTwoGiftList.firstObject;
|
|
}
|
|
self.giftView.datasource = self.levelTwoGiftList;
|
|
self.textField.text = @"10";
|
|
[self handleGiftDatasource:self.levelTwoGiftList giftId:giftInfo.giftId];
|
|
} else {
|
|
if (self.levelThirdGiftList.count > 0) {
|
|
giftInfo = self.levelThirdGiftList.firstObject;
|
|
}
|
|
self.textField.text = @"1";
|
|
[self handleGiftDatasource:self.levelThirdGiftList giftId:giftInfo.giftId];
|
|
}
|
|
self.giftInfo = giftInfo;
|
|
self.number = self.textField.text.intValue;
|
|
[self.giftView configDefaultInfo:giftInfo];
|
|
} else {
|
|
self.giftInfo = info;
|
|
}
|
|
}
|
|
|
|
#pragma mark - XPWishGiftProtocol
|
|
- (void)wishGiftConfigListSucces:(NSArray *)list level:(NSString *)level {
|
|
if (level.intValue == 1) {
|
|
self.levelOneGiftList = list;
|
|
if (list.count > 0 && !self.modifiGiftInfo) {
|
|
self.giftInfo = list.firstObject;
|
|
[self.giftView configDefaultInfo:self.giftInfo];
|
|
[self handleGiftDatasource:self.levelOneGiftList giftId:self.giftInfo.giftId];;
|
|
}
|
|
if (self.modifiGiftInfo) {
|
|
[self handleGiftDatasource:self.levelThirdGiftList giftId:_modifiGiftInfo.giftId];
|
|
}
|
|
} else if (level.intValue == 2) {
|
|
self.levelTwoGiftList = list;
|
|
if (self.modifiGiftInfo) {
|
|
[self handleGiftDatasource:self.levelTwoGiftList giftId:_modifiGiftInfo.giftId];
|
|
}
|
|
} else {
|
|
self.levelThirdGiftList = list;
|
|
if (self.modifiGiftInfo) {
|
|
[self handleGiftDatasource:self.levelThirdGiftList giftId:_modifiGiftInfo.giftId];
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)createWishGiftItemSuccess {
|
|
[self disMissView];
|
|
if(self.Dismiss) {
|
|
self.Dismiss(YES);
|
|
}
|
|
}
|
|
|
|
#pragma mark - Event Response
|
|
- (void)subtractButtonAction:(UIButton *)sener {
|
|
if([self canSubtractMinCount:self.number]) {
|
|
self.number --;
|
|
} else {
|
|
self.number = [self subtractMinCount];
|
|
}
|
|
self.textField.text = [NSString stringWithFormat:@"%d", self.number];
|
|
}
|
|
|
|
- (void)addButtonAction:(UIButton *)sener {
|
|
if([self canAddMaxCount:self.number]) {
|
|
self.number ++;
|
|
} else {
|
|
self.number = [self addMaxCount];
|
|
}
|
|
self.textField.text = [NSString stringWithFormat:@"%d", self.number];
|
|
}
|
|
|
|
- (void)textFieldDidChanged:(UITextField *)textField {
|
|
int number = textField.text.intValue;
|
|
if (![self canAddMaxCount:number]) {
|
|
textField.text = [NSString stringWithFormat:@"%d", [self addMaxCount]];
|
|
}
|
|
|
|
if (![self canSubtractMinCount:number]) {
|
|
textField.text = [NSString stringWithFormat:@"%d", [self subtractMinCount]];
|
|
}
|
|
self.number = textField.text.intValue;
|
|
}
|
|
|
|
- (void)disMissView {
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
|
}
|
|
#pragma mark - Event Response
|
|
- (void)sureButtonAction:(UIButton *)sender {
|
|
[self.presenter createWishGiftItem:self.roomUid itemId:@"" giftId:[NSString stringWithFormat:@"%ld", self.giftInfo.giftId] seq:self.levleInfo.level targetNum:self.number];
|
|
}
|
|
|
|
#pragma mark - Getters And Setters
|
|
- (void)setModifiGiftInfo:(WishGiftInfoModel *)modifiGiftInfo {
|
|
_modifiGiftInfo = modifiGiftInfo;
|
|
if (_modifiGiftInfo) {
|
|
WishGiftInfoModel * levelInfo;
|
|
for (WishGiftInfoModel * info in self.levelLists) {
|
|
if (info.level == _modifiGiftInfo.level) {
|
|
levelInfo = info;
|
|
}
|
|
}
|
|
if (levelInfo) {
|
|
self.levleInfo = levelInfo;
|
|
self.giftInfo = _modifiGiftInfo;
|
|
[self.levelView configDefaultInfo:levelInfo];
|
|
[self.giftView configDefaultInfo:_modifiGiftInfo];
|
|
///处理等级
|
|
[self handleLevleDatasource:levelInfo.level];
|
|
///处理等级
|
|
if (_modifiGiftInfo.level == WishGiftLevel_Normal) {
|
|
if (self.levelOneGiftList.count <=0) {
|
|
[self.presenter wishGiftConfigList:self.roomUid level:@"1"];
|
|
} else {
|
|
[self handleGiftDatasource:self.levelOneGiftList giftId:_modifiGiftInfo.giftId];
|
|
}
|
|
} else if (_modifiGiftInfo.level == WishGiftLevel_Middle) {
|
|
if (self.levelTwoGiftList.count <= 0) {
|
|
[self.presenter wishGiftConfigList:self.roomUid level:@"2"];
|
|
} else {
|
|
[self handleGiftDatasource:self.levelTwoGiftList giftId:_modifiGiftInfo.giftId];
|
|
}
|
|
} else {
|
|
if (self.levelThirdGiftList.count <= 0) {
|
|
[self.presenter wishGiftConfigList:self.roomUid level:@"3"];
|
|
} else {
|
|
[self handleGiftDatasource:self.levelThirdGiftList giftId:_modifiGiftInfo.giftId];
|
|
}
|
|
}
|
|
self.textField.text = [NSString stringWithFormat:@"%ld", _modifiGiftInfo.targetNum];
|
|
self.number = (int)_modifiGiftInfo.targetNum;
|
|
|
|
} else {
|
|
[self disMissView];
|
|
}
|
|
}
|
|
}
|
|
|
|
- (UIView *)topView {
|
|
if (!_topView) {
|
|
_topView = [[UIView alloc] init];
|
|
_topView.backgroundColor = [UIColor clearColor];
|
|
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMissView)];
|
|
[_topView addGestureRecognizer:tap];
|
|
}
|
|
return _topView;
|
|
}
|
|
|
|
|
|
- (UIView *)bottomView {
|
|
if (!_bottomView) {
|
|
_bottomView = [[UIView alloc] init];
|
|
_bottomView.backgroundColor = [UIColor clearColor];
|
|
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMissView)];
|
|
[_bottomView addGestureRecognizer:tap];
|
|
}
|
|
return _bottomView;
|
|
}
|
|
|
|
|
|
- (UIView *)backView {
|
|
if (!_backView) {
|
|
_backView = [[UIView alloc] init];
|
|
_backView.layer.masksToBounds = YES;
|
|
_backView.layer.cornerRadius = 20;
|
|
_backView.backgroundColor = [UIColor whiteColor];
|
|
}
|
|
return _backView;
|
|
}
|
|
|
|
- (XPWishGiftListView *)levelView {
|
|
if(!_levelView) {
|
|
_levelView = [[XPWishGiftListView alloc] init];
|
|
_levelView.delegate = self;
|
|
}
|
|
return _levelView;
|
|
}
|
|
|
|
- (XPWishGiftListView *)giftView {
|
|
if(!_giftView) {
|
|
_giftView = [[XPWishGiftListView alloc] init];
|
|
_giftView.delegate = self;
|
|
}
|
|
return _giftView;
|
|
}
|
|
|
|
- (UIButton *)subtractButton {
|
|
if (!_subtractButton) {
|
|
_subtractButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[_subtractButton setImage:[UIImage imageNamed:@"room_wish_gift_subtract"] forState:UIControlStateNormal];
|
|
[_subtractButton setImage:[UIImage imageNamed:@"room_wish_gift_subtract"] forState:UIControlStateSelected];
|
|
[_subtractButton addTarget:self action:@selector(subtractButtonAction:) forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _subtractButton;
|
|
}
|
|
|
|
|
|
- (UIButton *)addButton {
|
|
if (!_addButton) {
|
|
_addButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[_addButton setImage:[UIImage imageNamed:@"room_wish_gift_add"] forState:UIControlStateNormal];
|
|
[_addButton setImage:[UIImage imageNamed:@"room_wish_gift_add"] forState:UIControlStateSelected];
|
|
[_addButton addTarget:self action:@selector(addButtonAction:) forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _addButton;
|
|
}
|
|
|
|
- (UIStackView *)numberStackView {
|
|
if (!_numberStackView) {
|
|
_numberStackView = [[UIStackView alloc] init];
|
|
_numberStackView.axis = UILayoutConstraintAxisHorizontal;
|
|
_numberStackView.distribution = UIStackViewDistributionFill;
|
|
_numberStackView.alignment = UIStackViewAlignmentFill;
|
|
_numberStackView.spacing = 10;
|
|
}
|
|
return _numberStackView;
|
|
}
|
|
|
|
- (UITextField *)textField {
|
|
if (!_textField) {
|
|
_textField = [[UITextField alloc] init];
|
|
_textField.layer.cornerRadius = 4;
|
|
_textField.layer.masksToBounds = YES;
|
|
_textField.tintColor = [ThemeColor secondTextColor];
|
|
_textField.textColor = [ThemeColor mainTextColor];
|
|
_textField.backgroundColor = [UIColor clearColor];
|
|
_textField.font = [UIFont systemFontOfSize:10];
|
|
_textField.clearButtonMode = UITextFieldViewModeWhileEditing;
|
|
_textField.returnKeyType = UIReturnKeySearch;
|
|
_textField.enablesReturnKeyAutomatically = YES;
|
|
_textField.text = @"10";
|
|
_textField.textAlignment = NSTextAlignmentCenter;
|
|
_textField.backgroundColor = [ThemeColor colorWithHexString:@"#E4E4E4"];
|
|
[_textField addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
|
|
}
|
|
return _textField;
|
|
}
|
|
|
|
- (UIButton *)sureButton {
|
|
if (!_sureButton) {
|
|
_sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[_sureButton setTitle:@"确定" forState:UIControlStateNormal];
|
|
[_sureButton setTitleColor:[ThemeColor confirmButtonTextColor] forState:UIControlStateNormal];
|
|
_sureButton.titleLabel.font = [UIFont systemFontOfSize:14];
|
|
[_sureButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor], [ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateNormal];
|
|
_sureButton.layer.masksToBounds = YES;
|
|
_sureButton.layer.cornerRadius = 10;
|
|
[_sureButton addTarget:self action:@selector(sureButtonAction:) forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _sureButton;
|
|
}
|
|
|
|
- (NSArray *)levelLists {
|
|
if (!_levelLists) {
|
|
WishGiftInfoModel * levelOne = [[WishGiftInfoModel alloc] init];
|
|
levelOne.levelName = @"普通礼物";
|
|
levelOne.level = WishGiftLevel_Normal;
|
|
|
|
WishGiftInfoModel * levelTwo = [[WishGiftInfoModel alloc] init];
|
|
levelTwo.levelName = @"中级礼物";
|
|
levelTwo.level = WishGiftLevel_Middle;
|
|
|
|
WishGiftInfoModel * levelThree = [[WishGiftInfoModel alloc] init];
|
|
levelThree.levelName = @"高级礼物";
|
|
levelThree.level = WishGiftLevel_High;
|
|
_levelLists = @[levelOne, levelTwo, levelThree];
|
|
}
|
|
return _levelLists;
|
|
}
|
|
|
|
|
|
|
|
@end
|