口袋优选

KBMyCollectionViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //
  2. // KBMyCollectionViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/5/3.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBMyCollectionViewController.h"
  9. #import "JDragonTypeButtonView.h"
  10. #import "KBGoodHorizontalCollectionCell.h"
  11. #import "KBGoodCollectionCell.h"
  12. #import "KBFindRequestViewModel.h"
  13. #import "KBSearchNoDataView.h"
  14. #import "KBCollectionMainReusableView.h"
  15. #import "KBGoodDetailViewController.h"
  16. #import "KBCollectionNoDataReusableView.h"
  17. #import "KBCollectionUnloginReusableView.h"
  18. #import "KBLoginViewController.h"
  19. #import "KBCollectionNoDataReusableView.h"
  20. #import "KBGoodDetailRequestViewModel.h"
  21. static NSString *KHorizontalCellId = @"KBGoodHorizontalCollectionCell";
  22. static NSString *KVerticalCellId = @"KBGoodCollectionCell";
  23. static NSString *KSectionHeaderId = @"SectionHeaderId";
  24. static NSString *KNoDataViewId = @"NoDataViewId";
  25. static NSString *KUnloginId = @"UnloginId ";
  26. @interface KBMyCollectionViewController ()
  27. <
  28. JDragonTypeButtonActionDelegate,
  29. UICollectionViewDelegate,
  30. UICollectionViewDataSource,
  31. UICollectionViewDelegateFlowLayout
  32. >
  33. {
  34. NSInteger _youLikePage;
  35. NSInteger _collectionPage;
  36. NSInteger _page;
  37. }
  38. @property (nonatomic, strong) UICollectionView *collectionView;
  39. @property (nonatomic, strong) NSMutableArray *collectionDataArr;
  40. @property (nonatomic, strong) NSMutableArray *youLikeArr;
  41. @property (nonatomic, strong) NSArray *typeArr;
  42. @end
  43. @implementation KBMyCollectionViewController
  44. - (void)viewWillAppear:(BOOL)animated {
  45. [super viewWillAppear:animated];
  46. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  47. }
  48. - (void)viewDidLoad {
  49. [super viewDidLoad];
  50. [self configNavigationBar];
  51. [self configCollectionView];
  52. [self requestData];
  53. }
  54. - (void)configNavigationBar {
  55. _page = 1;
  56. [self.navigationBar setNavTitle:@"我的收藏"];
  57. self.navigationBar.backgroundColor = [UIColor changeColor];
  58. self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
  59. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  60. [leftBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
  61. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  62. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  63. }
  64. - (void)backAction {
  65. [self.navigationController popViewControllerAnimated:YES];
  66. }
  67. - (void)configCollectionView {
  68. _youLikePage = 1;
  69. _collectionPage = 1;
  70. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  71. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) collectionViewLayout:flowLayout];
  72. flowLayout.minimumInteritemSpacing = 2;
  73. [self.collectionView registerClass:[KBGoodHorizontalCollectionCell class] forCellWithReuseIdentifier:KHorizontalCellId];
  74. [self.collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:KVerticalCellId];
  75. [self.collectionView registerClass:[KBCollectionMainReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KSectionHeaderId];
  76. [self.collectionView registerClass:[KBCollectionNoDataReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KNoDataViewId];
  77. [self.collectionView registerClass:[KBCollectionUnloginReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KUnloginId];
  78. self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6];
  79. self.collectionView.showsVerticalScrollIndicator = NO;
  80. self.collectionView.delegate = self;
  81. self.collectionView.dataSource = self;
  82. self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  83. [self loadMoreData];
  84. }];
  85. [self.collectionView.mj_footer beginRefreshing];
  86. [self.view addSubview:self.collectionView];
  87. }
  88. - (void)requestData {
  89. if ([AccountTool isLogin]) {
  90. [self requestMyCollectionData];
  91. [self requestYouLikeData];
  92. }else {
  93. [self requestYouLikeData];
  94. }
  95. }
  96. - (void)requestMyCollectionData {
  97. NSDictionary *para = @{@"type":self.typeArr[self.selectedIndex],
  98. @"page":@(_collectionPage)
  99. };
  100. NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/favoriteslist",BaseURL];
  101. [KBHttp get:url params:para success:^(id json) {
  102. [self.collectionDataArr removeAllObjects];
  103. NSArray *list = [NSArray yy_modelArrayWithClass:[KBCollectionModel class] json:json[@"data"]];
  104. [self.collectionDataArr addObjectsFromArray:list];
  105. [self.collectionView reloadData];
  106. } failure:^(NSError *error) {
  107. }];
  108. }
  109. - (void)requestYouLikeData {
  110. [KBFindRequestViewModel requestGuessYouLikeParamPage:_youLikePage success:^(NSArray *array) {
  111. [self.youLikeArr addObjectsFromArray:array];
  112. [self.collectionView reloadData];
  113. [self noMoreDataWithArray:array];
  114. [self endRefreshing];
  115. } failure:^(NSError *error) {
  116. [MBProgressHUD showMessage:@"网络错误"];
  117. [SVProgressHUD dismiss];
  118. [self endRefreshing];
  119. }];
  120. }
  121. - (void)noMoreDataWithArray:(NSArray *)array {
  122. if (array.count <= 0) {
  123. MJRefreshAutoNormalFooter *foot = (MJRefreshAutoNormalFooter *)self.collectionView.mj_footer;
  124. [foot setTitle:@"到底啦" forState:MJRefreshStateIdle];
  125. }
  126. }
  127. - (void)loadMoreData {
  128. _youLikePage++;
  129. [self requestYouLikeData];
  130. }
  131. - (void)endRefreshing {
  132. [self.collectionView.mj_header endRefreshing];
  133. [self.collectionView.mj_footer endRefreshing];
  134. }
  135. /**
  136. 取消收藏
  137. */
  138. - (void)cancelCollectionGoodWith:(NSIndexPath *)indexPath {
  139. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否要取消收藏该商品?" preferredStyle:UIAlertControllerStyleAlert];
  140. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  141. }];
  142. UIAlertAction *sure = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  143. NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/delFavorites",BaseURL];
  144. KBCollectionModel *model = self.collectionDataArr[indexPath.row];
  145. NSDictionary *para = @{@"id":model.Id};
  146. [KBHttp get:url params:para success:^(id json) {
  147. [self.collectionDataArr removeObjectAtIndex:indexPath.row];
  148. // [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
  149. [self.collectionView reloadData];
  150. } failure:^(NSError *error) {
  151. }];
  152. }];
  153. [alert addAction:cancel];
  154. [alert addAction:sure];
  155. [self presentViewController:alert animated:YES completion:nil];
  156. }
  157. #pragma mark ============ UICollectionView Delegate && DataSource ==========
  158. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  159. {
  160. switch (section) {
  161. case 0:
  162. return self.collectionDataArr.count;
  163. break;
  164. case 1:
  165. return self.youLikeArr.count;
  166. default:
  167. return 0;
  168. break;
  169. }
  170. }
  171. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  172. return 2;
  173. }
  174. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  175. if (indexPath.section == 0) {
  176. return CGSizeMake(SCREEN_WIDTH, 120);
  177. }else {
  178. CGFloat width = (SCREEN_WIDTH-5)/2;
  179. CGFloat height = width + 102;
  180. return CGSizeMake(width, height);
  181. }
  182. }
  183. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  184. if (section == 0) {
  185. return 1;
  186. }else {
  187. return 5;
  188. }
  189. }
  190. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  191. {
  192. if (section == 0) {
  193. if (self.collectionDataArr.count == 0 || ![AccountTool isLogin]) {
  194. return CGSizeMake(kScreenWidth, FITSIZE(264));
  195. }else {
  196. return CGSizeZero;
  197. }
  198. }else {
  199. return CGSizeMake(SCREEN_WIDTH, 40);
  200. }
  201. }
  202. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  203. {
  204. return CGSizeMake(0, 0);
  205. }
  206. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  207. {
  208. if (indexPath.section == 0) {
  209. KBCollectionModel *model = self.collectionDataArr[indexPath.row];
  210. KBGoodHorizontalCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KHorizontalCellId forIndexPath:indexPath];
  211. cell.collectionModel = model;
  212. cell.cancelBlock = ^{
  213. [self cancelCollectionGoodWith:indexPath];
  214. };
  215. return cell;
  216. }else {
  217. KBChildGoodModel *model = self.youLikeArr[indexPath.row];
  218. KBGoodCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KVerticalCellId forIndexPath:indexPath];
  219. cell.model = model;
  220. return cell;
  221. }
  222. }
  223. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  224. if (indexPath.section == 0) {
  225. if (![AccountTool isLogin]) {
  226. KBCollectionUnloginReusableView *unlog = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KUnloginId forIndexPath:indexPath];
  227. unlog.loginClick = ^{
  228. KBLoginViewController *login = [[KBLoginViewController alloc] init];
  229. [self.parentViewController presentViewController:login animated:YES completion:nil];
  230. };
  231. return unlog;
  232. }else if (self.collectionDataArr.count <= 0){
  233. KBCollectionNoDataReusableView *noData = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KNoDataViewId forIndexPath:indexPath];
  234. noData.refreshClick = ^{
  235. [self requestMyCollectionData];
  236. };
  237. return noData;
  238. }else {
  239. return nil;
  240. }
  241. }else {
  242. KBCollectionMainReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KSectionHeaderId forIndexPath:indexPath];
  243. return header;
  244. }
  245. }
  246. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  247. KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init];
  248. if (indexPath.section == 0) {
  249. KBCollectionModel *model = self.collectionDataArr[indexPath.row];
  250. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithId:model.goods_id
  251. is_coupon:model.is_coupon
  252. coupon_price:model.coupon_price
  253. price:model.price
  254. discount_price:model.discount_price
  255. commission_rate:model.commission_rate
  256. coupon_start_time:model.coupon_start_time
  257. coupon_end_time:model.coupon_end_time];
  258. detail.requestModel = requestModel;
  259. KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:@"0" category_id:@"0" source:collectAction];
  260. detail.eventModel = evevtModel;
  261. }else {
  262. KBChildGoodModel *model = self.youLikeArr[indexPath.row];
  263. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  264. detail.requestModel = requestModel;
  265. KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:@"0" source:collectLikeAction];
  266. detail.eventModel = evevtModel;
  267. }
  268. [self.navigationController pushViewController:detail animated:YES];
  269. }
  270. #pragma mark ---- layzer ----
  271. - (NSMutableArray *)collectionDataArr {
  272. if (!_collectionDataArr) {
  273. _collectionDataArr = [NSMutableArray array];
  274. }
  275. return _collectionDataArr;
  276. }
  277. - (NSArray *)typeArr {
  278. if (!_typeArr) {
  279. _typeArr = @[@"all",@"no_expired",@"expired"];
  280. }
  281. return _typeArr;
  282. }
  283. - (NSMutableArray *)youLikeArr {
  284. if (!_youLikeArr) {
  285. _youLikeArr = [NSMutableArray array];
  286. }
  287. return _youLikeArr;
  288. }
  289. - (void)didReceiveMemoryWarning {
  290. [super didReceiveMemoryWarning];
  291. // Dispose of any resources that can be recreated.
  292. }
  293. /*
  294. #pragma mark - Navigation
  295. // In a storyboard-based application, you will often want to do a little preparation before navigation
  296. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  297. // Get the new view controller using [segue destinationViewController].
  298. // Pass the selected object to the new view controller.
  299. }
  300. */
  301. @end