// // KWMSearchFeedback.m // iCemarose // // Created by 陈荣科 on 16/9/1. // Copyright © 2016年 kollway. All rights reserved. // #import "KWMSearchFeedbackVC.h" #import "KWMBrandCaramelCell.h" #import "KWMSearchFeedBackView.h" #import "KWMNewProductVC.h" #import "MJRefresh.h" #import "KWMStringUtil.h" @interface KWMSearchFeedbackVC ()<UITableViewDelegate,UITableViewDataSource,KWMSearchFeedBackViewDelegate,KWMBrandCaramelCellDelegate> @property (nonatomic) KWMSearchFeedBackView *vSearchFB; //@property (nonatomic) NSArray<KWMDataProduct *> *data; @end @implementation KWMSearchFeedbackVC{ BUYProduct *_product; NSInteger endPage; NSInteger total; NSInteger mark;//标记展开标尺的pruduct下标 } +(NSString *)kwmTag{ return @"KWMSearchFeedbackVC"; } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [_vSearchFB removeFromSuperview]; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:YES]; self.vBackground.hidden = YES; [self initHeardView]; if (!self.searchText) { [self.vSearchFB.tfSearch becomeFirstResponder]; } } - (UITableView *)targetTableView{ return self.tbvSearch; } - (void)loadData{ [self getSearchProduct]; } - (void)reLoadData{ self.currentPage = @0; [self getSearchProduct]; } static NSString *idStr = @"KWMBrandCaramelCell"; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // _data = [NSArray array]; total = 0; mark = -1; self.tbvSearch.delegate = self; self.tbvSearch.separatorStyle = UITableViewCellSeparatorStyleNone; [self.tbvSearch registerNib:[UINib nibWithNibName:idStr bundle:nil] forCellReuseIdentifier:idStr]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackgroundView:)]; [self.vBackground addGestureRecognizer:tapGesture]; if (self.searchText) { [self getSearchProduct]; } } - (void)onClickBackgroundView:(UITapGestureRecognizer *)tapGesture{ [_vSearchFB.tfSearch resignFirstResponder]; self.vBackground.hidden = YES; } - (void)initHeardView{ self.vSearchFB = [[KWMSearchFeedBackView alloc] init]; self.vSearchFB.tfSearch.text = self.searchText; self.vSearchFB.lbGoodsNum.hidden = NO; self.vSearchFB.lbGoodsNum.text = [NSString stringWithFormat:@"%lu商品",(unsigned long)total]; self.vSearchFB.delegate = self; [self.view addSubview:self.vSearchFB]; [self initCustomTitleBarConstarints:self.vSearchFB]; } #pragma mark -- KWMBrandCaramelCellDelegate //点击左边商品回调 - (void)kwm_onClickLeftIamge:(KWMBrandCaramelCell *)brandCell{ KWMNewProductVC *productDetailVC = [KWMNewProductVC getNewInstance]; productDetailVC.productId = brandCell.leftDataProduct.id; [self.navigationController pushViewController:productDetailVC animated:YES]; } //点击右边商品回调 - (void)kwm_onClickRightIamge:(KWMBrandCaramelCell *)brandCell{ KWMNewProductVC *productDetailVC = [KWMNewProductVC getNewInstance]; productDetailVC.productId = brandCell.rightDataProduct.id; [self.navigationController pushViewController:productDetailVC animated:YES]; } - (void)kwm_openRule:(BUYProduct *)product Or:(KWMDataProduct *)dataProduct And:(KWMBrandCaramelCell *)brandCell{ if (product != nil) { [self.dataList enumerateObjectsUsingBlock:^(BUYProduct *obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isEqual:product]) { if (mark != idx) { mark = idx; [self.tbvSearch reloadData]; } *stop = YES; } }]; }else if (dataProduct != nil){ [self.dataList enumerateObjectsUsingBlock:^(KWMDataProduct *obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isEqual:dataProduct]) { if (mark != idx) { mark = idx; [self.tbvSearch reloadData]; } *stop = YES; } }]; } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if (mark % 2 ) { [brandCell openRule:NO]; }else{ [brandCell openRule:YES]; } }); } #pragma mark -- KWMSearchFeedBackViewDelegate - (void)kwm_cancelSearch{ [self.navigationController popViewControllerAnimated:YES]; } - (void)kwm_tfReturnSearchFinished:(NSString *)text{ [self.dataList removeAllObjects]; self.searchText = text; self.vBackground.hidden = YES; self.currentPage = @0; [self getSearchProduct]; } - (void)kwm_beginEditing{ self.vBackground.hidden = NO; } #warning -- 再次点击搜索 - (void)kwm_searchGoods{ NSLog(@"再次点击搜索"); } #pragma mark -- UITableViewDelegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataList.count%2 ? self.dataList.count/2 + 1 : self.dataList.count/2; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ KWMBrandCaramelCell *brandCaramelCell = [tableView dequeueReusableCellWithIdentifier:idStr forIndexPath:indexPath]; brandCaramelCell.delegate = self; NSInteger position = indexPath.row * 2; if(self.dataList!=nil && self.dataList.count>0){ KWMDataProduct *leftDataProduct = [self.dataList objectAtIndex:position]; KWMDataProduct *rightDataProduct = self.dataList.count>(position+1)?[self.dataList objectAtIndex:(position+1)]:nil; [brandCaramelCell setDataProduct:leftDataProduct rightProduct:rightDataProduct]; } return brandCaramelCell; } #pragma mark -- UITableViewDataSource - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 244; } //搜索API - (void)getSearchProduct{ if([KWMStringUtil isEmpty:_searchText]){ return; } if ([[_searchText lowercaseString] isEqualToString:@"bonpoint"]) { self.vNoGoods.hidden = NO; return; } if (self.currentPage.integerValue == endPage && endPage != 0) { [self showToast:@"已经是最后一页"]; [self hideLoading]; [self.tbvSearch.mj_footer endRefreshingWithNoMoreData]; return; } NSInteger tagetPage = self.currentPage.integerValue + 1; NSDictionary *parameters = @{ @"keyword":_searchText, @"page":@(tagetPage).stringValue, }; __weak KWMSearchFeedbackVC *weakSelf = self; [self.api searchProduct:parameters success:^(NSURLSessionDataTask *task, KWMSearchResult *result) { [weakSelf hideLoading]; total = result.products.total.integerValue; endPage = result.products.lastPage.integerValue; weakSelf.hasNextPage = tagetPage != endPage; if(tagetPage == 1){ [weakSelf.dataList removeAllObjects]; } NSArray *products = [result.productList rx_filterWithBlock:^BOOL(KWMDataProduct *each) { return [each available]; }]; [weakSelf appendDataList:products setPage:tagetPage]; _vSearchFB.lbGoodsNum.text = [NSString stringWithFormat:@"%lu商品",(unsigned long)total]; [self.tbvSearch reloadData]; if (self.dataList.count == 0) { self.vNoGoods.hidden = NO; }else{ self.vNoGoods.hidden = YES; } _vSearchFB.lbGoodsNum.hidden = NO; } failure:^(NSURLSessionDataTask *task, NSError *error) { _vSearchFB.lbGoodsNum.hidden = NO; [weakSelf showError:error]; NSLog(@"error = %@",error); }]; } @end