AddExtDocumentViewController.m 16 KB

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