RootViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. //
  2. // ViewController.m
  3. // AntsContract
  4. //
  5. // Created by Ray on 12/16/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RootViewController.h"
  9. #import "DocumentPageViewController.h"
  10. #import "LoginViewController.h"
  11. #import "AppDelegate.h"
  12. #import "RAUtils.h"
  13. #import "PDFViewController.h"
  14. #import "DocumentListViewController.h"
  15. #import "ACNetwork.h"
  16. #import "PDFUtils.h"
  17. #import "AddExtDocumentViewController.h"
  18. @interface RootViewController ()
  19. @property (nonatomic,assign) BOOL hasShowLoginView;
  20. @end
  21. @implementation RootViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. self.title= @"eSign";
  25. self.welcomeLabel.text=@"Not sign in";
  26. self.buttonHIC.enabled = false;
  27. self.buttonSRL.enabled = false;
  28. self.coverView.hidden = false;
  29. [self reloadBG];
  30. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  31. NSString* build =[infoDict objectForKey:@"CFBundleVersion"];
  32. NSString* version =[infoDict objectForKey:@"CFBundleShortVersionString"];
  33. NSString* versionNum = [NSString stringWithFormat:@"VeS1.%@.%@",version,build];
  34. self.labelVer.text = versionNum;
  35. // Do any additional setup after loading the view, typically from a nib.
  36. }
  37. -(void) viewDidAppear:(BOOL)animated
  38. {
  39. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  40. if(!appDelegate.bLogin)
  41. [self presentLogin:self.ibSignin];
  42. }
  43. -(void) presentLogin:(id)sender
  44. {
  45. // 确保只Presnet LoginView一次.
  46. if (self.hasShowLoginView) return;
  47. self.hasShowLoginView = YES;
  48. UIBarButtonItem* bbitem = sender;
  49. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  50. {
  51. LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  52. loginvc.returnValue = ^(bool blogin){
  53. // [self checklogin :true];
  54. self.hasShowLoginView = NO;
  55. if(blogin)
  56. {
  57. self.welcomeLabel.text=[NSString stringWithFormat:@"Welcome %@",appDelegate.user];
  58. [bbitem setTitle:@"Sign out"];
  59. self.buttonHIC.enabled = true;
  60. self.buttonSRL.enabled = true;
  61. self.coverView.hidden = true;
  62. // 处理三方app 传入文件
  63. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  64. if(appDelegate.shareFile)
  65. {
  66. AddExtDocumentViewController * addVC;
  67. addVC = [ [UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"AddExtDocumentViewController"];
  68. addVC.is_present = true;
  69. addVC.doc_number = nil;
  70. addVC.arr_documents = [NSMutableArray arrayWithObjects:appDelegate.shareFile, nil];
  71. appDelegate.shareFile=nil;
  72. // notesVC.docNotes = notes;
  73. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:addVC] ;
  74. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  75. [self presentViewController:navi animated:YES completion:^{
  76. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  77. DebugLog(@"filter present.........");
  78. // self.btop = false;
  79. // <#code#>
  80. }];
  81. }
  82. }
  83. else
  84. {
  85. appDelegate.shareFile=nil;
  86. self.welcomeLabel.text=@"Not sign in";
  87. self.buttonHIC.enabled = false;
  88. self.buttonSRL.enabled = false;
  89. self.coverView.hidden = false;
  90. }
  91. };
  92. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  93. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  94. [self presentViewController:navi animated:YES completion:^{
  95. DebugLog(@"login present.........");
  96. }];
  97. }
  98. }
  99. //-(void) leak_debug
  100. //{
  101. //
  102. //
  103. // CGPDFDocumentRef document = [PDFUtils OpenPDF:@"GEIC - Home Improvement Contract 2017.pdf"];
  104. // // self.total_page = CGPDFDocumentGetNumberOfPages (self.document);
  105. //
  106. // NSMutableDictionary * controlTemplate = [self templateInit:[PDFUtils loadControl:@"GEIC - Home Improvement Contract 2017.json"]];
  107. //
  108. //
  109. // NSString* pdffile = [PDFUtils SavePDF:controlTemplate source:document window_rect:CGRectMake(0, 0, 768, 960)];
  110. //
  111. // CGPDFDocumentRelease(document);
  112. //
  113. // // CFRelease(document);
  114. //
  115. // NSString* zipfile=[ACNetwork prepareUploadFile:pdffile json:controlTemplate];
  116. //
  117. // [self previewPDF:pdffile];
  118. //
  119. //
  120. //
  121. //
  122. //
  123. //
  124. //}
  125. //-(NSMutableDictionary*) templateInit:(NSMutableDictionary*) template
  126. //{
  127. //
  128. //
  129. // NSString* temp = NSTemporaryDirectory();
  130. // NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  131. // NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  132. // UIImage* qrimg=[RAUtils generateBarCode:@"leak test" width:300 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  133. // bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  134. //
  135. //
  136. //
  137. // for(int i=0;i<[template[@"count"] intValue];i++)
  138. // {
  139. // NSMutableDictionary* page =template[[NSString stringWithFormat:@"page_%d",i]];
  140. // for(int j=0;j<[page[@"count"] intValue];j++)
  141. // {
  142. // NSMutableDictionary* control =page[[NSString stringWithFormat:@"control_%d",j]];
  143. // NSString* type = control[@"type"];
  144. // NSString* field =control[@"field"];
  145. // if([type isEqualToString:@"Label"])
  146. // {
  147. // if([field isEqualToString:@"doc_number"])
  148. // {
  149. // control[@"value"]= @"leak test";
  150. // }
  151. // }
  152. // else if([type isEqualToString:@"Image"])
  153. // {
  154. // NSString* name =control[@"name"];
  155. // if([name isEqualToString:@"BarCode"])
  156. // {
  157. // if(bwrite)
  158. // control[@"value"]= qrpath;
  159. // }
  160. // }
  161. // else if([type isEqualToString:@"DatePicker"])
  162. // {
  163. // NSString* name =control[@"aname"];
  164. // if([name isEqualToString:@"Today's Date"])
  165. // {
  166. // NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  167. // [formatter setDateFormat:@"MM/dd/yyyy"];
  168. // NSString* date = [formatter stringFromDate:[NSDate date]] ;
  169. //
  170. // control[@"value"]= date;
  171. // }
  172. // }
  173. // }
  174. // }
  175. // return template;
  176. //}
  177. - (IBAction)onNewCustomerClick:(id)sender {
  178. // [self leak_debug];
  179. // return;
  180. __weak typeof(self) weakSelf = self;
  181. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Create customer"];
  182. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  183. NSMutableDictionary* json=[[ACNetwork new_document] mutableCopy];
  184. dispatch_async(dispatch_get_main_queue(), ^{
  185. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  186. if([json[@"result"] intValue]==RESULT_TRUE)
  187. {
  188. DocumentPageViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DocumentPageViewController" ];
  189. // dvc.templateName = @"debug.json";
  190. dvc.templateName = @"GEIC - Home Improvement Contract 2017 Update.json";
  191. dvc.pdfName = @"GEIC - Home Improvement Contract 2017 Update.pdf";
  192. dvc.docment_id = json[@"doc_number"];
  193. dvc.OnSave=^(NSString* file, NSString* subject)
  194. {
  195. [weakSelf previewPDF:file title:json[@"doc_number"] subject:subject];
  196. };
  197. [self.navigationController pushViewController:dvc animated:true];
  198. }
  199. else
  200. {
  201. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Create Customer" controller:self] ;
  202. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Create Customer" message:@"Can not create new customer on server, do you want to create an offline document?" preferredStyle:UIAlertControllerStyleAlert];
  203. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  204. DocumentPageViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DocumentPageViewController" ];
  205. // dvc.templateName = @"debug.json";
  206. dvc.templateName = @"GEIC - Home Improvement Contract 2017 Update.json";
  207. dvc.pdfName = @"GEIC - Home Improvement Contract 2017 Update.pdf";
  208. dvc.docment_id = [ACNetwork offline_docnumber];
  209. dvc.OnSave=^(NSString* file, NSString* subject)
  210. {
  211. [weakSelf previewPDF:file title:json[@"doc_number"] subject:subject];
  212. };
  213. [self.navigationController pushViewController:dvc animated:true];
  214. }];
  215. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  216. DebugLog(@"Cancel");
  217. }];
  218. [alertControl addAction:actionOne];
  219. [alertControl addAction:alertthree];
  220. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  221. [self presentViewController:alertControl animated:YES completion:nil];
  222. }
  223. });
  224. });
  225. }
  226. - (IBAction)onCustomerPerDocClick:(id)sender {
  227. DocumentListViewController* docVC =[self.storyboard instantiateViewControllerWithIdentifier:@"DocumentListViewController" ];
  228. [self.navigationController pushViewController:docVC animated:true];
  229. }
  230. - (void)didReceiveMemoryWarning {
  231. [super didReceiveMemoryWarning];
  232. // Dispose of any resources that can be recreated.
  233. }
  234. -(void) reloadBG
  235. {
  236. UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  237. switch (orientation) {
  238. case UIInterfaceOrientationPortrait:
  239. case UIInterfaceOrientationPortraitUpsideDown:
  240. {
  241. self.coverBG.image = [UIImage imageNamed:@"home_bg_p"];
  242. self.imageBG.image = [UIImage imageNamed:@"home_bg_p"];
  243. break;
  244. }
  245. case UIInterfaceOrientationLandscapeLeft:
  246. case UIInterfaceOrientationLandscapeRight:
  247. {
  248. self.coverBG.image = [UIImage imageNamed:@"home_bg_l"];
  249. self.imageBG.image = [UIImage imageNamed:@"home_bg_l"];
  250. break;
  251. }
  252. default:
  253. self.coverBG.image = [UIImage imageNamed:@"home_bg_p"];
  254. self.imageBG.image = [UIImage imageNamed:@"home_bg_p"];
  255. break;
  256. }
  257. }
  258. -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  259. {
  260. [self reloadBG];
  261. // [self initControl];
  262. // // [self.pdfPageView setNeedsLayout];
  263. // [self.pdfPageView setNeedsDisplay];
  264. // // UIDeviceOrientation dev_ori = [[UIDevice currentDevice]orientation];
  265. //
  266. // // UIApplication * app = [UIApplication sharedApplication];
  267. // // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  268. // // [appDelegate didRotated];
  269. //
  270. // // if (UIInterfaceOrientationIsLandscape(orientation)|| UIInterfaceOrientationIsPortrait(orientation))
  271. // // {
  272. // // self.orientation = orientation;
  273. // // [self.collectionview.collectionViewLayout invalidateLayout];
  274. // // DebugLog(@"routed");
  275. // // }
  276. }
  277. - (void)dealWithUILogout {
  278. self.welcomeLabel.text=@"Not sign in";
  279. // [sender setTitle:@"Sign in" forState:UIControlStateNormal];
  280. [self.ibSignin setTitle:@"Sign in"];
  281. self.buttonHIC.enabled = false;
  282. self.buttonSRL.enabled = false;
  283. self.coverView.hidden = false;
  284. }
  285. - (IBAction)OnSigninClick:(id)sender {
  286. UIBarButtonItem* bbitem = sender;
  287. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  288. if(appDelegate.bLogin)
  289. {
  290. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure to sign out?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  291. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  292. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Sign out"];
  293. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  294. NSDictionary* logout_json = nil;
  295. logout_json = [ACNetwork logout];
  296. dispatch_async(dispatch_get_main_queue(), ^{
  297. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  298. if([[logout_json valueForKey:@"result"] intValue]==2)
  299. {
  300. self.welcomeLabel.text=@"Not sign in";
  301. // [sender setTitle:@"Sign in" forState:UIControlStateNormal];
  302. [bbitem setTitle:@"Sign in"];
  303. self.buttonHIC.enabled = false;
  304. self.buttonSRL.enabled = false;
  305. self.coverView.hidden = false;
  306. }
  307. else
  308. {
  309. [RAUtils message_alert:[logout_json valueForKey:@"err_msg"] title:@"Sign out" controller:self] ;
  310. }
  311. });
  312. });
  313. }];
  314. // [alertControl addAction:actionOne];
  315. [alertControl addAction:alertthree];
  316. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  317. }];
  318. [alertControl addAction:alertcancel];
  319. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  320. [self presentViewController:alertControl animated:YES completion:nil];
  321. }
  322. else
  323. [self presentLogin:sender];
  324. }
  325. -(void) previewPDF:(NSString*)file title:(NSString*)title subject:(NSString*)subject
  326. {
  327. // return;
  328. NSString* name=@"";//[self.content_data[indexPath.row] valueForKey:@"tear_name"];
  329. NSString* url= file;//[self.content_data[indexPath.row] valueForKey:@"pdf_path"];
  330. bool isLocalFile= true;//[self.content_data[indexPath.row] valueForKey:@"isLocalFile"];
  331. PDFViewController *ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PDFViewController"];
  332. ViewController.url = url;
  333. ViewController.canSave = false;
  334. ViewController.isLocalfile=isLocalFile;
  335. ViewController.defaultStyle = true;
  336. ViewController.save_name=title;
  337. ViewController.title = title;
  338. // NSString* subject;
  339. // if (company.length==0) {
  340. // NSString* cur_time =[RAUtils current_date];
  341. // subject =name;//[NSString stringWithFormat:@"NPD Product List %@",cur_time];
  342. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  343. // NSString* customer_email= [appDelegate.customerInfo valueForKey:@"customer_email"];
  344. NSMutableArray* send_to = [[NSMutableArray alloc]init];
  345. // if(customer_email.length>0)
  346. // {
  347. // send_to=[[customer_email componentsSeparatedByString:NSLocalizedString(@";", nil)] mutableCopy];
  348. // // customer_email compo
  349. // // [send_to addObject:customer_email];
  350. // }
  351. ViewController.mail_to = send_to;
  352. // }
  353. // else
  354. // {
  355. // subject = [NSString stringWithFormat:@"Tear sheet for %@",company ];
  356. // }
  357. // ViewController.attachment_name = [NSString stringWithFormat:@"NPD_Product_List %@.pdf",cur_time];
  358. ViewController.mail_subject = subject;
  359. ViewController.hidenavi = false;
  360. [self.navigationController pushViewController:ViewController animated:YES];
  361. }
  362. //- (IBAction)OnHICClick:(id)sender {
  363. //
  364. //
  365. // __weak typeof(self) weakSelf = self;
  366. //
  367. //
  368. // DocumentPageViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DocumentPageViewController" ];
  369. //
  370. // dvc.templateName = @"GEIC - Home Improvement Contract 2016.json";
  371. // dvc.pdfName = @"GEIC - Home Improvement Contract 2017.pdf";
  372. //
  373. // dvc.OnSave=^(NSString* file)
  374. // {
  375. // // [weakSelf previewPDF:file];
  376. // };
  377. //
  378. // [self.navigationController pushViewController:dvc animated:true];
  379. //}
  380. //- (IBAction)OnSRLClick:(id)sender {
  381. //
  382. // DocumentPageViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DocumentPageViewController" ];
  383. //
  384. // dvc.templateName = @"SUBCONTRACTORS REFERRAL LIST.json";
  385. // dvc.pdfName = @"SUBCONTRACTORS REFERRAL LIST.pdf";
  386. // dvc.OnSave=^(NSString* file)
  387. // {
  388. //
  389. // //[self previewPDF:file];
  390. // };
  391. //
  392. // [self.navigationController pushViewController:dvc animated:true];
  393. //}
  394. @end