ModelModeViewController.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // ModelModeViewController.m
  3. // RA Image
  4. //
  5. // Created by Jack on 2017/5/2.
  6. // Copyright © 2017年 USAI. All rights reserved.
  7. //
  8. #import "ModelModeViewController.h"
  9. #import "JLKeyboardListener.h"
  10. #import <AVFoundation/AVFoundation.h>
  11. #import <AssetsLibrary/AssetsLibrary.h>
  12. #import "ManufacturerListController.h"
  13. #import "UploadViewController.h"
  14. #import "AESCrypt.h"
  15. @interface ModelModeViewController ()<UITextViewDelegate>
  16. @property (strong, nonatomic) IBOutlet UITextView *noteTextView;
  17. @property (strong, nonatomic) IBOutlet UILabel *barcodeLabel;
  18. @property (strong, nonatomic) IBOutlet UIButton *imgBtn;
  19. @property (strong, nonatomic) IBOutlet UIButton *manufactureBtn;
  20. @property (strong, nonatomic) IBOutlet UIButton *scanBtn;
  21. @property (strong, nonatomic) IBOutlet UIButton *cameraBtn;
  22. @property (strong, nonatomic) IBOutlet UIButton *uploadBtn;
  23. @property (strong, nonatomic) IBOutlet UIButton *typeBtn;
  24. @property (strong, nonatomic) IBOutlet UILabel *photoCountLabel;
  25. @property (nonatomic,copy) NSString *manufacturer;
  26. @end
  27. @implementation ModelModeViewController
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. // Do any additional setup after loading the view.
  31. self.noteTextView.layer.borderColor = [UIColor blackColor].CGColor;
  32. self.noteTextView.layer.borderWidth = 1.0f;
  33. [self registListenKeyboard];
  34. self.imgBtn.layer.borderColor = [UIColor blackColor].CGColor;
  35. self.imgBtn.layer.borderWidth = 1.0f;
  36. }
  37. - (void)didReceiveMemoryWarning {
  38. [super didReceiveMemoryWarning];
  39. // Dispose of any resources that can be recreated.
  40. }
  41. - (void)clear {
  42. self.barcode = nil;
  43. // self.manufacturer = nil;
  44. self.noteTextView.text = @"Note:";
  45. [self.imgBtn setBackgroundImage:nil forState:UIControlStateNormal];
  46. [self clearPhotos];
  47. }
  48. #pragma mark - Button Action
  49. - (IBAction)imgBtnClick:(UIButton *)sender {
  50. [self showPhotoList];
  51. }
  52. - (IBAction)manufactureBtnClick:(UIButton *)sender {
  53. ManufacturerListController *manuVC = (ManufacturerListController *)[self viewControllerInStoryboard:@"Mode" withId:@"ManufacturerListController"];
  54. __weak typeof(self) weakself = self;
  55. manuVC.returnValue = ^(NSString *value) {
  56. if (weakself) {
  57. __strong typeof(weakself) strongself = weakself;
  58. strongself.manufacturer = value;
  59. }
  60. };
  61. manuVC.manufacturerList = self.manufacturerList;
  62. [self.navigationController pushViewController:manuVC animated:YES];
  63. }
  64. - (IBAction)scanBtnClick:(UIButton *)sender {
  65. [self showScanner];
  66. }
  67. - (IBAction)cameraBtnClick:(id)sender {
  68. [self clickCameraButton];
  69. }
  70. - (IBAction)uploadBtnClick:(id)sender {
  71. if(self.barcode.length ==0)
  72. {
  73. [RAUtils message_alert:[NSString stringWithFormat:@"%@ cann't be blank",self.barcodeTitle] title:@"Warning" controller:self];
  74. return;
  75. }
  76. if(self.photos.count ==0)
  77. {
  78. [RAUtils message_alert:@"You must take at least one photo." title:@"Warning" controller:self];
  79. return;
  80. }
  81. AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  82. NSString *manufacturer = self.manufacturer;
  83. if (self.manufacturerList.count && self.manufacturer.length == 0) {
  84. [RAUtils message_alert:@"Manufacturer does not set." title:@"Warning" controller:self];
  85. return;
  86. }
  87. NSString *note = self.noteTextView.text;
  88. //prepare upload
  89. NSMutableArray* tasks = [NSMutableArray new];
  90. for(NSMutableDictionary* photo in self.photos)
  91. {
  92. NSMutableDictionary* task=[[NSMutableDictionary alloc]init];
  93. task[@"path"]=self.name;
  94. task[@"file"]=photo[@"file"];
  95. task[@"url"]=Appdelegate.address;
  96. NSString *md5 = [photo objectForKey:@"md5"];
  97. NSString* encryptu=[AESCrypt AES128Encrypt:Appdelegate.user key:@"usai"];
  98. NSString* encryptp=[AESCrypt AES128Encrypt:Appdelegate.password key:@"usai"];
  99. NSMutableDictionary* params = [@{
  100. @"user" : encryptu,
  101. @"password" : encryptp,
  102. @"mode":self.name,
  103. @"barcode":self.barcode,
  104. @"_operate":@"upload",
  105. @"platform":@"ios",
  106. @"md5":md5
  107. } mutableCopy];
  108. if (manufacturer) {
  109. [params setObject:manufacturer forKey:@"manufacturer"];
  110. }
  111. if (note) {
  112. [params setObject:note forKey:@"note"];
  113. }
  114. task[@"params"]=params;
  115. NSString *file = photo[@"file"];
  116. [self moveTmpImageToCache:file];
  117. [tasks addObject:task];
  118. }
  119. // // add upload tasks;
  120. [Appdelegate.uploadManager addTasks:tasks];
  121. [self clear];
  122. // UploadViewController *upVC = (UploadViewController *)[self viewControllerInStoryboard:@"Upload" withId:@"UploadViewController"];
  123. // [self.navigationController pushViewController:upVC animated:YES];
  124. }
  125. - (IBAction)typeBtnClick:(UIButton *)sender {
  126. [self showBarcodeInput:UIKeyboardTypeDefault];
  127. }
  128. #pragma mark - Setter
  129. - (void)setManufacturer:(NSString *)manufacturer {
  130. _manufacturer = manufacturer;
  131. NSString *title = @"Select Manufacturer";
  132. if (manufacturer.length) {
  133. title = manufacturer;
  134. }
  135. [self.manufactureBtn setTitle:title forState:UIControlStateNormal];
  136. }
  137. #pragma mark - Super Method
  138. - (void)receiveImage:(UIImage *)img {
  139. [self.imgBtn setBackgroundImage:img forState:UIControlStateNormal];
  140. }
  141. - (void)setBarcode:(NSString *)barcode {
  142. [super setBarcode:barcode];
  143. if (!barcode) {
  144. barcode = @"";
  145. }
  146. self.barcodeLabel.text = [NSString stringWithFormat:@"%@:%@",self.barcodeTitle,barcode];
  147. }
  148. - (void)setPhotoCount:(NSUInteger)photoCount {
  149. [super setPhotoCount:photoCount];
  150. NSString *str = nil;
  151. if (photoCount > 0) {
  152. str = [NSString stringWithFormat:@"%lu Photos",(unsigned long)photoCount];
  153. } else {
  154. str = @"No Photos";
  155. }
  156. self.photoCountLabel.text = str;
  157. }
  158. #pragma mark - TextView Delegate
  159. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
  160. self.currentFirstResponder = textView;
  161. if ([textView.text isEqualToString:@"Note:"]) {
  162. textView.text = nil;
  163. }
  164. return YES;
  165. }
  166. - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
  167. self.currentFirstResponder = nil;
  168. if (!textView.text.length) {
  169. textView.text = @"Note:";
  170. }
  171. return YES;
  172. }
  173. @end