// // TFRecommendBookController.m // TFReader // // Created by 谢腾飞 on 2020/12/14. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFRecommendBookController.h" #import "TFRecommendBookModel.h" #import "TFCollectionManager.h" #import "NSMutableArray+KVO.h" @interface TFRecommendBookController () @property (nonatomic ,strong) YYLabel *titleTop; @property (nonatomic ,strong) YYLabel *titleBottom; @property (nonatomic ,strong) UIScrollView *scrollView; @property (nonatomic ,strong) UIButton *nextStepButton; @property (nonatomic ,strong) NSMutableArray *addBookArray; @end @implementation TFRecommendBookController - (instancetype)init { if (self = [super init]) { self.view.backgroundColor = kWhiteColor; self.addBookArray = [NSMutableArray array]; [self setupSubview]; [self setupSubviewFrame]; } return self; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self setStatusBarDefaultStyle]; } - (void)viewDidLoad { [super viewDidLoad]; [self hiddenNavigationBar:YES]; } - (void)setupSubview { self.titleTop = [[YYLabel alloc] init]; self.titleTop.text = TFLocalizedString(@"独特的你"); self.titleTop.textColor = kBlackColor; self.titleTop.textAlignment = NSTextAlignmentCenter; self.titleTop.textVerticalAlignment = YYTextVerticalAlignmentBottom; self.titleTop.font = kFont18; [self.view addSubview:self.titleTop]; self.titleBottom = [[YYLabel alloc] init]; self.titleBottom.text = TFLocalizedString(@"请选择你感兴趣的书籍"); self.titleBottom.textColor = kGrayTextColor; self.titleBottom.textAlignment = NSTextAlignmentCenter; self.titleBottom.textVerticalAlignment = YYTextVerticalAlignmentTop; self.titleBottom.font = kMainFont; [self.view addSubview:self.titleBottom]; self.nextStepButton = [UIButton buttonWithType:UIButtonTypeCustom]; [self.nextStepButton setBackgroundColor:kColorRGBA(218, 218, 218, 1)]; [self.nextStepButton setTitleColor:kWhiteColor forState:UIControlStateNormal]; [self.nextStepButton.layer setCornerRadius:20]; [self.nextStepButton setUserInteractionEnabled:NO]; [self.nextStepButton setTitle:TFLocalizedString(@"完成") forState:UIControlStateNormal]; [self.nextStepButton addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.nextStepButton]; self.scrollView = [[UIScrollView alloc] init]; [self.view addSubview:self.scrollView]; } - (void)setupSubviewFrame { [self.titleTop mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(PUB_NAVBAR_OFFSET); make.left.mas_equalTo(0); make.width.mas_equalTo(self.view.mas_width); make.height.mas_equalTo(65); }]; [self.titleBottom mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.titleTop.mas_bottom).with.offset(kHalfMargin); make.left.mas_equalTo(0); make.width.mas_equalTo(self.view.mas_width); make.height.mas_equalTo(self.titleTop.mas_height); }]; WS(weakSelf) self.addBookArray.changeBlock = ^(NSMutableArray * _Nonnull newVal) { SS(strongSelf) if (newVal.count <= 0) { strongSelf.nextStepButton.backgroundColor = kColorRGBA(218, 218, 218, 1); strongSelf.nextStepButton.userInteractionEnabled = NO; } else { strongSelf.nextStepButton.backgroundColor = kMainColor; strongSelf.nextStepButton.userInteractionEnabled = YES; } }; [self.nextStepButton mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(- PUB_NAVBAR_OFFSET - kMargin); make.left.mas_equalTo(2 * kMargin); make.width.mas_equalTo(self.view.mas_width).with.offset(- 4 * kMargin); make.height.mas_equalTo(40); }]; [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.width.mas_equalTo(SCREEN_WIDTH); make.top.mas_equalTo(self.titleBottom.mas_bottom); make.bottom.mas_equalTo(self.nextStepButton.mas_top).with.offset(- kMargin); }]; } - (void)setProductionArray:(NSArray *)productionArray { _productionArray = productionArray; if (productionArray.count == 0) { return; } int buttonNum = 3; // 每行多少按钮 CGFloat button_W = BOOK_WIDTH; // 按钮宽 CGFloat button_H = BOOK_HEIGHT + BOOK_CELL_TITLE_HEIGHT + kQuarterMargin; // 按钮高 CGFloat margin_X = kHalfMargin; // 第一个按钮的X坐标 CGFloat margin_Y = 0; // 第一个按钮的Y坐标 CGFloat space_X = kHalfMargin; // 按钮间距 CGFloat space_Y = kMargin; // 行间距 [self.scrollView setContentSize:CGSizeMake(0, productionArray.count < 4 ? (button_H + space_Y) : 2 * (button_H + space_Y))]; for (int i = 0; i < (productionArray.count <= 6 ? productionArray.count : 6); i++) { int row = i / buttonNum; // 行号 int loc = i % buttonNum; // 列号 CGFloat button_X = margin_X + (space_X + button_W) * loc; CGFloat button_Y = margin_Y + (space_Y + button_H) * row; TFProductionModel *t_model = [productionArray objectOrNilAtIndex:i]; UIButton *bottomButton = [UIButton buttonWithType:UIButtonTypeCustom]; bottomButton.frame = CGRectMake(button_X, button_Y, button_W, button_H); bottomButton.tag = i; bottomButton.backgroundColor = [UIColor clearColor]; [bottomButton addTarget:self action:@selector(bookButtonClick:) forControlEvents:UIControlEventTouchUpInside]; bottomButton.selected = YES; [self.scrollView addSubview:bottomButton]; TFProductionCoverView *bookImageView = [[TFProductionCoverView alloc] initWithProductionType:t_model.productionType coverDirection:TFProductionCoverDirectionVertical]; bookImageView.coverImageUrl = t_model.cover; bookImageView.userInteractionEnabled = NO; [bottomButton addSubview:bookImageView]; [bookImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(bottomButton.mas_centerX); make.top.mas_equalTo(bottomButton.mas_top); make.width.mas_equalTo(BOOK_WIDTH); make.height.mas_equalTo(BOOK_HEIGHT); }]; UILabel *connerLabel = [[UILabel alloc] init]; connerLabel.font = kFont10; connerLabel.layer.cornerRadius = 4.0f; connerLabel.textAlignment = NSTextAlignmentCenter; connerLabel.textColor = kWhiteColor; connerLabel.clipsToBounds = YES; [bookImageView addSubview:connerLabel]; [connerLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(bookImageView.mas_right).with.offset(- kQuarterMargin); make.top.mas_equalTo(bookImageView.mas_top).with.offset(kQuarterMargin); make.width.mas_equalTo(40); make.height.mas_equalTo(20); }]; if (t_model.productionType == TFProductionTypeNovel) { connerLabel.backgroundColor = kMainColor; connerLabel.text = TFLocalizedString(@"小说"); } if (t_model.productionType == TFProductionTypeComic) { connerLabel.backgroundColor = kRedColor; connerLabel.text = TFLocalizedString(@"漫画"); } if (t_model.productionType == TFProductionTypeAudio) { connerLabel.backgroundColor = [UIColor colorWithHexString:@"#56a0ef"]; connerLabel.text = TFLocalizedString(@"听书"); } UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.numberOfLines = 2; titleLabel.text = t_model.name; titleLabel.backgroundColor = kWhiteColor; titleLabel.font = kMainFont; titleLabel.textAlignment = NSTextAlignmentLeft; titleLabel.textColor = kBlackColor; [bottomButton addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(bookImageView.mas_left); make.top.mas_equalTo(bookImageView.mas_bottom).with.offset(kQuarterMargin); make.width.mas_equalTo(bookImageView.mas_width); make.height.mas_equalTo(BOOK_CELL_TITLE_HEIGHT); }]; UIImageView *selectView = [[UIImageView alloc] init]; selectView.userInteractionEnabled = YES; selectView.image = [UIImage imageNamed:@"audio_download_select"]; [bottomButton addSubview:selectView]; [selectView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(bookImageView.mas_right).with.offset(- 5); make.bottom.mas_equalTo(bookImageView.mas_bottom).with.offset(- 5); make.height.width.mas_equalTo(18); }]; } [self.addBookArray KVO_addObjectsFromArray:productionArray]; } - (void)bookButtonClick:(UIButton *)sender { TFProductionModel *t_books = [self.productionArray objectOrNilAtIndex:sender.tag]; if (sender.selected) { [sender.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isKindOfClass:[TFProductionCoverView class]]) { obj.alpha = 0.5; } if ([obj isKindOfClass:[UIImageView class]]) { UIImageView *t_obj = (UIImageView *)obj; t_obj.image = [UIImage imageNamed:@"audio_download_unselect"]; *stop = YES; } }]; [self.addBookArray KVO_removeObject:t_books]; } else { [sender.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isKindOfClass:[TFProductionCoverView class]]) { obj.alpha = 1; } if ([obj isKindOfClass:[UIImageView class]]) { UIImageView *t_obj = (UIImageView *)obj; t_obj.image = [UIImage imageNamed:@"audio_download_select"]; *stop = YES; } }]; [self.addBookArray KVO_addObject:t_books]; } sender.selected = !sender.selected; } - (void)nextStep { for (TFProductionModel *t_model in self.addBookArray) { switch (t_model.productionType) { case TFProductionTypeNovel: [[TFCollectionManager shareManagerWithProductionType:TFProductionTypeNovel] addCollectionWithProductionModel:t_model atIndex:0]; break; case TFProductionTypeComic: [[TFCollectionManager shareManagerWithProductionType:TFProductionTypeComic] addCollectionWithProductionModel:t_model]; break; case TFProductionTypeAudio: [[TFCollectionManager shareManagerWithProductionType:TFProductionTypeAudio] addCollectionWithProductionModel:t_model]; break; default: break; } } [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Insterest_Change object:@"step_two"]; } @end