123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- //
- // FKPreferResController.m
- // FirstLink
- //
- // Created by jack on 16/5/28.
- // Copyright © 2016年 FirstLink. All rights reserved.
- //
- #import "FKPreferResController.h"
- #import "FKPreferResCell.h"
- #import "FLControllerHelper.h"
- #import "FKSexGuideController.h"
- #import "FKTargetConfigUtil.h"
- #import "FKGuideRequest.h"
- #import "FKGuideReform.h"
- static NSString *FK_PREFER_RES_CELL_IDENTIFY = @"FK_PREFER_RES_CELL_IDENTIFY";
- #define COMMON_TAG_WIDTH 80
- #define COMMON_TAG_HEIGHT 30
- #define REQ_PREFER_DATA 901
- @interface FKPreferResController () <UICollectionViewDataSource, UICollectionViewDelegate, FLNetworkDelegate>
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) UIImageView *iconImgView;
- @property (nonatomic, strong) UILabel *titleLabel;
- @property (nonatomic, strong) UILabel *indicatorLabel;
- @property (nonatomic, strong) UILabel *leftTagLabel;
- @property (nonatomic, strong) UILabel *middleTagLabel;
- @property (nonatomic, strong) UILabel *rightTagLabel;
- @property (nonatomic, strong) UIButton *goAroundBtn;
- @property (nonatomic, strong) UIButton *editBtn;
- @property (nonatomic, strong) UIButton *backButton;
- @property (nonatomic, strong) MASConstraint *middleConstraint;
- @property (nonatomic, assign) BOOL canEdit;
- @property (nonatomic, copy) preferFinish finishBlock;
- @property (nonatomic, copy) preferCancel cancelBlock;
- @property (nonatomic, strong) FKPreferinfoItem *infoItem;
- @end
- @implementation FKPreferResController
- - (instancetype)initWithEdit:(BOOL)edit
- infoItem:(FKPreferinfoItem *)infoItem
- finish:(preferFinish)finish
- cancel:(preferCancel)cancel{
- if (self = [super init]){
- self.canEdit = edit;
- self.finishBlock = finish;
- self.cancelBlock = cancel;
- self.infoItem = infoItem;
- }
- return self;
- }
- - (void)viewDidLoad{
- [super viewDidLoad];
-
- self.view.backgroundColor = [UIColor whiteColor];
- [self addAllSubviews];
- [self requestPreferData];
- }
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
-
- [self.navigationController setNavigationBarHidden:YES animated:YES];
-
- // if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
- // self.navigationController.interactivePopGestureRecognizer.delegate = self;
- // }
- }
- - (void)addAllSubviews{
-
- [self.view addSubview:self.iconImgView];
- [self.view addSubview:self.titleLabel];
- [self.view addSubview:self.leftTagLabel];
- [self.view addSubview:self.middleTagLabel];
- [self.view addSubview:self.rightTagLabel];
- [self.view addSubview:self.collectionView];
- [self.view addSubview:self.indicatorLabel];
- [self.view addSubview:self.goAroundBtn];
- [self.view addSubview:self.editBtn];
- [self.view addSubview:self.backButton];
-
- CGFloat indicatorBottom = 40.0f;
- CGFloat iconTop = 100.0f;
- if (UISCREENHEIGH <= 480){
- indicatorBottom = 25.0f;
- iconTop = 50.0f;
- }
-
-
- [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.view).offset(IS_IPHONE_X ? (iconTop+24) : iconTop);
- make.centerX.equalTo(self.view);
- }];
-
- [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.iconImgView.mas_bottom).offset(11);
- make.centerX.equalTo(self.view);
- }];
-
- [self.middleTagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.titleLabel.mas_bottom).offset(30);
- make.centerX.equalTo(self.view);
- self.middleConstraint = make.width.mas_equalTo(COMMON_TAG_WIDTH);
- make.height.mas_equalTo(COMMON_TAG_HEIGHT);
- }];
-
- [self.leftTagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.middleTagLabel);
- make.right.equalTo(self.middleTagLabel.mas_left).offset(- 20);
- make.size.mas_equalTo(CGSizeMake(COMMON_TAG_WIDTH, COMMON_TAG_HEIGHT));
- }];
-
- [self.rightTagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.middleTagLabel);
- make.left.equalTo(self.middleTagLabel.mas_right).offset(20);
- make.size.mas_equalTo(CGSizeMake(COMMON_TAG_WIDTH, COMMON_TAG_HEIGHT));
- }];
-
- [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.leftTagLabel.mas_bottom).offset(20);
- make.left.equalTo(self.view).offset(20);
- make.right.equalTo(self.view).offset(- 20);
- make.bottom.equalTo(self.indicatorLabel.mas_top).offset(- 10);
- }];
-
- [self.indicatorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(self.goAroundBtn.mas_top).offset(- indicatorBottom);
- make.centerX.equalTo(self.view);
- }];
-
- [self.goAroundBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(self.view).offset(IS_IPHONE_X ? -74 : -40);
- make.centerX.equalTo(self.view);
- make.size.mas_equalTo(CGSizeMake(160, 40));
- }];
-
- [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(self.view).offset(IS_IPHONE_X ? -74 : -40);
- make.centerX.equalTo(self.view);
- }];
-
- [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.view.mas_left).offset(28);
- make.centerY.equalTo(self.view.mas_top).offset(IS_IPHONE_X ? 66 : 42);
- make.size.mas_equalTo(CGSizeMake(50, 50));
- }];
- }
- - (void)configContent{
-
- switch (self.infoItem.sexType) {
- case kSexTypeMan:{
- self.leftTagLabel.text = @"男士";
- self.middleConstraint.mas_equalTo(0);
- }
- break;
- case kSexTypeWoman:{
- self.leftTagLabel.text = @"女士";
- self.middleConstraint.mas_equalTo(0);
- }
- break;
- case kSexTypeBoth:{
- self.leftTagLabel.text = @"男士";
- self.middleTagLabel.text = @"女士";
- }
- break;
- default:
- break;
- }
-
- switch (self.infoItem.ageType) {
- case kAgeTypeSeven:
- self.rightTagLabel.text = @"70后";
- break;
- case kAgeTypeEight:
- self.rightTagLabel.text = @"80后";
- break;
- case kAgeTypeNine:
- self.rightTagLabel.text = @"90后";
- default:
- break;
- }
-
- // 设置居中
- UICollectionViewFlowLayout *flow = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
- if (self.infoItem.categoryList.count == 1){
- flow.sectionInset = UIEdgeInsetsMake(0, (UISCREENWIDTH - 40 - COMMON_TAG_WIDTH) / 2.0f, 0, 0);
- }else if (self.infoItem.categoryList.count == 2){
- flow.sectionInset = UIEdgeInsetsMake(0, 50, 0, 50);
- }
- [self.collectionView reloadData];
-
- if (self.canEdit){
- self.goAroundBtn.hidden = YES;
- self.editBtn.hidden = NO;
- self.backButton.hidden = NO;
- self.backButton.userInteractionEnabled = YES;
- }else{
- self.goAroundBtn.hidden = NO;
- self.editBtn.hidden = YES;
- self.backButton.hidden = YES;
- self.backButton.userInteractionEnabled = NO;
- }
- }
- - (void)requestPreferData{
-
- if (!self.infoItem){
- [self.hudView show:YES];
- [FKGuideRequest reqPreferDataWithIdentify:REQ_PREFER_DATA delegate:self];
- }else{
- [self configContent];
- }
- }
- #pragma mark - response
- - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header{
-
- [self.hudView hide:NO];
-
- if (header.code.integerValue == RESPONSE_MSG_NORMAL){
- if (identify == REQ_PREFER_DATA){
- self.infoItem = [FKGuideReform parserInfoItemWithDict:response];
- [self configContent];
- }
- }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.infoItem.categoryList.count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
- FKPreferResCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FK_PREFER_RES_CELL_IDENTIFY
- forIndexPath:indexPath];
- FKPreferCategoryItem *item = [self.infoItem categoryItemAtIndex:indexPath.row];
- cell.titleLabel.text = item.name;
- return cell;
- }
- #pragma mark - action
- - (void)clickGoAroundBtn{
- if (self.finishBlock){
- self.finishBlock();
- return;
- }
- [self.navigationController popToRootViewControllerAnimated:YES];
- }
- - (void)clickEditBtn{
- FKSexGuideController *sexGuide = [[FKSexGuideController alloc]initWithInfoItem:self.infoItem
- finish:self.finishBlock
- cancel:self.cancelBlock];
- [self.navigationController pushViewController:sexGuide animated:YES];
- }
- - (void)clickBackBtn{
- [self.navigationController popViewControllerAnimated:YES];
- }
- #pragma mark - property
- - (UILabel *)titleLabel{
- if (_titleLabel == nil) {
- _titleLabel = [[UILabel alloc]init];
- _titleLabel.text = @"您的偏好标签是";
- _titleLabel.font = [UIFont systemFontOfSize:16];
- _titleLabel.textColor = UIColorFromRGB(0x9c9c9c);
- }
- return _titleLabel;
- }
- - (UILabel *)indicatorLabel{
- if (_indicatorLabel == nil) {
- _indicatorLabel = [[UILabel alloc]init];
- _indicatorLabel.font = [UIFont systemFontOfSize:14];
- _indicatorLabel.textColor = UIColorFromRGB(0x9c9c9c);
- _indicatorLabel.text = [NSString stringWithFormat:@"%@将根据您的不同偏好展示商品", [FKTargetConfigUtil appName]];
- }
- return _indicatorLabel;
- }
- - (UIImageView *)iconImgView{
- if (_iconImgView == nil) {
- _iconImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"people_guide_icon"]];
- }
- return _iconImgView;
- }
- - (UIButton *)goAroundBtn{
- if (_goAroundBtn == nil) {
- _goAroundBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_goAroundBtn setTitle:@"去逛逛" forState:UIControlStateNormal];
- [_goAroundBtn setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
- [_goAroundBtn setBackgroundColor:UIColorFromRGB(0xff6362)];
- _goAroundBtn.titleLabel.font = [UIFont systemFontOfSize:15.5];
- _goAroundBtn.layer.cornerRadius = 5.0f;
- _goAroundBtn.hidden = YES;
- [_goAroundBtn addTarget:self
- action:@selector(clickGoAroundBtn)
- forControlEvents:UIControlEventTouchUpInside];
-
- }
- return _goAroundBtn;
- }
- - (UIButton *)editBtn{
- if (_editBtn == nil) {
- _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _editBtn.hidden = YES;
-
- [_editBtn setImage:[UIImage imageNamed:@"prefer_edit"] forState:UIControlStateNormal];
- [_editBtn addTarget:self
- action:@selector(clickEditBtn)
- forControlEvents:UIControlEventTouchUpInside];
- }
- return _editBtn;
- }
- - (UIButton *)backButton{
- if (_backButton == nil) {
- _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_backButton setImage:[UIImage imageNamed:@"fk_common_back"] forState:UIControlStateNormal];
- [_backButton addTarget:self action:@selector(clickBackBtn) forControlEvents:UIControlEventTouchUpInside];
- _backButton.hidden = YES;
- _backButton.userInteractionEnabled = NO;
- }
- return _backButton;
- }
- - (UILabel *)leftTagLabel{
- if (_leftTagLabel == nil) {
- _leftTagLabel = [[UILabel alloc]init];
- _leftTagLabel.backgroundColor = UIColorFromRGB(0xe2e2e2);
- _leftTagLabel.font = [UIFont systemFontOfSize:16];
- _leftTagLabel.layer.cornerRadius = 3.0f;
- _leftTagLabel.layer.masksToBounds = YES;
- _leftTagLabel.textAlignment = NSTextAlignmentCenter;
- }
- return _leftTagLabel;
- }
- - (UILabel *)middleTagLabel{
- if (_middleTagLabel == nil) {
- _middleTagLabel = [[UILabel alloc]init];
- _middleTagLabel.backgroundColor = UIColorFromRGB(0xe2e2e2);;
- _middleTagLabel.font = [UIFont systemFontOfSize:16];
- _middleTagLabel.layer.cornerRadius = 3.0f;
- _middleTagLabel.layer.masksToBounds = YES;
- _middleTagLabel.textAlignment = NSTextAlignmentCenter;
- }
- return _middleTagLabel;
- }
- - (UILabel *)rightTagLabel{
- if (_rightTagLabel == nil) {
- _rightTagLabel = [[UILabel alloc]init];
- _rightTagLabel.backgroundColor = UIColorFromRGB(0xe2e2e2);
- _rightTagLabel.font = [UIFont systemFontOfSize:16];
- _rightTagLabel.layer.cornerRadius = 3.0f;
- _rightTagLabel.layer.masksToBounds = YES;
- _rightTagLabel.textAlignment = NSTextAlignmentCenter;
- }
- return _rightTagLabel;
- }
- - (UICollectionView *)collectionView{
- if (_collectionView == nil) {
-
- UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
- flow.minimumLineSpacing = 20.0f;
- flow.minimumInteritemSpacing = 20.0f;
- flow.itemSize = CGSizeMake(COMMON_TAG_WIDTH, COMMON_TAG_HEIGHT);
-
- _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flow];
- _collectionView.dataSource = self;
- _collectionView.delegate = self;
- _collectionView.backgroundColor = [UIColor clearColor];
- _collectionView.backgroundView = nil;
-
- [_collectionView registerClass:[FKPreferResCell class] forCellWithReuseIdentifier:FK_PREFER_RES_CELL_IDENTIFY];
- }
- return _collectionView;
- }
- @end
|