Ei kuvausta

FKPriceWarnListController.m 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // FKPriceWarnListController.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/5/27.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKPriceWarnListController.h"
  9. #import "FKPriceWarnListCell.h"
  10. #import "FKPriceWarnListRequest.h"
  11. #import "FKPriceWarnListReform.h"
  12. #import "FKPriceWarnListViewModel.h"
  13. #import "FKPriceWarnEditController.h"
  14. #import "FKProDetailController.h"
  15. static NSString *PRICE_WARN_LIST_CELL_IDENTIFY = @"PRICE_WARN_LIST_CELL_IDENTIFY";
  16. static const int REQ_WARN_LIST_FIRST = 821;
  17. static const int REQ_WARN_LIST_MORE = 822;
  18. static const int REQ_DELETE_REMIND = 823;
  19. @interface FKPriceWarnListController () <UITableViewDataSource, UITableViewDelegate, FLNetworkDelegate>
  20. @property (nonatomic, strong) FKPriceWarnListViewModel *viewModel;
  21. @property (nonatomic, strong) UITableView *tableView;
  22. @end
  23. @implementation FKPriceWarnListController
  24. - (void)viewDidLoad{
  25. [super viewDidLoad];
  26. self.view.backgroundColor = UIColorFromRGB(0xf4f4f4);
  27. [self addAllSubviews];
  28. [self addRefreshControl];
  29. }
  30. - (void)addAllSubviews{
  31. [self.view addSubview:self.tableView];
  32. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  33. make.edges.insets(UIEdgeInsetsZero);
  34. }];
  35. }
  36. - (void)addRefreshControl{
  37. [self initRefreshControlWithTableView:self.tableView];
  38. }
  39. - (void)viewWillAppear:(BOOL)animated{
  40. [super viewWillAppear:animated];
  41. [self requestDataList];
  42. [self.navigationController setNavigationBarHidden:NO animated:YES];
  43. self.navigationItem.title = @"降价提醒";
  44. }
  45. - (void)resetBottomRefresh{
  46. [self.refreshControl setBottomEnabled:[self.viewModel bottomRefreshEnable]];
  47. }
  48. - (void)requestDataList{
  49. [self.hudView show:YES];
  50. [FKPriceWarnListRequest reqWarnListWithIdentify:REQ_WARN_LIST_FIRST
  51. startRow:0
  52. delegate:self];
  53. }
  54. #pragma mark - refresh delegate
  55. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction{
  56. if (direction == RefreshDirectionTop) {
  57. [FKPriceWarnListRequest reqWarnListWithIdentify:REQ_WARN_LIST_FIRST
  58. startRow:0
  59. delegate:self];
  60. }else if (direction == RefreshDirectionBottom){
  61. [FKPriceWarnListRequest reqWarnListWithIdentify:REQ_WARN_LIST_MORE
  62. startRow:self.viewModel.dataArray.count
  63. delegate:self];
  64. }
  65. }
  66. #pragma mark - response
  67. - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header{
  68. [self.hudView hide:NO];
  69. [self.refreshControl finishRefreshingDirection:RefreshDirectionTop];
  70. [self.statusView removeFromSuperview];
  71. if (header.code.integerValue == RESPONSE_MSG_NORMAL){
  72. if (identify == REQ_WARN_LIST_FIRST){
  73. self.viewModel.dataArray = [FKPriceWarnListReform parserWarnListWithResponse:response];
  74. }else if (identify == REQ_WARN_LIST_MORE){
  75. [self.viewModel appendMoreArray:[FKPriceWarnListReform parserWarnListWithResponse:response]];
  76. }else if (identify == REQ_DELETE_REMIND){
  77. [self.viewModel deleteRemindItemAtIndex:self.viewModel.targetToDelete];
  78. [self.tableView reloadData];
  79. }
  80. if (self.viewModel.dataArray.count == 0) {
  81. [self showStatusTipInView:self.view image:[UIImage imageNamed:@"StatusNoMessageIcon"] title:@"没有发现商品哦"];
  82. }
  83. self.viewModel.totalCount = [FKPriceWarnListReform parserListTotlaWithResponse:response];
  84. [self resetBottomRefresh];
  85. [self.tableView reloadData];
  86. }else{
  87. [FLProgressHUDHelper showText:header.msg inView:self.view];
  88. }
  89. }
  90. - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header{
  91. [self.hudView hide:NO];
  92. [self.refreshControl finishRefreshingDirection:RefreshDirectionTop];
  93. [FLProgressHUDHelper showText:header.msg inView:self.view];
  94. }
  95. #pragma mark - tableView dataSource
  96. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  97. return self.viewModel.dataArray.count;
  98. }
  99. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  100. return 1;
  101. }
  102. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  103. FKPriceWarnListCell *cell = [tableView dequeueReusableCellWithIdentifier:PRICE_WARN_LIST_CELL_IDENTIFY];
  104. FKRemindProductItem *item = [self.viewModel remindItemAtIndex:indexPath.section];
  105. cell.bookPirceBtn.tag = indexPath.section;
  106. [cell.bookPirceBtn addTarget:self action:@selector(clickEditBtn:) forControlEvents:UIControlEventTouchUpInside];
  107. [cell configWithRemindItem:item];
  108. return cell;
  109. }
  110. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  111. return 105.0f;
  112. }
  113. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  114. return 10.0f;
  115. }
  116. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  117. UIView *view = [[UIView alloc]init];
  118. view.backgroundColor = [UIColor clearColor];
  119. return view;
  120. }
  121. #define TableView Edit
  122. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
  123. return YES;
  124. }
  125. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
  126. return UITableViewCellEditingStyleDelete;
  127. }
  128. - (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath;{
  129. return @"删除";
  130. }
  131. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
  132. if (editingStyle == UITableViewCellEditingStyleDelete){
  133. FKRemindProductItem *item = [self.viewModel remindItemAtIndex:indexPath.section];
  134. if (item && [FLStringHelper isValidString:item.goodsID]){
  135. self.viewModel.targetToDelete = indexPath.section;
  136. [self.hudView show:YES];
  137. [FKPriceWarnListRequest reqRemoveWarnWithIdentify:REQ_DELETE_REMIND
  138. goodsID:item.itemID
  139. delegate:self];
  140. }
  141. }
  142. }
  143. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  144. FKRemindProductItem *item = [self.viewModel remindItemAtIndex:indexPath.section];
  145. if (item && [FLStringHelper isValidString:item.productID]){
  146. FKProDetailController *detail = [[FKProDetailController alloc]initWithProductID:item.productID selectSpecId:item.goodsID];
  147. [self.navigationController pushViewController:detail animated:YES];
  148. }
  149. }
  150. #pragma mark - action
  151. - (void)clickEditBtn:(UIButton *)sender{
  152. FKRemindProductItem *item = [self.viewModel remindItemAtIndex:sender.tag];
  153. FKPriceWarnItem *warnItem = [item createPriceWarnItem];
  154. if (warnItem){
  155. FKPriceWarnEditController *controller = [[FKPriceWarnEditController alloc]initWithPriceWarnItem:warnItem];
  156. [controller setHidesBottomBarWhenPushed:YES];
  157. [self.navigationController pushViewController:controller animated:YES];
  158. }
  159. }
  160. #pragma mark - property
  161. - (UITableView *)tableView {
  162. if (_tableView == nil) {
  163. _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
  164. _tableView.dataSource = self;
  165. _tableView.delegate = self;
  166. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  167. _tableView.backgroundColor = [UIColor clearColor];
  168. _tableView.backgroundView = nil;
  169. [_tableView registerClass:[FKPriceWarnListCell class] forCellReuseIdentifier:PRICE_WARN_LIST_CELL_IDENTIFY];
  170. if (@available(iOS 11.0, *)) {
  171. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  172. }
  173. }
  174. return _tableView;
  175. }
  176. - (FKPriceWarnListViewModel *)viewModel{
  177. if (_viewModel == nil) {
  178. _viewModel = [[FKPriceWarnListViewModel alloc]init];
  179. }
  180. return _viewModel;
  181. }
  182. @end