AddExtDocumentViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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_box:@"Upload" message:@"To upload a file, you must share it from Third Party App." completion:nil];
  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. [RAUtils message_box:@"Upload" message:@"Upload list is empty." completion:nil];
  178. return;
  179. }
  180. NSString* docNumber = self.editDocNumber.text;
  181. if(docNumber.length==0)
  182. {
  183. // [RAUtils message_alert:@"Document number is empty." title:@"Upload" controller:self];
  184. [RAUtils message_box:@"Upload" message:@"Document number is empty." completion:nil];
  185. return;
  186. }
  187. NSString* zipfile = nil;
  188. NSMutableArray* arr_files= [[NSMutableArray alloc]init];
  189. for(int i=0;i<self.arr_documents.count;i++)
  190. {
  191. [arr_files addObject:self.arr_documents[i][@"url"] ];
  192. }
  193. zipfile =[RANetwork packfiles2zip:arr_files];
  194. __weak typeof(self) weakSelf = self;
  195. PopWaitAlert* pop =[RAUtils waiting_pop:@"Upload Documents" completion:nil];
  196. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Upload Documents" completion:^{
  197. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  198. NSDictionary* json = nil;
  199. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  200. params[@"doc_number"]=docNumber;
  201. json = [RANetwork upload_file:zipfile params:params];
  202. dispatch_async(dispatch_get_main_queue(), ^{
  203. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  204. // [waitalert dismissViewControllerAnimated:YES completion:^{
  205. [pop hide];
  206. if([[json valueForKey:@"result"] intValue]==2)
  207. {
  208. NSMutableArray* arr = [json[@"documents"] mutableCopy];
  209. if(self.is_present)
  210. [weakSelf dismissViewControllerAnimated:false completion:^{
  211. if(weakSelf.uploadSuccess)
  212. weakSelf.uploadSuccess(arr);
  213. }];
  214. else
  215. {
  216. if(weakSelf.uploadSuccess)
  217. weakSelf.uploadSuccess(arr);
  218. [self.navigationController popViewControllerAnimated:false];
  219. }
  220. // if(self.uploadSuccess)
  221. // self.uploadSuccess(self.arr_documents);
  222. }
  223. else
  224. {
  225. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload Documents" controller:self] ;
  226. [RAUtils message_box:@"Upload Documents" message:[json valueForKey:@"err_msg"] completion:nil];
  227. }
  228. // }];
  229. });
  230. });
  231. // }];
  232. }
  233. #pragma mark - Table view data source
  234. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  235. {
  236. return 44;
  237. }
  238. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  239. {
  240. return 1;
  241. }
  242. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  243. //{
  244. // return 44;
  245. //}
  246. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  247. {
  248. return self.arr_documents.count;
  249. }
  250. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  251. {
  252. NSString *CellIdentifier = @"ADTableViewCell";
  253. UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  254. if (cell == nil) {
  255. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  256. }
  257. NSDictionary * item_json = self.arr_documents[indexPath.row];
  258. NSString* type = item_json[@"type"];
  259. NSString* file_name = item_json[@"file_name"];
  260. NSString* upload_date = item_json[@"upload_date"];
  261. NSString* url = item_json[@"url"];
  262. if([type isEqualToString:@"file"])
  263. cell.imageView.image = [UIImage imageNamed:@"file_icon"];
  264. else
  265. cell.imageView.image = [UIImage imageNamed:@"picture_icon"];
  266. cell.textLabel.text = file_name;
  267. cell.detailTextLabel.text = upload_date;
  268. return cell;
  269. }
  270. #pragma mark tableview delegate
  271. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  272. {
  273. NSDictionary * item_json = self.arr_documents[indexPath.row];
  274. NSString* url = item_json[@"url"];
  275. bool isLocalFile= true;//[self.content_data[indexPath.row] valueForKey:@"isLocalFile"];
  276. RAPDFViewController *ViewController = [[UIStoryboard storyboardWithName:@"wkweb" bundle:nil] instantiateViewControllerWithIdentifier:@"RAPDFViewController"];
  277. ViewController.url = url;
  278. ViewController.canSave = false;
  279. ViewController.isLocalfile=isLocalFile;
  280. ViewController.defaultStyle = true;
  281. ViewController.hidenavi = false;
  282. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:ViewController] ;
  283. navi.modalPresentationStyle = UIModalPresentationFullScreen;//有三种状态,自己看看是哪种
  284. [self presentViewController:navi animated:YES completion:^{
  285. }];
  286. }
  287. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  288. {
  289. // NSDictionary * item_json = self.arr_documents[indexPath.row];
  290. UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  291. [self.arr_documents removeObjectAtIndex:indexPath.row];
  292. [self.tableView reloadData];
  293. }];
  294. deleteAction.backgroundColor = UIColorFromRGB(0xff9933);
  295. return @[deleteAction];
  296. }
  297. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  298. return YES;
  299. }
  300. - (void)tableView:(UITableView *)tableView
  301. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  302. }
  303. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  304. return UITableViewCellEditingStyleDelete;
  305. }
  306. #pragma mark - imagePicker delegate
  307. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
  308. {
  309. // self.dirty = true;
  310. /*-------------------------------相机拍照--------------------------------------*/
  311. if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  312. UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
  313. image=[ImageUtils img_compress:image kbsize:1024];
  314. NSString *path = NSTemporaryDirectory();
  315. NSString *filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]]];
  316. [UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath atomically:YES];
  317. NSMutableDictionary* item = [[NSMutableDictionary alloc]init];
  318. item[@"type"]=@"image";
  319. item[@"url"]= filePath;
  320. item[@"file_name"]= [filePath lastPathComponent];
  321. [self.arr_documents addObject:item];
  322. [self.tableView reloadData];
  323. [picker dismissViewControllerAnimated:YES completion:^{
  324. }];
  325. } else if (picker .sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
  326. /**
  327. * UIImagePickerControllerEditedImage 取得的是被编辑过的图片,需要allowsEditing设置为YES
  328. * UIImagePickerControllerOriginalImage 取得的是相册中原始图片
  329. */
  330. UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
  331. image=[ImageUtils img_compress:image kbsize:1024];
  332. NSString *path = NSTemporaryDirectory();
  333. NSString *filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]]];
  334. [UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath atomically:YES];
  335. NSMutableDictionary* item = [[NSMutableDictionary alloc]init];
  336. item[@"type"]=@"image";
  337. item[@"url"]= filePath;
  338. item[@"file_name"]= [filePath lastPathComponent];
  339. [self.arr_documents addObject:item];
  340. [self.tableView reloadData];
  341. [picker dismissViewControllerAnimated:YES completion:nil];
  342. }
  343. }
  344. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  345. [picker dismissViewControllerAnimated:YES completion:nil];
  346. // [picker presentViewController:cameraPicker animated:YES completion:nil];
  347. // DebugLog(@"%s",__FUNCTION__);
  348. }
  349. /*
  350. #pragma mark - Navigation
  351. // In a storyboard-based application, you will often want to do a little preparation before navigation
  352. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  353. // Get the new view controller using [segue destinationViewController].
  354. // Pass the selected object to the new view controller.
  355. }
  356. */
  357. @end