// // RQProfileUserAndVipCell.m // jiaPei // // Created by 张嵘 on 2022/4/26. // Copyright © 2022 JCZ. All rights reserved. // #import "RQProfileUserAndVipCell.h" @interface RQProfileUserAndVipCell () @property (nonatomic, readwrite, strong) RQProfileUserAndVipItemViewModel *viewModel; @property (weak, nonatomic) IBOutlet UIView *vipContentView; @property (weak, nonatomic) IBOutlet UIImageView *myBgImageView; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnsViewToTop; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *headImagViewToTop; @property (weak, nonatomic) IBOutlet QMUIButton *settingBtn; @property (weak, nonatomic) IBOutlet QMUIButton *messageBtn; @property (weak, nonatomic) IBOutlet UIImageView *myHeadImageView; @property (weak, nonatomic) IBOutlet UILabel *userNameLabel; @property (weak, nonatomic) IBOutlet QMUIButton *telePhoneBtn; @property (weak, nonatomic) IBOutlet QMUIButton *carTypeBtn; @property (weak, nonatomic) IBOutlet UIImageView *detailArrowImg; @property (weak, nonatomic) IBOutlet QMUIButton *schoolNameBtn; @property (nonatomic, readwrite, assign) CGFloat btnsViewHeight; @property (nonatomic, readwrite, assign) CGFloat headImageViewHeight; @property (nonatomic, readwrite, assign) CGFloat headImageViewCornerRadiusHeight; @property (nonatomic, readwrite, assign) BOOL isScrolling; @property (nonatomic, readwrite, strong) UICollectionView *vipCollectionView; @property (nonatomic, readwrite, strong) QMUICollectionViewPagingLayout *vipCollectionViewFlowLayout; @end @implementation RQProfileUserAndVipCell #pragma mark - PublicMethods + (instancetype)cellWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"RQProfileUserAndVipCell"; [collectionView registerNib:[UINib nibWithNibName:ID bundle:nil] forCellWithReuseIdentifier:ID]; RQProfileUserAndVipCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; if (!cell) cell = [self rq_viewFromXib]; [cell.vipContentView addSubview:cell.vipCollectionView]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.fillColor = [UIColor redColor].CGColor; maskLayer.lineWidth = 1; CGFloat cellHeight = RQ_COMMON_MANAGER.APP_SWITCH? RQ_FIT_HORIZONTAL(158.f) : RQ_FIT_HORIZONTAL(328.f); CGFloat a = RQ_FIT_HORIZONTAL(22.f); CGFloat radius = (((RQ_SCREEN_WIDTH / 2.f) * (RQ_SCREEN_WIDTH / 2.f)) - (a * a)) / (2 * a); UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(RQ_SCREEN_WIDTH / 2.f, cellHeight - radius) radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:YES]; maskLayer.path = aPath.CGPath; cell.vipContentView.layer.mask = maskLayer; return cell; } - (void)bindViewModel:(RQProfileUserAndVipItemViewModel *)viewModel { self.viewModel = viewModel; @weakify(self); [[RACObserve(viewModel, userModel.photo) takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(NSString *imageName) { @strongify(self); [self.myHeadImageView yy_setImageWithURL:[NSURL URLWithString:viewModel.userModel.photo] placeholder:RQWebAvatarImagePlaceholder() options:RQWebImageOptionAutomatic completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) { if(image) { image = [image qmui_imageResizedInLimitedSize:CGSizeMake(RQ_FIT_HORIZONTAL(70.f), RQ_FIT_HORIZONTAL(70.f)) resizingMode:QMUIImageResizingModeScaleAspectFill]; image = [image qmui_imageWithBorderColor:UIColor.whiteColor borderWidth:1 cornerRadius:RQ_FIT_HORIZONTAL(70.f) / 2.f]; self.myHeadImageView.image = image; } }]; }]; RAC(_userNameLabel, text) = [RACObserve(viewModel, userModel.nickName) takeUntil:self.rac_prepareForReuseSignal]; RAC(_telePhoneBtn, rq_titleStr) = [RACObserve(viewModel, userModel.telphone) takeUntil:self.rac_prepareForReuseSignal]; RAC(_carTypeBtn, rq_titleStr) = [RACObserve(viewModel, userModel.carType) takeUntil:self.rac_prepareForReuseSignal]; RAC(_schoolNameBtn, rq_titleStr) = [[RACObserve(viewModel, userModel.schoolName) map:^id _Nullable(id _Nullable value) { if (RQObjectIsNil(viewModel.userModel.schInfo)) { return RQStringIsNotEmpty(viewModel.userModel.schoolName)? viewModel.userModel.schoolName : @"未获取到驾校名称"; } else { return RQStringIsNotEmpty(viewModel.userModel.schInfo.name)? viewModel.userModel.schInfo.name : (RQStringIsNotEmpty(viewModel.userModel.schoolName)? viewModel.userModel.schoolName : @"未获取到驾校名称"); } }] takeUntil:self.rac_prepareForReuseSignal]; } #pragma mark - SystemMethods - (void)awakeFromNib { [super awakeFromNib]; [self btnsViewHeight]; [self headImageViewCornerRadiusHeight]; _headImagViewToTop.constant = RQ_APPLICATION_STATUS_BAR_HEIGHT + 16.f + (RQ_FIT_HORIZONTAL(20.f) * (17.f / 15.f)) + 16.f; _btnsViewToTop.constant = RQ_APPLICATION_STATUS_BAR_HEIGHT + 16.f; [_messageBtn setQmui_badgeInteger:14]; self.myHeadImageView.layer.cornerRadius = self.headImageViewCornerRadiusHeight; CGFloat myWidth = RQ_SCREEN_WIDTH - 16 - 16 + 10; RACSignal *signal = [[RACSignal combineLatest:@[RACObserve(self.vipCollectionView, contentOffset), RACObserve(self, isScrolling)] reduce:^id (id _Nullable x, id _Nullable y) { if (!_isScrolling) { NSLog(@"%.f-----%f",self.vipCollectionView.contentOffset.x, round(self.vipCollectionView.contentOffset.x / myWidth)); NSInteger a = round(self.vipCollectionView.contentOffset.x / myWidth); RQ_Profile_Module.profileSubject = a; switch (a) { case 0: self.myBgImageView.image = RQ_VIP_Module.isSubject1Vip? RQImageNamed(@"科一科四 已开通") : RQImageNamed(@"科一科四"); self.userNameLabel.textColor = RQ_VIP_Module.isSubject1Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor; [self.telePhoneBtn setTitleColor:RQ_VIP_Module.isSubject1Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.telePhoneBtn setImage:RQ_VIP_Module.isSubject1Vip? RQImageNamed(@"手机-黑") : RQImageNamed(@"手机") forState:UIControlStateNormal]; [self.carTypeBtn setTitleColor:RQ_VIP_Module.isSubject1Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.carTypeBtn setImage:RQ_VIP_Module.isSubject1Vip? RQImageNamed(@"驾照类型-黑") : RQImageNamed(@"驾照类型") forState:UIControlStateNormal]; [self.schoolNameBtn setTitleColor:RQ_VIP_Module.isSubject1Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.schoolNameBtn setImage:RQ_VIP_Module.isSubject1Vip? RQImageNamed(@"公司-黑") : RQImageNamed(@"公司") forState:UIControlStateNormal]; [self.detailArrowImg setImage:RQ_VIP_Module.isSubject1Vip? RQImageNamed(@"查看详情-黑") : RQImageNamed(@"查看详情")]; [self.settingBtn setBackgroundImage:RQ_VIP_Module.isSubject1Vip? RQImageNamed(@"设置-黑") : RQImageNamed(@"设置") forState:UIControlStateNormal]; [self.messageBtn setBackgroundImage:RQ_VIP_Module.isSubject1Vip? RQImageNamed(@"消息-黑") : RQImageNamed(@"消息") forState:UIControlStateNormal]; break; case 1: self.myBgImageView.image = RQ_VIP_Module.isSubject2Vip? RQImageNamed(@"科二 已开通") : RQImageNamed(@"科二"); self.userNameLabel.textColor = RQ_VIP_Module.isSubject2Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor; [self.telePhoneBtn setTitleColor:RQ_VIP_Module.isSubject2Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.telePhoneBtn setImage:RQ_VIP_Module.isSubject2Vip? RQImageNamed(@"手机-黑") : RQImageNamed(@"手机") forState:UIControlStateNormal]; [self.carTypeBtn setTitleColor:RQ_VIP_Module.isSubject2Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.carTypeBtn setImage:RQ_VIP_Module.isSubject2Vip? RQImageNamed(@"驾照类型-黑") : RQImageNamed(@"驾照类型") forState:UIControlStateNormal]; [self.schoolNameBtn setTitleColor:RQ_VIP_Module.isSubject2Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.schoolNameBtn setImage:RQ_VIP_Module.isSubject2Vip? RQImageNamed(@"公司-黑") : RQImageNamed(@"公司") forState:UIControlStateNormal]; [self.detailArrowImg setImage:RQ_VIP_Module.isSubject2Vip? RQImageNamed(@"查看详情-黑") : RQImageNamed(@"查看详情")]; [self.settingBtn setBackgroundImage:RQ_VIP_Module.isSubject2Vip? RQImageNamed(@"设置-黑") : RQImageNamed(@"设置") forState:UIControlStateNormal]; [self.messageBtn setBackgroundImage:RQ_VIP_Module.isSubject2Vip? RQImageNamed(@"消息-黑") : RQImageNamed(@"消息") forState:UIControlStateNormal]; break; case 2: self.myBgImageView.image = RQ_VIP_Module.isSubject3Vip? RQImageNamed(@"科三 已开通") : RQImageNamed(@"科三"); self.userNameLabel.textColor = RQ_VIP_Module.isSubject3Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor; [self.telePhoneBtn setTitleColor:RQ_VIP_Module.isSubject3Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.telePhoneBtn setImage:RQ_VIP_Module.isSubject3Vip? RQImageNamed(@"手机-黑") : RQImageNamed(@"手机") forState:UIControlStateNormal]; [self.carTypeBtn setTitleColor:RQ_VIP_Module.isSubject3Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.carTypeBtn setImage:RQ_VIP_Module.isSubject3Vip? RQImageNamed(@"驾照类型-黑") : RQImageNamed(@"驾照类型") forState:UIControlStateNormal]; [self.schoolNameBtn setTitleColor:RQ_VIP_Module.isSubject3Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.schoolNameBtn setImage:RQ_VIP_Module.isSubject3Vip? RQImageNamed(@"公司-黑") : RQImageNamed(@"公司") forState:UIControlStateNormal]; [self.detailArrowImg setImage:RQ_VIP_Module.isSubject3Vip? RQImageNamed(@"查看详情-黑") : RQImageNamed(@"查看详情")]; [self.settingBtn setBackgroundImage:RQ_VIP_Module.isSubject3Vip? RQImageNamed(@"设置-黑") : RQImageNamed(@"设置") forState:UIControlStateNormal]; [self.messageBtn setBackgroundImage:RQ_VIP_Module.isSubject3Vip? RQImageNamed(@"消息-黑") : RQImageNamed(@"消息") forState:UIControlStateNormal]; break; case 3: self.myBgImageView.image = RQ_VIP_Module.isSubject4Vip? RQImageNamed(@"科一科四 已开通") : RQImageNamed(@"科一科四"); self.userNameLabel.textColor = RQ_VIP_Module.isSubject4Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor; [self.telePhoneBtn setTitleColor:RQ_VIP_Module.isSubject4Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.telePhoneBtn setImage:RQ_VIP_Module.isSubject4Vip? RQImageNamed(@"手机-黑") : RQImageNamed(@"手机") forState:UIControlStateNormal]; [self.carTypeBtn setTitleColor:RQ_VIP_Module.isSubject4Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.carTypeBtn setImage:RQ_VIP_Module.isSubject4Vip? RQImageNamed(@"驾照类型-黑") : RQImageNamed(@"驾照类型") forState:UIControlStateNormal]; [self.schoolNameBtn setTitleColor:RQ_VIP_Module.isSubject4Vip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.schoolNameBtn setImage:RQ_VIP_Module.isSubject4Vip? RQImageNamed(@"公司-黑") : RQImageNamed(@"公司") forState:UIControlStateNormal]; [self.detailArrowImg setImage:RQ_VIP_Module.isSubject4Vip? RQImageNamed(@"查看详情-黑") : RQImageNamed(@"查看详情")]; [self.settingBtn setBackgroundImage:RQ_VIP_Module.isSubject4Vip? RQImageNamed(@"设置-黑") : RQImageNamed(@"设置") forState:UIControlStateNormal]; [self.messageBtn setBackgroundImage:RQ_VIP_Module.isSubject4Vip? RQImageNamed(@"消息-黑") : RQImageNamed(@"消息") forState:UIControlStateNormal]; break; case 4: self.myBgImageView.image = RQ_VIP_Module.isSubjectAllVip? RQImageNamed(@"科四 已开通") : RQImageNamed(@"科四"); self.userNameLabel.textColor = RQ_VIP_Module.isSubjectAllVip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor; [self.telePhoneBtn setTitleColor:RQ_VIP_Module.isSubjectAllVip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.telePhoneBtn setImage:RQ_VIP_Module.isSubjectAllVip? RQImageNamed(@"手机-黑") : RQImageNamed(@"手机") forState:UIControlStateNormal]; [self.carTypeBtn setTitleColor:RQ_VIP_Module.isSubjectAllVip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.carTypeBtn setImage:RQ_VIP_Module.isSubjectAllVip? RQImageNamed(@"驾照类型-黑") : RQImageNamed(@"驾照类型") forState:UIControlStateNormal]; [self.schoolNameBtn setTitleColor:RQ_VIP_Module.isSubjectAllVip? RQ_MAIN_TEXT_COLOR_1 : UIColor.whiteColor forState:UIControlStateNormal]; [self.schoolNameBtn setImage:RQ_VIP_Module.isSubjectAllVip? RQImageNamed(@"公司-黑") : RQImageNamed(@"公司") forState:UIControlStateNormal]; [self.detailArrowImg setImage:RQ_VIP_Module.isSubjectAllVip? RQImageNamed(@"查看详情-黑") : RQImageNamed(@"查看详情")]; [self.settingBtn setBackgroundImage:RQ_VIP_Module.isSubjectAllVip? RQImageNamed(@"设置-黑") : RQImageNamed(@"设置") forState:UIControlStateNormal]; [self.messageBtn setBackgroundImage:RQ_VIP_Module.isSubjectAllVip? RQImageNamed(@"消息-黑") : RQImageNamed(@"消息") forState:UIControlStateNormal]; break; default: break; } } return @""; }] takeUntil:self.rac_prepareForReuseSignal]; [signal subscribeNext:^(NSArray *allArr) { }]; } - (void)layoutSubviews { [super layoutSubviews]; @weakify(self) [self.vipCollectionView mas_remakeConstraints:^(MASConstraintMaker *make) { @strongify(self) make.top.mas_equalTo(self.myHeadImageView.mas_bottom).mas_offset(20); make.left.right.mas_equalTo(self.vipContentView); make.bottom.mas_equalTo(self.vipContentView.mas_bottom).mas_offset(12); // make.height.mas_equalTo(RQ_FIT_HORIZONTAL(169.f)); }]; } #pragma mark - PrivateMethods - (IBAction)settingAction:(id)sender { [RQ_APPDELEGATE.services pushViewModel:[[RQSettingViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:nil] animated:YES]; } #pragma mark - UICollectionViewDatasource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 5; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.row) { case 0: { RQProfileSubjectOneVipCell *cell = [RQProfileSubjectOneVipCell cellWithCollectionView:collectionView forIndexPath:indexPath]; RQProfileVipItemViewModel *itemViewModel = [[RQProfileVipItemViewModel alloc] init]; itemViewModel.profileSubject = indexPath.row; [cell bindViewModel:itemViewModel]; return cell; } case 1: { RQProfileSubjectTwoVipCell *cell = [RQProfileSubjectTwoVipCell cellWithCollectionView:collectionView forIndexPath:indexPath]; RQProfileVipItemViewModel *itemViewModel = [[RQProfileVipItemViewModel alloc] init]; itemViewModel.profileSubject = indexPath.row; [cell bindViewModel:itemViewModel]; return cell; } case 2: { RQProfileSubjectThreeVipCell *cell = [RQProfileSubjectThreeVipCell cellWithCollectionView:collectionView forIndexPath:indexPath]; RQProfileVipItemViewModel *itemViewModel = [[RQProfileVipItemViewModel alloc] init]; itemViewModel.profileSubject = indexPath.row; [cell bindViewModel:itemViewModel]; return cell; } case 3: { RQProfileSubjectFourVipCell *cell = [RQProfileSubjectFourVipCell cellWithCollectionView:collectionView forIndexPath:indexPath]; RQProfileVipItemViewModel *itemViewModel = [[RQProfileVipItemViewModel alloc] init]; itemViewModel.profileSubject = indexPath.row; [cell bindViewModel:itemViewModel]; return cell; } default: { RQProfileVipCell *cell = [RQProfileVipCell cellWithCollectionView:collectionView forIndexPath:indexPath]; RQProfileVipItemViewModel *itemViewModel = [[RQProfileVipItemViewModel alloc] init]; itemViewModel.profileSubject = indexPath.row; [cell bindViewModel:itemViewModel]; return cell; } } } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; } #pragma mark - UICollectionViewDelegateFlowLayout - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGSize size = CGSizeMake(CGRectGetWidth(self.vipCollectionView.bounds) - UIEdgeInsetsGetHorizontalValue(self.vipCollectionViewFlowLayout.sectionInset), CGRectGetHeight(self.vipCollectionView.bounds) - UIEdgeInsetsGetVerticalValue(self.vipCollectionViewFlowLayout.sectionInset)); return size; } - (void)scrollViewDidScroll:(UIScrollView *)sender { self.isScrolling = YES; [NSObject cancelPreviousPerformRequestsWithTarget:self]; [self performSelector:@selector(scrollViewDidEndScrollingAnimation:) withObject:sender afterDelay:0.1]; } - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { self.isScrolling = NO; [NSObject cancelPreviousPerformRequestsWithTarget:self]; } #pragma mark - LazyLoad - (CGFloat)btnsViewHeight { if (_btnsViewHeight == 0) _btnsViewHeight = (RQ_FIT_HORIZONTAL(60.f) - 16.f) / 2.f; return _btnsViewHeight; } - (CGFloat)headImageViewHeight { if (_headImageViewHeight == 0) _headImageViewHeight = (RQ_FIT_HORIZONTAL(70.f)); return _headImageViewHeight; } - (CGFloat)headImageViewCornerRadiusHeight { if (_headImageViewCornerRadiusHeight == 0) _headImageViewCornerRadiusHeight = self.headImageViewHeight / 2.f; return _headImageViewCornerRadiusHeight; } - (UICollectionView *)vipCollectionView { if (!_vipCollectionView) { _vipCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_myHeadImageView.frame) + 20, RQ_SCREEN_WIDTH, RQ_FIT_HORIZONTAL(169.f)) collectionViewLayout:self.vipCollectionViewFlowLayout]; _vipCollectionView.delegate = self; _vipCollectionView.dataSource = self; _vipCollectionView.showsHorizontalScrollIndicator = NO; _vipCollectionView.backgroundColor = UIColorClear; [_vipCollectionView registerNib:[UINib nibWithNibName:@"RQProfileVipCell" bundle:nil] forCellWithReuseIdentifier:@"RQProfileVipCell"]; } return _vipCollectionView; } - (QMUICollectionViewPagingLayout *)vipCollectionViewFlowLayout { if (!_vipCollectionViewFlowLayout) { _vipCollectionViewFlowLayout = [[QMUICollectionViewPagingLayout alloc] initWithStyle:QMUICollectionViewPagingLayoutStyleDefault]; _vipCollectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16); _vipCollectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _vipCollectionViewFlowLayout.allowsMultipleItemScroll = NO; } return _vipCollectionViewFlowLayout; } @end