// // YMMineCommonFunctionsTwoView.m // MSYOUPAI // // Created by YoMi on 2024/2/15. // Copyright © 2024 MS. All rights reserved. // #import "YMMineCommonFunctionsTwoView.h" #import "YMMineViewModel.h" @interface YMMineCommonFunctionsTwoView () /// 我的VM @property (nonatomic, strong) YMMineViewModel *viewModel; /// 常用功能宫格视图 @property (nonatomic, strong) UITableView *commonFunctionsListView; @end @implementation YMMineCommonFunctionsTwoView - (void)ym_setupViews{ self.backgroundColor = UIColor.clearColor; [self addSubview:self.commonFunctionsListView]; [self setNeedsUpdateConstraints]; [self updateConstraintsIfNeeded]; } - (void)updateConstraints{ [self.commonFunctionsListView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(adapt(0)); make.left.equalTo(self).offset(adapt(0)); make.right.equalTo(self).offset(adapt(0)); make.bottom.equalTo(self).offset(adapt(0)); }]; [super updateConstraints]; } - (void)ym_bindViewModel:(YMMineViewModel *)viewModel{ if (!viewModel) { return; } _viewModel = viewModel; @weakify(self) [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) { @strongify(self) [self.commonFunctionsListView reloadData]; [self.commonFunctionsListView layoutIfNeeded]; //刷新高度 CGFloat commonFunctionsListViewHeight = self.commonFunctionsListView.contentSize.height; [self.commonFunctionsListView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(commonFunctionsListViewHeight + adapt(10)); }]; }]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.viewModel.commonFunctionsTwoListDataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ YMCommonFunctionsListCell *cell = [[YMCommonFunctionsListCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NSStringFromClass([YMCommonFunctionsListCell class])]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.contentView.backgroundColor = UIColor.clearColor; __weak typeof(self) weakSelf = self; cell.callBlock = ^(YMCommonFunctionsType functionsType) { [weakSelf.viewModel.commonFunctionsOperationSubject sendNext:@(functionsType)]; }; [cell ym_bindViewModel:self.viewModel.commonFunctionsTwoListDataArray[indexPath.item]]; return cell; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell isKindOfClass:[YMCommonFunctionsListCell class]]) { YMCommonFunctionsListCell *myCell = (YMCommonFunctionsListCell *)cell; NSDictionary *viewModel = self.viewModel.commonFunctionsTwoListDataArray[indexPath.row]; NSString *title = [viewModel stringValueForKey:@"title" defaultValue:@""]; if ([title isEqualToString:@"视频开关"]) { UIView *bgv = myCell.bgv; [bgv addRectCorner:(UIRectCornerTopLeft | UIRectCornerTopRight) radius:12]; } if ([title isEqualToString:@"私信音效"]) { UIView *bgv = myCell.bgv; [bgv addRectCorner:(UIRectCornerBottomLeft | UIRectCornerBottomRight) radius:12]; } } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return adapt(60); } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } - (UITableView *)commonFunctionsListView{ if (!_commonFunctionsListView) { _commonFunctionsListView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain]; _commonFunctionsListView.delegate = self; _commonFunctionsListView.dataSource = self; _commonFunctionsListView.estimatedRowHeight = 0; _commonFunctionsListView.estimatedSectionHeaderHeight = 0; _commonFunctionsListView.estimatedSectionFooterHeight = 0; _commonFunctionsListView.showsVerticalScrollIndicator = NO; _commonFunctionsListView.showsHorizontalScrollIndicator = NO; _commonFunctionsListView.scrollEnabled = NO; _commonFunctionsListView.separatorColor = UIColor.clearColor; _commonFunctionsListView.backgroundColor = UIColor.clearColor; [_commonFunctionsListView registerClass:[YMCommonFunctionsListCell class] forCellReuseIdentifier:NSStringFromClass([YMCommonFunctionsListCell class])]; //_commonFunctionsListView.layer.cornerRadius = adapt(8); //_commonFunctionsListView.layer.masksToBounds = YES; } return _commonFunctionsListView; } @end @interface YMCommonFunctionsListCell () @end @implementation YMCommonFunctionsListCell - (void)ym_setupViews{ self.backgroundColor = UIColor.clearColor; self.contentView.backgroundColor = UIColor.clearColor; [self.contentView addSubview:self.bgv]; [self.contentView addSubview:self.functionsIcon]; [self.contentView addSubview:self.functionsLb]; [self.contentView addSubview:self.switchBtn]; [self setNeedsUpdateConstraints]; [self updateConstraintsIfNeeded]; } - (void)updateConstraints{ [self.bgv mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(UIEdgeInsetsMake(0, adapt(15), 0, adapt(15))); make.height.mas_equalTo(adapt(60) - 8); }]; [self.functionsIcon mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.contentView.mas_centerY); make.left.equalTo(self.bgv).offset(20); }]; [self.functionsLb mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.contentView.mas_centerY); make.left.equalTo(self.bgv.mas_left).offset(adapt(14)); }]; [self.switchBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.contentView.mas_centerY); make.right.equalTo(self.bgv).offset(-22); make.width.mas_equalTo(adapt(48)); make.height.mas_equalTo(adapt(30)); }]; [super updateConstraints]; } - (void)ym_bindViewModel:(NSDictionary*)viewModel{ //self.functionsIcon.image = ImageByName([viewModel stringValueForKey:@"icon" defaultValue:@""]); self.functionsLb.text = [viewModel stringValueForKey:@"title" defaultValue:@""]; self.switchBtn.tag = [viewModel integerValueForKey:ParamsCategoryType defaultValue:YMCommonFunctionsTypePrivateLetterChat]; BOOL isOpen = [viewModel boolValueForKey:@"isOpen" defaultValue:NO]; if (isOpen) { [self.switchBtn setImage:ImageByName(@"ym_mine_switch_selected_icon") forState:UIControlStateNormal]; } else { [self.switchBtn setImage:ImageByName(@"ym_mine_switch_normal_icon") forState:UIControlStateNormal]; } } - (UIView *)bgv { if (!_bgv) { _bgv = [[UIView alloc]init]; _bgv.backgroundColor = HexColorFromRGB(0xFFFFFF); // _bgv.layer.cornerRadius = adapt(8); // _bgv.layer.masksToBounds = YES; } return _bgv; } - (UIImageView *)functionsIcon{ if(!_functionsIcon){ _functionsIcon = [[UIImageView alloc]init]; _functionsIcon.hidden = YES; } return _functionsIcon; } - (UILabel *)functionsLb{ if (!_functionsLb) { _functionsLb = [[UILabel alloc]init]; _functionsLb.font = LCFont(16); _functionsLb.textColor = HexColorFromRGB(0x171A1D); _functionsLb.textAlignment = NSTextAlignmentLeft; _functionsLb.text = @"******"; } return _functionsLb; } - (UIButton *)switchBtn{ if (!_switchBtn) { _switchBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_switchBtn setImage:ImageByName(@"ym_mine_switch_normal_icon") forState:UIControlStateNormal]; WS(weakSelf) [[[_switchBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(__kindof UIButton * _Nullable sender) { if (weakSelf.callBlock) { weakSelf.callBlock(sender.tag); } }]; } return _switchBtn; } @end