YBGoodsLikeVC.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //
  2. // YBGoodsLikeVC.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2020/10/19.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "YBGoodsLikeVC.h"
  9. #import "YBGoodsLikeCell.h"
  10. #import "YBGoodsInfoVC.h"
  11. #import "OutsideGoodsDetailVC.h"
  12. #import "CommodityDetailVC.h"
  13. @interface YBGoodsLikeVC ()<UITableViewDelegate,UITableViewDataSource>
  14. {
  15. int _paging;
  16. BOOL _currentEditeState;
  17. NSMutableArray *_flagDel;
  18. NSMutableArray *_delCollectionGoods;
  19. BOOL _singleDel;
  20. }
  21. @property(nonatomic,strong)UITableView *tableView;
  22. @property(nonatomic,strong)NSMutableArray *dataArray;
  23. @end
  24. @implementation YBGoodsLikeVC
  25. - (void)viewWillAppear:(BOOL)animated{
  26. [super viewWillAppear:animated];
  27. [self pullData];
  28. }
  29. - (UIStatusBarStyle)preferredStatusBarStyle {
  30. if (@available(iOS 13.0,*)) {
  31. return UIStatusBarStyleDarkContent;
  32. }
  33. return UIStatusBarStyleDefault;
  34. }
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. self.titleL.text = YZMsg(@"我的收藏");
  38. self.view.backgroundColor = UIColor.whiteColor;
  39. self.dataArray = [NSMutableArray array];
  40. _paging = 1;
  41. _flagDel = [NSMutableArray array];
  42. _delCollectionGoods = [NSMutableArray array];
  43. _currentEditeState = NO;
  44. self.rightBtn.hidden = NO;
  45. [self.rightBtn setTitle:YZMsg(@"管理") forState:0];
  46. self.rightBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 6);
  47. [self.rightBtn setTitleColor:UIColor.blackColor forState:0];
  48. self.subNavi.backgroundColor = UIColor.whiteColor;
  49. self.titleL.textColor = UIColor.blackColor;
  50. [self.leftBtn setImage:[UIImage imageNamed:@"pub_back_black"] forState:0];
  51. self.naviLine.hidden = NO;
  52. self.naviLine.backgroundColor = RGB(245, 245, 245);
  53. [self.view addSubview:self.tableView];
  54. }
  55. #pragma mark -
  56. -(void)pullData {
  57. YBWeakSelf;
  58. [YBNetworking postWithUrl:@"Shop.getGoodsCollect" Dic:@{@"p":@(_paging)} Suc:^(int code, id info, NSString *msg) {
  59. [_tableView.mj_header endRefreshing];
  60. [_tableView.mj_footer endRefreshing];
  61. if (code == 0) {
  62. NSArray *infoA = [NSArray arrayWithArray:info];
  63. if (_paging == 1) {
  64. [_dataArray removeAllObjects];
  65. }
  66. if (infoA.count <= 0) {
  67. [_tableView.mj_footer endRefreshingWithNoMoreData];
  68. }else {
  69. [_dataArray addObjectsFromArray:infoA];
  70. }
  71. if (_dataArray.count <=0) {
  72. [PublicView showTextNoData:_tableView text1:@"" text2:YZMsg(@"暂无数据") centerY:0.8];
  73. }else {
  74. [PublicView hiddenTextNoData:_tableView];
  75. }
  76. [weakSelf resetDelFlag];
  77. _currentEditeState = NO;
  78. [_tableView reloadData];
  79. }else {
  80. [MBProgressHUD showPop:msg];
  81. }
  82. } Fail:^(id fail) {
  83. [_tableView.mj_header endRefreshing];
  84. [_tableView.mj_footer endRefreshing];
  85. }];
  86. }
  87. #pragma mark - UITableViewDelegate、UITableViewDataSource
  88. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  89. return 0;
  90. }
  91. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  92. return nil;
  93. }
  94. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  95. return 0;
  96. }
  97. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  98. return nil;
  99. }
  100. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  101. return 100;
  102. }
  103. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  104. return _dataArray.count;
  105. }
  106. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  107. YBGoodsLikeCell *cell = [YBGoodsLikeCell cellWithTab:tableView index:indexPath];
  108. cell.dataDic = _dataArray[indexPath.row];
  109. cell.selectionStyle = 0;
  110. if (_currentEditeState == NO) {
  111. cell.flagWidth.constant = 0;
  112. cell.thumbLeft.constant = 0;
  113. }else {
  114. cell.flagWidth.constant = 18;
  115. cell.thumbLeft.constant = 8;
  116. }
  117. NSString *flagStr = _flagDel[indexPath.row];
  118. if ([flagStr isEqual:@"1"]) {
  119. cell.flagIV.image = [UIImage imageNamed:@"下一步"];
  120. }else {
  121. cell.flagIV.image = [UIImage imageNamed:@"编辑-圈圈"];
  122. }
  123. return cell;
  124. }
  125. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  126. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  127. NSDictionary *subDic = _dataArray[indexPath.row];
  128. if (_currentEditeState) {
  129. //编辑
  130. NSString *flagStr = _flagDel[indexPath.row];
  131. if ([flagStr isEqual:@"1"]) {
  132. //取消选中
  133. [_flagDel replaceObjectAtIndex:indexPath.row withObject:@"0"];
  134. [_delCollectionGoods removeObject:subDic];
  135. }else {
  136. //选中
  137. [_flagDel replaceObjectAtIndex:indexPath.row withObject:@"1"];
  138. [_delCollectionGoods addObject:subDic];
  139. }
  140. [self changeRigthTitle];
  141. [tableView reloadData];
  142. }else {
  143. //进详情
  144. [self goGoodsInfo:indexPath];
  145. }
  146. }
  147. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  148. YBWeakSelf;
  149. UITableViewRowAction *delAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:YZMsg(@"删除") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  150. //NSLog(@"====del");
  151. _singleDel = YES;
  152. NSDictionary *subDic = _dataArray[indexPath.row];
  153. [_delCollectionGoods addObject:subDic];
  154. [weakSelf showDelAlert];
  155. }];
  156. delAction.backgroundColor = Pink_Cor;
  157. return @[delAction];
  158. }
  159. -(void)goGoodsInfo:(NSIndexPath *)indexPath {
  160. NSDictionary *subDic = _dataArray[indexPath.row];
  161. [PublicObj checkGoodsExistenceWithID:minstr([subDic valueForKey:@"id"]) Existence:^(int code, NSString *msg) {
  162. if (code ==0) {
  163. if ([minstr([subDic valueForKey:@"type"]) isEqual:@"1"]) {
  164. OutsideGoodsDetailVC *detail = [[OutsideGoodsDetailVC alloc]init];
  165. detail.goodsID = minstr([subDic valueForKey:@"id"]);
  166. // detail.liveUid= _liveUid;
  167. [[YBBaseAppDelegate sharedAppDelegate] pushViewController:detail animated:YES];
  168. }else{
  169. CommodityDetailVC *detail = [[CommodityDetailVC alloc]init];
  170. detail.goodsID = minstr([subDic valueForKey:@"id"]);
  171. // detail.backTags = @"shop";
  172. // detail.liveUid= _liveUid;
  173. [[YBBaseAppDelegate sharedAppDelegate] pushViewController:detail animated:YES];
  174. }
  175. }else{
  176. [MBProgressHUD showError:msg];
  177. }
  178. }];
  179. // YBGoodsInfoVC *infoVC = [[YBGoodsInfoVC alloc]init];
  180. // infoVC.infoType = InfoEnterType_Collect;
  181. // infoVC.touserID = minstr([subDic valueForKey:@"uid"]);
  182. // infoVC.goodsID = minstr([subDic valueForKey:@"id"]);
  183. // [[YBBaseAppDelegate sharedAppDelegate]pushViewController: infoVC animated:YES];
  184. }
  185. -(void)changeRigthTitle {
  186. BOOL haveSelDel = [self seldelCollectionGoods];
  187. if (haveSelDel) {
  188. [self.rightBtn setTitle:YZMsg(@"删除") forState:0];
  189. }else {
  190. [self.rightBtn setTitle:YZMsg(@"管理") forState:0];
  191. }
  192. }
  193. -(void)resetDelFlag {
  194. [_flagDel removeAllObjects];
  195. for (int i=0; i<_dataArray.count; i++) {
  196. [_flagDel addObject:@"0"];
  197. }
  198. [_delCollectionGoods removeAllObjects];
  199. [self changeRigthTitle];
  200. }
  201. -(BOOL)seldelCollectionGoods {
  202. BOOL haveSelDel = NO;
  203. for (NSString *delFlag in _flagDel) {
  204. if ([delFlag isEqual:@"1"]) {
  205. haveSelDel = YES;
  206. break;
  207. }
  208. }
  209. return haveSelDel;
  210. }
  211. - (void)clickNaviRightBtn {
  212. BOOL haveSelDel = [self seldelCollectionGoods];
  213. if (haveSelDel) {
  214. _singleDel = NO;
  215. [self showDelAlert];
  216. }else {
  217. //没选中 就切换编辑状态
  218. _currentEditeState = !_currentEditeState;
  219. [_tableView reloadData];
  220. }
  221. }
  222. -(void)sureDelSelCollection {
  223. NSString *idStrs = @"";
  224. for (NSDictionary *delDic in _delCollectionGoods) {
  225. idStrs = [idStrs stringByAppendingFormat:@"%@,",[delDic valueForKey:@"id"]];
  226. }
  227. YBWeakSelf;
  228. if (idStrs.length > 1) {
  229. //去掉最后一个逗号
  230. idStrs = [idStrs substringToIndex:[idStrs length] - 1];
  231. [MBProgressHUD showMessage:@""];
  232. [YBNetworking postWithUrl:@"Shop.batchDelCollect" Dic:@{@"goodsids":idStrs} Suc:^(int code, id info, NSString *msg) {
  233. [MBProgressHUD hideHUD];
  234. [MBProgressHUD showError:msg];
  235. if (code == 0) {
  236. [_dataArray removeObjectsInArray:_delCollectionGoods];
  237. //删除成功后
  238. [weakSelf resetDelFlag];
  239. _currentEditeState = NO;
  240. if (_dataArray.count <=0) {
  241. [PublicView showTextNoData:_tableView text1:@"" text2:YZMsg(@"暂无数据") centerY:0.8];
  242. }else {
  243. [PublicView hiddenTextNoData:_tableView];
  244. }
  245. [_tableView reloadData];
  246. }
  247. } Fail:^(id fail) {
  248. [MBProgressHUD hideHUD];
  249. }];
  250. }
  251. }
  252. -(void)showDelAlert{
  253. YBWeakSelf;
  254. NSString *msg = _singleDel?YZMsg(@"确定删除该条记录?"):YZMsg(@"确定删除所选收藏?");
  255. NSDictionary *contentDic = @{@"title":@"",@"msg":msg,@"left":YZMsg(@"取消"),@"right":YZMsg(@"确定"),@"richImg":@""};
  256. [YBAlertView showAlertView:contentDic complete:^(int eventType) {
  257. if (eventType == 1) {
  258. //删除
  259. [weakSelf sureDelSelCollection];
  260. }else {
  261. //取消
  262. [weakSelf resetDelFlag];
  263. _currentEditeState = NO;
  264. [_tableView reloadData];
  265. }
  266. }];
  267. }
  268. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  269. return !_currentEditeState;
  270. }
  271. #pragma mark - set/get
  272. -(UITableView *)tableView {
  273. if (!_tableView) {
  274. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,64+statusbarHeight, _window_width, _window_height - 64-statusbarHeight)style:UITableViewStylePlain];
  275. _tableView.delegate = self;
  276. _tableView.dataSource = self;
  277. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  278. _tableView.backgroundColor = UIColor.whiteColor;
  279. YBWeakSelf;
  280. _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  281. _paging = 1;
  282. [weakSelf pullData];
  283. }];
  284. _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  285. _paging +=1;
  286. [weakSelf pullData];
  287. }];
  288. }
  289. return _tableView;
  290. }
  291. @end