// // SearchViewController.m // iShop // // Created by Rui Zhang on 1/24/24. // #import "SearchViewController.h" #import "RAUtils.h" #import "RANetwork.h" #import "CatalogCellSmall.h" #import "const.h" #import "FileCache.h" #import "CatalogModelDetailViewController.h" #define PAGE_ITEM 30 @interface SearchViewController () @property (nonatomic,strong) NSOperationQueue *dataOperationQueue; @end @implementation SearchViewController - (void)viewDidLoad { [super viewDidLoad]; self.searchBar.backgroundImage = [UIImage new]; // Do any additional setup after loading the view. } /* #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. } */ - (NSOperationQueue *)dataOperationQueue { if (!_dataOperationQueue) { _dataOperationQueue = [[NSOperationQueue alloc] init]; _dataOperationQueue.maxConcurrentOperationCount = 1; } return _dataOperationQueue; } -(void) operation_loadMore { if (self.dataOperationQueue.operationCount > 1) { return; } dispatch_async(dispatch_get_main_queue(), ^{ if(self.loadall) { if(self.showalert) { self.showalert=false; [RAUtils message_box:@"Loading items" message:@"All loaded" completion:nil]; } return; } self.mum.hidden = false; [self.mum startAnimating]; self.mum.center = self.view.center; self.isrefreshing=true; [RANetwork request_search:self.offset limit:PAGE_ITEM keywords:self.keywords matchfull:self.loadmore_matchfull completionHandler:^(NSMutableDictionary *result) { NSMutableDictionary* category_more=result; [self.mum stopAnimating]; self.isrefreshing=false; if (self.dataOperationQueue.operationCount > 1) { return; } NSDictionary* more_items=[category_more objectForKey:@"items"]; if([[category_more valueForKey:@"result"] intValue]==2) { // self.category_data = category_data; NSMutableDictionary* items_json= [[self.category_data objectForKey:@"items"] mutableCopy]; int count =[[more_items valueForKey:@"count"] intValue]; for(int i=0;i 1) { return; } dispatch_async(dispatch_get_main_queue(), ^{ if(self.isrefreshing) return; self.showalert=true; self.isrefreshing=true; self.loadall = false; UIRefreshControl *reF = (UIRefreshControl *)[self.collectionview viewWithTag:200]; [reF endRefreshing]; reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"]; self.label_net_err.hidden=true; self.collectionview.hidden=true; self.mum.center = self.view.center; self.mum.hidden = false; [self.mum startAnimating]; self.loadmore_matchfull = false;//self.switch_matchfull.isOn; [RANetwork request_search:0 limit:PAGE_ITEM keywords:self.keywords matchfull:self.loadmore_matchfull completionHandler:^(NSMutableDictionary *result) { NSMutableDictionary* category_data=result; [self.mum stopAnimating]; self.isrefreshing=false; if (self.dataOperationQueue.operationCount > 1) { return; } self.category_data = [category_data mutableCopy]; [self.collectionview reloadData]; if([[category_data valueForKey:@"result"] intValue]==2) { self.collectionview.hidden=false; NSDictionary * item_json = [self.category_data objectForKey:@"items"]; self.offset =[[item_json valueForKey:@"count"] intValue]; int total = [[self.category_data valueForKey:@"item_total_count"]intValue]; } else if([[category_data valueForKey:@"result"] intValue]==RESULT_NET_ERROR) { self.collectionview.hidden=true; self.label_net_err.hidden=false; } else { [RAUtils message_box:@"Search" message:[category_data valueForKey:@"err_msg"] completion:nil]; } self.isrefreshing=false; }]; return; }); } -(void) reload { __weak typeof(self) weakself = self; NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ if (weakself) { __strong typeof(weakself) strongself = weakself; [strongself operation_reload]; } }]; [self.dataOperationQueue addOperation:operation]; } - (IBAction)onChangeDisplay:(UIButton *)sender { UICollectionViewFlowLayout *flowLayout =(UICollectionViewFlowLayout*)[self.collectionview collectionViewLayout]; flowLayout.scrollDirection= UICollectionViewScrollDirectionVertical; if(sender.tag==2&&self.categoryViewType != CATEGORY_VIEWTYPE_SMALL) { self.categoryViewType = CATEGORY_VIEWTYPE_SMALL; // [self.collectionview.collectionViewLayout invalidateLayout]; // DebugLog(@"frame %f",self.collectionview.frame.size.height); // DebugLog(@"offset %f",self.self.collectionview.contentOffset.y); // DebugLog(@"content %f",self.collectionview.contentSize.height); // float pos =self.collectionview.contentOffset.y/self.collectionview.contentSize.height; [self.collectionview reloadData]; [self.btnDisplay setImage:[UIImage imageNamed:@"24_layout_list"] forState:UIControlStateNormal]; sender.tag=4; // CGPoint offset = self.collectionview.contentOffset; // offset.y=pos*self.collectionview.contentSize.height; // self.collectionview.contentOffset=offset; } else if(sender.tag==4&&self.categoryViewType != CATEGORY_VIEWTYPE_LIST) { self.categoryViewType = CATEGORY_VIEWTYPE_LIST; // DebugLog(@"frame %f",self.collectionview.frame.size.height); // DebugLog(@"offset %f",self.self.collectionview.contentOffset.y); // DebugLog(@"content %f",self.collectionview.contentSize.height); // float pos =self.collectionview.contentOffset.y/self.collectionview.contentSize.height; // [self.collectionview reloadData]; // [self.collectionview.collectionViewLayout invalidateLayout]; [self.collectionview reloadData]; [self.btnDisplay setImage:[UIImage imageNamed:@"24_layout_grid"] forState:UIControlStateNormal]; sender.tag=2; // CGPoint offset = self.collectionview.contentOffset; // offset.y=pos*self.collectionview.contentSize.height; // self.collectionview.contentOffset=offset; // // self.collectionview move } [self showCategory]; // [self showCategory]; } -(void) showCategory { if(self.showDetail==false) return; // UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation; // int width=175; // // int height; // if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight) // { // width = 370; // // height = 400; // } // else // { // // height = 300; // width = 175; // } // CGRect frame = CGRectMake(0, 48, self.view.bounds.size.width , self.view.bounds.size.height-48); [UIView animateWithDuration:0.5 animations:^{ self.collectionview.frame=frame; }]; self.collectionview.autoresizingMask = self.collectionview.autoresizingMask | UIViewAutoresizingFlexibleWidth; self.showDetail = false; // self.cagegoryLabel.hidden = false; } #pragma mark - searchBar delegate; - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { self.keywords=searchBar.text; self.offset = 0; // [self.content_data removeAllObjects]; [self reload]; DebugLog(@"search"); [self.view endEditing:true]; // [self.searchBar.searchTextField endEditing:true]; } - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { self.maskView.hidden =false; } - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { self.maskView.hidden =true; } // - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText // { // // if(searchText.length == 0) // [self.view endEditing:true]; // // } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[self view] endEditing:YES]; } //- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar //{ // //} #pragma mark -- UICollectionViewDataSource -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { // 每个Section的item个数 NSDictionary * item_json = [self.category_data objectForKey:@"items"]; int count =[[item_json valueForKey:@"count"] intValue]; return count; } -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { // NSString* value =[DefaultAppearance get_noneappearance_value:@"CategoryViewController" valuename:@"cell_border_color"]; // // if(value==nil) // value=@""; // unsigned long color = strtoul([value UTF8String],0,16); if(self.categoryViewType == CATEGORY_VIEWTYPE_SMALL) { static NSString * CellIdentifier = @"CatalogCellSmall"; CatalogCellSmall * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; NSDictionary * item_json =[[self.category_data objectForKey:@"items" ] objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]]; NSString* description =[item_json valueForKey:@"description"]; NSString* name =[item_json valueForKey:@"fash_name"]; // bool wish_exist = [[item_json valueForKey:@"wish_exists"]boolValue]; // bool cart_exists = [[item_json valueForKey:@"cart_exists"]boolValue]; bool closeout = [[item_json valueForKey:@"is_closeout"]boolValue]; // BOOL more_color = [[item_json valueForKey:@"more_color"] boolValue]; // cell.mark_moreColor.hidden = !more_color; // if(wish_exist) // cell.mark_wish.hidden=false; // else // cell.mark_wish.hidden=true; // if(cart_exists) // cell.mark_order.hidden=false; // else // cell.mark_order.hidden=true; // if(closeout) // cell.mark_closeout.hidden=false; // else // cell.mark_closeout.hidden=true; // NSString* old_price =[item_json valueForKey:@"old_price"]; // NSString* price =[item_json valueForKey:@"price"]; if(closeout) cell.ic_closeout.hidden=false; else cell.ic_closeout.hidden=true; bool checked =[[item_json valueForKey:@"checked"] boolValue]; if(!checked) { // cell.checkImageView.hidden = true; [cell setBackgroundColor:[UIColor whiteColor]/* UIColorFromRGB(0x996633)*/]; } else { // cell.checkImageView.hidden = false; [cell setBackgroundColor: UIColorFromRGB(0x009966)]; } cell.nameLabel.text = description; cell.modelNoLabel.text=name; // cell.cellDescription.text = description; // cell.oldPrice.text = old_price; // cell.Price.text = price; // cell.layer.borderColor = UIColorFromRGB(color).CGColor; cell.layer.borderWidth = 0.4; cell.layer.cornerRadius=15; cell.layer.masksToBounds=true; #ifdef BUILD_UWAVER cell.mark_wish.hidden = YES; cell.mark_order.hidden = YES; cell.mark_closeout.hidden = YES; cell.mark_moreColor.hidden = YES; #endif return cell; } else { static NSString * CellIdentifier = @"CatalogCellList"; CatalogCellSmall * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; NSDictionary * item_json =[[self.category_data objectForKey:@"items" ] objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]]; NSString* description =[item_json valueForKey:@"description"]; NSString* name =[item_json valueForKey:@"fash_name"]; // bool wish_exist = [[item_json valueForKey:@"wish_exists"]boolValue]; // bool cart_exists = [[item_json valueForKey:@"cart_exists"]boolValue]; bool closeout = [[item_json valueForKey:@"is_closeout"]boolValue]; // BOOL more_color = [[item_json valueForKey:@"more_color"] boolValue]; if(closeout) cell.ic_closeout.hidden=false; else cell.ic_closeout.hidden=true; // NSString* old_price =[item_json valueForKey:@"old_price"]; // NSString* price =[item_json valueForKey:@"price"]; // bool checked =[[item_json valueForKey:@"checked"] boolValue]; cell.nameLabel.text = description; cell.modelNoLabel.text=name; // cell.cellDescription.text = description; // cell.oldPrice.text = old_price; // cell.Price.text = price; // cell.layer.borderColor = UIColorFromRGB(color).CGColor; cell.layer.borderWidth = 0.2; //cell.layer.cornerRadius=15; cell.layer.masksToBounds=true; #ifdef BUILD_UWAVER cell.mark_wish.hidden = YES; cell.mark_order.hidden = YES; cell.mark_closeout.hidden = YES; cell.mark_moreColor.hidden = YES; #endif return cell; } } - (void)collectionCell:(CatalogCellSmall *)cell setImageForIndexPath:(NSIndexPath *)indexPath { NSDictionary * item_json =[[self.category_data objectForKey:@"items" ] objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]]; NSString* img_url =[item_json valueForKey:@"img"]; NSString *loading = @"loading_l"; NSString *notFound = @"notfound_l"; if (self.categoryViewType == CATEGORY_VIEWTYPE_LARGE) { loading = @"loading_l"; notFound = @"notfound_l"; img_url =[item_json valueForKey:@"img_big"]; // large 大图 } else if (self.categoryViewType == CATEGORY_VIEWTYPE_SMALL) { loading = @"loading_s"; notFound = @"notfound_s"; img_url =[item_json valueForKey:@"img"]; // large 大图 } NSString* file_name=[img_url lastPathComponent]; if (![cell.imageName isEqualToString:img_url]) { cell.imageName = img_url; cell.cellImageView.image = [UIImage imageNamed:loading]; NSData* img_data=[FileCache load_cached_img:file_name loadFrom:img_url]; if(img_data!=nil) { UIImage * img =[UIImage imageWithData:img_data]; cell.cellImageView.image = img; } else { DebugLog(@"load image"); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]]; dispatch_async(dispatch_get_main_queue(), ^{ if(downloadimg_data!=nil) { [FileCache cache_img:downloadimg_data filename:file_name saveTo:img_url]; UIImage * img =[UIImage imageWithData:downloadimg_data]; cell.cellImageView.image = img; } else cell.cellImageView.image = [UIImage imageNamed:notFound]; }); }); } } } - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath { CatalogCellSmall * son_cell = (CatalogCellSmall *)cell; son_cell.imageName = nil; [self collectionCell:son_cell setImageForIndexPath:indexPath]; } - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath { CatalogCellSmall * son_cell = (CatalogCellSmall *)cell; NSString *loading = @"loading_l"; if (self.categoryViewType == CATEGORY_VIEWTYPE_LARGE) { loading = @"loading_l"; } else if (self.categoryViewType == CATEGORY_VIEWTYPE_SMALL) { loading = @"loading_s"; } son_cell.cellImageView.image = [UIImage imageNamed:loading]; } #pragma mark --UICollectionViewDelegateFlowLayout ////定义每个UICollectionView 的大小 //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath //{ // return CGSizeMake(96, 100); //} //定义每个UICollectionView 的 margin - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if(self.categoryViewType ==CATEGORY_VIEWTYPE_LIST) return CGSizeMake(self.view.frame.size.width, 80); return CGSizeMake((self.view.frame.size.width-30)/2, 251); } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { if(self.categoryViewType ==CATEGORY_VIEWTYPE_LIST) return 0; return 10; } -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { if(self.showDetail) return UIEdgeInsetsMake(10, 5, 10, 5); if(self.categoryViewType ==CATEGORY_VIEWTYPE_LARGE) return UIEdgeInsetsMake(10, 10, 10, 10); else if(self.categoryViewType ==CATEGORY_VIEWTYPE_LIST) return UIEdgeInsetsMake(10, 0, 10, 0); return UIEdgeInsetsMake(10, 10, 10, 10); return UIEdgeInsetsMake(10, 10, 10, 10); } #pragma mark --UICollectionViewDelegate -(void) showDetailat:(NSString*) item_id category_id:(NSString*) category_id name:(NSString *)name index:(long)index { CatalogModelDetailViewController* dvc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CatalogModelDetailViewController" ]; dvc.OnBack=^(bool add_cart,bool add_wish,bool update_data) { NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy]; NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%ld",index]] mutableCopy]; if(add_cart) item[@"cart_exists"]=@"true"; if(add_wish) item[@"wish_exists"]=@"true"; items[[NSString stringWithFormat:@"item_%ld",index]] = item; self.category_data[@"items"]=items; [self.collectionview reloadData]; }; dvc.product_id = item_id; dvc.category_id=category_id; dvc.ispush = true; dvc.model_name = name; [dvc reload]; [self.navigationController pushViewController:dvc animated:true]; return; } //UICollectionView被选中时调用的方法 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy]; NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy]; // if(self.isSelectionMode) // { // bool checked =[[item valueForKey:@"checked"] boolValue]; // // CategoryCellNPD *currentSelecteCell = (CategoryCellNPD *)[self.collectionview cellForItemAtIndexPath:indexPath]; // // // if(checked) // { // self.select_count--; // if(self.select_count==0) // { // // self.addWishBtn.enabled = false; // // self.addCartBtn.enabled = false; // // self.addPortfolioBtn.enabled=false; // } // [item setValue:@"false" forKey:@"checked"]; // // currentSelecteCell.checkImageView.hidden = true; // if(self.categoryViewType != CATEGORY_VIEWTYPE_LIST) // [currentSelecteCell setBackgroundColor: [UIColor whiteColor]]; // else // [currentSelecteCell setBackgroundColor: [UIColor whiteColor]]; // } // else // { // self.select_count++; // // self.addWishBtn.enabled = true; // // self.addCartBtn.enabled = true; // // self.addPortfolioBtn.enabled=true; // [item setValue:@"true" forKey:@"checked"]; // currentSelecteCell.checkImageView.hidden = false; // [currentSelecteCell setBackgroundColor: UIColorFromRGB(0x009966)]; // } // [items setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]]; // [self.category_data setObject:items forKey:@"items"]; // // // } // else { NSString* detail_id = [NSString stringWithFormat:@"%@",[item valueForKey:@"product_id"]]; [self showDetailat:detail_id category_id:nil name:[item objectForKey:@"fash_name"] index:indexPath.row]; } } @end