KWMSearchFeedbackVC.m 7.72 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11
//
//  KWMSearchFeedback.m
//  iCemarose
//
//  Created by 陈荣科 on 16/9/1.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMSearchFeedbackVC.h"
#import "KWMBrandCaramelCell.h"
#import "KWMSearchFeedBackView.h"
u  
lee committed
12
#import "KWMNewProductVC.h"
13
#import "MJRefresh.h"
houweibin committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

@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];
lee committed
41 42 43
    if (!self.searchText) {
        [self.vSearchFB.tfSearch becomeFirstResponder];
    }
houweibin committed
44 45 46 47 48 49 50 51 52 53 54 55
}

- (UITableView *)targetTableView{
    return self.tbvSearch;
}

- (void)loadData{
    [self getSearchProduct];
}

- (void)reLoadData{
   
lee committed
56
//    [self getSearchProduct];
houweibin committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70
}
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];
lee committed
71 72 73
    if (self.searchText) {
        [self getSearchProduct];
    }
houweibin committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
}

- (void)onClickBackgroundView:(UITapGestureRecognizer *)tapGesture{
    [_vSearchFB.tfSearch resignFirstResponder];
    self.vBackground.hidden = YES;
}

- (void)initHeardView{

    _vSearchFB = [[KWMSearchFeedBackView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 64)];
    _vSearchFB.tfSearch.text = self.searchText;
    _vSearchFB.lbGoodsNum.hidden = NO;
    _vSearchFB.lbGoodsNum.text = [NSString stringWithFormat:@"%lu商品",(unsigned long)total];
    _vSearchFB.delegate = self;
    [self.view addSubview:_vSearchFB];
}

#pragma mark -- KWMBrandCaramelCellDelegate
//点击左边商品回调
- (void)kwm_onClickLeftIamge:(KWMBrandCaramelCell *)brandCell{
u  
lee committed
94
    KWMNewProductVC *productDetailVC = [KWMNewProductVC getNewInstance];
houweibin committed
95 96 97 98 99 100
    productDetailVC.productId = brandCell.leftDataProduct.id;
    [self.navigationController pushViewController:productDetailVC animated:YES];
}

//点击右边商品回调
- (void)kwm_onClickRightIamge:(KWMBrandCaramelCell *)brandCell{
u  
lee committed
101
    KWMNewProductVC *productDetailVC = [KWMNewProductVC getNewInstance];
houweibin committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    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;
149
    self.currentPage = @0;
houweibin committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    [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{
    
193 194 195 196 197
    if ([[_searchText lowercaseString] isEqualToString:@"bonpoint"]) {
        self.vNoGoods.hidden = NO;
        return;
    }
    
houweibin committed
198 199 200
    if (self.currentPage.integerValue == endPage && endPage != 0) {
        [self showToast:@"已经是最后一页"];
        [self hideLoading];
201
        [self.tbvSearch.mj_footer endRefreshingWithNoMoreData];
houweibin committed
202 203 204 205 206 207 208 209 210 211 212 213
        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;
214 215 216 217
        weakSelf.hasNextPage = tagetPage != endPage;
        if(tagetPage == 1){
            [weakSelf.dataList removeAllObjects];
        }
lee committed
218
        
u  
lee committed
219 220
        NSArray *products = [result.productList rx_filterWithBlock:^BOOL(KWMDataProduct *each) {
            return [each available];
lee committed
221 222
        }];
        [weakSelf appendDataList:products setPage:tagetPage];
houweibin committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        _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