// // YMPickerPopupView.m // MSYOUPAI // // Created by YoMi on 2024/2/11. // Copyright © 2024 MS. All rights reserved. // #import "YMPickerPopupView.h" @interface YMPickerPopupView () /** 选择器VM*/ @property (nonatomic, strong) YMPickerViewModel *viewModel; /** 标题标签*/ @property (nonatomic, strong) UILabel *titleLb; /** 取消按钮*/ @property (nonatomic, strong) UIButton *cancelBtn; /** 确认按钮*/ @property (nonatomic, strong) UIButton *confirmBtn; /** 选择器*/ @property (nonatomic, strong) UIPickerView *pickerView; /** 当前选中数据*/ @property (nonatomic, strong) NSArray *currentSelectedDataArray; @end @implementation YMPickerPopupView - (void)ym_setupViews{ self.frame = CGRectMake(0, 0, kFrameWidth, 250); self.backgroundColor = HexColorFromRGB(0xFFFFFF); self.titleText = @"请选择选项"; self.titleColor = HexColorFromRGB(0x000000); self.titleFont = LCFont(13); self.cancelBgColor = [UIColor clearColor]; self.confirmBgColor = [UIColor clearColor]; self.cancelTitleColor = HexColorFromRGB(0x333333); self.confirmTitleColor = HexColorFromRGB(0x333333); self.cancelFont = LCFont(14); self.confirmFont = LCFont(14); self.cancelTitle = @"取消"; self.confirmTitle = @"确定"; self.cancelRadius = 10; self.confirmRadius = 10; self.cancelBorderColor = [UIColor clearColor]; self.confirmBorderColor = [UIColor clearColor]; self.cancelBorderWidth = 0; self.confirmBorderWidth = 0; [self addSubview:self.cancelBtn]; [self addSubview:self.titleLb]; [self addSubview:self.confirmBtn]; [self addSubview:self.pickerView]; [self setNeedsUpdateConstraints]; [self updateConstraintsIfNeeded]; } - (void)updateConstraints{ [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(adapt(10)); make.left.equalTo(self).offset(adapt(10)); make.width.mas_equalTo(adapt(60)); make.height.mas_equalTo(adapt(30)); }]; [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(adapt(10)); make.left.equalTo(self.cancelBtn.mas_right).offset(adapt(10)); make.right.equalTo(self.confirmBtn.mas_left).offset(adapt(-10)); make.height.mas_equalTo(adapt(30)); }]; [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(adapt(10)); make.right.equalTo(self).offset(adapt(-10)); make.width.mas_equalTo(adapt(60)); make.height.mas_equalTo(adapt(30)); }]; [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.titleLb.mas_bottom); make.left.equalTo(self); make.right.equalTo(self); make.bottom.equalTo(self).offset(adapt(-15)); }]; [super updateConstraints]; } - (void)ym_bindViewModel:(YMPickerViewModel *)viewModel{ if (!viewModel) { return; } _viewModel = viewModel; [self.pickerView reloadAllComponents]; for (int i = 0; i < self.viewModel.componentDataSource.count; i++) { //每次显示picker的时候都选中第一行 [self.pickerView selectRow:0 inComponent:i animated:YES]; //实现默认选中第一行的点击方法,将第一行的title传给控制器 [self pickerView:self.pickerView didSelectRow:0 inComponent:i]; } } //返回UIPickerView中Component列的宽度 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ return self.viewModel.componentDataSource.count <= 1 ? self.frame.size.width : self.viewModel.componentDataSource[component].componentWidth; } //返回UIPickerView中Component列中每行的高度 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return 50; } //该方法返回值决定该控件包含多少列 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{ return self.viewModel.componentDataSource.count; } //该方法的返回值决定该控件指定列包含多少个列表项 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return self.viewModel.componentDataSource[component].rowDataSource.count; } // UIPickerView中指定列和列表项上显示的标题 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent:(NSInteger)component{ return self.viewModel.componentDataSource[component].rowDataSource[row].rowName; } // 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ self.viewModel.componentDataSource[component].currentSelectedSectionIndex = component; self.viewModel.componentDataSource[component].currentSelectedRowIndex = row; self.viewModel.componentDataSource[component].currentSelectedValue = self.viewModel.componentDataSource[component].rowDataSource[row].rowName; } - (void)setTitleText:(NSString *)titleText{ _titleText = titleText; self.titleLb.text = titleText; } - (void)setCancelBgColor:(UIColor *)cancelBgColor{ _cancelBgColor = cancelBgColor; _cancelBtn.backgroundColor = cancelBgColor; } - (void)setConfirmBgColor:(UIColor *)confirmBgColor{ _confirmBgColor = confirmBgColor; self.confirmBtn.backgroundColor = confirmBgColor; } - (void)setCancelTitleColor:(UIColor *)cancelTitleColor{ _cancelTitleColor = cancelTitleColor; [self.cancelBtn setTitleColor:cancelTitleColor forState:UIControlStateNormal]; } - (void)setConfirmTitleColor:(UIColor *)confirmTitleColor{ _confirmTitleColor = confirmTitleColor; [self.confirmBtn setTitleColor:confirmTitleColor forState:UIControlStateNormal]; } - (void)setCancelFont:(UIFont *)cancelFont{ _cancelFont = cancelFont; self.cancelBtn.titleLabel.font = cancelFont; } - (void)setConfirmFont:(UIFont *)confirmFont{ _confirmFont = confirmFont; self.confirmBtn.titleLabel.font = confirmFont; } - (void)setCancelTitle:(NSString *)cancelTitle{ _cancelTitle = cancelTitle; [self.cancelBtn setTitle:cancelTitle forState:UIControlStateNormal]; } - (void)setConfirmTitle:(NSString *)confirmTitle{ _confirmTitle = confirmTitle; [self.confirmBtn setTitle:confirmTitle forState:UIControlStateNormal]; } - (void)setCancelRadius:(CGFloat)cancelRadius{ _cancelRadius = cancelRadius; self.cancelBtn.layer.cornerRadius = cancelRadius; } - (void)setConfirmRadius:(CGFloat)confirmRadius{ _confirmRadius = confirmRadius; self.confirmBtn.layer.cornerRadius = confirmRadius; } - (void)setCancelBorderColor:(UIColor *)cancelBorderColor{ _cancelBorderColor = cancelBorderColor; self.cancelBtn.layer.borderColor = cancelBorderColor.CGColor; } - (void)setConfirmBorderColor:(UIColor *)confirmBorderColor{ _confirmBorderColor = confirmBorderColor; self.confirmBtn.layer.borderColor = confirmBorderColor.CGColor; } - (void)setCancelBorderWidth:(CGFloat)cancelBorderWidth{ _cancelBorderWidth = cancelBorderWidth; self.cancelBtn.layer.borderWidth = cancelBorderWidth; } - (void)setConfirmBorderWidth:(CGFloat)confirmBorderWidth{ _confirmBorderWidth = confirmBorderWidth; self.confirmBtn.layer.borderWidth = confirmBorderWidth; } - (UILabel *)titleLb{ if (!_titleLb) { _titleLb = [[UILabel alloc]init]; _titleLb.textAlignment = NSTextAlignmentCenter; _titleLb.textColor = self.titleColor; _titleLb.font = self.titleFont; _titleLb.text = self.titleText; } return _titleLb; } - (UIButton *)cancelBtn { if(!_cancelBtn){ _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _cancelBtn.backgroundColor = self.cancelBgColor; _cancelBtn.titleLabel.font = self.cancelFont; [_cancelBtn setTitleColor:self.cancelTitleColor forState:UIControlStateNormal]; [_cancelBtn setTitle:self.cancelTitle forState:UIControlStateNormal]; _cancelBtn.layer.cornerRadius = self.cancelRadius; _cancelBtn.layer.borderWidth = self.cancelBorderWidth; _cancelBtn.layer.borderColor = self.cancelBorderColor.CGColor; WS(weakSelf) [[[_cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) { if (weakSelf.buttonBlock) { weakSelf.buttonBlock(NO, @[]); } }]; } return _cancelBtn; } - (UIButton *)confirmBtn { if(!_confirmBtn){ _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _confirmBtn.backgroundColor = self.confirmBgColor; _confirmBtn.titleLabel.font = self.confirmFont; [_confirmBtn setTitleColor:self.confirmTitleColor forState:UIControlStateNormal]; [_confirmBtn setTitle:self.confirmTitle forState:UIControlStateNormal]; _confirmBtn.layer.cornerRadius = self.confirmRadius; _confirmBtn.layer.borderWidth = self.confirmBorderWidth; _confirmBtn.layer.borderColor = self.confirmBorderColor.CGColor; WS(weakSelf) [[[_confirmBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) { if (weakSelf.buttonBlock) { weakSelf.currentSelectedDataArray = [weakSelf.viewModel.componentDataSource.rac_sequence map:^(YMPickerComponentViewModel *_Nullable componentModel) { NSMutableDictionary *mDic = [NSMutableDictionary dictionary]; [mDic setObject:componentModel.componentName forKey:@"componentName"]; [mDic setObject:@(componentModel.currentSelectedSectionIndex) forKey:@"currentSelectedSectionIndex"]; [mDic setObject:@(componentModel.currentSelectedRowIndex) forKey:@"currentSelectedRowIndex"]; [mDic setObject:componentModel.currentSelectedValue forKey:@"currentSelectedValue"]; return mDic; }].array; weakSelf.buttonBlock(YES, weakSelf.currentSelectedDataArray); } }]; } return _confirmBtn; } - (UIPickerView *)pickerView{ if (!_pickerView) { _pickerView = [[UIPickerView alloc]init]; _pickerView.delegate = self; _pickerView.dataSource = self; _pickerView.showsSelectionIndicator = YES; } return _pickerView; } - (NSArray *)currentSelectedDataArray{ if (!_currentSelectedDataArray) { _currentSelectedDataArray = [NSArray array]; } return _currentSelectedDataArray; } @end @interface YMPickerViewModel () /** 行列表 */ @property (nonatomic, strong, readwrite) NSArray *componentDataSource; @end @implementation YMPickerViewModel - (void)ym_initialize{ [super ym_initialize]; if([[self.params allKeys] containsObject:@"componentDataSource"]){ self.componentDataSource = [[self.params arrayValueForKey:@"componentDataSource" defaultValue:@[]].rac_sequence map:^(id _Nullable componentModel) { YMPickerComponentViewModel *viewModel = [[YMPickerComponentViewModel alloc]initWithParams:componentModel]; return viewModel; }].array; } } @end @interface YMPickerComponentViewModel () /** 分组Id */ @property (nonatomic, copy, readwrite) NSString *componentId; /** 分组名 */ @property (nonatomic, copy, readwrite) NSString *componentName; /** 分组宽度 */ @property (nonatomic, assign, readwrite) CGFloat componentWidth; /** 行列表 */ @property (nonatomic, strong, readwrite) NSArray *rowDataSource; @end @implementation YMPickerComponentViewModel - (void)ym_initialize{ [super ym_initialize]; if([[self.params allKeys] containsObject:@"componentId"]){ self.componentId = [self.params stringValueForKey:@"componentId" defaultValue:@""]; } if([[self.params allKeys] containsObject:@"componentName"]){ self.componentName = [self.params stringValueForKey:@"componentName" defaultValue:@""]; } self.componentWidth = [self.params floatValueForKey:@"componentWidth" defaultValue:100.0f]; if([[self.params allKeys] containsObject:@"rowDataSource"]){ self.rowDataSource = [[self.params arrayValueForKey:@"rowDataSource" defaultValue:@[]].rac_sequence map:^(id _Nullable rowModel) { YMPickerRowViewModel *viewModel = [[YMPickerRowViewModel alloc]initWithParams:rowModel]; return viewModel; }].array; } } @end @interface YMPickerRowViewModel () /** 分组Id */ @property (nonatomic, copy, readwrite) NSString *componentId; /** 行Id */ @property (nonatomic, copy, readwrite) NSString *rowId; /** 标题名 */ @property (nonatomic, copy, readwrite) NSString *rowName; @end @implementation YMPickerRowViewModel - (void)ym_initialize{ [super ym_initialize]; if([[self.params allKeys] containsObject:@"componentId"]){ self.componentId = [self.params stringValueForKey:@"componentId" defaultValue:@""]; } if([[self.params allKeys] containsObject:@"rowId"]){ self.rowId = [self.params stringValueForKey:@"rowId" defaultValue:@""]; } if([[self.params allKeys] containsObject:@"rowName"]){ self.rowName = [self.params stringValueForKey:@"rowName" defaultValue:@""]; } } @end