123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- //
- // PindanCollectionController.m
- // FirstLink
- //
- // Created by Lemon on 15/4/28.
- // Copyright (c) 2015年 FirstLink. All rights reserved.
- //
- #import "CollectViewController.h"
- #import "PindanDetailController.h"
- #import "FKProDetailController.h"
- #import "FKVipCollectionCell.h"
- #import "FKCollectRequest.h"
- #import "FKCollectReform.h"
- #import "CollectViewModel.h"
- #import "FLImageHelper.h"
- static NSString *const PindanCollectionCellIdentifier = @"PindanCollectionCellIdentifier";
- @interface CollectViewController ()
- <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate, FLNetworkDelegate>
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) CollectViewModel *viewModel;
- @end
- @implementation CollectViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.navigationItem.title = @"我的收藏";
-
- [self configCollectionView];
- [self addAllSubViews];
- }
- #pragma mark - Life Cyclelife
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- [[self navigationController] setNavigationBarHidden:NO animated:YES];
-
- [self.collectionView reloadData];
-
- [self.hudView show:YES];
- [self refreshPindanPostRequest:REQUEST_LIST
- startRow:0
- pageSize:MAX(PAGE_RECORD_COUNT, (int)[self.viewModel numberOfRows])];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- }
- #pragma mark - Request Data
- - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
- // [self.hudView show:YES];
- if (direction == RefreshDirectionTop) {
- self.refreshControl.bottomEnabled = YES;
- [self refreshPindanPostRequest:REQUEST_LIST startRow:0 pageSize:PAGE_RECORD_COUNT];
- } else if (direction == RefreshDirectionBottom) {
- [self refreshPindanPostRequest:REQUEST_NEXT_PAGE
- startRow:(int)[self.viewModel numberOfRows]
- pageSize:PAGE_RECORD_COUNT];
- }
- }
- - (void)refreshPindanPostRequest:(int)identify
- startRow:(int)startRow
- pageSize:(int)pageSize {
- [FKCollectRequest requestItems:identify
- startRow:[NSString stringWithFormat:@"%d", startRow]
- pageSize:[NSString stringWithFormat:@"%d", pageSize]
- deleagate:self];
- }
- - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header {
- [self.hudView hide:YES];
- [self finishLoadingData];
-
- if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
- if (identify == REQUEST_LIST) {
- self.viewModel = [FKCollectReform parseCollectViewModel:response];
- }
- if (identify == REQUEST_NEXT_PAGE) {
- NSArray *items = [FKCollectReform parseCollectViewModel:response].itemsArray;
- [self.viewModel.itemsArray addObjectsFromArray:items];
-
- if (items.count == 0) {
- self.refreshControl.bottomEnabled = FALSE;
- }
- }
- if ([self.viewModel numberOfRows] == 0) {
- [self showStatusTipInView:self.view image:[UIImage imageNamed:@"StatusNoCollectIcon"] title:@"没有发现商品哦"];
- } else {
- [self.statusView removeFromSuperview];
- }
- [self.collectionView reloadData];
- }
- }
- - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header {
- [self.hudView hide:YES];
- [self finishLoadingData];
- [FLProgressHUDHelper showText:header.msg inView:self.view];
- }
- #pragma mark - UITableView Delegate & DataSource
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return 1;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
- return [self.viewModel numberOfRows];
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
- FKVipCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FKVipCollectionCell class])
- forIndexPath:indexPath];
-
- CollectItem *item = [self.viewModel itemAtIndexPath:indexPath];
- if (item) {
- [cell.imageView setImageWithURL:item.photoURL cdnWidth:(UISCREENWIDTH - 30)/2];
- cell.titleLabel.text = item.title;
- cell.priceLabel.text = [FLStringHelper convertFenToYuan:item.price];
- // cell.cutPrice = [FLStringHelper convertFenStringToYuanValue:item.referPrice];
- cell.sourceLabel.text = item.source;
- cell.contentView.layer.borderWidth = 0.0f;
-
- cell.statusImageView.hidden = YES;
- if ([item.operatnStatus isEqualToString:@"0"]) {
- cell.statusImageView.hidden = NO;
- cell.statusImageView.image = [UIImage imageNamed:@"ProductInvalidIcon"];
- } else if ([item.status isEqualToString:@"2"]) {
- cell.statusImageView.hidden = NO;
- cell.statusImageView.image = [UIImage imageNamed:@"SoldOutIcon"];
- }
- }
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- CollectItem *item = [self.viewModel itemAtIndexPath:indexPath];
- if (item) {
- UIViewController *controller;
- if ([item.type isEqualToString:FK_PRODUCT_TYPE_VALUE] && item.itemID.length > 0) {
- controller = [[FKProDetailController alloc]initWithProductID:item.itemID];
- } else {
- if (item.userID.length > 0 && item.itemID.length > 0) {
- PindanDetailController *pindanController = [[PindanDetailController alloc] initWithUserID:item.userID
- postID:item.itemID];
- controller = pindanController;
- }
- }
- controller.hidesBottomBarWhenPushed = YES;
- [self.navigationController pushViewController:controller animated:YES];
- }
- }
- #pragma mark - Method
- - (void)configCollectionView {
- [self initRefreshControlWithTableView:(UITableView *)self.collectionView];
- }
- - (void)addAllSubViews{
- [self.view addSubview:self.collectionView];
- [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.insets(UIEdgeInsetsZero);
- }];
- }
- #pragma mark - Property
- - (UICollectionView *)collectionView {
- if (_collectionView == nil) {
- CGFloat imgH = (UISCREENWIDTH - 30) / 2;
-
- UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
- flow.headerReferenceSize = CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds), CGFLOAT_MIN);
- flow.minimumLineSpacing = 10;
- flow.minimumInteritemSpacing = 10;
- flow.itemSize = CGSizeMake((UISCREENWIDTH - 30)/2, imgH + 100);
- flow.sectionInset = UIEdgeInsetsMake(15, 10, 20, 10);
-
- _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flow];
- _collectionView.dataSource = self;
- _collectionView.delegate = self;
- _collectionView.allowsMultipleSelection = NO;
- _collectionView.backgroundColor = UIColorFromRGB(0xf4f4f4);
- _collectionView.alwaysBounceVertical = YES;
-
- [_collectionView registerClass:[FKVipCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([FKVipCollectionCell class])];
-
- if (@available(iOS 11.0, *)) {
- _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- }
- }
- return _collectionView;
- }
- @end
|