No Description

FKCategoryGuideController.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //
  2. // FKBrandGuideController.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/4/27.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKCategoryGuideController.h"
  9. #import "FKGuideReform.h"
  10. #import "FKGuideRequest.h"
  11. #import "FKGuideBrandItem.h"
  12. #import "FKGuideManager.h"
  13. #import "FKCategoryCollectionCell.h"
  14. static NSString *FK_CATEGORY_COLLECTION_IDENTIFY = @"FK_CATEGORY_COLLECTION_IDENTIFY";
  15. #define FK_PREFER_CATEGORY_REQ 788
  16. #define FK_PREFER_SAVE_REQ 789
  17. #define CATEGORY_CELL_MARGIN (floor((UISCREENWIDTH - 30 - 20) / 3.0f))
  18. @interface FKCategoryGuideController () <UICollectionViewDataSource, UICollectionViewDelegate, FLNetworkDelegate>
  19. @property (nonatomic, assign) kSexType sexType;
  20. @property (nonatomic, assign) kAgeType ageType;
  21. @property (nonatomic, strong) UILabel *titleLabel;
  22. @property (nonatomic, strong) UIButton *confirmBtn;
  23. @property (nonatomic, strong) UIImageView *bgImgView;
  24. @property (nonatomic, strong) UICollectionView *collectionView;
  25. @property (nonatomic, strong) NSArray *categoryArray;
  26. @property (nonatomic, strong) FKPreferinfoItem *infoItem;
  27. @property (nonatomic, copy) preferFinish finishBlock;
  28. @end
  29. @implementation FKCategoryGuideController
  30. - (instancetype)initWithInfoItem:(FKPreferinfoItem *)infoItem
  31. finish:(preferFinish)finish{
  32. if (self = [super init]) {
  33. self.infoItem = infoItem;
  34. self.finishBlock = finish;
  35. }
  36. return self;
  37. }
  38. - (void)viewDidLoad{
  39. [super viewDidLoad];
  40. self.view.backgroundColor = UIColorFromRGB(0xf4f4f4);
  41. [self requestCategoryList];
  42. [self addAllSubviews];
  43. }
  44. - (void)viewWillAppear:(BOOL)animated{
  45. [super viewWillAppear:animated];
  46. // if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  47. // self.navigationController.interactivePopGestureRecognizer.delegate = self;
  48. // }
  49. }
  50. - (void)requestCategoryList{
  51. [self.hudView show:YES];
  52. [FKGuideRequest reqPreferCategoryListWithIdentify:FK_PREFER_CATEGORY_REQ delegate:self];
  53. }
  54. - (void)addAllSubviews{
  55. // [self.view addSubview:self.bgImgView];
  56. [self.view addSubview:self.collectionView];
  57. [self.view addSubview:self.titleLabel];
  58. [self.view addSubview:self.confirmBtn];
  59. // CGFloat imgWidth = UISCREENHEIGH * 750.0f / 1334.0f;
  60. //
  61. // [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  62. // make.top.bottom.equalTo(self.view);
  63. // make.centerX.equalTo(self.view);
  64. // make.width.mas_equalTo(imgWidth);
  65. // }];
  66. // CGFloat confirmMargin = 70;
  67. CGFloat titleTop = 100.0f;
  68. if (UISCREENHEIGH <= 480){
  69. titleTop = 70;
  70. // confirmMargin = 30;
  71. // CGFloat imgHeight = UISCREENWIDTH * 1334.0f / 750.0f;
  72. // [self.bgImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
  73. // make.top.bottom.equalTo(self.view);
  74. // make.left.right.equalTo(self.view);
  75. // make.height.mas_equalTo(imgHeight);
  76. // }];
  77. }
  78. [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  79. make.bottom.equalTo(self.view).offset(IS_IPHONE_X ? -55 : -45);
  80. make.centerX.equalTo(self.view);
  81. make.size.mas_equalTo(CGSizeMake(140, 40));
  82. }];
  83. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  84. make.top.equalTo(self.view).offset(titleTop);
  85. make.centerX.equalTo(self.view);
  86. }];
  87. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.top.equalTo(self.titleLabel.mas_bottom).offset(20);
  89. make.left.equalTo(self.view).offset(15);
  90. make.right.equalTo(self.view).offset(- 15);
  91. make.bottom.equalTo(self.confirmBtn.mas_top).offset(- 20);
  92. }];
  93. }
  94. //- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
  95. // return NO;
  96. //}
  97. #pragma mark - response
  98. - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header {
  99. [self.hudView hide:NO];
  100. if (header.code.intValue == RESPONSE_MSG_NORMAL) {
  101. if (identify == FK_PREFER_CATEGORY_REQ){
  102. self.categoryArray = [FKGuideReform parserCategoryListWithDict:response];
  103. [self setItemSelectState];
  104. [self.collectionView reloadData];
  105. }else if (identify == FK_PREFER_SAVE_REQ){
  106. [self saveIntoInfoItem];
  107. FKPreferResController *perferRes = [[FKPreferResController alloc]initWithEdit:NO
  108. infoItem:self.infoItem
  109. finish:self.finishBlock
  110. cancel:nil];
  111. [self.navigationController pushViewController:perferRes animated:YES];
  112. }
  113. }else{
  114. [FLProgressHUDHelper showText:header.msg inView:self.view];
  115. }
  116. }
  117. - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header{
  118. [self.hudView hide:NO];
  119. [FLProgressHUDHelper showText:header.msg inView:self.view];
  120. }
  121. #pragma mark - collectionView dataSource
  122. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  123. return self.categoryArray.count;
  124. }
  125. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  126. FKCategoryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FK_CATEGORY_COLLECTION_IDENTIFY
  127. forIndexPath:indexPath];
  128. FKPreferCategoryItem *item = [self categoryItemAtIndex:indexPath.row];
  129. cell.selectImgView.hidden = !item.selected;
  130. cell.coverView.hidden = !item.selected;
  131. if (item.picUrl){
  132. [cell.imageView setImageWithURL:item.picUrl cdnWidth:CATEGORY_CELL_MARGIN];
  133. }
  134. return cell;
  135. }
  136. #pragma mark - collectionView delegate
  137. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  138. FKPreferCategoryItem *item = [self categoryItemAtIndex:indexPath.row];
  139. item.selected = !item.selected;
  140. [self.collectionView reloadData];
  141. }
  142. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
  143. FKPreferCategoryItem *item = [self categoryItemAtIndex:indexPath.row];
  144. item.selected = !item.selected;
  145. [self.collectionView reloadData];
  146. }
  147. #pragma mark - method
  148. - (void)saveIntoInfoItem{
  149. NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:self.categoryArray.count];
  150. for (FKPreferCategoryItem *item in self.categoryArray) {
  151. if (item.selected){
  152. [arrayM addObject:item];
  153. }
  154. }
  155. self.infoItem.categoryList = arrayM;
  156. }
  157. - (void)setItemSelectState{
  158. for (FKPreferCategoryItem *selectItem in self.infoItem.categoryList) {
  159. for (FKPreferCategoryItem *sourceItem in self.categoryArray) {
  160. if ([sourceItem.itemID isEqualToString:selectItem.itemID]){
  161. sourceItem.selected = YES;
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. - (FKPreferCategoryItem *)categoryItemAtIndex:(NSUInteger)index{
  168. if (index < self.categoryArray.count){
  169. return self.categoryArray[index];
  170. }
  171. return nil;
  172. }
  173. - (NSArray *)selectedCategoryIdList{
  174. NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:self.categoryArray.count];
  175. for (FKPreferCategoryItem *item in self.categoryArray) {
  176. if (item.selected){
  177. [arrayM addObject:item.itemID];
  178. }
  179. }
  180. if (!arrayM.count) return nil;
  181. return arrayM;
  182. }
  183. #pragma mark - action
  184. - (void)clickConfirmBtn:(UIButton *)sender{
  185. if (![self selectedCategoryIdList].count){
  186. [FLProgressHUDHelper showText:@"请至少选择一个类目" inView:self.view];
  187. return;
  188. }
  189. [self.hudView show:YES];
  190. [FKGuideRequest reqSavePreferWithIdentify:FK_PREFER_SAVE_REQ
  191. sexType:self.infoItem.sexType
  192. ageType:self.infoItem.ageType
  193. categoryList:[self selectedCategoryIdList]
  194. delegate:self];
  195. }
  196. #pragma mark - property
  197. - (UILabel *)titleLabel{
  198. if (_titleLabel == nil) {
  199. _titleLabel = [[UILabel alloc]init];
  200. _titleLabel.font = [UIFont systemFontOfSize:14];
  201. _titleLabel.textColor = UIColorFromRGB(0x9c9c9c);
  202. _titleLabel.text = @"选择感兴趣的\n可多选";
  203. _titleLabel.textAlignment = NSTextAlignmentCenter;
  204. _titleLabel.numberOfLines = 2;
  205. [_titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
  206. }
  207. return _titleLabel;
  208. }
  209. - (UIButton *)confirmBtn{
  210. if (_confirmBtn == nil) {
  211. _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  212. [_confirmBtn setTitle:@"确 认" forState:UIControlStateNormal];
  213. [_confirmBtn setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
  214. [_confirmBtn setBackgroundColor:UIColorFromRGB(0xff6362)];
  215. _confirmBtn.titleLabel.font = [UIFont systemFontOfSize:15.5];
  216. [_confirmBtn addTarget:self action:@selector(clickConfirmBtn:) forControlEvents:UIControlEventTouchUpInside];
  217. _confirmBtn.layer.cornerRadius = 5.0f;
  218. }
  219. return _confirmBtn;
  220. }
  221. - (UICollectionView *)collectionView{
  222. if (_collectionView == nil) {
  223. UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
  224. flow.minimumLineSpacing = 10.0f;
  225. flow.minimumInteritemSpacing = 10.0f;
  226. flow.itemSize = CGSizeMake(CATEGORY_CELL_MARGIN, CATEGORY_CELL_MARGIN);
  227. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flow];
  228. _collectionView.dataSource = self;
  229. _collectionView.delegate = self;
  230. _collectionView.backgroundColor = [UIColor clearColor];
  231. _collectionView.backgroundView = nil;
  232. _collectionView.allowsMultipleSelection = YES;
  233. [_collectionView registerClass:[FKCategoryCollectionCell class] forCellWithReuseIdentifier:FK_CATEGORY_COLLECTION_IDENTIFY];
  234. }
  235. return _collectionView;
  236. }
  237. - (UIImageView *)bgImgView{
  238. if (_bgImgView == nil) {
  239. NSString *bgStr = @"guideMan";
  240. if (self.sexType == kSexTypeWoman) bgStr = @"guideWoman";
  241. _bgImgView = [[UIImageView alloc ]initWithImage:[UIImage imageNamed:bgStr]];
  242. _bgImgView.contentMode = UIViewContentModeScaleAspectFit;
  243. }
  244. return _bgImgView;
  245. }
  246. @end