123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- //
- // LDMyCollectionViewController.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/5/3.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "LDMyCollectionViewController.h"
- #import "JDragonTypeButtonView.h"
- #import "LDGoodHorizontalCollectionCell.h"
- #import "LDGoodCollectionCell.h"
- #import "LDFindRequestViewModel.h"
- #import "LDSearchNoDataView.h"
- #import "LDCollectionMainReusableView.h"
- #import "LDCollectionNoDataReusableView.h"
- #import "LDCollectionUnloginReusableView.h"
- #import "LDLoginViewController.h"
- #import "LDCollectionNoDataReusableView.h"
- #import "LDGoodDetailRequestViewModel.h"
- static NSString *KHorizontalCellId = @"LDGoodHorizontalCollectionCell";
- static NSString *KVerticalCellId = @"LDGoodCollectionCell";
- static NSString *KSectionHeaderId = @"SectionHeaderId";
- static NSString *KNoDataViewId = @"NoDataViewId";
- static NSString *KUnloginId = @"UnloginId ";
- @interface LDMyCollectionViewController ()
- <
- JDragonTypeButtonActionDelegate,
- UICollectionViewDelegate,
- UICollectionViewDataSource,
- UICollectionViewDelegateFlowLayout
- >
- {
- NSInteger _youLikePage;
- NSInteger _collectionPage;
- }
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) NSMutableArray *collectionDataArr;
- @property (nonatomic, strong) NSMutableArray *youLikeArr;
- @property (nonatomic, strong) NSArray *typeArr;
- @end
- @implementation LDMyCollectionViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self configCollectionView];
- [self requestData];
- }
- - (void)configCollectionView {
- _youLikePage = 1;
- _collectionPage = 1;
- UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
- self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-40) collectionViewLayout:flowLayout];
- flowLayout.minimumInteritemSpacing = 2;
- [self.collectionView registerClass:[LDGoodHorizontalCollectionCell class] forCellWithReuseIdentifier:KHorizontalCellId];
- [self.collectionView registerClass:[LDGoodCollectionCell class] forCellWithReuseIdentifier:KVerticalCellId];
- [self.collectionView registerClass:[LDCollectionMainReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KSectionHeaderId];
- [self.collectionView registerClass:[LDCollectionNoDataReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KNoDataViewId];
- [self.collectionView registerClass:[LDCollectionUnloginReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KUnloginId];
-
- self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6];
- self.collectionView.showsVerticalScrollIndicator = NO;
- self.collectionView.delegate = self;
- self.collectionView.dataSource = self;
- self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
- [self loadMoreData];
- }];
- [self.collectionView.mj_footer beginRefreshing];
- [self.view addSubview:self.collectionView];
- }
- - (void)requestData {
- if ([AccountTool isLogin]) {
- [self requestMyCollectionData];
- [self requestYouLikeData];
- }else {
- [self requestYouLikeData];
- }
- }
- - (void)requestMyCollectionData {
-
- NSDictionary *para = @{@"type":self.typeArr[self.selectedIndex],
- @"page":@(_collectionPage)
- };
- NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/favoriteslist",BaseURL];
-
- [LDHttp get:url params:para success:^(id json) {
- [self.collectionDataArr removeAllObjects];
- NSArray *list = [NSArray yy_modelArrayWithClass:[LDCollectionModel class] json:json[@"data"]];
-
- [self.collectionDataArr addObjectsFromArray:list];
- [self.collectionView reloadData];
- } failure:^(NSError *error) {
-
- }];
-
- }
- - (void)requestYouLikeData {
- [LDFindRequestViewModel requestGuessYouLikeParamPage:_youLikePage success:^(NSArray *array) {
- [self.youLikeArr addObjectsFromArray:array];
- [self.collectionView reloadData];
- [self noMoreDataWithArray:array];
- [self endRefreshing];
-
- } failure:^(NSError *error) {
- [MBProgressHUD showMessage:@"网络错误"];
- [SVProgressHUD dismiss];
- [self endRefreshing];
- }];
- }
- - (void)noMoreDataWithArray:(NSArray *)array {
- if (array.count <= 0) {
- MJRefreshAutoNormalFooter *foot = (MJRefreshAutoNormalFooter *)self.collectionView.mj_footer;
- [foot setTitle:@"到底啦" forState:MJRefreshStateIdle];
- }
- }
- - (void)loadMoreData {
- _youLikePage++;
- [self requestYouLikeData];
- }
- - (void)endRefreshing {
- [self.collectionView.mj_header endRefreshing];
- [self.collectionView.mj_footer endRefreshing];
- }
- /**
- 取消收藏
- */
- - (void)cancelCollectionGoodWith:(NSIndexPath *)indexPath {
-
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否要取消收藏该商品?" preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
-
- }];
-
- UIAlertAction *sure = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
-
- NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/delFavorites",BaseURL];
- LDCollectionModel *model = self.collectionDataArr[indexPath.row];
- NSDictionary *para = @{@"id":model.Id};
- [LDHttp get:url params:para success:^(id json) {
- [self.collectionDataArr removeObjectAtIndex:indexPath.row];
- // [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
- [self.collectionView reloadData];
- } failure:^(NSError *error) {
-
- }];
-
- }];
- [alert addAction:cancel];
- [alert addAction:sure];
- alert.modalPresentationStyle = UIModalPresentationFullScreen;
- [self presentViewController:alert animated:YES completion:nil];
-
-
- }
- #pragma mark ============ UICollectionView Delegate && DataSource ==========
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- {
- switch (section) {
- case 0:
- return self.collectionDataArr.count;
- break;
- case 1:
- return self.youLikeArr.count;
- default:
- return 0;
- break;
- }
-
-
- }
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return 2;
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
-
-
-
- if (indexPath.section == 0) {
- return CGSizeMake(SCREEN_WIDTH, 120);
- }else {
- LDChildGoodModel *model = self.youLikeArr[indexPath.row];
- CGFloat width = (SCREEN_WIDTH-5)/2;
- CGFloat height = width + 135;
- if (![AccountTool isLogin] && ![model.red_active_status boolValue]) {
- height = width +112;
- }
- return CGSizeMake(width, height);
- }
-
- }
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
- if (section == 0) {
- return 1;
- }else {
- return 5;
- }
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
- {
- if (section == 0) {
- if (self.collectionDataArr.count == 0 || ![AccountTool isLogin]) {
- return CGSizeMake(kScreenWidth, FITSIZE(264));
- }else {
- return CGSizeZero;
- }
- }else {
- return CGSizeMake(SCREEN_WIDTH, 40);
- }
-
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
- {
- return CGSizeMake(0, 0);
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- {
-
- if (indexPath.section == 0) {
- LDCollectionModel *model = self.collectionDataArr[indexPath.row];
- LDGoodHorizontalCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KHorizontalCellId forIndexPath:indexPath];
- cell.collectionModel = model;
- cell.cancelBlock = ^{
- [self cancelCollectionGoodWith:indexPath];
- };
- return cell;
- }else {
- LDChildGoodModel *model = self.youLikeArr[indexPath.row];
- LDGoodCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KVerticalCellId forIndexPath:indexPath];
-
- cell.model = model;
- return cell;
- }
-
- }
- - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
- if (indexPath.section == 0) {
-
- if (![AccountTool isLogin]) {
- LDCollectionUnloginReusableView *unlog = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KUnloginId forIndexPath:indexPath];
- unlog.loginClick = ^{
- LDNewLoginVC *login = [[LDNewLoginVC alloc] init];
- login.modalPresentationStyle = UIModalPresentationFullScreen;
- [self.parentViewController presentViewController:login animated:YES completion:nil];
- };
- return unlog;
- }else if (self.collectionDataArr.count <= 0){
- LDCollectionNoDataReusableView *noData = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KNoDataViewId forIndexPath:indexPath];
- noData.refreshClick = ^{
- [self requestMyCollectionData];
- };
- return noData;
-
- }else {
- return nil;
- }
-
- }else {
- LDCollectionMainReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KSectionHeaderId forIndexPath:indexPath];
-
- return header;
- }
-
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
-
- LDGoodDetailViewController *detail = [[LDGoodDetailViewController alloc] init];
-
- if (indexPath.section == 0) {
- LDCollectionModel *model = self.collectionDataArr[indexPath.row];
- DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithId:model.goods_id
- is_coupon:model.is_coupon
- coupon_price:model.coupon_price
- price:model.price
- discount_price:model.discount_price
- commission_rate:model.commission_rate
- coupon_start_time:model.coupon_start_time
- coupon_end_time:model.coupon_end_time
- coupon_id:model.coupon_id];
- detail.requestModel = requestModel;
-
- LDEventModel *evevtModel = [[LDEventModel alloc] initWithOrigin:@"0" category_id:@"0" source:collectAction];
- detail.eventModel = evevtModel;
- }else {
- LDChildGoodModel *model = self.youLikeArr[indexPath.row];
- DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
- detail.requestModel = requestModel;
-
- LDEventModel *evevtModel = [[LDEventModel alloc] initWithOrigin:model.origin category_id:@"0" source:collectLikeAction];
- detail.eventModel = evevtModel;
- }
-
-
-
- [self.navigationController pushViewController:detail animated:YES];
-
- }
- #pragma mark ---- layzer ----
- - (NSMutableArray *)collectionDataArr {
- if (!_collectionDataArr) {
- _collectionDataArr = [NSMutableArray array];
- }
- return _collectionDataArr;
- }
- - (NSArray *)typeArr {
- if (!_typeArr) {
- _typeArr = @[@"all",@"no_expired",@"expired"];
- }
- return _typeArr;
- }
- - (NSMutableArray *)youLikeArr {
- if (!_youLikeArr) {
- _youLikeArr = [NSMutableArray array];
- }
- return _youLikeArr;
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|