暫無描述

FKCircleProductController.m 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // FKCircleProductController.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 16/6/14.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKCircleProductController.h"
  9. #import "FKCircleProductCell.h"
  10. #import "FKCircleProductRequest.h"
  11. #import "FKCircleProductReform.h"
  12. #import "FKCircleChoiceProductItem.h"
  13. @interface FKCircleProductController ()
  14. <UITableViewDelegate, UITableViewDataSource, FLNetworkDelegate>
  15. @property (nonatomic, assign) FKCircleProductType productType;
  16. @property (nonatomic, weak ) FKCircleProductViewModel *viewModel;
  17. @property (nonatomic, strong) UITableView *tableView;
  18. @end
  19. @implementation FKCircleProductController
  20. - (instancetype)initWithType:(FKCircleProductType)productType viewModel:(FKCircleProductViewModel *)viewModel {
  21. self = [super init];
  22. if (self) {
  23. _productType = productType;
  24. _viewModel = viewModel;
  25. [self.view addSubview:self.tableView];
  26. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  27. make.left.right.top.bottom.equalTo(self.view);
  28. }];
  29. }
  30. return self;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. // Do any additional setup after loading the view.
  35. [self requestProductData:FKCircleProductRequestNew startRow:0];
  36. }
  37. - (void)viewWillAppear:(BOOL)animated {
  38. [super viewWillAppear:animated];
  39. }
  40. - (void)didReceiveMemoryWarning {
  41. [super didReceiveMemoryWarning];
  42. // Dispose of any resources that can be recreated.
  43. }
  44. /*
  45. #pragma mark - Navigation
  46. // In a storyboard-based application, you will often want to do a little preparation before navigation
  47. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  48. // Get the new view controller using [segue destinationViewController].
  49. // Pass the selected object to the new view controller.
  50. }
  51. */
  52. #pragma mark - Request
  53. - (void)requestProductData:(int)identify startRow:(NSInteger)startRow {
  54. [self.hudView show:YES];
  55. [FKCircleProductRequest requestItems:identify
  56. startRow:[NSString stringWithFormat:@"%ld", (long)startRow]
  57. pageSize:[NSString stringWithFormat:@"%d", PAGE_RECORD_COUNT]
  58. productType:self.productType
  59. deleagate:self];
  60. }
  61. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
  62. // [self.hudView show:YES];
  63. if (direction==RefreshDirectionTop) {
  64. [self requestProductData:FKCircleProductRequestNew startRow:0];
  65. } else if (direction==RefreshDirectionBottom) {
  66. [self requestProductData:FKCircleProductRequestNextPage startRow:[self.viewModel countOfItems:self.productType]];
  67. }
  68. }
  69. #pragma mark - Response
  70. - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header {
  71. [self finishLoadingData];
  72. [self.hudView hide:YES];
  73. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  74. NSArray *items = [FKCircleProductReform parseItems:response];
  75. if (identify == FKCircleProductRequestNew) {
  76. [self.viewModel removeAllObjects:self.productType];
  77. [self.viewModel saveItems:items productType:self.productType];
  78. } else if (identify == FKCircleProductRequestNextPage) {
  79. [self.viewModel saveItems:items productType:self.productType];
  80. }
  81. [self.tableView reloadData];
  82. // 显示无内容提示
  83. if ([self.viewModel countOfItems:self.productType] == 0) {
  84. [self showStatusTipInView:self.tableView image:[UIImage imageNamed:@"StatusNoOrderIcon"] title:@"没有内容哦"];
  85. } else {
  86. [self.statusView removeFromSuperview];
  87. }
  88. self.refreshControl.bottomEnabled = items.count > 0 ? YES : NO;
  89. } else {
  90. [FLProgressHUDHelper showText:header.msg inView:self.view];
  91. }
  92. }
  93. - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header {
  94. [self finishLoadingData];
  95. [self.hudView hide:YES];
  96. [FLProgressHUDHelper showText:header.msg inView:self.view];
  97. }
  98. #pragma mark - UITableViewDelegate, UITableViewDataSource
  99. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  100. return [self.viewModel countOfItems:self.productType];
  101. }
  102. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  103. return 1;
  104. }
  105. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  106. return 10;
  107. }
  108. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  109. return CGFLOAT_MIN;
  110. }
  111. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  112. return 105;
  113. }
  114. - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  115. FKCircleProductCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKCircleProductCell class])];
  116. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  117. id item = [self.viewModel itemAtIndex:indexPath.section productType:self.productType];
  118. [cell configWithItem:item];
  119. cell.choiceButton.tag = indexPath.section;
  120. [cell.choiceButton addTarget:self action:@selector(clickSelectButton:) forControlEvents:UIControlEventTouchUpInside];
  121. return cell;
  122. }
  123. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  124. // FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:indexPath.section classify:_classify];
  125. // if (item&& self.delegate && [self.delegate respondsToSelector:@selector(orderController:didClickOrderDetailAction:orderType:)]) {
  126. // [self.delegate orderController:self didClickOrderDetailAction:item.paymentID orderType:kOrderTypeProduct];
  127. // }
  128. }
  129. #pragma mark - Action
  130. - (IBAction)clickSelectButton:(UIButton *)sender {
  131. sender.selected = !sender.selected;
  132. FKCircleChoiceProductItem *item = [self.viewModel itemAtIndex:sender.tag productType:self.productType];
  133. if ([item isKindOfClass:[FKCircleChoiceProductItem class]]) {
  134. item.isSelected = sender.selected;
  135. }
  136. }
  137. #pragma mark - Method
  138. #pragma mark - Layout
  139. #pragma mark - Property
  140. - (UITableView *)tableView {
  141. if (!_tableView) {
  142. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  143. _tableView.dataSource = self;
  144. _tableView.delegate = self;
  145. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  146. _tableView.backgroundColor = UIColorFromRGB(0xf4f4f4);
  147. [self initRefreshControlWithTableView:self.tableView];
  148. [self.tableView registerClass:[FKCircleProductCell class] forCellReuseIdentifier:NSStringFromClass([FKCircleProductCell class])];
  149. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  150. }
  151. return _tableView;
  152. }
  153. @end