// // FKBrandGuideController.m // FirstLink // // Created by jack on 16/4/27. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKCategoryGuideController.h" #import "FKGuideReform.h" #import "FKGuideRequest.h" #import "FKGuideBrandItem.h" #import "FKGuideManager.h" #import "FKCategoryCollectionCell.h" static NSString *FK_CATEGORY_COLLECTION_IDENTIFY = @"FK_CATEGORY_COLLECTION_IDENTIFY"; #define FK_PREFER_CATEGORY_REQ 788 #define FK_PREFER_SAVE_REQ 789 #define CATEGORY_CELL_MARGIN (floor((UISCREENWIDTH - 30 - 20) / 3.0f)) @interface FKCategoryGuideController () @property (nonatomic, assign) kSexType sexType; @property (nonatomic, assign) kAgeType ageType; @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UIButton *confirmBtn; @property (nonatomic, strong) UIImageView *bgImgView; @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSArray *categoryArray; @property (nonatomic, strong) FKPreferinfoItem *infoItem; @property (nonatomic, copy) preferFinish finishBlock; @end @implementation FKCategoryGuideController - (instancetype)initWithInfoItem:(FKPreferinfoItem *)infoItem finish:(preferFinish)finish{ if (self = [super init]) { self.infoItem = infoItem; self.finishBlock = finish; } return self; } - (void)viewDidLoad{ [super viewDidLoad]; self.view.backgroundColor = UIColorFromRGB(0xf4f4f4); [self requestCategoryList]; [self addAllSubviews]; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; // if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { // self.navigationController.interactivePopGestureRecognizer.delegate = self; // } } - (void)requestCategoryList{ [self.hudView show:YES]; [FKGuideRequest reqPreferCategoryListWithIdentify:FK_PREFER_CATEGORY_REQ delegate:self]; } - (void)addAllSubviews{ // [self.view addSubview:self.bgImgView]; [self.view addSubview:self.collectionView]; [self.view addSubview:self.titleLabel]; [self.view addSubview:self.confirmBtn]; // CGFloat imgWidth = UISCREENHEIGH * 750.0f / 1334.0f; // // [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.bottom.equalTo(self.view); // make.centerX.equalTo(self.view); // make.width.mas_equalTo(imgWidth); // }]; // CGFloat confirmMargin = 70; CGFloat titleTop = 100.0f; if (UISCREENHEIGH <= 480){ titleTop = 70; // confirmMargin = 30; // CGFloat imgHeight = UISCREENWIDTH * 1334.0f / 750.0f; // [self.bgImgView mas_remakeConstraints:^(MASConstraintMaker *make) { // make.top.bottom.equalTo(self.view); // make.left.right.equalTo(self.view); // make.height.mas_equalTo(imgHeight); // }]; } [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.view).offset(IS_IPHONE_X ? -55 : -45); make.centerX.equalTo(self.view); make.size.mas_equalTo(CGSizeMake(140, 40)); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(titleTop); make.centerX.equalTo(self.view); }]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.titleLabel.mas_bottom).offset(20); make.left.equalTo(self.view).offset(15); make.right.equalTo(self.view).offset(- 15); make.bottom.equalTo(self.confirmBtn.mas_top).offset(- 20); }]; } //- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ // return NO; //} #pragma mark - response - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header { [self.hudView hide:NO]; if (header.code.intValue == RESPONSE_MSG_NORMAL) { if (identify == FK_PREFER_CATEGORY_REQ){ self.categoryArray = [FKGuideReform parserCategoryListWithDict:response]; [self setItemSelectState]; [self.collectionView reloadData]; }else if (identify == FK_PREFER_SAVE_REQ){ [self saveIntoInfoItem]; FKPreferResController *perferRes = [[FKPreferResController alloc]initWithEdit:NO infoItem:self.infoItem finish:self.finishBlock cancel:nil]; [self.navigationController pushViewController:perferRes animated:YES]; } }else{ [FLProgressHUDHelper showText:header.msg inView:self.view]; } } - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header{ [self.hudView hide:NO]; [FLProgressHUDHelper showText:header.msg inView:self.view]; } #pragma mark - collectionView dataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.categoryArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ FKCategoryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FK_CATEGORY_COLLECTION_IDENTIFY forIndexPath:indexPath]; FKPreferCategoryItem *item = [self categoryItemAtIndex:indexPath.row]; cell.selectImgView.hidden = !item.selected; cell.coverView.hidden = !item.selected; if (item.picUrl){ [cell.imageView setImageWithURL:item.picUrl cdnWidth:CATEGORY_CELL_MARGIN]; } return cell; } #pragma mark - collectionView delegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ FKPreferCategoryItem *item = [self categoryItemAtIndex:indexPath.row]; item.selected = !item.selected; [self.collectionView reloadData]; } - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ FKPreferCategoryItem *item = [self categoryItemAtIndex:indexPath.row]; item.selected = !item.selected; [self.collectionView reloadData]; } #pragma mark - method - (void)saveIntoInfoItem{ NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:self.categoryArray.count]; for (FKPreferCategoryItem *item in self.categoryArray) { if (item.selected){ [arrayM addObject:item]; } } self.infoItem.categoryList = arrayM; } - (void)setItemSelectState{ for (FKPreferCategoryItem *selectItem in self.infoItem.categoryList) { for (FKPreferCategoryItem *sourceItem in self.categoryArray) { if ([sourceItem.itemID isEqualToString:selectItem.itemID]){ sourceItem.selected = YES; break; } } } } - (FKPreferCategoryItem *)categoryItemAtIndex:(NSUInteger)index{ if (index < self.categoryArray.count){ return self.categoryArray[index]; } return nil; } - (NSArray *)selectedCategoryIdList{ NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:self.categoryArray.count]; for (FKPreferCategoryItem *item in self.categoryArray) { if (item.selected){ [arrayM addObject:item.itemID]; } } if (!arrayM.count) return nil; return arrayM; } #pragma mark - action - (void)clickConfirmBtn:(UIButton *)sender{ if (![self selectedCategoryIdList].count){ [FLProgressHUDHelper showText:@"请至少选择一个类目" inView:self.view]; return; } [self.hudView show:YES]; [FKGuideRequest reqSavePreferWithIdentify:FK_PREFER_SAVE_REQ sexType:self.infoItem.sexType ageType:self.infoItem.ageType categoryList:[self selectedCategoryIdList] delegate:self]; } #pragma mark - property - (UILabel *)titleLabel{ if (_titleLabel == nil) { _titleLabel = [[UILabel alloc]init]; _titleLabel.font = [UIFont systemFontOfSize:14]; _titleLabel.textColor = UIColorFromRGB(0x9c9c9c); _titleLabel.text = @"选择感兴趣的\n可多选"; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.numberOfLines = 2; [_titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; } return _titleLabel; } - (UIButton *)confirmBtn{ if (_confirmBtn == nil) { _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_confirmBtn setTitle:@"确 认" forState:UIControlStateNormal]; [_confirmBtn setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal]; [_confirmBtn setBackgroundColor:UIColorFromRGB(0xff6362)]; _confirmBtn.titleLabel.font = [UIFont systemFontOfSize:15.5]; [_confirmBtn addTarget:self action:@selector(clickConfirmBtn:) forControlEvents:UIControlEventTouchUpInside]; _confirmBtn.layer.cornerRadius = 5.0f; } return _confirmBtn; } - (UICollectionView *)collectionView{ if (_collectionView == nil) { UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init]; flow.minimumLineSpacing = 10.0f; flow.minimumInteritemSpacing = 10.0f; flow.itemSize = CGSizeMake(CATEGORY_CELL_MARGIN, CATEGORY_CELL_MARGIN); _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flow]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.backgroundColor = [UIColor clearColor]; _collectionView.backgroundView = nil; _collectionView.allowsMultipleSelection = YES; [_collectionView registerClass:[FKCategoryCollectionCell class] forCellWithReuseIdentifier:FK_CATEGORY_COLLECTION_IDENTIFY]; } return _collectionView; } - (UIImageView *)bgImgView{ if (_bgImgView == nil) { NSString *bgStr = @"guideMan"; if (self.sexType == kSexTypeWoman) bgStr = @"guideWoman"; _bgImgView = [[UIImageView alloc ]initWithImage:[UIImage imageNamed:bgStr]]; _bgImgView.contentMode = UIViewContentModeScaleAspectFit; } return _bgImgView; } @end