ProductLibraryViewController.m 15.7 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1
//
2
//  ProductLibraryViewController.m
曹云霄's avatar
曹云霄 committed
3 4
//  Lighting
//
5
//  Created by 曹云霄 on 16/5/4.
曹云霄's avatar
曹云霄 committed
6 7 8
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

9
#import "ProductLibraryViewController.h"
曹云霄's avatar
曹云霄 committed
10
#import "ProductCollectionViewCell.h"
曹云霄's avatar
曹云霄 committed
11
#import "ProductDetailsViewController.h"
曹云霄's avatar
曹云霄 committed
12 13 14
#import "ScreeningView.h"
#import "screeningFirstView.h"
#import "screeningSecondView.h"
曹云霄's avatar
曹云霄 committed
15 16


17
@interface ProductLibraryViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIGestureRecognizerDelegate,ScreeningButtonClickdelegate,TableviewSelectedIndexdelegate,SelectedCollectionItemDelegate>
曹云霄's avatar
曹云霄 committed
18

曹云霄's avatar
曹云霄 committed
19 20 21 22
/**
 *  筛选背景框View
 */
@property (nonatomic,strong) ScreeningView *screenView;
曹云霄's avatar
曹云霄 committed
23

曹云霄's avatar
曹云霄 committed
24

曹云霄's avatar
曹云霄 committed
25 26 27 28 29 30 31 32 33
/**
 *  分类View
 */
@property (nonatomic,strong) screeningFirstView *screenFirstView;

/**
 *  筛选View
 */
@property (nonatomic,strong) screeningSecondView *screenSecondView;
曹云霄's avatar
曹云霄 committed
34

曹云霄's avatar
曹云霄 committed
35 36 37 38 39
/**
 *  筛选数据源
 */
@property (nonatomic,strong) NSMutableArray *screeningDatas;

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/**
 *  当前筛选按钮选中状态
 */
@property (nonatomic,assign) NSInteger selectedIndex;

/**
 *  商品数据源对象
 */
@property (nonatomic,strong) GoodsResponse *datasArray;

/**
 *  复选返回时临时保存数据
 */
@property (nonatomic,strong) GoodsResponse *temporaryArray;

曹云霄's avatar
曹云霄 committed
55 56
@end

57
@implementation ProductLibraryViewController
曹云霄's avatar
曹云霄 committed
58

曹云霄's avatar
曹云霄 committed
59 60 61 62 63 64 65 66 67 68 69 70 71
/**
 *  初始化筛选数据源
 */
- (NSMutableArray *)screeningDatas
{
    if (_screeningDatas == nil) {
        
        _screeningDatas = [NSMutableArray array];
    }
    return _screeningDatas;
}


勾芒's avatar
勾芒 committed
72 73 74 75 76 77 78
- (void)viewWillAppear:(BOOL)animated
{
    if (_screeningDatas != nil) {
        
        [self getGoodsListDatas];
    }
}
79

曹云霄's avatar
曹云霄 committed
80 81 82
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
83
    self.view.backgroundColor = [UIColor blueColor];
曹云霄's avatar
曹云霄 committed
84 85
    
    [self uiConfigAction];
勾芒's avatar
勾芒 committed
86
    [self getGoodsListDatas];
曹云霄's avatar
曹云霄 committed
87
    [self getScreeningdatas];
曹云霄's avatar
曹云霄 committed
88 89
}

曹云霄's avatar
曹云霄 committed
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#pragma mark -获取商品列表数据
- (void)getGoodsListDatas
{
    
    //默认数据
    GoodsCondition *conditon = [[GoodsCondition alloc]init];
    //分页数据
    DataPage *Newpage = [[DataPage alloc]init];
    Newpage.page = 0;
    conditon.page = Newpage;
    
    __weak typeof(self)weakSelf = self;
    [self getGoodsListdatas:conditon returnResponse:^(GoodsResponse *response) {
        
        if (response.goodsEntity.count == 0) {
            
            [self ErrorMBProgressView:@"暂无数据"];
            return ;
        }
        weakSelf.datasArray = response;
        [weakSelf.productCollectionView reloadData];
    }];
}

