123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- //
- // 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 () <UICollectionViewDataSource, UICollectionViewDelegate, FLNetworkDelegate>
- @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
|