猎豆优选

LDMyCollectionViewController.m 13KB

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