曹云霄's avatar
曹云霄 committed
115
#pragma mark -获取产品筛选数据
曹云霄's avatar
曹云霄 committed
116 117 118
- (void)getScreeningdatas
{
    
曹云霄's avatar
曹云霄 committed
119 120 121 122 123 124 125 126 127 128
  [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/goods/getGoodsFilter"] WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
      
      if ([returnValue[@"code"] isEqualToNumber:@0]) {
          
          GoodsFilter *goodsStyle = [[GoodsFilter alloc]initWithDictionary:returnValue[@"data"] error:nil];
          [self.screeningDatas addObject:goodsStyle.styles];
          [self.screeningDatas addObject:goodsStyle.categories];
          [self.screeningDatas addObject:goodsStyle.space];
          [self.screeningDatas addObject:goodsStyle.material];
          [self.screeningDatas addObject:goodsStyle.price];
勾芒's avatar
勾芒 committed
129
    }
曹云霄's avatar
曹云霄 committed
130 131 132 133 134 135 136 137 138 139 140
      else
      {
          [self ErrorMBProgressView:returnValue[@"message"]];
      }
      
  } WithErrorCodeBlock:^(id errorCodeValue) {
      
  } WithFailureBlock:^(id error) {
      
      NSLog(@"%@",error);
  }];
曹云霄's avatar
曹云霄 committed
141 142 143
}


勾芒's avatar
勾芒 committed
144

145 146 147
#pragma mark -获取商品列表数据
- (void)getGoodsListdatas:(GoodsCondition *)conditon returnResponse:(void(^)(GoodsResponse *))finish
{
勾芒's avatar
勾芒 committed
148
    
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    [self CreateMBProgressHUDLoding];
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/goods/query"] WithRequestType:0 WithParameter:conditon WithReturnValueBlock:^(id returnValue) {
        
        [self RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
            GoodsResponse *sponse = [[GoodsResponse alloc]initWithDictionary:returnValue[@"data"] error:nil];
            //返回结果
            finish(sponse);
        }
        else
        {
           [self ErrorMBProgressView:returnValue[@"message"]];
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
    } WithFailureBlock:^(id error) {
        
        [self RemoveMBProgressHUDLoding];
    }];
}


曹云霄's avatar
曹云霄 committed
173 174 175
#pragma mark -布局
- (void)uiConfigAction
{
176
    
曹云霄's avatar
曹云霄 committed
177 178 179 180 181
    self.productCollectionLayout.itemSize = CGSizeMake((ScreenWidth-100)/3, (ScreenWidth-100)/3);
    self.productCollectionLayout.sectionInset = UIEdgeInsetsMake(20, 30, 20, 30);
    self.productCollectionLayout.minimumLineSpacing = 20;
    self.productCollectionLayout.minimumInteritemSpacing = 20;
    self.productCollectionView.dataSource = self;
曹云霄's avatar
曹云霄 committed
182
    self.productCollectionView.delegate = self;
183
    self.productCollectionView.alwaysBounceVertical = YES;
曹云霄's avatar
曹云霄 committed
184
    [self CreatescreeningButton];
185
    
勾芒's avatar
勾芒 committed
186
    //搜索关键字
勾芒's avatar
勾芒 committed
187
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchInputString:) name:SEARCHSTRING object:nil];
勾芒's avatar
勾芒 committed
188
    
勾芒's avatar
勾芒 committed
189 190 191 192 193 194 195 196 197
    //返回根视图
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popTorootviewController:) name:POPROOTCONTROLLER object:nil];

}

#pragma mark -返回根视图
- (void)popTorootviewController:(NSNotification *)not
{
    [self.navigationController popViewControllerAnimated:YES];
曹云霄's avatar
曹云霄 committed
198 199 200
}


