Açıklama Yok

CollectViewController.m 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // PindanCollectionController.m
  3. // FirstLink
  4. //
  5. // Created by Lemon on 15/4/28.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "CollectViewController.h"
  9. #import "PindanDetailController.h"
  10. #import "FKProDetailController.h"
  11. #import "FKVipCollectionCell.h"
  12. #import "FKCollectRequest.h"
  13. #import "FKCollectReform.h"
  14. #import "CollectViewModel.h"
  15. #import "FLImageHelper.h"
  16. static NSString *const PindanCollectionCellIdentifier = @"PindanCollectionCellIdentifier";
  17. @interface CollectViewController ()
  18. <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate, FLNetworkDelegate>
  19. @property (nonatomic, strong) UICollectionView *collectionView;
  20. @property (nonatomic, strong) CollectViewModel *viewModel;
  21. @end
  22. @implementation CollectViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. // Do any additional setup after loading the view.
  26. self.navigationItem.title = @"我的收藏";
  27. [self configCollectionView];
  28. [self addAllSubViews];
  29. }
  30. #pragma mark - Life Cyclelife
  31. - (void)viewWillAppear:(BOOL)animated {
  32. [super viewWillAppear:animated];
  33. [[self navigationController] setNavigationBarHidden:NO animated:YES];
  34. [self.collectionView reloadData];
  35. [self.hudView show:YES];
  36. [self refreshPindanPostRequest:REQUEST_LIST
  37. startRow:0
  38. pageSize:MAX(PAGE_RECORD_COUNT, (int)[self.viewModel numberOfRows])];
  39. }
  40. - (void)viewWillDisappear:(BOOL)animated {
  41. [super viewWillDisappear:animated];
  42. }
  43. #pragma mark - Request Data
  44. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
  45. // [self.hudView show:YES];
  46. if (direction == RefreshDirectionTop) {
  47. self.refreshControl.bottomEnabled = YES;
  48. [self refreshPindanPostRequest:REQUEST_LIST startRow:0 pageSize:PAGE_RECORD_COUNT];
  49. } else if (direction == RefreshDirectionBottom) {
  50. [self refreshPindanPostRequest:REQUEST_NEXT_PAGE
  51. startRow:(int)[self.viewModel numberOfRows]
  52. pageSize:PAGE_RECORD_COUNT];
  53. }
  54. }
  55. - (void)refreshPindanPostRequest:(int)identify
  56. startRow:(int)startRow
  57. pageSize:(int)pageSize {
  58. [FKCollectRequest requestItems:identify
  59. startRow:[NSString stringWithFormat:@"%d", startRow]
  60. pageSize:[NSString stringWithFormat:@"%d", pageSize]
  61. deleagate:self];
  62. }
  63. - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header {
  64. [self.hudView hide:YES];
  65. [self finishLoadingData];
  66. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  67. if (identify == REQUEST_LIST) {
  68. self.viewModel = [FKCollectReform parseCollectViewModel:response];
  69. }
  70. if (identify == REQUEST_NEXT_PAGE) {
  71. NSArray *items = [FKCollectReform parseCollectViewModel:response].itemsArray;
  72. [self.viewModel.itemsArray addObjectsFromArray:items];
  73. if (items.count == 0) {
  74. self.refreshControl.bottomEnabled = FALSE;
  75. }
  76. }
  77. if ([self.viewModel numberOfRows] == 0) {
  78. [self showStatusTipInView:self.view image:[UIImage imageNamed:@"StatusNoCollectIcon"] title:@"没有发现商品哦"];
  79. } else {
  80. [self.statusView removeFromSuperview];
  81. }
  82. [self.collectionView reloadData];
  83. }
  84. }
  85. - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header {
  86. [self.hudView hide:YES];
  87. [self finishLoadingData];
  88. [FLProgressHUDHelper showText:header.msg inView:self.view];
  89. }
  90. #pragma mark - UITableView Delegate & DataSource
  91. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  92. return 1;
  93. }
  94. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  95. return [self.viewModel numberOfRows];
  96. }
  97. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  98. FKVipCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FKVipCollectionCell class])
  99. forIndexPath:indexPath];
  100. CollectItem *item = [self.viewModel itemAtIndexPath:indexPath];
  101. if (item) {
  102. [cell.imageView setImageWithURL:item.photoURL cdnWidth:(UISCREENWIDTH - 30)/2];
  103. cell.titleLabel.text = item.title;
  104. cell.priceLabel.text = [FLStringHelper convertFenToYuan:item.price];
  105. // cell.cutPrice = [FLStringHelper convertFenStringToYuanValue:item.referPrice];
  106. cell.sourceLabel.text = item.source;
  107. cell.contentView.layer.borderWidth = 0.0f;
  108. cell.statusImageView.hidden = YES;
  109. if ([item.operatnStatus isEqualToString:@"0"]) {
  110. cell.statusImageView.hidden = NO;
  111. cell.statusImageView.image = [UIImage imageNamed:@"ProductInvalidIcon"];
  112. } else if ([item.status isEqualToString:@"2"]) {
  113. cell.statusImageView.hidden = NO;
  114. cell.statusImageView.image = [UIImage imageNamed:@"SoldOutIcon"];
  115. }
  116. }
  117. return cell;
  118. }
  119. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  120. CollectItem *item = [self.viewModel itemAtIndexPath:indexPath];
  121. if (item) {
  122. UIViewController *controller;
  123. if ([item.type isEqualToString:FK_PRODUCT_TYPE_VALUE] && item.itemID.length > 0) {
  124. controller = [[FKProDetailController alloc]initWithProductID:item.itemID];
  125. } else {
  126. if (item.userID.length > 0 && item.itemID.length > 0) {
  127. PindanDetailController *pindanController = [[PindanDetailController alloc] initWithUserID:item.userID
  128. postID:item.itemID];
  129. controller = pindanController;
  130. }
  131. }
  132. controller.hidesBottomBarWhenPushed = YES;
  133. [self.navigationController pushViewController:controller animated:YES];
  134. }
  135. }
  136. #pragma mark - Method
  137. - (void)configCollectionView {
  138. [self initRefreshControlWithTableView:(UITableView *)self.collectionView];
  139. }
  140. - (void)addAllSubViews{
  141. [self.view addSubview:self.collectionView];
  142. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  143. make.edges.insets(UIEdgeInsetsZero);
  144. }];
  145. }
  146. #pragma mark - Property
  147. - (UICollectionView *)collectionView {
  148. if (_collectionView == nil) {
  149. CGFloat imgH = (UISCREENWIDTH - 30) / 2;
  150. UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
  151. flow.headerReferenceSize = CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds), CGFLOAT_MIN);
  152. flow.minimumLineSpacing = 10;
  153. flow.minimumInteritemSpacing = 10;
  154. flow.itemSize = CGSizeMake((UISCREENWIDTH - 30)/2, imgH + 100);
  155. flow.sectionInset = UIEdgeInsetsMake(15, 10, 20, 10);
  156. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flow];
  157. _collectionView.dataSource = self;
  158. _collectionView.delegate = self;
  159. _collectionView.allowsMultipleSelection = NO;
  160. _collectionView.backgroundColor = UIColorFromRGB(0xf4f4f4);
  161. _collectionView.alwaysBounceVertical = YES;
  162. [_collectionView registerClass:[FKVipCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([FKVipCollectionCell class])];
  163. if (@available(iOS 11.0, *)) {
  164. _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  165. }
  166. }
  167. return _collectionView;
  168. }
  169. @end