省钱达人

DRMyCollectionViewController.m 14KB

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