123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- //
- // KBMyCollectionViewController.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/5/3.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "KBMyCollectionViewController.h"
- #import "JDragonTypeButtonView.h"
- #import "KBGoodHorizontalCollectionCell.h"
- #import "KBGoodCollectionCell.h"
- #import "KBFindRequestViewModel.h"
- #import "KBSearchNoDataView.h"
- #import "KBCollectionMainReusableView.h"
- #import "KBGoodDetailViewController.h"
- #import "KBCollectionNoDataReusableView.h"
- #import "KBCollectionUnloginReusableView.h"
- #import "KBLoginViewController.h"
- #import "KBCollectionNoDataReusableView.h"
- #import "KBGoodDetailRequestViewModel.h"
- static NSString *KHorizontalCellId = @"KBGoodHorizontalCollectionCell";
- static NSString *KVerticalCellId = @"KBGoodCollectionCell";
- static NSString *KSectionHeaderId = @"SectionHeaderId";
- static NSString *KNoDataViewId = @"NoDataViewId";
- static NSString *KUnloginId = @"UnloginId ";
- @interface KBMyCollectionViewController ()
- <
- JDragonTypeButtonActionDelegate,
- UICollectionViewDelegate,
- UICollectionViewDataSource,
- UICollectionViewDelegateFlowLayout
- >
- {
- NSInteger _youLikePage;
- NSInteger _collectionPage;
- NSInteger _page;
- }
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) NSMutableArray *collectionDataArr;
- @property (nonatomic, strong) NSMutableArray *youLikeArr;
- @property (nonatomic, strong) NSArray *typeArr;
- @end
- @implementation KBMyCollectionViewController
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self configNavigationBar];
- [self configCollectionView];
- [self requestData];
- }
- - (void)configNavigationBar {
- _page = 1;
- [self.navigationBar setNavTitle:@"我的收藏"];
- self.navigationBar.backgroundColor = [UIColor changeColor];
- self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
- UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
- [leftBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
- [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
- [self.navigationBar setCustomLeftButtons:@[leftBtn]];
- }
- - (void)backAction {
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (void)configCollectionView {
- _youLikePage = 1;
- _collectionPage = 1;
- UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
- self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) collectionViewLayout:flowLayout];
- flowLayout.minimumInteritemSpacing = 2;
- [self.collectionView registerClass:[KBGoodHorizontalCollectionCell class] forCellWithReuseIdentifier:KHorizontalCellId];
- [self.collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:KVerticalCellId];
- [self.collectionView registerClass:[KBCollectionMainReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KSectionHeaderId];
- [self.collectionView registerClass:[KBCollectionNoDataReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KNoDataViewId];
- [self.collectionView registerClass:[KBCollectionUnloginReusableView 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];
-
- [KBHttp get:url params:para success:^(id json) {
- [self.collectionDataArr removeAllObjects];
- NSArray *list = [NSArray yy_modelArrayWithClass:[KBCollectionModel class] json:json[@"data"]];
-
- [self.collectionDataArr addObjectsFromArray:list];
- [self.collectionView reloadData];
- } failure:^(NSError *error) {
-
- }];
-
- }
- - (void)requestYouLikeData {
- [KBFindRequestViewModel 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];
- KBCollectionModel *model = self.collectionDataArr[indexPath.row];
- NSDictionary *para = @{@"id":model.Id};
- [KBHttp 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];
-
- [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 {
- CGFloat width = (SCREEN_WIDTH-5)/2;
- CGFloat height = width + 102;
-
- 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) {
- KBCollectionModel *model = self.collectionDataArr[indexPath.row];
- KBGoodHorizontalCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KHorizontalCellId forIndexPath:indexPath];
- cell.collectionModel = model;
- cell.cancelBlock = ^{
- [self cancelCollectionGoodWith:indexPath];
- };
- return cell;
- }else {
- KBChildGoodModel *model = self.youLikeArr[indexPath.row];
- KBGoodCollectionCell *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]) {
- KBCollectionUnloginReusableView *unlog = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KUnloginId forIndexPath:indexPath];
- unlog.loginClick = ^{
- KBLoginViewController *login = [[KBLoginViewController alloc] init];
- [self.parentViewController presentViewController:login animated:YES completion:nil];
- };
- return unlog;
- }else if (self.collectionDataArr.count <= 0){
- KBCollectionNoDataReusableView *noData = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KNoDataViewId forIndexPath:indexPath];
- noData.refreshClick = ^{
- [self requestMyCollectionData];
- };
- return noData;
-
- }else {
- return nil;
- }
-
- }else {
- KBCollectionMainReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KSectionHeaderId forIndexPath:indexPath];
-
- return header;
- }
-
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
-
- KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init];
-
- if (indexPath.section == 0) {
- KBCollectionModel *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];
- detail.requestModel = requestModel;
-
- KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:@"0" category_id:@"0" source:collectAction];
- detail.eventModel = evevtModel;
- }else {
- KBChildGoodModel *model = self.youLikeArr[indexPath.row];
- DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
- detail.requestModel = requestModel;
-
- KBEventModel *evevtModel = [[KBEventModel 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
|