// // PDFListViewController.m // RedAnt ERP Mobile // // Created by Ray on 11/23/15. // Copyright © 2015 United Software Applications, Inc. All rights reserved. // #import "PDFListViewController.h" #import "PDFListTableViewCell.h" #import "RANetwork.h" #import "RAPDFViewController.h" #import "MainViewController.h" #import "DefaultAppearance.h" #import "DefaultTableHeaderView.h" #import "SortItemViewController.h" #import "UIColor+JK_HEX.h" #import "TextUtils.h" #import "RAConvertor.h" @interface PDFListViewController () @end @implementation PDFListViewController -(void) logout { self.offset = 0; self.limit = 25; [self.content_data removeAllObjects]; } - (void)viewDidLoad { [super viewDidLoad]; self.offset = 0; self.limit = 25; self.content_data = [[NSMutableArray alloc]init]; UIRefreshControl *ref = [[UIRefreshControl alloc]init]; ref.tag = 201 ; 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.table 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; CGPoint center = self.mum.center; self.mum.frame = CGRectMake(0, 0, 100, 100); self.mum.center = center; self.mum.backgroundColor = [UIColor colorWithHEX:0xA5A5A5 alpha:0.5]; self.mum.layer.cornerRadius = 5.0f; self.mum.layer.masksToBounds = YES; 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)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void) reload_container_getdata:(bool) update_data { [super reload_container_getdata:update_data]; if(update_data) { self.offset = 0; [self.content_data removeAllObjects]; self.norefresh=true; [self loadpage]; } else [self.table reloadData]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // if(!self.norefresh)//避免 多次刷新 // { // self.norefresh = true; // self.offset = 0; // [self.content_data removeAllObjects]; // [self loadpage]; // // } [[self navigationController] setNavigationBarHidden:YES animated:NO]; } -(void)manually_refresh { UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:201]; if(self.isrefreshing) { [reF endRefreshing]; return; } reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"]; [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1]; } -(void)ReloadData { [self.content_data removeAllObjects]; [self.table reloadData]; // self.table_order.hidden = true; UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:201]; [reF endRefreshing]; reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"]; self.keywords=self.searchbar.text; self.offset = 0; // [self.content_data removeAllObjects]; // [self.table_order reloadData]; [self loadpage]; // [self loadpage]; } -(void) loadpage { dispatch_async(dispatch_get_main_queue(), ^{ if(self.isrefreshing) return; self.label_net_err.hidden=true; self.isrefreshing=true; self.table.hidden =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), ^{ NSMutableDictionary* content=nil; content=[[RANetwork request_PDFList:self.offset limit : self.limit keywords:self.keywords] mutableCopy]; dispatch_async(dispatch_get_main_queue(), ^{ [self.mum stopAnimating]; int result=[[content valueForKey:@"result"] intValue]; if(result==2||result==0) { self.table.hidden =false; int count = [[content valueForKey:@"count" ] intValue] ; // self.time_zone = [content valueForKey:@"time_zone" ]; self.offset += count; for(int i=0;iself.content_data.count) // { // int debug=true; // } // DebugLog(@"row %ld",(long)indexPath.row); if(indexPath.row==self.content_data.count) { UITableViewCell *moreCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mores"]; UILabel *moreDataLabel=[[UILabel alloc] init]; moreDataLabel.tag=200; moreDataLabel.text=self.load_more_hint; [moreDataLabel setFont:[UIFont systemFontOfSize:14.0f]]; [moreDataLabel setTextAlignment:NSTextAlignmentCenter]; moreDataLabel.frame=CGRectMake(0, 10, self.table.bounds.size.width, 20); [moreDataLabel setBackgroundColor:[UIColor clearColor]]; // [moreDataLabel addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged]; [moreCell.contentView addSubview:moreDataLabel]; if([self.load_more_hint isEqualToString:@"Load more..."]) [self performSelector:@selector(loadmore) withObject:nil afterDelay:1]; // DebugLog(@"更多..."); moreCell.backgroundColor = [UIColor whiteColor]; return moreCell; } else { NSString *CellIdentifier = @"PDFListTableViewCell"; PDFListTableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // int _id= [[self.content_data[indexPath.row] valueForKey:@"_id"] intValue]; NSString* name= [self.content_data[indexPath.row] valueForKey:@"tear_name"]; NSString* note= [self.content_data[indexPath.row] valueForKey:@"tear_note"]; NSString* create_time= [self.content_data[indexPath.row] valueForKey:@"create_time"]; // NSString* type = [self.content_data[indexPath.row] valueForKey:@"type"] ; // NSString* file_name = [self.content_data[indexPath.row] valueForKey:@"file_name"]; cell.labelName.text = name; cell.labelTime.text = create_time; cell.labelNote.text = note; // cell.labelType.text = type; // cell.labelprice.text=price; // cell.label_date.text=purchase_time; // cell.label_contact.text=customer_name; // cell.label_model.text = model_count; // // cell.label_status.text=status; cell.backgroundColor = [UIColor whiteColor]; return cell; } // NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]]; // NSDictionary * combine_json =[item_json objectForKey:@"combine"]; // cell.bundle_item=combine_json; // NSString* img_url = [item_json valueForKey:@"img_url"]; // NSString* description = [item_json valueForKey:@"description"]; // // NSString* identifier = [item_json valueForKey:@"identifier"]; // // NSString* attribute = [item_json valueForKey:@"attribute"]; // NSString* currency = [item_json valueForKey:@"currency"]; // int count =[[item_json valueForKey:@"count"] intValue]; // double price =[[item_json valueForKey:@"price"] doubleValue]; // // cell.labelAttribute.text = attribute; // cell.labelCurrency.text = currency; // cell.labelDescription.text = description; // // cell.labelIdentifier.text = identifier; // cell.labelPrice.text = [NSString stringWithFormat:@"%.2f",price]; // // // // [cell set_Count:count]; // NSString* file_name=[img_url lastPathComponent]; // NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url]; // if(img_data!=nil) // { // // UIImage * img =[UIImage imageWithData:img_data]; // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal]; // } // else // { // // 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) // { // [iSalesDB cache_img:downloadimg_data :file_name ]; // // UIImage * img =[UIImage imageWithData:downloadimg_data]; // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal]; // } // // }); // }); // // // } // } // else // { // NSString *CellIdentifier = @"OrderInfoListItem"; // UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // return cell; // } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(self.content_data.count== indexPath.row) return; NSString* name=[self.content_data[indexPath.row] valueForKey:@"tear_name"]; NSString* url= [self.content_data[indexPath.row] valueForKey:@"pdf_path"]; if(url==nil) { NSMutableDictionary* params = [[RAConvertor string2dict:[self.content_data[indexPath.row] valueForKey:@"off_params"]] mutableCopy]; NSMutableDictionary* values = [[RAConvertor string2dict:[self.content_data[indexPath.row] valueForKey:@"model_info"]] mutableCopy]; params[@"replaceValue"]=values; params[@"tearsheetsId"]=[self.content_data[indexPath.row] valueForKey:@"tearsheetsId"]; NSString* createpdf = [RANetwork create_portfolio:params]; if(createpdf!=nil) url=createpdf; // if() } BOOL isLocalFile= [[self.content_data[indexPath.row] valueForKey:@"isLocalFile"] boolValue]; RAPDFViewController *ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"RAPDFViewController"]; ViewController.url = url; ViewController.canSave = false; ViewController.isLocalfile=isLocalFile; NSString* subject; // if (company.length==0) { // NSString* cur_time =[RAUtils current_date]; subject =name;//[NSString stringWithFormat:@"NPD Product List %@",cur_time]; AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; NSString* customer_email= [appDelegate.customerInfo valueForKey:@"customer_email"]; NSMutableArray* send_to = [[NSMutableArray alloc]init]; if(customer_email.length>0) { send_to=[[customer_email componentsSeparatedByString:NSLocalizedString(@";", nil)] mutableCopy]; // customer_email compo // [send_to addObject:customer_email]; } ViewController.mail_to = send_to; // } // else // { // subject = [NSString stringWithFormat:@"Tear sheet for %@",company ]; // } NSString* lfilename = [TextUtils legalFilename:name]; // ViewController.attachment_name = [NSString stringWithFormat:@"%@.pdf",lfilename]; ViewController.filename = [NSString stringWithFormat:@"%@.pdf",lfilename]; ViewController.mail_subject = subject; ViewController.hidenavi = false; [self.navigationController pushViewController:ViewController animated:YES]; //[self.navigationController setNavigationBarHidden:NO animated:NO]; } #pragma mark - searchBar delegate; - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { self.keywords=searchBar.text; self.offset = 0; [self.content_data removeAllObjects]; [self loadpage]; } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if([searchText isEqualToString:@""] && !self.reset) { DebugLog(@"clear"); self.reset=true; self.keywords=nil; self.offset = 0; [self.content_data removeAllObjects]; [self loadpage]; } else self.reset = false; } #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { NSString* listid= [self.content_data[indexPath.row] valueForKey:@"tearsheetsId"]; UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { DebugLog(@"delete click"); // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Delete confirm", nil) message:NSLocalizedString(@"Are you sure to remove portfolio?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil]; // // alert.tag = ALERT_DEL; // // alert. // [alert show]; // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure to remove this portfolio?" message:nil preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Remove Portfilio"]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSDictionary* cart_json = [RANetwork delete_portfoliolist:listid]; dispatch_async(dispatch_get_main_queue(), ^{ [waitalert dismissWithClickedButtonIndex:0 animated:FALSE]; if([[cart_json valueForKey:@"result"] intValue]==2) { [self ReloadData]; } else { [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Portfolio" controller:self] ; } }); }); }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { DebugLog(@"No"); }]; [alertControl addAction:actionOne]; [alertControl addAction:alertthree]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [self presentViewController:alertControl animated:YES completion:nil]; }]; deleteRowAction.backgroundColor = UIColorFromRGB(0x336699); NSString* create_user= [self.content_data[indexPath.row] valueForKey:@"create_user"]; AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; if([create_user isEqualToString:appDelegate.user]) { return @[deleteRowAction]; } return @[]; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { } - (void)reRefreshView { [self.table reloadData]; } #pragma mark - RA_NOTIFICAITON -(void) refresh_ui { [self.table reloadData]; } -(void) reload_data { self.offset = 0; [self.content_data removeAllObjects]; [self.table reloadData]; self.norefresh=true; [self loadpage]; } @end