《省钱达人》与《猎豆优选》UI相同版。域名tbk

DRMyCollectionViewController.m 13KB

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