勾芒's avatar
勾芒 committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
#pragma mark -搜索关键字
- (void)searchInputString:(NSNotification *)not
{
    //默认数据
    GoodsCondition *conditon = [[GoodsCondition alloc]init];
    //分页数据
    DataPage *Newpage = [[DataPage alloc]init];
    Newpage.page = 0;
    conditon.page = Newpage;
    conditon.nameLike = not.object;
    
    __weak typeof(self)weakSelf = self;
    [self getGoodsListdatas:conditon returnResponse:^(GoodsResponse *response) {
        
        if (response.goodsEntity.count == 0) {
            
            [self ErrorMBProgressView:@"暂无数据"];
            return ;
        }
        weakSelf.datasArray = response;
        [weakSelf.productCollectionView reloadData];
    }];
}

曹云霄's avatar
曹云霄 committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

#pragma mark -筛选按钮
- (void)CreatescreeningButton
{
    UIButton *screeningbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    screeningbutton.frame = CGRectMake(ScreenWidth-100, ScreenHeight*6/10, 50, 50);
    
    //阴影层
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(ScreenWidth-97.5, ScreenHeight*6/10+2.5, 45, 45);//保证layer的size比筛选按钮高宽都短5像素
    layer.backgroundColor = [UIColor blackColor].CGColor;
    layer.shadowOffset = CGSizeMake(0, 8);
    layer.shadowOpacity = 0.7;
    layer.cornerRadius = 25;
    [self.view.layer addSublayer:layer];
    
    //筛选按钮
    screeningbutton.layer.masksToBounds = YES;
    screeningbutton.layer.cornerRadius = 25;
    [screeningbutton setTitle:@"筛选" forState:UIControlStateNormal];
    screeningbutton.titleLabel.font = [UIFont systemFontOfSize:15];
    [screeningbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [screeningbutton setTitleColor:kMainBlueColor forState:UIControlStateSelected];
曹云霄's avatar
曹云霄 committed
248
    [screeningbutton addTarget:self action:@selector(ScreeningButtonClick) forControlEvents:UIControlEventTouchUpInside];
曹云霄's avatar
曹云霄 committed
249 250 251 252 253 254 255 256 257
    screeningbutton.backgroundColor = kMainBlueColor;
    [self.view addSubview:screeningbutton];
    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"productcell" forIndexPath:indexPath];
258 259 260
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",ServerAddress,[[self.datasArray.goodsEntity objectAtIndex_opple:indexPath.row] pictures]]];
    [cell.productImageView sd_setImageWithURL:url placeholderImage:nil];
    
曹云霄's avatar
曹云霄 committed
261
    
曹云霄's avatar
曹云霄 committed
262 263 264 265 266
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
267
    return self.datasArray.goodsEntity.count;
曹云霄's avatar
曹云霄 committed
268 269
}

曹云霄's avatar
曹云霄 committed
270 271 272 273 274
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardwithCYX" bundle:nil];
    ProductDetailsViewController *productDetails = [storyboard instantiateViewControllerWithIdentifier:@"productdetails"];
曹云霄's avatar
曹云霄 committed
275
    productDetails.goodsID = [[self.datasArray.goodsEntity objectAtIndex_opple:indexPath.item] fid];
曹云霄's avatar
曹云霄 committed
276 277
    [self.navigationController pushViewController:productDetails animated:YES];
}
曹云霄's avatar
曹云霄 committed
278 279


曹云霄's avatar
曹云霄 committed
280 281 282
#pragma mark -筛选
- (void)ScreeningButtonClick
{
勾芒's avatar
勾芒 committed
283

勾芒's avatar
勾芒 committed
284
    
曹云霄's avatar
曹云霄 committed
285 286 287
    self.screenView = [[[NSBundle mainBundle] loadNibNamed:@"ScreeningView" owner:self options:nil]firstObject];
    self.screenView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    self.screenView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
曹云霄's avatar
曹云霄 committed
288
    self.screenView.delegate = self;
曹云霄's avatar
曹云霄 committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    //点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(DismissScreenView)];
    tap.delegate = self;
    tap.cancelsTouchesInView = NO;
    [self.screenView addGestureRecognizer:tap];
    self.screenView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    [self.view.window addSubview:self.screenView];
    self.screenView.alpha = 0;
    
    //监听Segmented菜单
    [self.screenView.sortingSegmented addTarget:self action:@selector(sortingSegmentedClick:) forControlEvents:UIControlEventValueChanged];
    
    [self CreateClassificationView];
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.screenView.alpha = 1;
    }];
}


