AddExtDocumentViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. //
  2. // AddExtDocumentViewController.m
  3. // Granite Expo eSign
  4. //
  5. // Created by Ray on 01/03/2017.
  6. // Copyright © 2017 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "AddExtDocumentViewController.h"
  9. //#import "MenuViewController.h"
  10. //#import "RAMenu.h"
  11. #import "ImageUtils.h"
  12. #import "const.h"
  13. #import "RAUtils.h"
  14. #import "RANetwork.h"
  15. #import "RAPDFViewController.h"
  16. #import "ScannerViewController.h"
  17. @interface AddExtDocumentViewController ()
  18. @end
  19. @implementation AddExtDocumentViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.editDocNumber.text = self.doc_number;
  23. if(self.doc_number.length>0)
  24. {
  25. self.editDocNumber.enabled = false;
  26. self.btnScan.enabled = false;
  27. }
  28. // self.edgesForExtendedLayout = UIRectEdgeNone;
  29. if(self.arr_documents==nil)
  30. self.arr_documents = [[NSMutableArray alloc]init];
  31. if (self.is_present) {
  32. UIBarButtonItem *closeButton =nil;
  33. closeButton=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  34. style:UIBarButtonItemStylePlain
  35. target:self
  36. action:@selector( onCloseClick:)];
  37. self.navigationItem.rightBarButtonItem = closeButton;
  38. }
  39. // else {
  40. // [self dismissViewControllerAnimated:YES completion:nil];
  41. // }
  42. // {
  43. //
  44. //
  45. //
  46. // }
  47. // Do any additional setup after loading the view.
  48. }
  49. -(void) refresh_table
  50. {
  51. [self.tableView reloadData];
  52. }
  53. - (void)didReceiveMemoryWarning {
  54. [super didReceiveMemoryWarning];
  55. // Dispose of any resources that can be recreated.
  56. }
  57. - (IBAction)onCloseClick:(id)sender {
  58. [self dismissViewControllerAnimated:false completion:nil];
  59. }
  60. - (IBAction)onScanClicked:(id)sender {
  61. __weak typeof(self) weakself = self;
  62. ScannerViewController * scannerVC =[ [UIStoryboard storyboardWithName:@"cam_scan" bundle:nil] instantiateViewControllerWithIdentifier:@"ScannerViewController"];;
  63. scannerVC.returnCode = ^(NSString* code){
  64. weakself.editDocNumber.text = code;
  65. };
  66. [self presentViewController:scannerVC animated:false completion:nil];
  67. }
  68. - (IBAction)onAddImageClicked:(id)sender {
  69. __weak typeof(self) weakself = self;
  70. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add Image From" message:nil preferredStyle:UIAlertControllerStyleAlert];
  71. //block代码块取代了delegate
  72. // [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  73. // textField.text = self.save_name;
  74. //
  75. //
  76. // }];
  77. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Album" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  78. NSUInteger sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
  79. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  80. imagePickerController.delegate = weakself;
  81. imagePickerController.allowsEditing = NO;
  82. imagePickerController.sourceType = sourceType;
  83. [self presentViewController:imagePickerController animated:YES completion:^{}];
  84. }];
  85. UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"Take a photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  86. if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  87. {
  88. return;
  89. }
  90. NSUInteger sourceType=UIImagePickerControllerSourceTypeCamera;
  91. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  92. imagePickerController.delegate = weakself;
  93. imagePickerController.allowsEditing = YES;
  94. imagePickerController.sourceType = sourceType;
  95. [self presentViewController:imagePickerController animated:YES completion:^{}];
  96. }];
  97. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  98. }];
  99. [alertControl addAction:actionOne];
  100. [alertControl addAction:action2];
  101. [alertControl addAction:alertthree];
  102. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  103. [self presentViewController:alertControl animated:YES completion:nil];
  104. return;
  105. /*
  106. NSMutableArray* menuItem = [NSArray arrayWithObjects:@"aaa",@"bbb",nil];
  107. UIView* v= sender;
  108. RAMenu * menu= [[RAMenu alloc]initWithFrame:CGRectMake(v.frame.origin.x, v.frame.origin.y, 120, 88)];
  109. // menu.backgroundColor= [UIColor whiteColor];
  110. menu.arr_menuItems = menuItem;
  111. [self.view addSubview:menu];
  112. menu.hidden = false;
  113. return;
  114. MenuViewController *menuVC = [[MenuViewController alloc]init];
  115. menuVC.arr_menuItems = [menuItem mutableCopy];
  116. */
  117. //
  118. // menuVC.title=@"menu";
  119. // signatureVC.signatureData = self.signatureData;
  120. //
  121. // signatureVC.blk_Add = ^()
  122. // {
  123. //
  124. // __block UIImage* signimg=nil;
  125. // SignatureViewController * vc =[ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureViewController"];
  126. //
  127. // vc.onReturnImg = ^(UIImage* img)
  128. // {
  129. //
  130. //
  131. // signimg = img;
  132. //
  133. // // if(signimg!=nil)
  134. // // [PDFUtils addSignature:signimg to:self.signatureData];
  135. //
  136. //
  137. //
  138. //
  139. // };
  140. //
  141. // // orderinfoVC.url_type = URL_REMOTE;
  142. // // orderinfoVC.request_url=URL_CARTDELIVERY;
  143. // //
  144. // // orderinfoVC.params = params;
  145. // //
  146. // // orderinfoVC.delegate=self;
  147. // //
  148. // // if(checked.count==count)
  149. // // {
  150. // // orderinfoVC.have_tail = true
  151. // // }
  152. //
  153. // [self.navigationController pushViewController:vc animated:true];
  154. //
  155. //
  156. //
  157. // };
  158. /*
  159. UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:menuVC];
  160. navi.modalPresentationStyle=UIModalPresentationPopover;
  161. UIPopoverPresentationController* popPc = navi.popoverPresentationController;
  162. popPc.permittedArrowDirections = UIPopoverArrowDirectionUp;
  163. popPc.sourceView = sender;
  164. popPc.delegate = nil;
  165. [self presentViewController:navi animated:true completion:nil];
  166. */
  167. }
  168. - (IBAction)onAddFileClicked:(id)sender {
  169. [RAUtils message_alert:@"To upload a file, you must share it from Third Party App." title:@"Upload" controller:self];
  170. // [RAUtils message_alert:msg title:@"Cannot sign-in" controller:self];
  171. return;
  172. }
  173. - (IBAction)onUploadClicked:(id)sender {
  174. if(self.arr_documents.count==0)
  175. {
  176. [RAUtils message_alert:@"Upload list is empty." title:@"Upload" controller:self];
  177. return;
  178. }
  179. NSString* docNumber = self.editDocNumber.text;
  180. if(docNumber.length==0)
  181. {
  182. [RAUtils message_alert:@"Document number is empty." title:@"Upload" controller:self];
  183. return;
  184. }
  185. NSString* zipfile = nil;
  186. NSMutableArray* arr_files= [[NSMutableArray alloc]init];
  187. for(int i=0;i<self.arr_documents.count;i++)
  188. {
  189. [arr_files addObject:self.arr_documents[i][@"url"] ];
  190. }
  191. zipfile =[RANetwork packfiles2zip:arr_files];
  192. __weak typeof(self) weakSelf = self;
  193. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Upload Documents"];
  194. __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Upload Documents" completion:^{
  195. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  196. NSDictionary* json = nil;
  197. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  198. params[@"doc_number"]=docNumber;
  199. json = [RANetwork upload_file:zipfile params:params];
  200. dispatch_async(dispatch_get_main_queue(), ^{
  201. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  202. [waitalert dismissViewControllerAnimated:YES completion:^{
  203. if([[json valueForKey:@"result"] intValue]==2)
  204. {
  205. NSMutableArray* arr = [json[@"documents"] mutableCopy];
  206. if(self.is_present)
  207. [weakSelf dismissViewControllerAnimated:false completion:^{
  208. if(weakSelf.uploadSuccess)
  209. weakSelf.uploadSuccess(arr);
  210. }];
  211. else
  212. {
  213. if(weakSelf.uploadSuccess)
  214. weakSelf.uploadSuccess(arr);
  215. [self.navigationController popViewControllerAnimated:false];
  216. }
  217. // if(self.uploadSuccess)
  218. // self.uploadSuccess(self.arr_documents);
  219. }
  220. else
  221. {
  222. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload Documents" controller:self] ;
  223. }
  224. }];
  225. });
  226. });
  227. }];
  228. }
  229. #pragma mark - Table view data source
  230. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  231. {
  232. return 44;
  233. }
  234. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  235. {
  236. return 1;
  237. }
  238. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  239. //{
  240. // return 44;
  241. //}
  242. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  243. {
  244. return self.arr_documents.count;
  245. }
  246. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  247. {
  248. NSString *CellIdentifier = @"ADTableViewCell";
  249. UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  250. if (cell == nil) {
  251. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  252. }
  253. NSDictionary * item_json = self.arr_documents[indexPath.row];
  254. NSString* type = item_json[@"type"];
  255. NSString* file_name = item_json[@"file_name"];
  256. NSString* upload_date = item_json[@"upload_date"];
  257. NSString* url = item_json[@"url"];
  258. if([type isEqualToString:@"file"])
  259. cell.imageView.image = [UIImage imageNamed:@"file_icon"];
  260. else
  261. cell.imageView.image = [UIImage imageNamed:@"picture_icon"];
  262. cell.textLabel.text = file_name;
  263. cell.detailTextLabel.text = upload_date;
  264. return cell;
  265. }
  266. #pragma mark tableview delegate
  267. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  268. {
  269. NSDictionary * item_json = self.arr_documents[indexPath.row];
  270. NSString* url = item_json[@"url"];
  271. bool isLocalFile= true;//[self.content_data[indexPath.row] valueForKey:@"isLocalFile"];
  272. RAPDFViewController *ViewController = [[UIStoryboard storyboardWithName:@"wkweb" bundle:nil] instantiateViewControllerWithIdentifier:@"RAPDFViewController"];
  273. ViewController.url = url;
  274. ViewController.canSave = false;
  275. ViewController.isLocalfile=isLocalFile;
  276. ViewController.defaultStyle = true;
  277. ViewController.hidenavi = false;
  278. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:ViewController] ;
  279. navi.modalPresentationStyle = UIModalPresentationFullScreen;//有三种状态,自己看看是哪种
  280. [self presentViewController:navi animated:YES completion:^{
  281. }];
  282. }
  283. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  284. {
  285. // NSDictionary * item_json = self.arr_documents[indexPath.row];
  286. UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  287. [self.arr_documents removeObjectAtIndex:indexPath.row];
  288. [self.tableView reloadData];
  289. }];
  290. deleteAction.backgroundColor = UIColorFromRGB(0xff9933);
  291. return @[deleteAction];
  292. }
  293. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  294. return YES;
  295. }
  296. - (void)tableView:(UITableView *)tableView
  297. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  298. }
  299. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  300. return UITableViewCellEditingStyleDelete;
  301. }
  302. #pragma mark - imagePicker delegate
  303. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
  304. {
  305. // self.dirty = true;
  306. /*-------------------------------相机拍照--------------------------------------*/
  307. if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  308. UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
  309. image=[ImageUtils img_compress:image kbsize:1024];
  310. NSString *path = NSTemporaryDirectory();
  311. NSString *filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]]];
  312. [UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath atomically:YES];
  313. NSMutableDictionary* item = [[NSMutableDictionary alloc]init];
  314. item[@"type"]=@"image";
  315. item[@"url"]= filePath;
  316. item[@"file_name"]= [filePath lastPathComponent];
  317. [self.arr_documents addObject:item];
  318. [self.tableView reloadData];
  319. [picker dismissViewControllerAnimated:YES completion:^{
  320. }];
  321. } else if (picker .sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
  322. /**
  323. * UIImagePickerControllerEditedImage 取得的是被编辑过的图片,需要allowsEditing设置为YES
  324. * UIImagePickerControllerOriginalImage 取得的是相册中原始图片
  325. */
  326. UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
  327. image=[ImageUtils img_compress:image kbsize:1024];
  328. NSString *path = NSTemporaryDirectory();
  329. NSString *filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]]];
  330. [UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath atomically:YES];
  331. NSMutableDictionary* item = [[NSMutableDictionary alloc]init];
  332. item[@"type"]=@"image";
  333. item[@"url"]= filePath;
  334. item[@"file_name"]= [filePath lastPathComponent];
  335. [self.arr_documents addObject:item];
  336. [self.tableView reloadData];
  337. [picker dismissViewControllerAnimated:YES completion:nil];
  338. }
  339. }
  340. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  341. [picker dismissViewControllerAnimated:YES completion:nil];
  342. // [picker presentViewController:cameraPicker animated:YES completion:nil];
  343. // DebugLog(@"%s",__FUNCTION__);
  344. }
  345. /*
  346. #pragma mark - Navigation
  347. // In a storyboard-based application, you will often want to do a little preparation before navigation
  348. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  349. // Get the new view controller using [segue destinationViewController].
  350. // Pass the selected object to the new view controller.
  351. }
  352. */
  353. @end