| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510 |
- //
- // DocumentTemplateViewController.m
- // Granite Expo eSign
- //
- // Created by Ray on 23/09/2017.
- // Copyright © 2017 United Software Applications, Inc. All rights reserved.
- //
- #import "DocumentTemplateViewController.h"
- #import "TemplateViewController.h"
- #import "SignatureListViewController.h"
- #import "SignatureViewController.h"
- #import "PDFUtils.h"
- #import "RANetwork.h"
- //#import "AFHTTPSessionManager.h"
- #import "AppDelegate.h"
- #import "RAUtils.h"
- //#import "RAPDFViewController.h"
- #import "config.h"
- #import "AESCrypt.h"
- #import "CustomIOSAlertView.h"
- #import "ImageUtils.h"
- #import "RAPDFPage.h"
- #import "TextDrawable.h"
- #import "ImageDrawable.h"
- #import "RectDrawable.h"
- #import "ItemTableDataSourceAndDelegate.h"
- #import "AttributeTableDataSourceAndDelegate.h"
- #import "ControlTemplateVC.h"
- @interface DocumentTemplateViewController () <TemplateUpdateControlFrame,TemplateSelectionDelegate>
- @property (strong, nonatomic) IBOutlet UIView *positionControlBackground;
- @property (strong, nonatomic) IBOutlet UIStepper *xStepper;
- @property (strong, nonatomic) IBOutlet UIStepper *yStepper;
- @property (strong, nonatomic) IBOutlet UIStepper *wStepper;
- @property (strong, nonatomic) IBOutlet UIStepper *hStepper;
- @property (strong, nonatomic) IBOutlet UITextField *xTextField;
- @property (strong, nonatomic) IBOutlet UITextField *yTextField;
- @property (strong, nonatomic) IBOutlet UITextField *wTextField;
- @property (strong, nonatomic) IBOutlet UITextField *hTextField;
- @property (weak, nonatomic) IBOutlet UITableView *itemTableView;
- @property (weak, nonatomic) IBOutlet UITableView *attributeTableView;
- @property (nonatomic,strong) ItemTableDataSourceAndDelegate *itemTableDataSource;
- @property (nonatomic,strong) AttributeTableDataSourceAndDelegate *attrTableDataSource;
- @property (nonatomic,weak) UIView *focusView;
- @property (nonatomic,strong) UIColor *focusViewNormalColor;
- @property (nonatomic,strong) TemplateViewController *currentPageViewController;
- @end
- @implementation DocumentTemplateViewController
- - (IBAction)onLeftButtonClick:(id)sender {
-
-
- // - (NSUInteger) indexOfViewController:(PageViewController *)viewController
-
- UIViewController* pvc = [self pageViewController:self.pageViewController viewControllerBeforeViewController:self.pageViewController.viewControllers[0]];
-
- if(pvc==nil)
- return;
-
-
- __weak typeof(self) weakSelf = self;
- [self.pageViewController setViewControllers:@[pvc] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:^(BOOL finished){
-
- long index=[weakSelf indexOfViewController:(TemplateViewController*)pvc];
- if(index==0)
- weakSelf.left_button.hidden=true;
- else
- weakSelf.left_button.hidden=false;
-
- if(index==weakSelf.total_page-1)
- weakSelf.right_button.hidden=true;
- else
- weakSelf.right_button.hidden=false;
- }];
-
- // self.pageViewController
- }
- - (IBAction)onRightButtonClick:(id)sender {
-
- UIViewController* pvc = [self pageViewController:self.pageViewController viewControllerAfterViewController:self.pageViewController.viewControllers[0]];
-
- if(pvc==nil)
- return;
-
-
- __weak typeof(self) weakSelf = self;
- [self.pageViewController setViewControllers:@[pvc] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:^(BOOL finished){
-
- long index=[weakSelf indexOfViewController:(TemplateViewController*)pvc];
- if(index==0)
- weakSelf.left_button.hidden=true;
- else
- weakSelf.left_button.hidden=false;
-
- if(index==weakSelf.total_page-1)
- weakSelf.right_button.hidden=true;
- else
- weakSelf.right_button.hidden=false;
- }];
-
- }
- -(NSMutableDictionary*) templateInit:(NSMutableDictionary*) template
- {
- AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
-
- NSString* temp = NSTemporaryDirectory();
- NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
- NSString* qrpath=[temp stringByAppendingPathComponent:filename];
- UIImage* qrimg=[ImageUtils generateBarCode:self.docment_id width:300 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
-
-
- bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
-
-
-
- for(int i=0;i<[template[@"count"] intValue];i++)
- {
- NSMutableDictionary* page =template[[NSString stringWithFormat:@"page_%d",i]];
- for(int j=0;j<[page[@"count"] intValue];j++)
- {
- NSMutableDictionary* control =page[[NSString stringWithFormat:@"control_%d",j]];
- NSString* type = control[@"type"];
- NSString* field =control[@"field"];
- bool disable = [control[@"disable"] boolValue];
- control[@"dirty"]=[NSNumber numberWithBool:false];
- // if([type isEqualToString:@"Label"])
- // {
- // if([field isEqualToString:@"doc_number"])
- // {
- // control[@"value"]= self.docment_id;
- // }
- // }
- // else if([type isEqualToString:@"Image"])
- // {
- // NSString* name =control[@"name"];
- // if([name isEqualToString:@"BarCode"])
- // {
- // if(bwrite)
- // control[@"value"]= qrpath;
- // }
- // }
- // else if([type isEqualToString:@"DatePicker"])
- // {
- // NSString* name =control[@"aname"];
- // if([name isEqualToString:@"Today's Date"]||[name isEqualToString:@"Handwrite date"])
- // {
- // NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
- // [formatter setDateFormat:@"MM/dd/yyyy"];
- // NSString* date = [formatter stringFromDate:[NSDate date]] ;
- //
- // control[@"value"]= date;
- // }
- // }
- // else if([type isEqualToString:@"Signature"]&&disable)
- // {
- // control[@"value"]= appDelegate.na_path;
- //
- // }
- // else if([type isEqualToString:@"TextView"])
- // {
- // NSString* name =control[@"aname"];
- // if([name isEqualToString:@"Agent Name"]||[name isEqualToString:@"Sales Person"])
- // {
- // control[@"value"]= appDelegate.agentName;
- // }
- //
- //
- //
- // }
- }
- }
- return template;
- }
- -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
- {
-
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.title = self.docment_id;
-
- if(self.hide_control)
- [self.ibSave setTitle:@"Upload"];
- else
- [self.ibSave setTitle:@"Save"];
-
-
-
- // self.delegate = self;
- // self.dataSource = self;
-
- // 修复初始化时视图向下偏移64点
- UIView *v = [[UIView alloc] init];
- [self.view insertSubview:v atIndex:0];
-
- self.signatureData= [[NSMutableDictionary alloc] init];
-
-
- self.document = [PDFUtils OpenPDF:self.pdfPath];
- self.total_page = CGPDFDocumentGetNumberOfPages (self.document);
- self.controlTemplate = [self templateInit:[PDFUtils loadControl:self.templatePath]];
-
-
- // self.title = [NSString stringWithFormat:@"%@ [%d/%ld]", self.docment_id,1, self.total_page ];
-
-
- TemplateViewController* startingViewController=[self viewControllerAtIndex:0];
-
- // NSArray* viewControllers =[NSArray arrayWithObjects:page_0,nil];
-
- self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
- self.pageViewController.delegate = self;
-
- // PageViewController *startingViewController = [self viewControllerAtIndex:0 storyboard:self.storyboard];
- NSArray *viewControllers = @[startingViewController];
-
-
-
- [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
-
- self.pageViewController.dataSource = self;
-
- [self addChildViewController:self.pageViewController];
- [self.view addSubview:self.pageViewController.view];
-
- // Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
- CGRect pageViewRect = self.view.bounds;
- if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
- pageViewRect = pageViewRect;//CGRectInset(pageViewRect, 40.0, 40.0);
- }
- self.pageViewController.view.frame = pageViewRect;
-
- [self.pageViewController didMoveToParentViewController:self];
-
- self.left_button.hidden=true;
-
- self.right_button.hidden=false;
-
-
- [self.view bringSubviewToFront:self.left_button];
- [self.view bringSubviewToFront:self.right_button];
- // Do any additional setup after loading the view.
-
- [self.view bringSubviewToFront:self.positionControlBackground];
-
-
- // Template
- // self.itemTableDataSource = [[ItemTableDataSourceAndDelegate alloc] initWithTableView:self.itemTableView Controller:self];
- // self.attrTableDataSource = [[AttributeTableDataSourceAndDelegate alloc] initWithTableView:self.attributeTableView Controller:self];
- }
- - (ItemTableDataSourceAndDelegate *)itemTableDataSource {
- if (_itemTableDataSource == nil) {
- _itemTableDataSource = [[ItemTableDataSourceAndDelegate alloc] initWithTableView:self.itemTableView Controller:self];
- }
- return _itemTableDataSource;
- }
- - (AttributeTableDataSourceAndDelegate *)attrTableDataSource {
- if (_attrTableDataSource == nil) {
- _attrTableDataSource = [[AttributeTableDataSourceAndDelegate alloc] initWithTableView:self.attributeTableView Controller:self];
- }
- return _attrTableDataSource;
- }
- -(void) dealloc
- {
- CFRelease(self.document);
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (IBAction)OnCancelClicked:(id)sender {
-
-
- if(self.no_save|| self.hide_control)
- {
- [self.navigationController popViewControllerAnimated:false];
- return;
- }
- UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure to cancel the customer ? You will loose all the information you entered." message:nil preferredStyle:UIAlertControllerStyleAlert];
-
- UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
- [self.navigationController popViewControllerAnimated:false];
- }];
- // [alertControl addAction:actionOne];
-
- [alertControl addAction:alertthree];
-
-
- UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- }];
- [alertControl addAction:alertcancel];
- //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
-
-
-
-
- [self presentViewController:alertControl animated:YES completion:nil];
- }
- - (IBAction)onSignatureClicked:(id)sender {
-
- // CGRect cellrect_screen = [RAUtils relativeFrameForScreenWithView:trigger];
- //
- //
- // CGRect rect1=[self.view convertRect:cellrect_screen fromView:[[[UIApplication sharedApplication] delegate] window]];
- //
- // DebugLog(@"convert1 %@",NSStringFromCGRect(rect1));
- SignatureListViewController *signatureVC = [ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureListViewController"];
- signatureVC.title=@"Signatures";
- signatureVC.signatureData = self.signatureData;
-
-
- signatureVC.blk_Add = ^()
- {
-
- __block UIImage* signimg=nil;
- SignatureViewController * vc =[ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureViewController"];
-
- vc.onReturnImg = ^(UIImage* img)
- {
-
-
- signimg = img;
-
- // if(signimg!=nil)
- // [PDFUtils addSignature:signimg to:self.signatureData];
-
-
-
-
- };
-
- // orderinfoVC.url_type = URL_REMOTE;
- // orderinfoVC.request_url=URL_CARTDELIVERY;
- //
- // orderinfoVC.params = params;
- //
- // orderinfoVC.delegate=self;
- //
- // if(checked.count==count)
- // {
- // orderinfoVC.have_tail = true
- // }
-
- [self.navigationController pushViewController:vc animated:true];
-
-
-
- };
-
- // signatureVC.blk_Fill =^(NSString* file, bool fillFullDoc)
- // {
- //
- //
- // for(int i=0;i<[self.controlTemplate[@"count"] intValue];i++)
- // {
- // NSMutableDictionary* page =self.controlTemplate[[NSString stringWithFormat:@"page_%d",i]];
- // if(!fillFullDoc && i!=self.current_page)
- // continue;
- // for(int j=0;j<[page[@"count"] intValue];j++)
- // {
- // NSMutableDictionary* control =page[[NSString stringWithFormat:@"control_%d",j]];
- // NSString* type = control[@"type"];
- // if(![type isEqualToString:@"Signature"])
- // continue;
- //
- // control[@"value"]=file;
- //
- //// NSString* name = control[@"aname"];
- //// bool required = [control[@"required"] boolValue];
- ////
- //// if(required==false)
- //// continue;
- //// if([type isEqualToString:@"Check"])
- //// {
- //// NSArray* arr = control[@"value"];
- //// if(arr.count==0)
- //// {
- //// ret=false;
- ////
- //// NSString* msg=[NSString stringWithFormat:@"%u Page %d %@ is missing.",fields.count+1,i+1,name];
- //// if(fields.count<15)
- //// [fields addObject:msg];
- //// else
- //// more++;
- //// }
- ////
- //// }
- //// else if(/*[type isEqualToString:@"Signature"]||*/[type isEqualToString:@"Image"]||[type isEqualToString:@"Label"])
- //// {
- //// continue;
- //// }
- //// else
- //// {
- //// NSString* str = control[@"value"];
- //// if(str.length==0)
- //// {
- //// ret=false;
- //// NSString* msg=[NSString stringWithFormat:@"%lu Page %d %@ is missing.",fields.count+1,i+1,name];
- //// if(fields.count<15)
- //// [fields addObject:msg];
- //// else
- //// more++;
- //// }
- //// }
- // }
- // }
- //
- //
- // // NSString* file_name=[file lastPathComponent];
- ////
- //// NSData* img_data=[ImageUtils load_img:file];
- ////
- //// if(img_data!=nil)
- //// {
- ////
- //// UIImage* image=[UIImage imageWithData:img_data];
- //// touchImageView.image = image;
- ////
- //// long index = touchImageView.tag - CONTROL_BASE;
- //// self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
- ////
- //// }
- //
- // };
-
- UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:signatureVC];
- navi.modalPresentationStyle=UIModalPresentationPopover;
-
- UIPopoverPresentationController* popPc = navi.popoverPresentationController;
- popPc.permittedArrowDirections = UIPopoverArrowDirectionUp;
- popPc.barButtonItem = sender;
- popPc.delegate = nil;
-
-
-
- [self presentViewController:navi animated:true completion:nil];
-
- //// menu.selector = self.selector;
- ////
- //// menu.selectordelegate = self;
- //
- // // 1.创建一个UIPopover
- // UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:[[UINavigationController alloc] initWithRootViewController:menu]];
- //
- //
- //
- // UIPopoverPresentationController
- // // 2.设置尺寸
- // // popover.popoverContentSize = CGSizeMake(320, 44 * 5);
- //
- // // 3.从哪里显示出来 --> 指向item
- //
- // [popover presentPopoverFromRect:self.view.frame inView:self.view permittedArrowDirections:0 animated:YES];
- // // [popover presentPopoverFromBarButtonItem:item permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
- //
- //// self.popover = popover;
- // popover pop
- //
- // [self performSegueWithIdentifier:@"selector_popover" sender:self];
-
- }
- -(CGRect)WindowRect2PDFRect:(CGRect)rect pdf_rect:(CGRect)pdf_rect window_size:(CGSize)window_size
- {
- CGRect pdf_expand;
- float offset_x = 0;
- float offset_y = 0;
- float scale =1;
- if(pdf_rect.size.height>=pdf_rect.size.width)
- {
- pdf_expand = CGRectMake(0, 0, pdf_rect.size.height*window_size.width/window_size.height, pdf_rect.size.height);
- offset_x = (pdf_expand.size.width-pdf_rect.size.width)/2;
- scale = pdf_rect.size.height/window_size.height;
-
- }
- else
- {
- pdf_expand = CGRectMake(0, 0, pdf_rect.size.width, pdf_rect.size.width*window_size.height/window_size.width);
- offset_y = (pdf_expand.size.height-pdf_rect.size.height)/2;
- scale = pdf_rect.size.width/window_size.width;
- }
- float x=rect.origin.x*scale-offset_x;
- float y=rect.origin.y*scale-offset_y;
- float width = rect.size.width*scale;
- float height = rect.size.height*scale;
-
- return CGRectMake(x, y, width, height);
-
- }
- -(void) EndEditing
- {
-
-
- NSArray* arr =self.pageViewController.viewControllers;
-
- TemplateViewController *currentViewController = (TemplateViewController*)self.pageViewController.viewControllers[0];
- // PageViewController *currentViewController = (PageViewController*)self.pageViewController.viewControllers[self.current_page];
- [currentViewController EndEditing];
- }
- - (IBAction)onSaveClicked:(id)sender {
-
- // save contro json
- NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
-
-
- path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"template_%@.json",[RAUtils current_date_forfile]]];
-
-
-
-
- NSString* str= [RAConvertor dict2string:self.controlTemplate];
-
-
-
- NSError *error=nil;
-
- [str writeToFile:path atomically:true encoding:NSUTF8StringEncoding error:&error];
-
- NSLog(path);
-
-
- // [self.controlTemplate writeToFile:path atomically:NO];
-
- return;
- //
- // [self EndEditing];
- // if(self.no_save)
- // {
- // [RAUtils message_alert:@"Can not save pending document under offline mode." title:@"Save document" controller:self] ;
- // return;
- // }
- //
- // if([self verifyData]==false)
- // return;
- //
- // NSString* pdffile = nil;
- // NSString* zipfile = nil;
- // NSString* jsonfile = nil;
- // // for(int l=0;l<1;l++)
- // {
- //
- // if(self.hide_control)
- // {
- // pdffile = self.pdfPath;
- // }
- // else
- // {
- // pdffile = [PDFUtils SavePDF:self.controlTemplate source:self.document window_rect:CGRectMake(0, 0, 768, 960) name:self.docment_id isnew:self.action==PDFEditActionNew];
- // }
- //
- // zipfile=[ACNetwork prepareUploadFile:pdffile json:self.controlTemplate];
- //
- // //jsonfile = [ACNetwork getjsonPath:zip];
- //
- // }
- //
- // __weak typeof(self) weakSelf = self;
- // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- // if(appDelegate.accessMode == AppAccessModeOffline)
- // {
- //
- // NSMutableDictionary * json = [[NSMutableDictionary alloc]init];
- //
- // json[@"json"]=@"json.json";
- // json[@"pdf"]=pdffile.lastPathComponent;
- // json[@"zip"]=zipfile.lastPathComponent;
- // json[@"serial"]=[json[@"zip"] stringByDeletingPathExtension];
- //
- //
- //
- // json[@"create_by"]=appDelegate.user;
- //
- //
- // json[@"agent_name"]=[self extract_data:@"agent_name"];
- // json[@"doc_number"]=[self extract_data:@"doc_number"];
- // json[@"job_date"]=[self extract_data:@"job_date"];
- // json[@"customer_name"]=[self extract_data:@"customer_name"];
- // json[@"signed_date"]=[self extract_data:@"signed_date"];
- //
- //
- //
- // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- // NSMutableArray * arr_pending = [[defaults arrayForKey:@"pending_doc"] mutableCopy];
- // if(arr_pending==nil)
- // arr_pending = [[NSMutableArray alloc]init];
- // [arr_pending addObject:json];
- // [defaults setObject:arr_pending forKey:@"pending_doc"];
- // [defaults synchronize];
- //
- //
- // [weakSelf.navigationController popViewControllerAnimated:false];
- // if(weakSelf.OnSave)
- // weakSelf.OnSave(pdffile,@"");
- //
- // }
- // else
- // {
- // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Saving"];
- //
- // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- //
- // NSDictionary* json = nil;
- //
- //
- //
- // json = [ACNetwork save_document:zipfile isnew:self.action==PDFEditActionNew md5:md5];
- //
- // dispatch_async(dispatch_get_main_queue(), ^{
- //
- //
- // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
- // // if([[logout_json valueForKey:@"result"] intValue]==2)
- // // {
- // // self.welcomeLabel.text=@"Not sign in";
- // // // [sender setTitle:@"Sign in" forState:UIControlStateNormal];
- // // [bbitem setTitle:@"Sign in"];
- // //
- // // self.buttonHIC.enabled = false;
- // // self.buttonSRL.enabled = false;
- // // self.coverView.hidden = false;
- // // }
- // // else
- // // {
- // // [RAUtils message_alert:[logout_json valueForKey:@"err_msg"] title:@"Sign out" controller:self] ;
- // // }
- //
- // if([[json valueForKey:@"result"] intValue]==2)
- // {
- //
- //
- // [weakSelf.navigationController popViewControllerAnimated:false];
- // if(weakSelf.OnSave)
- // weakSelf.OnSave(pdffile,json[@"subject"]);
- //
- //
- // }
- // else
- // {
- // // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Save document" controller:self] ;
- //
- // if(self.hide_control)
- // {
- // //提交离线合同
- // [RAUtils message_alert:@"Can not upload pending documents please try again later." title:@"Save document" controller:self] ;
- // return;
- // }
- //
- // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Create Customer" message:@"Can not create new customer on server, do you want to create an offline document?" preferredStyle:UIAlertControllerStyleAlert];
- //
- //
- // UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
- // __block NSString* offline_docnumber =[ACNetwork offline_docnumber];
- // if(offline_docnumber==nil)
- // {
- // [RAUtils message_alert:@"Offline document number prefix not found, Please check your user name and make sure you had signed in at least once." title:@"Create Customer" controller:self] ;
- // return;
- // }
- // [appDelegate SwitchToOffline:appDelegate.user];
- //
- //
- // NSMutableDictionary * json = [[NSMutableDictionary alloc]init];
- //
- // json[@"json"]=@"json.json";
- // json[@"pdf"]=pdffile.lastPathComponent;
- // json[@"zip"]=zipfile.lastPathComponent;
- // json[@"serial"]=[json[@"zip"] stringByDeletingPathExtension];
- //
- //
- //
- // json[@"create_by"]=appDelegate.user;
- //
- //
- // json[@"agent_name"]=[self extract_data:@"agent_name"];
- // json[@"doc_number"]=[self extract_data:@"doc_number"];
- // json[@"job_date"]=[self extract_data:@"job_date"];
- // json[@"customer_name"]=[self extract_data:@"customer_name"];
- // json[@"signed_date"]=[self extract_data:@"signed_date"];
- //
- //
- //
- // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- // NSMutableArray * arr_pending = [[defaults arrayForKey:@"pending_doc"] mutableCopy];
- // if(arr_pending==nil)
- // arr_pending = [[NSMutableArray alloc]init];
- // [arr_pending addObject:json];
- // [defaults setObject:arr_pending forKey:@"pending_doc"];
- // [defaults synchronize];
- //
- //
- // [weakSelf.navigationController popViewControllerAnimated:false];
- // if(weakSelf.OnSave)
- // weakSelf.OnSave(pdffile,@"");
- //
- //
- // }];
- //
- // UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- // DebugLog(@"Cancel");
- // }];
- // [alertControl addAction:actionOne];
- //
- // [alertControl addAction:alertthree];
- //
- // //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
- //
- //
- // [self presentViewController:alertControl animated:YES completion:nil];
- //
- // }
- //
- //
- // });
- // });
- // }
-
-
-
-
-
-
- }
- -(void) addSignature :(UIImage*) img to:(NSMutableDictionary*)signatureData
- {
-
-
- if(true)
- {
- NSString* newfile=[self saveTempSignature:UIImagePNGRepresentation(img)];
-
- int newidx = [self.signatureData[@"count"] intValue];
-
- // [@"file"]= newfile;
-
- NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
- item[@"file"]=newfile;
- signatureData[[NSString stringWithFormat:@"item_%d",newidx]] = item;
- signatureData[@"count"]=[NSNumber numberWithInt:newidx+1];
-
-
-
- }
-
-
-
- }
- -(NSString *) saveTempSignature:(NSData *) image
- {
- NSString* tempDir = NSTemporaryDirectory();
-
- NSString *saveFileName=[NSString stringWithFormat:@"%@.png",[[NSUUID UUID ] UUIDString] ] ;
-
- NSString *newFilePath=[tempDir stringByAppendingPathComponent:saveFileName];
-
-
- bool bsuccess=[image writeToFile:newFilePath atomically:YES];
- if(bsuccess)
- {
-
- return newFilePath;
- }
- else
- {
- return nil;
- }
-
- }
- /*
- #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.
- }
- */
- - (TemplateViewController *)viewControllerAtIndex:(NSUInteger)index
- {
- //Return the RAPDFViewController for the given index.
- if ((self.total_page == 0 )|| (index > self.total_page) ) {
- return nil;
- }
-
- //Create a new view controller and pass suitable data.
- // PageViewController *PageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
- TemplateViewController *TemplateViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TemplateViewController"];
-
- TemplateViewController.pageRef = CGPDFDocumentGetPage (self.document , index+1);
- TemplateViewController.pageIndex = index;
- TemplateViewController.hide_control = self.hide_control;
-
- __weak typeof(self) weakSelf = self;
- TemplateViewController.blk_Fill = ^(NSString* file,NSString* subtype,bool fillFullDoc)
- {
-
-
- for(int i=0;i<[weakSelf.controlTemplate[@"count"] intValue];i++)
- {
- NSMutableDictionary* page =weakSelf.controlTemplate[[NSString stringWithFormat:@"page_%d",i]];
- if(!fillFullDoc && i!=weakSelf.current_page)
- continue;
- for(int j=0;j<[page[@"count"] intValue];j++)
- {
- NSMutableDictionary* dest =page[[NSString stringWithFormat:@"control_%d",j]];
- NSString* type = dest[@"type"];
- if(![type isEqualToString:@"Signature"])
- continue;
-
- if([dest[@"disable"] boolValue])
- continue;
- if([dest[@"name"] isEqualToString:subtype])
- {
- dest[@"value"]=file;
- }
- }
- }
-
-
- // NSString* file_name=[file lastPathComponent];
- //
- // NSData* img_data=[ImageUtils load_img:file];
- //
- // if(img_data!=nil)
- // {
- //
- // UIImage* image=[UIImage imageWithData:img_data];
- // touchImageView.image = image;
- //
- // long index = touchImageView.tag - CONTROL_BASE;
- // self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
- //
- // }
-
- };
- TemplateViewController.page_controlTemplate = self.controlTemplate[[NSString stringWithFormat:@"page_%lu",(unsigned long)index]];
- TemplateViewController.doc_controlTemplate = self.controlTemplate;
- TemplateViewController.signatureData = self.signatureData;
- //PageViewController.pdfview = [self.pagePDF objectAtIndex:index];
-
- /*
- PageViewController.pdfview = [[PDFView alloc]initWithFrame:self.view.frame atPage:index];
- [PageViewController.view addSubview:PageViewController.pdfview];
- NSLog(@"index = %d",index);
-
-
- */
-
- TemplateViewController.controlUpdateDelegate = self;
-
- return TemplateViewController;
- }
- - (NSUInteger) indexOfViewController:(TemplateViewController *)viewController
- {
- return viewController.pageIndex;//[self.pagePDF indexOfObject:viewController.pdfview];
- }
- - (NSString*) extract_data:(NSString*)field_name
- {
- for(int p=0;p<[self.controlTemplate[@"count"] intValue];p++)
- {
- NSDictionary* page=self.controlTemplate[[NSString stringWithFormat:@"page_%d",p]];
- for(int c=0;c<[page[@"count"] intValue];c++)
- {
- NSDictionary* control=page[[NSString stringWithFormat:@"control_%d",c]];
- if([control[@"field"] isEqualToString:field_name])
- {
- return control[@"value"]==nil?@"":control[@"value"];
- }
- }
- }
- return @"";
- }
- #pragma mark verify data
- - (UIView *)createDemoView:(NSString*) msg
- {
- UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 290, 200)];
-
- // UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 270, 180)];
- // [imageView setImage:[UIImage imageNamed:@"demo"]];
-
- UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 270, 10)];
- titleLabel.font = [UIFont boldSystemFontOfSize:20];
- titleLabel.text=@"Missing fields:";
- titleLabel.numberOfLines = 0;
- titleLabel.lineBreakMode =NSLineBreakByWordWrapping;
- // titleLabel.textAlignment = NSTextAlignmentRight;
- titleLabel.textAlignment = NSTextAlignmentCenter;
- CGSize constraintkey = CGSizeMake(270, 20000.0f);//key label width is 40% cell width;
- CGSize sizetitle = [@"Missing fields:" sizeWithFont:[UIFont systemFontOfSize:20.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
-
- //[titleLabel sizeToFit];
- // titleLabel.center.x = demoView.center.x;
- titleLabel.frame = CGRectMake(10, 10, 270, sizetitle.height);
- [demoView addSubview:titleLabel];
-
- UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, titleLabel.frame.origin.y+ titleLabel.frame.size.height+20, 260, 10)];
- lineLabel.font = [UIFont boldSystemFontOfSize:17];
- lineLabel.text=msg;
- lineLabel.numberOfLines = 0;
- lineLabel.lineBreakMode =NSLineBreakByWordWrapping;
-
-
- constraintkey = CGSizeMake(260, 20000.0f);
- CGSize sizemsg = [msg sizeWithFont:[UIFont boldSystemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
-
- lineLabel.frame = CGRectMake(15, titleLabel.frame.origin.y+ titleLabel.frame.size.height+20, 260, sizemsg.height);
- //[lineLabel sizeToFit];
-
- [demoView addSubview:lineLabel];
- demoView.frame = CGRectMake(0, 0, 290, lineLabel.frame.origin.y+lineLabel.frame.size.height+25);
- return demoView;
- }
- -(bool) verifyData
- {
-
- if(NO_SAVE_VERIFY)
- return true;
- // return true;
- bool ret=true;
- int more=0;
- NSMutableArray* fields=[[NSMutableArray alloc] init];
-
- for(int i=0;i<[self.controlTemplate[@"count"] intValue];i++)
- {
- NSMutableDictionary* page =self.controlTemplate[[NSString stringWithFormat:@"page_%d",i]];
- for(int j=0;j<[page[@"count"] intValue];j++)
- {
- NSMutableDictionary* control =page[[NSString stringWithFormat:@"control_%d",j]];
- NSString* type = control[@"type"];
- NSString* name = control[@"aname"];
- bool required = [control[@"required"] boolValue];
-
- if(required==false || [type.lowercaseString isEqualToString:@"ignor"])
- continue;
- if([type isEqualToString:@"Check"])
- {
- NSArray* arr = control[@"value"];
- if(arr.count==0)
- {
- ret=false;
-
- NSString* msg=[NSString stringWithFormat:@"%u. Page %d: %@",fields.count+1,i+1,name];
- if(fields.count<15)
- [fields addObject:msg];
- else
- more++;
- }
-
- }
- else if(/*[type isEqualToString:@"Signature"]||*/[type isEqualToString:@"Image"]||[type isEqualToString:@"Label"])
- {
- continue;
- }
- else
- {
- NSString* str = control[@"value"];
- if(str.length==0)
- {
- ret=false;
- NSString* msg=[NSString stringWithFormat:@"%lu. Page %d: %@",fields.count+1,i+1,name];
- if(fields.count<15)
- [fields addObject:msg];
- else
- more++;
- }
- }
- }
- }
- if(ret==false)
- {
- if(more>0)
- {
- NSString* msg=[NSString stringWithFormat:@"And %d more missing fields.",more];
-
- [fields addObject:@"......"];
- [fields addObject:msg];
- }
- NSString* missfields=[fields componentsJoinedByString:@"\n"];
- NSString* msg = [NSString stringWithFormat:@"%@",missfields];
-
- CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
-
-
-
-
- // Add some custom content to the alert view
- [alertView setContainerView:[self createDemoView:msg]];
-
- // Modify the parameters
-
-
-
- [alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"OK", nil]];
- //[alertView setDelegate:self];
-
- // You may use a Block, rather than a delegate.
- [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
- // DebugLog(@"Block: Button at position %d is clicked on alertView %d.", buttonIndex, (int)[alertView tag]);
- [alertView close];
- }];
-
- // [alertView setUseMotionEffects:true];
-
- // And launch the dialog
-
- [alertView sizeToFit];
- [alertView show];
-
-
- }
- return ret;
- }
- #pragma mark - Page View Controller Data Source
- - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
- {
- NSUInteger index = [self indexOfViewController:(TemplateViewController *)viewController];
- if ((index == 0 ) || (index == NSNotFound)){
- return nil;
- }
- // self.current_page= index;
-
- index--;
- return [self viewControllerAtIndex:index];
- }
- - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
- {
- NSUInteger index = [self indexOfViewController:(TemplateViewController *)viewController];
- if (index == NSNotFound)
- {
- return nil;
- }
- // self.current_page= index;
-
- index++;
-
- if (index == self.total_page){
- return nil;
- }
- return [self viewControllerAtIndex:index];
- }
- #pragma mark - UIPageViewController delegate methods
- - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
- {
- NSLog(@"didFinishAnimating");
-
-
-
- long index=[self indexOfViewController:pageViewController.viewControllers[0]];
- if(index==0)
- self.left_button.hidden=true;
- else
- self.left_button.hidden=false;
-
- if(index==self.total_page-1)
- self.right_button.hidden=true;
- else
- self.right_button.hidden=false;
- // self.title = [NSString stringWithFormat:@"%@ [%ld/%ld]", self.docment_id,((PageViewController*)self.pageViewController.viewControllers[0]).pageIndex+1, self.total_page ];
- }
- - (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation {
- if (true||UIInterfaceOrientationIsPortrait(orientation) || ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)) {
- // In portrait orientation or on iPhone: Set the spine position to "min" and the page view controller's view controllers array to contain just one view controller. Setting the spine position to 'UIPageViewControllerSpineLocationMid' in landscape orientation sets the doubleSided property to YES, so set it to NO here.
-
- UIViewController *currentViewController = self.pageViewController.viewControllers[0];
- NSArray *viewControllers = @[currentViewController];
-
- __weak typeof(self) weakSelf = self;
- [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished){
-
- long index=[weakSelf indexOfViewController:(TemplateViewController*)currentViewController];
- if(index==0)
- weakSelf.left_button.hidden=true;
- else
- weakSelf.left_button.hidden=false;
-
- if(index==weakSelf.total_page-1)
- weakSelf.right_button.hidden=true;
- else
- weakSelf.right_button.hidden=false;
- }];
-
- self.pageViewController.doubleSided = NO;
- return UIPageViewControllerSpineLocationMin;
- }
-
- // // In landscape orientation: Set set the spine location to "mid" and the page view controller's view controllers array to contain two view controllers. If the current page is even, set it to contain the current and next view controllers; if it is odd, set the array to contain the previous and current view controllers.
- // PageViewController *currentViewController = self.pageViewController.viewControllers[0];
- // NSArray *viewControllers = nil;
- //
- // NSUInteger indexOfCurrentViewController = [self indexOfViewController:currentViewController];
- // if (indexOfCurrentViewController == 0 || indexOfCurrentViewController % 2 == 0) {
- // UIViewController *nextViewController = [self pageViewController:self.pageViewController viewControllerAfterViewController:currentViewController];
- // viewControllers = @[currentViewController, nextViewController];
- // } else {
- // UIViewController *previousViewController = [self pageViewController:self.pageViewController viewControllerBeforeViewController:currentViewController];
- // viewControllers = @[previousViewController, currentViewController];
- // }
- // [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
- //
- //
- // return UIPageViewControllerSpineLocationMid;
- }
- #pragma mark - Position Update
- - (IBAction)topBtnClick:(UIButton *)sender {
-
- CGRect frame = self.positionControlBackground.frame;
- frame.origin.y = 84.f;
- self.positionControlBackground.frame = frame;
- self.positionControlBackground.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
-
- }
- - (IBAction)bottomBtnClick:(UIButton *)sender {
-
- CGRect vcFrame = self.view.frame;
- CGRect frame = self.positionControlBackground.frame;
- frame.origin.y = vcFrame.size.height - 20.f - frame.size.height;
- self.positionControlBackground.frame = frame;
- self.positionControlBackground.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
- }
- - (IBAction)xStepperClick:(UIStepper *)sender {
- if (!self.focusView) {
- return;
- }
- CGRect frame = self.focusView.frame;
- frame.origin.x = sender.value;
- self.focusView.frame = frame;
-
- self.xTextField.text = [NSString stringWithFormat:@"%.2f",sender.value];
-
- [self savePageControl];
- }
- - (IBAction)yStepperClick:(UIStepper *)sender {
- if (!self.focusView) {
- return;
- }
- CGRect frame = self.focusView.frame;
- frame.origin.y = sender.value;
- self.focusView.frame = frame;
-
- self.yTextField.text = [NSString stringWithFormat:@"%.2f",sender.value];
-
- [self savePageControl];
- }
- - (IBAction)wStepperClick:(UIStepper *)sender {
- if (!self.focusView) {
- return;
- }
- CGRect frame = self.focusView.frame;
- frame.size.width = sender.value;
- self.focusView.frame = frame;
- self.wTextField.text = [NSString stringWithFormat:@"%.2f",sender.value];
- [self savePageControl];
- }
- - (IBAction)hStepperClick:(UIStepper *)sender {
- if (!self.focusView) {
- return;
- }
- CGRect frame = self.focusView.frame;
- frame.size.height = sender.value;
- self.focusView.frame = frame;
- self.hTextField.text = [NSString stringWithFormat:@"%.2f",sender.value];
- [self savePageControl];
- }
- - (IBAction)okBtnClick:(UIButton *)sender {
-
- float x = [self.xTextField.text floatValue];
- float y = [self.yTextField.text floatValue];
- float w = [self.wTextField.text floatValue];
- float h = [self.hTextField.text floatValue];
-
- [self setX:x Y:y W:w H:h];
-
- CGRect frame = CGRectMake(x, y, w, h);
- if (self.focusView) {
- self.focusView.frame = frame;
-
- [self savePageControl];
- }
-
- }
- - (void)savePageControl {
-
- if(!self.focusView)
- return;
- if (self.currentPageViewController) {
-
- TemplateViewController *vc = self.currentPageViewController;
- NSDictionary *page = [vc updatePageControl:self.focusView];
- self.controlTemplate[[NSString stringWithFormat:@"page_%lu",(unsigned long)vc.pageIndex]] = page;
- }
- }
- #pragma mark - Control Update Delegate
- - (void) setX:(CGFloat) x Y:(CGFloat)y W:(CGFloat)w H:(CGFloat)h {
- self.xStepper.value = x;
- self.yStepper.value = y;
- self.wStepper.value = w;
- self.hStepper.value = h;
-
- self.xTextField.text = [NSString stringWithFormat:@"%.2f",x];
- self.yTextField.text = [NSString stringWithFormat:@"%.2f",y];
- self.wTextField.text = [NSString stringWithFormat:@"%.2f",w];
- self.hTextField.text = [NSString stringWithFormat:@"%.2f",h];
- }
- - (void)focusControlView:(UIView *)view frame:(CGRect)frame{
-
- [self recoveryFocusView];
-
- self.focusView = view;
- if (self.focusView != nil) {
-
- CGRect frame = self.focusView.frame;
-
- [self setX:frame.origin.x Y:frame.origin.y W:frame.size.width H:frame.size.height];
-
- self.focusViewNormalColor = self.focusView.backgroundColor;
- self.focusView.backgroundColor = [UIColor redColor];
-
- if ([self.focusView isKindOfClass:[UIImageView class]]) {
- UIImageView *iv = (UIImageView *)self.focusView;
- UIImage *ig = iv.image;
- if (ig) {
- [iv setImage:[ig imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
- }
- } else if ([self.focusView isKindOfClass:[UITextView class]]) {
- UITextView *tv = (UITextView *)self.focusView;
- if (tv.text == nil || tv.text.length == 0) {
- [tv setText:@"Text"];
- }
- } else if ([self.focusView isKindOfClass:[UITextField class]]) {
- UITextField *tf = (UITextField *)self.focusView;
- if (tf.text == nil || tf.text.length == 0) {
- [tf setText:@"Text"];
- }
- } else if ([self.focusView isKindOfClass:[UILabel class]]) {
- UILabel *lb = (UILabel *)self.focusView;
- if (lb.text == nil || lb.text.length == 0) {
- [lb setText:@"Text"];
- }
- } else if ([self.focusView isKindOfClass:[UIButton class]]) {
- UIButton *btn = (UIButton *)self.focusView;
- NSString *str = [btn titleForState:UIControlStateNormal];
- if (str == nil || str.length == 0) {
- [btn setTitle:@"Text" forState:UIControlStateNormal];
- }
- }
-
- NSInteger tag = self.focusView.tag;
- NSInteger section = -1;
- NSInteger row = -1;
- if (tag >= CONTROL_BASE && tag < CHECK_BASE) {
- section = tag - CONTROL_BASE;
- } else if (tag >= CHECK_BASE) {
- section = (tag - CHECK_BASE) / 1000;
- row = (tag - CHECK_BASE) % 1000;
- }
- [self.itemTableDataSource selectSection:section Row:row];
-
- }
- }
- - (void)recoveryFocusView {
- if (self.focusView) {
- self.focusView.backgroundColor = self.focusViewNormalColor;
-
- if ([self.focusView isKindOfClass:[UIImageView class]]) {
- UIImageView *iv = (UIImageView *)self.focusView;
- UIImage *ig = iv.image;
- if (ig) {
- [iv setImage:[ig imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
- }
- }
- }
- }
- #pragma mark - Template Attributes
- - (void)currentShowViewController:(TemplateViewController *)vc {
- self.currentPageViewController = vc;
-
- [self pageChanged];
- [self.itemTableDataSource changeData:vc.page_controlTemplate];
- [self.attrTableDataSource changeData:nil type:ItemTypeNone forIndex:-1];
- }
- - (void)pageChanged {
- [self setX:0 Y:0 W:0 H:0];
- }
- -(void)clickItem:(NSInteger)tag {
- if (self.currentPageViewController) {
-
- UIView *v = [self.currentPageViewController.view viewWithTag:tag];
- if (v) {
- [self focusControlView:v frame:[self.currentPageViewController getViewFrame:v]];
- } else {
- [self setX:0 Y:0 W:0 H:0];
- NSInteger section = -1;
- NSInteger row = -1;
- if (tag >= CONTROL_BASE && tag < CHECK_BASE) {
- section = tag - CONTROL_BASE;
- } else if (tag >= CHECK_BASE) {
- section = (tag - CHECK_BASE) / 1000;
- row = (tag - CHECK_BASE) % 1000;
- }
- [self.itemTableDataSource selectSection:section Row:row];
- }
- }
- }
- - (void)selectControl:(NSDictionary *)controlItem index:(NSInteger)index {
- NSString *type = [controlItem objectForKey:@"type"];
-
- if ([type isEqualToString:@"TextView"]) {
- [self.attrTableDataSource changeData:[controlItem mutableCopy] type:ItemTypeTextView forIndex:index];
- }else if ([type isEqualToString:@"ignor"]) {
- [self.attrTableDataSource changeData:[controlItem mutableCopy] type:ItemTypeIgnore forIndex:index];
- } else if([type isEqualToString:@"Button"]) {
- [self.attrTableDataSource changeData:[controlItem mutableCopy] type:ItemTypeButton forIndex:index];
- } else if([type isEqualToString:@"Check"]) {
- [self.attrTableDataSource changeData:[controlItem mutableCopy] type:ItemTypeCheck forIndex:index];
- } else if([type isEqualToString:@"Signature"]) {
- [self.attrTableDataSource changeData:[controlItem mutableCopy] type:ItemTypeSignature forIndex:index];
- } else if([type isEqualToString:@"Image"]) {
- [self.attrTableDataSource changeData:[controlItem mutableCopy] type:ItemTypeImage forIndex:index];
- } else if([type isEqualToString:@"Label"]) {
- [self.attrTableDataSource changeData:[controlItem mutableCopy] type:ItemTypeLabel forIndex:index];
- } else if([type isEqualToString:@"DatePicker"]) {
- [self.attrTableDataSource changeData:[controlItem mutableCopy] type:ItemTypeDatePicker forIndex:index];
- } else {
- [self.attrTableDataSource changeData:nil type:ItemTypeNone forIndex:-1];
- }
-
- }
- - (void)selectCheck:(NSDictionary *)control Section:(NSInteger)section Row:(NSInteger)row {
- [self.attrTableDataSource changeCheckOptionData:[control mutableCopy] forSection:section Row:row];
- }
- - (void)updatePageControlProperty:(NSDictionary *)control_dic forIndex:(NSInteger)index Row:(NSInteger)row updateItemList:(BOOL)updateItemList {
- // 刷新Page数据
- if (self.currentPageViewController) {
- [self.currentPageViewController updatePageControl:control_dic forIndex:index];
- }
- // 更新Item List视图
- if (updateItemList) {
- if (self.itemTableDataSource) {
- [self.itemTableDataSource updateData:control_dic Section:index Row:row];
- }
- }
- }
- #pragma mark - Add & Delete
- - (IBAction)addControlBtnClick:(UIButton *)sender {
- ControlTemplateVC *vc = [[ControlTemplateVC alloc] init];
- vc.delegate = self;
- UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
- nav.preferredContentSize = CGSizeMake(380, 430);
- nav.modalPresentationStyle = UIModalPresentationFormSheet;
-
- [self presentViewController:nav animated:YES completion:nil];
- }
- - (IBAction)deleteControlBtnClick:(UIButton *)sender {
-
- if (self.focusView == nil) {
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Please Select A Control" preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *action = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- }];
- [alert addAction:action];
- [self presentViewController:alert animated:YES completion:nil];
- } else {
- if (self.currentPageViewController) {
- [self.currentPageViewController deleteControl:self.focusView];
- self.focusView = nil;
- [self setX:0 Y:0 W:0 H:0];
- [self.itemTableDataSource changeData:self.currentPageViewController.page_controlTemplate];
- [self.attrTableDataSource changeData:nil type:ItemTypeNone forIndex:-1];
- }
- }
-
- }
- - (void)selectTemplateStyle:(TemplateStyle)style {
-
- NSString *path = [[NSBundle mainBundle] pathForResource:@"template_example.json" ofType:nil];
- NSData *json_data = [NSData dataWithContentsOfFile:path];
- NSDictionary *template = [NSJSONSerialization JSONObjectWithData:json_data options:NSJSONReadingMutableLeaves error:nil];
-
- NSString *key = nil;
- switch (style) {
- case TemplateStyleTextView: {
- key = @"TextView";
- }
- break;
- case TemplateStyleLabel: {
- key = @"Label";
- }
- break;
- case TemplateStyleSignature: {
- key = @"Signature";
- }
- break;
- case TemplateStyleImage: {
- key = @"Image";
- }
- break;
- case TemplateStyleButton: {
- key = @"Button";
- }
- break;
- case TemplateStyleCheck: {
- key = @"Check_0";
- }
- break;
- case TemplateStyleDatePicker: {
- key = @"DatePicker";
- }
- break;
- case TemplateStyleCheckNoMarker: {
- key = @"Check_1";
- }
- break;
- default:
- break;
- }
- if (key != nil) {
- NSDictionary *control = [template objectForKey:key];
- NSInteger control_tag = [self.currentPageViewController addNewControl:control];
- [self.itemTableDataSource changeData:self.currentPageViewController.page_controlTemplate];
-
- [self clickItem:control_tag];
- }
- }
- - (void)deleteCheckItemAtSection:(NSInteger)section Row:(NSInteger)row {
-
- if (self.currentPageViewController) {
- [self.currentPageViewController deleteCheckItemAtSection:section Row:row];
- self.focusView = nil;
- [self setX:0 Y:0 W:0 H:0];
- [self.itemTableDataSource changeData:self.currentPageViewController.page_controlTemplate];
- [self.attrTableDataSource changeData:nil type:ItemTypeNone forIndex:-1];
- }
- }
- - (void)addCheckOptionForSection:(NSInteger)section {
- NSString *path = [[NSBundle mainBundle] pathForResource:@"template_example.json" ofType:nil];
- NSData *json_data = [NSData dataWithContentsOfFile:path];
- NSDictionary *template = [NSJSONSerialization JSONObjectWithData:json_data options:NSJSONReadingMutableLeaves error:nil];
- NSArray *option = [template objectForKey:@"Check_Option"];
- NSInteger marker_tag = [self.currentPageViewController addCheckOption:option ForSection:section];
- [self.itemTableDataSource changeData:self.currentPageViewController.page_controlTemplate];
-
- [self clickItem:marker_tag];
- }
- @end
|