口袋优选

KBClassifyListController.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //
  2. // KBClassifyListController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/5/9.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBClassifyListController.h"
  9. #import "KBGoodCollectionCell.h"
  10. #import "KBGoodDetailViewController.h"
  11. #import "KBTypeButtonHeader.h"
  12. #import "KBGoodDetailViewController.h"
  13. #import "KBOtherFilterView.h"
  14. #import "KBClassifyTagsView.h"
  15. #import "KBClassifyTagsModel.h"
  16. static NSString *cellID = @"KBGoodCollectionCell";
  17. #define KHeaderBarHeight 40
  18. @interface KBClassifyListController ()
  19. <
  20. UICollectionViewDelegate,
  21. UICollectionViewDataSource,
  22. ClassifySelectViewDelegate,
  23. UICollectionViewDelegateFlowLayout
  24. >
  25. {
  26. NSInteger _page;
  27. NSInteger _type;
  28. NSInteger _is_has_coupon;
  29. }
  30. @property (nonatomic, strong) UICollectionView *collectionView;
  31. @property (nonatomic, strong) NSMutableArray *goodsArr;
  32. @property (nonatomic, strong) NSArray *tagsArr;
  33. @property (nonatomic, strong) KBOtherFilterView *filterView;
  34. @property (nonatomic, strong) KBClassifyTagsView *tagsView;
  35. @end
  36. @implementation KBClassifyListController
  37. - (void)viewWillAppear:(BOOL)animated {
  38. [super viewWillAppear:animated];
  39. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  40. }
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43. [self initHUD];
  44. [self configParam];
  45. [self configNavigationBar];
  46. [self configCollectionView];
  47. [self loadCategoryGoodsList:NO switchButton:nil];
  48. [self loadTagsData];
  49. }
  50. #pragma mark - HUD
  51. - (void)initHUD {
  52. [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
  53. [SVProgressHUD setForegroundColor:[UIColor YHColorWithHex:0xff2420]];
  54. [SVProgressHUD setBackgroundColor:[UIColor YHColorWithHex:0xf5f4f4]];
  55. [SVProgressHUD show];
  56. }
  57. - (void)configParam {
  58. _page = 1;
  59. _type = 1; //默认请求推荐的数据
  60. _is_has_coupon = 0;
  61. }
  62. - (void)configNavigationBar {
  63. self.view.backgroundColor = [UIColor whiteColor];
  64. [self.navigationBar setNavTitle:self.name];
  65. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  66. [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  67. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  68. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  69. [self.navigationBar setShowNavigationBarBottomLine:YES];
  70. }
  71. - (void)configCollectionView {
  72. self.filterView = [[KBOtherFilterView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, KHeaderBarHeight) withArr:nil];
  73. self.filterView.delegate = self;
  74. [self.view addSubview:self.filterView];
  75. self.tagsView = [[KBClassifyTagsView alloc] initWithFrame:CGRectMake(0, self.filterView.bottom, SCREEN_WIDTH, 40)];
  76. self.tagsView.backgroundColor = [UIColor yhGrayColor];
  77. __weak typeof(self) weakSelf = self;
  78. self.tagsView.selectBlock = ^(NSInteger index) {
  79. KBClassifyTagsModel *model = weakSelf.tagsArr[index];
  80. weakSelf.cate_id = model.Id;
  81. _page = 1;
  82. [weakSelf loadCategoryGoodsList:YES switchButton:nil];
  83. };
  84. [self.view addSubview:self.tagsView];
  85. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  86. flowLayout.minimumLineSpacing = 5;
  87. flowLayout.minimumInteritemSpacing = 0;
  88. flowLayout.headerReferenceSize = CGSizeMake(0, 0);
  89. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight+KHeaderBarHeight+self.tagsView.height, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-self.tagsView.height) collectionViewLayout:flowLayout];
  90. [self.collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:cellID];
  91. self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6];
  92. self.collectionView.showsVerticalScrollIndicator = NO;
  93. self.collectionView.delegate = self;
  94. self.collectionView.dataSource = self;
  95. self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  96. [self loadMoreData];
  97. }];
  98. [self.view addSubview: self.collectionView];
  99. }
  100. - (void)backAction {
  101. [self.navigationController popViewControllerAnimated:YES];
  102. }
  103. #pragma mark ----
  104. #pragma mark ClassifySelectViewDelegate
  105. - (void)ClassifyselectTopButton:(KBOtherFilterView *)selectView withIndex:(NSInteger)index withButtonType:(JCButtonClickType)type button:(UIButton *)button {
  106. [SVProgressHUD show];
  107. //价格
  108. if (index == 2&& type) {
  109. switch (type) {
  110. case JCButtonClickTypeNormal:
  111. //正常价格
  112. {
  113. NSLog(@"上边按钮的正常价格");
  114. }
  115. break;
  116. case JCButtonClickTypeUp:
  117. //价格升序排列
  118. {
  119. NSLog(@"上边按钮的价格升序排列");
  120. _type = 3;
  121. }
  122. break;
  123. case JCButtonClickTypeDown:
  124. //价格降序排列
  125. {
  126. NSLog(@"上边按钮的价格降序排列");
  127. _type = 4;
  128. }
  129. break;
  130. default:
  131. break;
  132. }
  133. }else if (index == 0){//综合
  134. NSLog(@"上边按钮的综合");
  135. _type = 1;
  136. }else if (index == 1){//销量
  137. NSLog(@"上边按钮的销量");
  138. _type = 2;
  139. }else{//筛选
  140. _is_has_coupon = button.selected ? 0 : 1;
  141. [self loadCategoryGoodsList:YES switchButton:button];
  142. return;
  143. NSLog(@"上边有券按钮");
  144. }
  145. [self loadCategoryGoodsList:YES switchButton:nil];
  146. }
  147. /**
  148. 上拉加载
  149. */
  150. - (void)loadMoreData {
  151. _page++;
  152. [self loadCategoryGoodsList:NO switchButton:nil];
  153. }
  154. /**
  155. 加载下部商品列表
  156. */
  157. - (void)loadCategoryGoodsList:(BOOL)refresh switchButton:(UIButton *)switchButton{
  158. if (![self.collectionView.mj_footer isRefreshing]) {
  159. [SVProgressHUD show];
  160. }
  161. NSDictionary *para = @{@"page":@(_page),
  162. @"category_id":self.firstCategoryId,
  163. @"sort":@(_type),
  164. @"sub_category_id":self.cate_id,
  165. @"stype":@"0",
  166. @"is_has_coupon":@(_is_has_coupon)
  167. };
  168. [KBHttp post:StocklistByCategoryId params:para success:^(id json) {
  169. if (refresh) [self.goodsArr removeAllObjects];
  170. NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]];
  171. [self.goodsArr addObjectsFromArray:list];
  172. [self.collectionView reloadData];
  173. if (list.count > 0) {
  174. [self.collectionView.mj_footer endRefreshing];
  175. }else {
  176. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  177. }
  178. [SVProgressHUD dismiss];
  179. switchButton.selected = !switchButton.selected;
  180. } failure:^(NSError *error) {
  181. [self.collectionView.mj_footer endRefreshing];
  182. [SVProgressHUD dismiss];
  183. [MBProgressHUD showMessage:@"加载失败"];
  184. }];
  185. }
  186. - (void)noMoreDataWithArray:(NSArray *)array {
  187. if (array.count > 0) {
  188. self.collectionView.footRefreshState = MJFooterRefreshStateNoMore;
  189. }else {
  190. self.collectionView.footRefreshState = MJFooterRefreshStateLoadMore;
  191. }
  192. }
  193. - (void)loadTagsData {
  194. NSString *url = [NSString stringWithFormat:@"%@/api/v2/category/childlist",BaseURL];
  195. NSString *cateId = self.cate_id==nil?@"":self.cate_id;
  196. NSDictionary *para = @{@"category_id":cateId};
  197. [KBHttp get:url params:para success:^(id json) {
  198. self.tagsArr = [NSArray yy_modelArrayWithClass:[KBClassifyTagsModel class] json:json[@"data"]];
  199. NSMutableArray *tags = [NSMutableArray array];
  200. for (KBClassifyTagsModel *model in self.tagsArr) {
  201. [tags addObject:model.name];
  202. }
  203. [self.tagsView setTagsData:tags];
  204. if (self.tagsArr.count == 0) {
  205. self.collectionView.y = NavBarHeight+KHeaderBarHeight;
  206. self.collectionView.height = SCREEN_HEIGHT-NavBarHeight-KHeaderBarHeight;
  207. self.tagsView.height = 0;
  208. }
  209. } failure:^(NSError *error) {
  210. }];
  211. }
  212. #pragma mark - scrollView
  213. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  214. {
  215. //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势
  216. UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
  217. //获取到拖拽的速度 >0 向下拖动 <0 向上拖动
  218. CGFloat velocity = [pan velocityInView:scrollView].y;
  219. if (velocity <- 10) {
  220. //向上拖动,隐藏导航栏
  221. if (self.navigationBar.y == -NavBarHeight) {
  222. return;
  223. }
  224. [UIView animateWithDuration:0.15 animations:^{
  225. self.navigationBar.y = -NavBarHeight;
  226. self.filterView.y = KStatusBarHeight;
  227. self.collectionView.y = KStatusBarHeight + KHeaderBarHeight+self.tagsView.height;
  228. self.collectionView.height = SCREEN_HEIGHT-KHeaderBarHeight-KStatusBarHeight-self.tagsView.height;
  229. self.tagsView.y = KStatusBarHeight + KHeaderBarHeight;
  230. }];
  231. }else if (velocity > 10) {
  232. //向下拖动,显示导航栏
  233. if (self.navigationBar.y == 0) {
  234. return;
  235. }
  236. [UIView animateWithDuration:0.15 animations:^{
  237. self.navigationBar.y = 0;
  238. self.filterView.y = NavBarHeight;
  239. self.tagsView.y = NavBarHeight + KHeaderBarHeight;
  240. self.collectionView.y = NavBarHeight + KHeaderBarHeight+self.tagsView.height;
  241. }];
  242. }else if(velocity == 0){
  243. //停止拖拽
  244. }
  245. }
  246. #pragma mark ============ UICollectionView Delegate && DataSource ==========
  247. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  248. {
  249. return self.goodsArr.count;
  250. }
  251. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  252. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  253. CGFloat width = (SCREEN_WIDTH-5)/2;
  254. CGFloat height = width + 102;
  255. return CGSizeMake(width, height);
  256. }
  257. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  258. {
  259. return CGSizeMake(0, 0);
  260. }
  261. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  262. {
  263. return CGSizeMake(0, 0);
  264. }
  265. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  266. {
  267. KBGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  268. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  269. cell.model = model;
  270. return cell;
  271. }
  272. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  273. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  274. //详情
  275. KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init];
  276. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  277. detailVC.requestModel = requestModel;
  278. if (self.isOtherPage) {
  279. KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:self.cate_id source:HomeClassifyAction];
  280. detailVC.eventModel = evevtModel;
  281. }else {
  282. KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:self.cate_id source:ListClassifyAction];
  283. detailVC.eventModel = evevtModel;
  284. }
  285. [self.navigationController pushViewController:detailVC animated:YES];
  286. }
  287. #pragma mark -------------
  288. - (NSMutableArray *)goodsArr {
  289. if (!_goodsArr) {
  290. _goodsArr = [NSMutableArray array];
  291. }
  292. return _goodsArr;
  293. }
  294. - (void)didReceiveMemoryWarning {
  295. [super didReceiveMemoryWarning];
  296. // Dispose of any resources that can be recreated.
  297. }
  298. /*
  299. #pragma mark - Navigation
  300. // In a storyboard-based application, you will often want to do a little preparation before navigation
  301. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  302. // Get the new view controller using [segue destinationViewController].
  303. // Pass the selected object to the new view controller.
  304. }
  305. */
  306. @end