No Description

FKPreferResController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // FKPreferResController.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/5/28.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKPreferResController.h"
  9. #import "FKPreferResCell.h"
  10. #import "FLControllerHelper.h"
  11. #import "FKSexGuideController.h"
  12. #import "FKTargetConfigUtil.h"
  13. #import "FKGuideRequest.h"
  14. #import "FKGuideReform.h"
  15. static NSString *FK_PREFER_RES_CELL_IDENTIFY = @"FK_PREFER_RES_CELL_IDENTIFY";
  16. #define COMMON_TAG_WIDTH 80
  17. #define COMMON_TAG_HEIGHT 30
  18. #define REQ_PREFER_DATA 901
  19. @interface FKPreferResController () <UICollectionViewDataSource, UICollectionViewDelegate, FLNetworkDelegate>
  20. @property (nonatomic, strong) UICollectionView *collectionView;
  21. @property (nonatomic, strong) UIImageView *iconImgView;
  22. @property (nonatomic, strong) UILabel *titleLabel;
  23. @property (nonatomic, strong) UILabel *indicatorLabel;
  24. @property (nonatomic, strong) UILabel *leftTagLabel;
  25. @property (nonatomic, strong) UILabel *middleTagLabel;
  26. @property (nonatomic, strong) UILabel *rightTagLabel;
  27. @property (nonatomic, strong) UIButton *goAroundBtn;
  28. @property (nonatomic, strong) UIButton *editBtn;
  29. @property (nonatomic, strong) UIButton *backButton;
  30. @property (nonatomic, strong) MASConstraint *middleConstraint;
  31. @property (nonatomic, assign) BOOL canEdit;
  32. @property (nonatomic, copy) preferFinish finishBlock;
  33. @property (nonatomic, copy) preferCancel cancelBlock;
  34. @property (nonatomic, strong) FKPreferinfoItem *infoItem;
  35. @end
  36. @implementation FKPreferResController
  37. - (instancetype)initWithEdit:(BOOL)edit
  38. infoItem:(FKPreferinfoItem *)infoItem
  39. finish:(preferFinish)finish
  40. cancel:(preferCancel)cancel{
  41. if (self = [super init]){
  42. self.canEdit = edit;
  43. self.finishBlock = finish;
  44. self.cancelBlock = cancel;
  45. self.infoItem = infoItem;
  46. }
  47. return self;
  48. }
  49. - (void)viewDidLoad{
  50. [super viewDidLoad];
  51. self.view.backgroundColor = [UIColor whiteColor];
  52. [self addAllSubviews];
  53. [self requestPreferData];
  54. }
  55. - (void)viewWillAppear:(BOOL)animated{
  56. [super viewWillAppear:animated];
  57. [self.navigationController setNavigationBarHidden:YES animated:YES];
  58. // if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  59. // self.navigationController.interactivePopGestureRecognizer.delegate = self;
  60. // }
  61. }
  62. - (void)addAllSubviews{
  63. [self.view addSubview:self.iconImgView];
  64. [self.view addSubview:self.titleLabel];
  65. [self.view addSubview:self.leftTagLabel];
  66. [self.view addSubview:self.middleTagLabel];
  67. [self.view addSubview:self.rightTagLabel];
  68. [self.view addSubview:self.collectionView];
  69. [self.view addSubview:self.indicatorLabel];
  70. [self.view addSubview:self.goAroundBtn];
  71. [self.view addSubview:self.editBtn];
  72. [self.view addSubview:self.backButton];
  73. CGFloat indicatorBottom = 40.0f;
  74. CGFloat iconTop = 100.0f;
  75. if (UISCREENHEIGH <= 480){
  76. indicatorBottom = 25.0f;
  77. iconTop = 50.0f;
  78. }
  79. [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.top.equalTo(self.view).offset(IS_IPHONE_X ? (iconTop+24) : iconTop);
  81. make.centerX.equalTo(self.view);
  82. }];
  83. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  84. make.top.equalTo(self.iconImgView.mas_bottom).offset(11);
  85. make.centerX.equalTo(self.view);
  86. }];
  87. [self.middleTagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.top.equalTo(self.titleLabel.mas_bottom).offset(30);
  89. make.centerX.equalTo(self.view);
  90. self.middleConstraint = make.width.mas_equalTo(COMMON_TAG_WIDTH);
  91. make.height.mas_equalTo(COMMON_TAG_HEIGHT);
  92. }];
  93. [self.leftTagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.top.equalTo(self.middleTagLabel);
  95. make.right.equalTo(self.middleTagLabel.mas_left).offset(- 20);
  96. make.size.mas_equalTo(CGSizeMake(COMMON_TAG_WIDTH, COMMON_TAG_HEIGHT));
  97. }];
  98. [self.rightTagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  99. make.top.equalTo(self.middleTagLabel);
  100. make.left.equalTo(self.middleTagLabel.mas_right).offset(20);
  101. make.size.mas_equalTo(CGSizeMake(COMMON_TAG_WIDTH, COMMON_TAG_HEIGHT));
  102. }];
  103. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  104. make.top.equalTo(self.leftTagLabel.mas_bottom).offset(20);
  105. make.left.equalTo(self.view).offset(20);
  106. make.right.equalTo(self.view).offset(- 20);
  107. make.bottom.equalTo(self.indicatorLabel.mas_top).offset(- 10);
  108. }];
  109. [self.indicatorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  110. make.bottom.equalTo(self.goAroundBtn.mas_top).offset(- indicatorBottom);
  111. make.centerX.equalTo(self.view);
  112. }];
  113. [self.goAroundBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  114. make.bottom.equalTo(self.view).offset(IS_IPHONE_X ? -74 : -40);
  115. make.centerX.equalTo(self.view);
  116. make.size.mas_equalTo(CGSizeMake(160, 40));
  117. }];
  118. [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  119. make.bottom.equalTo(self.view).offset(IS_IPHONE_X ? -74 : -40);
  120. make.centerX.equalTo(self.view);
  121. }];
  122. [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
  123. make.centerX.equalTo(self.view.mas_left).offset(28);
  124. make.centerY.equalTo(self.view.mas_top).offset(IS_IPHONE_X ? 66 : 42);
  125. make.size.mas_equalTo(CGSizeMake(50, 50));
  126. }];
  127. }
  128. - (void)configContent{
  129. switch (self.infoItem.sexType) {
  130. case kSexTypeMan:{
  131. self.leftTagLabel.text = @"男士";
  132. self.middleConstraint.mas_equalTo(0);
  133. }
  134. break;
  135. case kSexTypeWoman:{
  136. self.leftTagLabel.text = @"女士";
  137. self.middleConstraint.mas_equalTo(0);
  138. }
  139. break;
  140. case kSexTypeBoth:{
  141. self.leftTagLabel.text = @"男士";
  142. self.middleTagLabel.text = @"女士";
  143. }
  144. break;
  145. default:
  146. break;
  147. }
  148. switch (self.infoItem.ageType) {
  149. case kAgeTypeSeven:
  150. self.rightTagLabel.text = @"70后";
  151. break;
  152. case kAgeTypeEight:
  153. self.rightTagLabel.text = @"80后";
  154. break;
  155. case kAgeTypeNine:
  156. self.rightTagLabel.text = @"90后";
  157. default:
  158. break;
  159. }
  160. // 设置居中
  161. UICollectionViewFlowLayout *flow = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
  162. if (self.infoItem.categoryList.count == 1){
  163. flow.sectionInset = UIEdgeInsetsMake(0, (UISCREENWIDTH - 40 - COMMON_TAG_WIDTH) / 2.0f, 0, 0);
  164. }else if (self.infoItem.categoryList.count == 2){
  165. flow.sectionInset = UIEdgeInsetsMake(0, 50, 0, 50);
  166. }
  167. [self.collectionView reloadData];
  168. if (self.canEdit){
  169. self.goAroundBtn.hidden = YES;
  170. self.editBtn.hidden = NO;
  171. self.backButton.hidden = NO;
  172. self.backButton.userInteractionEnabled = YES;
  173. }else{
  174. self.goAroundBtn.hidden = NO;
  175. self.editBtn.hidden = YES;
  176. self.backButton.hidden = YES;
  177. self.backButton.userInteractionEnabled = NO;
  178. }
  179. }
  180. - (void)requestPreferData{
  181. if (!self.infoItem){
  182. [self.hudView show:YES];
  183. [FKGuideRequest reqPreferDataWithIdentify:REQ_PREFER_DATA delegate:self];
  184. }else{
  185. [self configContent];
  186. }
  187. }
  188. #pragma mark - response
  189. - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header{
  190. [self.hudView hide:NO];
  191. if (header.code.integerValue == RESPONSE_MSG_NORMAL){
  192. if (identify == REQ_PREFER_DATA){
  193. self.infoItem = [FKGuideReform parserInfoItemWithDict:response];
  194. [self configContent];
  195. }
  196. }else{
  197. [FLProgressHUDHelper showText:header.msg inView:self.view];
  198. }
  199. }
  200. - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header{
  201. [self.hudView hide:NO];
  202. [FLProgressHUDHelper showText:header.msg inView:self.view];
  203. }
  204. #pragma mark - collectionView dataSource
  205. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  206. return self.infoItem.categoryList.count;
  207. }
  208. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  209. FKPreferResCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FK_PREFER_RES_CELL_IDENTIFY
  210. forIndexPath:indexPath];
  211. FKPreferCategoryItem *item = [self.infoItem categoryItemAtIndex:indexPath.row];
  212. cell.titleLabel.text = item.name;
  213. return cell;
  214. }
  215. #pragma mark - action
  216. - (void)clickGoAroundBtn{
  217. if (self.finishBlock){
  218. self.finishBlock();
  219. return;
  220. }
  221. [self.navigationController popToRootViewControllerAnimated:YES];
  222. }
  223. - (void)clickEditBtn{
  224. FKSexGuideController *sexGuide = [[FKSexGuideController alloc]initWithInfoItem:self.infoItem
  225. finish:self.finishBlock
  226. cancel:self.cancelBlock];
  227. [self.navigationController pushViewController:sexGuide animated:YES];
  228. }
  229. - (void)clickBackBtn{
  230. [self.navigationController popViewControllerAnimated:YES];
  231. }
  232. #pragma mark - property
  233. - (UILabel *)titleLabel{
  234. if (_titleLabel == nil) {
  235. _titleLabel = [[UILabel alloc]init];
  236. _titleLabel.text = @"您的偏好标签是";
  237. _titleLabel.font = [UIFont systemFontOfSize:16];
  238. _titleLabel.textColor = UIColorFromRGB(0x9c9c9c);
  239. }
  240. return _titleLabel;
  241. }
  242. - (UILabel *)indicatorLabel{
  243. if (_indicatorLabel == nil) {
  244. _indicatorLabel = [[UILabel alloc]init];
  245. _indicatorLabel.font = [UIFont systemFontOfSize:14];
  246. _indicatorLabel.textColor = UIColorFromRGB(0x9c9c9c);
  247. _indicatorLabel.text = [NSString stringWithFormat:@"%@将根据您的不同偏好展示商品", [FKTargetConfigUtil appName]];
  248. }
  249. return _indicatorLabel;
  250. }
  251. - (UIImageView *)iconImgView{
  252. if (_iconImgView == nil) {
  253. _iconImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"people_guide_icon"]];
  254. }
  255. return _iconImgView;
  256. }
  257. - (UIButton *)goAroundBtn{
  258. if (_goAroundBtn == nil) {
  259. _goAroundBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  260. [_goAroundBtn setTitle:@"去逛逛" forState:UIControlStateNormal];
  261. [_goAroundBtn setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
  262. [_goAroundBtn setBackgroundColor:UIColorFromRGB(0xff6362)];
  263. _goAroundBtn.titleLabel.font = [UIFont systemFontOfSize:15.5];
  264. _goAroundBtn.layer.cornerRadius = 5.0f;
  265. _goAroundBtn.hidden = YES;
  266. [_goAroundBtn addTarget:self
  267. action:@selector(clickGoAroundBtn)
  268. forControlEvents:UIControlEventTouchUpInside];
  269. }
  270. return _goAroundBtn;
  271. }
  272. - (UIButton *)editBtn{
  273. if (_editBtn == nil) {
  274. _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  275. _editBtn.hidden = YES;
  276. [_editBtn setImage:[UIImage imageNamed:@"prefer_edit"] forState:UIControlStateNormal];
  277. [_editBtn addTarget:self
  278. action:@selector(clickEditBtn)
  279. forControlEvents:UIControlEventTouchUpInside];
  280. }
  281. return _editBtn;
  282. }
  283. - (UIButton *)backButton{
  284. if (_backButton == nil) {
  285. _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
  286. [_backButton setImage:[UIImage imageNamed:@"fk_common_back"] forState:UIControlStateNormal];
  287. [_backButton addTarget:self action:@selector(clickBackBtn) forControlEvents:UIControlEventTouchUpInside];
  288. _backButton.hidden = YES;
  289. _backButton.userInteractionEnabled = NO;
  290. }
  291. return _backButton;
  292. }
  293. - (UILabel *)leftTagLabel{
  294. if (_leftTagLabel == nil) {
  295. _leftTagLabel = [[UILabel alloc]init];
  296. _leftTagLabel.backgroundColor = UIColorFromRGB(0xe2e2e2);
  297. _leftTagLabel.font = [UIFont systemFontOfSize:16];
  298. _leftTagLabel.layer.cornerRadius = 3.0f;
  299. _leftTagLabel.layer.masksToBounds = YES;
  300. _leftTagLabel.textAlignment = NSTextAlignmentCenter;
  301. }
  302. return _leftTagLabel;
  303. }
  304. - (UILabel *)middleTagLabel{
  305. if (_middleTagLabel == nil) {
  306. _middleTagLabel = [[UILabel alloc]init];
  307. _middleTagLabel.backgroundColor = UIColorFromRGB(0xe2e2e2);;
  308. _middleTagLabel.font = [UIFont systemFontOfSize:16];
  309. _middleTagLabel.layer.cornerRadius = 3.0f;
  310. _middleTagLabel.layer.masksToBounds = YES;
  311. _middleTagLabel.textAlignment = NSTextAlignmentCenter;
  312. }
  313. return _middleTagLabel;
  314. }
  315. - (UILabel *)rightTagLabel{
  316. if (_rightTagLabel == nil) {
  317. _rightTagLabel = [[UILabel alloc]init];
  318. _rightTagLabel.backgroundColor = UIColorFromRGB(0xe2e2e2);
  319. _rightTagLabel.font = [UIFont systemFontOfSize:16];
  320. _rightTagLabel.layer.cornerRadius = 3.0f;
  321. _rightTagLabel.layer.masksToBounds = YES;
  322. _rightTagLabel.textAlignment = NSTextAlignmentCenter;
  323. }
  324. return _rightTagLabel;
  325. }
  326. - (UICollectionView *)collectionView{
  327. if (_collectionView == nil) {
  328. UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
  329. flow.minimumLineSpacing = 20.0f;
  330. flow.minimumInteritemSpacing = 20.0f;
  331. flow.itemSize = CGSizeMake(COMMON_TAG_WIDTH, COMMON_TAG_HEIGHT);
  332. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flow];
  333. _collectionView.dataSource = self;
  334. _collectionView.delegate = self;
  335. _collectionView.backgroundColor = [UIColor clearColor];
  336. _collectionView.backgroundView = nil;
  337. [_collectionView registerClass:[FKPreferResCell class] forCellWithReuseIdentifier:FK_PREFER_RES_CELL_IDENTIFY];
  338. }
  339. return _collectionView;
  340. }
  341. @end