// // DetailViewController.m // RedAnt ERP Mobile // // Created by Ray on 7/25/15. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved. // #import "DetailViewController.h" #import "DetailHeaderCell.h" #import "DetailImageCell.h" #import "iSalesNetwork.h" #import "RTLabel.h" #import "DetailKVCell.h" #import "LineView.h" //#import "SelectorViewController.h" #import "RAUtils.h" #import "MainViewController.h" #import "QRCodeGenerator.h" #define DEF_CELL_HEIGHT 44 #define DEF_TABLE_HEIGHT 44 #define LINE_WIDTH 0 #define CELL_MARGIN 0 #define LABEL_MARGIN 10 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) #define NavColor ([UIColor colorWithRed:237/255.0 green:20/255.0f blue:91/255.0f alpha:1.0f]) @interface DetailViewController () @property (nonatomic,strong) UIBezierPath *path; @end @implementation DetailViewController { CALayer *_layer; } -(void) reload_container_getdata:(bool) update_data { [super reload_container_getdata:update_data]; if(update_data) [self reload]; else { [self.detailTable reloadData]; // [self refresh_total]; } } -(void)manually_refresh { UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:300]; if(self.isrefreshing) { [reF endRefreshing]; return; } reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"]; if ([self respondsToSelector:@selector(ReloadData)]) [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1]; // DebugLog(@"refresh!!!!!!!!"); } -(void) ReloadData { UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:300]; [reF endRefreshing]; reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"]; [self reload]; } -(void) reload { dispatch_async(dispatch_get_main_queue(), ^{ if(self.isrefreshing) return; self.label_net_err.hidden=true; self.detailTable.hidden = true; self.isrefreshing=true; self.mum.center = self.view.center; self.mum.hidden = false; [self.mum startAnimating]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSDictionary* detail_json = [iSalesNetwork request_ItemDetail:self.product_id model_name:self.model_name category_id:self.category_id use_name:self.use_model_name]; dispatch_async(dispatch_get_main_queue(), ^{ [self.mum stopAnimating]; self.detail_data = [detail_json mutableCopy]; self.product_id = [NSString stringWithFormat:@"%d",[[detail_json valueForKey:@"product_id"] intValue] ]; NSDictionary* imgsection = [detail_json objectForKey:@"img_section"]; // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; if(/*appDelegate.user_type==USER_ROLE_CUSTOMER*/true) self.quantity =[[imgsection valueForKey:@"stockUom"] intValue]; else self.quantity =1; if(self.quantity==0) self.quantity=1; self.step=self.quantity; [self.detailTable reloadData]; if([[detail_json valueForKey:@"result"] intValue]==2) { self.detailTable.hidden = false; } else if([[detail_json valueForKey:@"result"] intValue]==RESULT_NET_ERROR) { self.detailTable.hidden = true; self.label_net_err.hidden=false; } else if([[detail_json valueForKey:@"result"] intValue]==RESULT_NO_MODEL) { self.detailTable.hidden = true; [RAUtils message_alert:[detail_json valueForKey:@"err_msg"] title:@"Model Detail"/*@"Loading Model Detail Failed."*/ controller:self] ; if(self.ispush) [ self.navigationController popViewControllerAnimated:true]; } else { [RAUtils message_alert:[detail_json valueForKey:@"err_msg"] title:@"Model Detail"/*@"Loading Model Detail Failed."*/ controller:self] ; } self.isrefreshing=false; }); }); }); } - (void)onBackClick:(UIButton *)sender { if(self.OnBack) { self.OnBack(self.add_cart,self.add_wish,self.update_data); } [self.navigationController popViewControllerAnimated:(false)]; // [self.navigationController dismissViewControllerAnimated:true completion:^{ // ; // }]; } - (void)viewDidLoad { [super viewDidLoad]; self.refresh_type = REFRESH_VIEW; UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector( onBackClick:)]; self.navigationItem.leftBarButtonItem = backButton; // self.detailTable.style = UITableViewStylePlain; self.edgesForExtendedLayout = UIRectEdgeNone; if(self.ispush) { UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.000001)]; [line setBackgroundColor:[UIColor clearColor]]; self.detailTable.tableHeaderView = line; } // // // self.automaticallyAdjustsScrollViewInsets = false; self.step=1; self.quantity=1; UIRefreshControl *ref = [[UIRefreshControl alloc]init]; ref.tag = 300 ; ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"]; ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7]; // ref.hidden = true; [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged]; [self.detailTable addSubview:ref]; self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor; self.label_net_err.layer.borderWidth = 2.0; self.label_net_err.layer.cornerRadius=15; self.label_net_err.layer.masksToBounds=true; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)]; // tap.minimumPressDuration = 0.8; //定义按的时间 [self.label_net_err addGestureRecognizer:tap]; // Do any additional setup after loading the view. } - (void)viewWillAppear:(BOOL)animated { if(self.ispush) { [super viewWillAppear:animated]; [[self navigationController] setNavigationBarHidden:NO animated:NO]; } } - (void) viewWillDisappear:(BOOL)animated { if(self.ispush) [[self navigationController] setNavigationBarHidden:YES animated:NO]; } - (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. } */ - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { //将page2设定成Storyboard Segue的目标UIViewController id page2 = segue.destinationViewController; //将值透过Storyboard Segue带给页面2的string变数 [page2 setValue: _selector forKey:@"selector"]; [page2 setValue: self forKey:@"selectordelegate"]; } - (IBAction)onShareClick:(id)sender { // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; // // [appDelegate test_onDecodedDataResult:@"371252"]; // [self onDecodedData:@"3712524-235"]; return; } -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { // [self.detailTable reloadData]; // return; [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; // NSMutableIndexSet *sectionToReload = [[NSMutableIndexSet alloc] init]; // int section_count =[[self.detail_data valueForKey:@"detail_section_count"] intValue]; // // for(int i=0;i 指向item [popover presentPopoverFromRect:rect1 inView:self.view permittedArrowDirections:0 animated:YES]; // [popover presentPopoverFromBarButtonItem:item permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; self.popover = popover; // [self performSegueWithIdentifier:@"selector_popover" sender:self]; */ } -(void) stepClicked:(int) value { self.quantity = value; } #pragma mark - Topic cell delegate -(void) TopicItemClicked:(NSString*) product_id category:(NSString*) category { DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ]; // dvc dvc.product_id=product_id; dvc.category_id=category; dvc.ispush=true; [dvc reload]; [self.navigationController pushViewController:dvc animated:true]; } #pragma mark - Table view data source //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section //{ // return 0; //} //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section //{ // return 0; //} //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section //{ // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)]; // return myView; // //} //- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section //{ // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)]; //// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7]; //// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)]; //// titleLabel.textColor=[UIColor whiteColor]; //// titleLabel.backgroundColor = [UIColor clearColor]; //// if(section==0) //// titleLabel.text=NSLocalizedString(@"display_items", nil); //// else //// titleLabel.text=NSLocalizedString(@"hide_items", nil); //// [titleLabel sizeToFit]; //// [myView addSubview:titleLabel]; //// // return myView; //} - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if(section==0) return nil; NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"]; if(related_json==nil) { NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)(section-1)]]; return [section_json valueForKey:@"title"] ; } else { if(section==1) return @"related model"; else { NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)section-2]]; return [section_json valueForKey:@"title"] ; } } } //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section //{ // return 33; //} -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section]; if (sectionTitle == nil) { return nil; } UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)]; myView.backgroundColor = UIColorFromRGB(0x996633); myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath; myView.layer.shadowColor = [UIColor blackColor].CGColor; myView.layer.shadowOffset = CGSizeMake(0, 0); myView.layer.shadowOpacity = 0.5; myView.layer.shadowRadius = 2.0; UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5.5, 90, 22)]; titlelabel.textColor=[UIColor whiteColor]; titlelabel.backgroundColor = [UIColor clearColor]; titlelabel.text=sectionTitle; [titlelabel sizeToFit]; [myView addSubview:titlelabel]; return myView; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { int section_count =[[self.detail_data valueForKey:@"detail_section_count"] intValue]; NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"]; if(related_json==nil) return section_count+1; else return section_count+2; } //- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath //{ // DebugLog(@"canMoveRowAtIndexPath"); // // if([tableView numberOfRowsInSection:indexPath.section]>1) // return true; // else // return false; //} //- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath //{ // NSMutableArray * from; // NSMutableArray * to ; // if(fromIndexPath.section==0) // from = self.displayfields; // else // from = self.hidefields; // if(toIndexPath.section==0) // to = self.displayfields; // else // to = self.hidefields; // id content=from[fromIndexPath.row]; // [from removeObjectAtIndex:fromIndexPath.row]; // [to insertObject:content atIndex:toIndexPath.row]; // // [tableView reloadData]; // //} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(section==0) return 1; NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"]; if(related_json==nil) { NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)section-1]]; NSString* type=[section_json valueForKey:@"type"] ; if([type isEqualToString:@"content"]) return 1; return [[section_json valueForKey:@"count"] intValue]; } else { if(section==1) return 1; else { NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)section-2]]; NSString* type=[section_json valueForKey:@"type"] ; if([type isEqualToString:@"content"]) return 1; return [[section_json valueForKey:@"count"] intValue]; } } } //- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath //{ // return UITableViewCellEditingStyleNone; //} //- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath //{ // //如果两次点击的时间间隔小于1秒,则断定为双击事件 // // if(tableView.editing==false) // return; // // // if([tableView numberOfRowsInSection:indexPath.section]<=1) // return; // // int between = 1; // NSUInteger curr = [[NSDate date] timeIntervalSince1970]; // // if(indexPath.row==self.lasttap.row&&indexPath.section==self.lasttap.section) // { // if (curr-self.taptime<1) { // DebugLog(@"double click!"); // self.lasttap =nil; // // NSMutableArray * from; // NSMutableArray * to ; // int section; // if(indexPath.section==0) // { // from = self.displayfields; // to = self.hidefields; // section = 1; // } // else // { // // from = self.hidefields; // to = self.displayfields; // section = 0; // } // NSIndexPath * toIndexPath = [NSIndexPath indexPathForRow:0 inSection:section]; // // NSIndexPath * headerIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section]; // // NSIndexPath * headerIndexPath1 = toIndexPath; // // id content=from[indexPath.row]; // [tableView beginUpdates]; // [from removeObjectAtIndex:indexPath.row]; // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; // // [to insertObject:content atIndex:0]; // [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:toIndexPath] withRowAnimation:UITableViewRowAnimationFade]; // [tableView endUpdates]; // // // [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:headerIndexPath] withRowAnimation:UITableViewRowAnimationFade]; // // [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:headerIndexPath1] withRowAnimation:UITableViewRowAnimationFade]; // [tableView reloadData]; // // // } // self.taptime = curr; // // // } // else // { // self.taptime = curr; // self.lasttap = indexPath; // } //} - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if(section ==0) return 0; else return 33; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; { if(indexPath.section ==0) return 550; NSDictionary* nsdic = self.detail_data; NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"]; if(related_json==nil) { NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)indexPath.section-1]]; NSDictionary* item_json =[section_json objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row ]]; NSString* type=[section_json valueForKey:@"type"] ; if([type isEqualToString:@"kv"]) { float width = tableView.frame.size.width; width-=CELL_MARGIN*2; CGSize constraintkey = CGSizeMake(width*0.4-2*LABEL_MARGIN, 20000.0f);//key label width is 40% cell width; CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width; // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row]; NSString* key =[item_json valueForKey:@"key"] ; // [cell.keyLabel sizeToFit]; NSString* val=[item_json valueForKey:@"val"] ; if([val isEqual:[NSNull null]]) val=@""; if(val==nil) val=@""; if([val isEqualToString:@"null"]) val=@""; CGRect frame; frame.size = constraintval; frame.origin.x=0; frame.origin.y=0; RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame]; [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping]; // rtlabel.lineSpacing = 20.0; [rtlabel setText: val]; CGSize sizeval=rtlabel.optimumSize; CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping]; // CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping]; float height = MAX(sizekey.height,sizeval.height); height = MAX(height+LINE_WIDTH, DEF_CELL_HEIGHT); return height; } else if([type isEqualToString:@"img"]) { int img_count = [[item_json valueForKey:@"count"] intValue]; double ret = 0; for(int j=0;j0) { cell.cqyt_label.hidden=false; cell.cqyt_label.text=[NSString stringWithFormat:@"Model QTY in Cart:%d",cqty]; } else { cell.cqyt_label.hidden=true; cell.cqyt_label.text=nil; } cell.descriptionRTLabel.text =[NSString stringWithFormat:@"%@\n\n",[section_json valueForKey:@"model_descrition"]]; // cell.descriptionLabel.text =[NSString stringWithFormat:@"%@\n\n",[section_json valueForKey:@"model_descrition"]]; cell.model_label.text =[section_json valueForKey:@"model_name"]; // UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:80]; // cell.qrcode_imageview.image = qrimg; if(appDelegate.bLogin==true) { if(appDelegate.can_see_price&&appDelegate.price_hidden==false) { } else { price=nil; } } cell.priceLabel.text =price; // cell.shipLabel.text = [section_json valueForKey:@"ship"]; cell.count_Label.text =[section_json valueForKey:@"Availability"] ; NSString* eta =[section_json valueForKey:@"ETA"] ; if(eta.length>0) { cell.eta_label.hidden = false; cell.etaval_label.text = eta; cell.etaval_label.hidden = false; cell.incoming_stock_label.hidden=false; cell.incomingcount_Label.text =[section_json valueForKey:@"incoming_stock"] ; cell.incomingcount_Label.hidden = false; } else { cell.eta_label.hidden = true; cell.etaval_label.text = nil; cell.etaval_label.hidden = true; cell.incoming_stock_label.hidden = true; cell.incomingcount_Label.text = nil; cell.incomingcount_Label.hidden = true; } self.selector = [section_json objectForKey:@"selector"]; cell.selector_nameLabel.text =[self.selector valueForKey:@"name"]; int selector_count = [[self.selector valueForKey:@"count"] intValue]; cell.selector_imageView.image = [UIImage imageNamed:@"loading_s"]; for(int i=0;i