#pragma mark -分类、筛选view切换
- (void)sortingSegmentedClick:(UISegmentedControl *)sender {
    
    switch (sender.selectedSegmentIndex) {
        case 0://分类
        {
            NSLog(@"分类");
            [self.screenSecondView removeFromSuperview];
            [self CreateClassificationView];
        }
            break;
        case 1://筛选
        {
            NSLog(@"筛选");
            [self.screenFirstView removeFromSuperview];
            [self CreateScreenSubView];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark -创建分类View
- (void)CreateClassificationView
{
    //分类View
    self.screenFirstView = [[[NSBundle mainBundle] loadNibNamed:@"screeningFirstView" owner:self options:nil] firstObject];
    self.screenFirstView.frame = CGRectMake(0, 50, 470, 310);
340 341
    self.screenFirstView.delegate = self;
     [self screeningButtonClick:self.selectedIndex];
曹云霄's avatar
曹云霄 committed
342 343 344
    [self.screenView.backgroundView addSubview:self.screenFirstView];
}

曹云霄's avatar
曹云霄 committed
345

曹云霄's avatar
曹云霄 committed
346 347 348 349 350
#pragma mark -创建筛选选项View
- (void)CreateScreenSubView
{
    self.screenSecondView = [[[NSBundle mainBundle] loadNibNamed:@"screeningSecondView" owner:self options:nil] firstObject];
    self.screenSecondView.frame = CGRectMake(0, 50, 470, 310);
曹云霄's avatar
曹云霄 committed
351
    self.screenSecondView.delegate = self;
352 353
    [self screeningButtonClick:self.selectedIndex];
    self.screenSecondView.totalNumber = self.datasArray.total;
曹云霄's avatar
曹云霄 committed
354 355
    [self.screenView.backgroundView addSubview:self.screenSecondView];
}
曹云霄's avatar
曹云霄 committed
356

曹云霄's avatar
曹云霄 committed
357 358 359 360 361


#pragma mark -ScreeningButtonClickdelegate代理
- (void)screeningButtonClick:(NSInteger)buttontag
{
362
    self.selectedIndex = buttontag;
曹云霄's avatar
曹云霄 committed
363 364 365 366 367 368 369 370 371
    //分类
    if (self.screenView.sortingSegmented.selectedSegmentIndex == 0) {
        
        self.screenFirstView.selectedIndex = buttontag;
        self.screenFirstView.datasArray = [self.screeningDatas objectAtIndex_opple:buttontag];
    }
    //筛选
    else if (self.screenView.sortingSegmented.selectedSegmentIndex == 1)
    {
372
        self.screenSecondView.selectedIndex = buttontag;
曹云霄's avatar
曹云霄 committed
373
        self.screenSecondView.datasArray = [self.screeningDatas objectAtIndex_opple:buttontag];
曹云霄's avatar
曹云霄 committed
374 375 376
    }
}

377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
#pragma mark -选中条件
- (void)selectedItem:(NSString *)typecode
{
    [self DismissScreenView];
    GoodsCondition *condition = [[GoodsCondition alloc]init];
    DataPage *page = [[DataPage alloc]init];
    page.page = 0;
    condition.page = page;
    condition.categoryEquals = typecode;
    //分类模块筛选
    __weak typeof(self)Weakself = self;
    [self getGoodsListdatas:condition returnResponse:^(GoodsResponse *response) {
        
        
        Weakself.datasArray = response;
        [Weakself.productCollectionView reloadData];
        if (response.goodsEntity.count == 0) {
            
            [self ErrorMBProgressView:@"暂无数据"];
        }
    }];
}



曹云霄's avatar
曹云霄 committed
402

曹云霄's avatar
曹云霄 committed
403 404 405
#pragma mark -筛选子模块的
- (void)tableSelectedindex:(NSInteger)index
{
406
    
曹云霄's avatar
曹云霄 committed
407 408 409 410
    [self screeningButtonClick:index];
    self.screenView.indexselected = index;
    
}
曹云霄's avatar
曹云霄 committed
411 412


413 414 415 416 417 418 419 420 421 422 423 424
#pragma mark -筛选复选模块选中的uicollection cell
- (void)collectionSelectedindexWithStyleCode:(NSString *)styleCode
                              withCategories:(NSString *)categoriesCode
                                   withSpace:(NSString *)spaceCode
                                withMaterial:(NSString *)materialCode
                                   withPrice:(NSString *)priceTitle
{
    
    GoodsCondition *condition = [[GoodsCondition alloc]init];
    DataPage *page = [[DataPage alloc]init];
    page.page = 0;
    condition.page = page;
曹云霄's avatar
曹云霄 committed
425 426 427 428 429 430 431 432 433 434 435 436
    if (styleCode.length != 0) {
       condition.styleEquals = styleCode;
    }
    if (categoriesCode.length != 0) {
       condition.categoryEquals = categoriesCode;
    }
    if (spaceCode.length!= 0) {
        condition.spaceEquals = spaceCode;
    }
    if (materialCode.length!= 0) {
        condition.materialEqueals = materialCode;
    }
437

曹云霄's avatar
曹云霄 committed
438 439 440 441 442 443 444 445
    if (![priceTitle isEqualToString:@"全部"]) {
     
        //价格字符串切割
        NSArray *spacrArray = [priceTitle componentsSeparatedByString:@"到"];
        condition.startprice = [NSNumber numberWithInteger:[[spacrArray objectAtIndex_opple:0] integerValue]];
        condition.endprice = [NSNumber numberWithInteger:[[spacrArray objectAtIndex_opple:1] integerValue]];
    }

446
    __weak typeof (self) Weakself = self;
曹云霄's avatar
曹云霄 committed
447
    NSLog(@"%@",[condition toDictionary]);
448 449 450 451 452 453 454
    [self getGoodsListdatas:condition returnResponse:^(GoodsResponse *response) {
        
        Weakself.screenSecondView.totalNumber = response.total;
        //临时保存
        self.temporaryArray = response;
    }];
}
曹云霄's avatar
曹云霄 committed
455 456


457 458 459 460 461 462 463 464 465 466 467 468 469
#pragma mark -显示筛选后的结果
- (void)ShowScreeningResponse
{
    if (self.temporaryArray.goodsEntity.count == 0) {
        
        [self ErrorMBProgressView:@"暂无数据"];
        return;
    }
    self.datasArray = self.temporaryArray;
    [self.productCollectionView reloadData];
    [self DismissScreenView];
}

曹云霄's avatar
曹云霄 committed
470

曹云霄's avatar
曹云霄 committed
471 472 473 474 475 476 477 478 479 480 481 482 483
#pragma mark -移除筛选框
- (void)DismissScreenView
{
    [UIView animateWithDuration:0.2 animations:^{
        
        self.screenView.alpha = 0;
        
    }completion:^(BOOL finished) {
        
        [self.screenView removeFromSuperview];
        
    }];
}
曹云霄's avatar
曹云霄 committed
484

曹云霄's avatar
曹云霄 committed
485

曹云霄's avatar
曹云霄 committed
486 487 488 489 490 491 492 493 494 495
#pragma mark -UIGestureRecognizerDelegate代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    //取消子视图的的时间穿透,否则子视图的点击无效,会被传递到父视图响应
    if (CGRectContainsPoint(self.screenView.backgroundView.frame, [touch locationInView:self.screenView])) {
        
        return NO;
    }
    return YES;
}
曹云霄's avatar
曹云霄 committed
496 497


勾芒's avatar
勾芒 committed
498 499 500 501 502 503 504 505 506 507
#pragma mark -释放
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}





曹云霄's avatar
曹云霄 committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end