AddExtDocumentViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. NSMutableArray* menuItem = [NSArray arrayWithObjects:@"aaa",@"bbb",nil];
  106. UIView* v= sender;
  107. RAMenu * menu= [[RAMenu alloc]initWithFrame:CGRectMake(v.frame.origin.x, v.frame.origin.y, 120, 88)];
  108. // menu.backgroundColor= [UIColor whiteColor];
  109. menu.arr_menuItems = menuItem;
  110. [self.view addSubview:menu];
  111. menu.hidden = false;
  112. return;
  113. MenuViewController *menuVC = [[MenuViewController alloc]init];
  114. menuVC.arr_menuItems = [menuItem mutableCopy];
  115. //
  116. // menuVC.title=@"menu";
  117. // signatureVC.signatureData = self.signatureData;
  118. //
  119. // signatureVC.blk_Add = ^()
  120. // {
  121. //
  122. // __block UIImage* signimg=nil;
  123. // SignatureViewController * vc =[ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureViewController"];
  124. //
  125. // vc.onReturnImg = ^(UIImage* img)
  126. // {
  127. //
  128. //
  129. // signimg = img;
  130. //
  131. // // if(signimg!=nil)
  132. // // [PDFUtils addSignature:signimg to:self.signatureData];
  133. //
  134. //
  135. //
  136. //
  137. // };
  138. //
  139. // // orderinfoVC.url_type = URL_REMOTE;
  140. // // orderinfoVC.request_url=URL_CARTDELIVERY;
  141. // //
  142. // // orderinfoVC.params = params;
  143. // //
  144. // // orderinfoVC.delegate=self;
  145. // //
  146. // // if(checked.count==count)
  147. // // {
  148. // // orderinfoVC.have_tail = true
  149. // // }
  150. //
  151. // [self.navigationController pushViewController:vc animated:true];
  152. //
  153. //
  154. //
  155. // };
  156. UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:menuVC];
  157. navi.modalPresentationStyle=UIModalPresentationPopover;
  158. UIPopoverPresentationController* popPc = navi.popoverPresentationController;
  159. popPc.permittedArrowDirections = UIPopoverArrowDirectionUp;
  160. popPc.sourceView = sender;
  161. popPc.delegate = nil;
  162. [self presentViewController:navi animated:true completion:nil];
  163. }
  164. - (IBAction)onAddFileClicked:(id)sender {
  165. [RAUtils alert_view:@"To upload a file, you must share it from thrid-part App." title:@"Upload"];
  166. return;
  167. }
  168. - (IBAction)onUploadClicked:(id)sender {
  169. if(self.arr_documents.count==0)
  170. {
  171. [RAUtils alert_view:@"Upload list is empty." title:@"Upload"];
  172. return;
  173. }
  174. NSString* docNumber = self.editDocNumber.text;
  175. if(docNumber.length==0)
  176. {
  177. [RAUtils alert_view:@"Document number is empty." title:@"Upload"];
  178. return;
  179. }
  180. NSString* zipfile = nil;
  181. NSMutableArray* arr_files= [[NSMutableArray alloc]init];
  182. for(int i=0;i<self.arr_documents.count;i++)
  183. {
  184. [arr_files addObject:self.arr_documents[i][@"url"] ];
  185. }
  186. zipfile =[ACNetwork packfiles2zip:arr_files];
  187. __weak typeof(self) weakSelf = self;
  188. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Upload Documents"];
  189. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  190. NSDictionary* json = nil;
  191. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  192. params[@"doc_number"]=docNumber;
  193. json = [ACNetwork upload_file:zipfile params:params];
  194. dispatch_async(dispatch_get_main_queue(), ^{
  195. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  196. if([[json valueForKey:@"result"] intValue]==2)
  197. {
  198. // if(self.is_present)
  199. [weakSelf dismissViewControllerAnimated:false completion:^{
  200. if(weakSelf.uploadSuccess)
  201. weakSelf.uploadSuccess(weakSelf.arr_documents);
  202. }];
  203. // else
  204. // [self.navigationController popViewControllerAnimated:false];
  205. // if(self.uploadSuccess)
  206. // self.uploadSuccess(self.arr_documents);
  207. }
  208. else
  209. {
  210. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload Documents" controller:self] ;
  211. }
  212. });
  213. });
  214. }
  215. #pragma mark - Table view data source
  216. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  217. {
  218. return 44;
  219. }
  220. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  221. {
  222. return 1;
  223. }
  224. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  225. //{
  226. // return 44;
  227. //}
  228. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  229. {
  230. return self.arr_documents.count;
  231. }
  232. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  233. {
  234. NSString *CellIdentifier = @"ADTableViewCell";
  235. UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  236. if (cell == nil) {
  237. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  238. }
  239. NSDictionary * item_json = self.arr_documents[indexPath.row];
  240. NSString* type = item_json[@"type"];
  241. NSString* file_name = item_json[@"file_name"];
  242. NSString* upload_date = item_json[@"upload_date"];
  243. NSString* url = item_json[@"url"];
  244. if([type isEqualToString:@"file"])
  245. cell.imageView.image = [UIImage imageNamed:@"file_icon"];
  246. else
  247. cell.imageView.image = [UIImage imageNamed:@"picture_icon"];
  248. cell.textLabel.text = file_name;
  249. cell.detailTextLabel.text = upload_date;
  250. return cell;
  251. }
  252. #pragma mark tableview delegate
  253. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  254. {
  255. NSDictionary * item_json = self.arr_documents[indexPath.row];
  256. NSString* url = item_json[@"url"];
  257. bool isLocalFile= true;//[self.content_data[indexPath.row] valueForKey:@"isLocalFile"];
  258. PDFViewController *ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PDFViewController"];
  259. ViewController.url = url;
  260. ViewController.canSave = false;
  261. ViewController.isLocalfile=isLocalFile;
  262. ViewController.defaultStyle = true;
  263. ViewController.hidenavi = false;
  264. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:ViewController] ;
  265. navi.modalPresentationStyle = UIModalPresentationFullScreen;//有三种状态,自己看看是哪种
  266. [self presentViewController:navi animated:YES completion:^{
  267. }];
  268. }
  269. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  270. {
  271. // NSDictionary * item_json = self.arr_documents[indexPath.row];
  272. UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  273. [self.arr_documents removeObjectAtIndex:indexPath.row];
  274. [self.tableView reloadData];
  275. }];
  276. deleteAction.backgroundColor = UIColorFromRGB(0xff9933);
  277. return @[deleteAction];
  278. }
  279. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  280. return YES;
  281. }
  282. - (void)tableView:(UITableView *)tableView
  283. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  284. }
  285. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  286. return UITableViewCellEditingStyleDelete;
  287. }
  288. #pragma mark - imagePicker delegate
  289. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
  290. {
  291. // self.dirty = true;
  292. /*-------------------------------相机拍照--------------------------------------*/
  293. if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  294. UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
  295. image=[ImageUtils img_compress:image kbsize:1024];
  296. NSString *path = NSTemporaryDirectory();
  297. NSString *filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]]];
  298. [UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath atomically:YES];
  299. NSMutableDictionary* item = [[NSMutableDictionary alloc]init];
  300. item[@"type"]=@"image";
  301. item[@"url"]= filePath;
  302. item[@"file_name"]= [filePath lastPathComponent];
  303. [self.arr_documents addObject:item];
  304. [self.tableView reloadData];
  305. [picker dismissViewControllerAnimated:YES completion:^{
  306. }];
  307. } else if (picker .sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
  308. /**
  309. * UIImagePickerControllerEditedImage 取得的是被编辑过的图片,需要allowsEditing设置为YES
  310. * UIImagePickerControllerOriginalImage 取得的是相册中原始图片
  311. */
  312. UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
  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:nil];
  324. }
  325. }
  326. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  327. [picker dismissViewControllerAnimated:YES completion:nil];
  328. // [picker presentViewController:cameraPicker animated:YES completion:nil];
  329. // DebugLog(@"%s",__FUNCTION__);
  330. }
  331. /*
  332. #pragma mark - Navigation
  333. // In a storyboard-based application, you will often want to do a little preparation before navigation
  334. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  335. // Get the new view controller using [segue destinationViewController].
  336. // Pass the selected object to the new view controller.
  337. }
  338. */
  339. @end