AddExtDocumentViewController.m 14 KB

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