ScanSearchViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. //
  2. // ScanSearchViewController.m
  3. // HMLG Scan Order
  4. //
  5. // Created by Rui Zhang on 3/30/22.
  6. // Copyright © 2022 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ScanSearchViewController.h"
  9. #import "ScanListCell.h"
  10. #import "RASingleton.h"
  11. #import "RADataProvider.h"
  12. #define PAGE_ITEM 20000
  13. @interface ScanSearchViewController ()
  14. //@property (nonatomic,assign) UIInterfaceOrientation toOrientation;///<屏幕将要旋转到的方向
  15. @property (nonatomic,strong) NSOperationQueue *dataOperationQueue;
  16. @end
  17. @implementation ScanSearchViewController
  18. - (NSOperationQueue *)dataOperationQueue {
  19. if (!_dataOperationQueue) {
  20. _dataOperationQueue = [[NSOperationQueue alloc] init];
  21. _dataOperationQueue.maxConcurrentOperationCount = 1;
  22. }
  23. return _dataOperationQueue;
  24. }
  25. - (IBAction)onUpdateStock:(id)sender {
  26. [RADataProvider updateStock:self];
  27. }
  28. - (void)viewWillAppear:(BOOL)animated
  29. {
  30. [super viewWillAppear:animated];
  31. self.labelStock.text = [RADataProvider queryStockUpdateTime];
  32. }
  33. -(void) refreshStock
  34. {
  35. // NSDictionary *addressDic1 = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanStock"];
  36. // if (addressDic1) {
  37. // NSString* updatetime=addressDic1[@"updatetime"];
  38. // self.labelStock.text = updatetime;
  39. // }
  40. self.labelStock.text = [RADataProvider queryStockUpdateTime];
  41. [self.tableview reloadData];
  42. }
  43. #ifndef RA_NOTIFICATION
  44. -(void) reload_container_getdata:(bool)update_data
  45. {
  46. if(self.disable_refresh)
  47. return;
  48. [super reload_container_getdata:update_data];
  49. if(update_data)
  50. {
  51. self.offset = 0;
  52. // [self.content_data removeAllObjects];
  53. [self reload];
  54. }
  55. else
  56. {
  57. [self.tableview reloadData];
  58. }
  59. return;
  60. // if(!self.showDetail)
  61. // return;
  62. // NSArray* a= self.childViewControllers;
  63. //
  64. // for (int i=0;i<a.count;i++)
  65. // {
  66. // if([a[i] isKindOfClass:[DetailViewController class]])
  67. // {
  68. // DetailViewController *detailvc=a[i];
  69. // [detailvc reload];
  70. // }
  71. // }
  72. }
  73. #endif
  74. - (void)viewDidLoad {
  75. [super viewDidLoad];
  76. self.modellist = [NSMutableArray new];
  77. // Do any additional setup after loading the view.
  78. //添加四个边阴影
  79. // self.headerView.layer.shadowColor = [UIColor grayColor].CGColor;
  80. // self.headerView.layer.shadowOffset = CGSizeMake(0, 0);
  81. // self.headerView.layer.shadowOpacity = 0.5;
  82. // self.headerView.layer.shadowRadius = 2.0;
  83. //添加两个边阴影
  84. self.tableview.layer.masksToBounds = false;
  85. self.tableview.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.tableview.bounds].CGPath;
  86. self.tableview.layer.shadowColor = [UIColor grayColor].CGColor;
  87. self.tableview.layer.shadowOffset = CGSizeMake(0, 0);
  88. self.tableview.layer.shadowOpacity = 0.5;
  89. self.tableview.layer.shadowRadius = 2.0;
  90. //2024.3
  91. self.tableview.separatorColor = UIColor.blackColor;
  92. //刷新
  93. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  94. ref.tag = 201 ;
  95. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  96. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  97. // ref.hidden = true;
  98. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  99. [self.tableview addSubview:ref];
  100. self.tableview.alwaysBounceVertical = YES;
  101. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clear) name:@"Clear" object:nil];
  102. // 购物车更新后刷新高亮
  103. [[NSNotificationCenter defaultCenter] addObserver:self
  104. selector:@selector(onCartUpdatedRefreshHighlight)
  105. name:@"ScanListShouldRefreshHighlight"
  106. object:nil];
  107. // [self refreshStock];
  108. }
  109. -(void)clear
  110. {
  111. [self.modellist removeAllObjects];
  112. [self.tableview reloadData];
  113. }
  114. - (void)dealloc {
  115. [[NSNotificationCenter defaultCenter] removeObserver:self];
  116. }
  117. -(void)manually_refresh
  118. {
  119. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:201];
  120. if(self.isrefreshing)
  121. {
  122. [reF endRefreshing];
  123. return;
  124. }
  125. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  126. if ([self respondsToSelector:@selector(reload)])
  127. [self performSelector:@selector(reload) withObject:nil afterDelay:1];
  128. }
  129. -(void)viewWillLayoutSubviews
  130. {
  131. // if(self.showDetail==true)
  132. // {
  133. // UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  134. // int width=175;
  135. // // int height;
  136. // if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight)
  137. // {
  138. // width = 370;
  139. // // height = 400;
  140. // }
  141. // else
  142. // {
  143. // // height = 300;
  144. // width = 175;
  145. // }
  146. //
  147. //
  148. // CGRect frame = CGRectMake(0, 64, width, self.view.bounds.size.height-64);
  149. //
  150. // self.collectionview.frame=frame;
  151. //
  152. //
  153. //
  154. // CGRect framedetail = CGRectMake(width,64 ,self.view.bounds.size.width-width,self.view.bounds.size.height-64);
  155. // self.detailView.frame = framedetail;
  156. // }
  157. self.headerView.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.headerView.bounds].CGPath;
  158. self.tableview.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.tableview.bounds].CGPath;
  159. }
  160. /*
  161. #pragma mark - Navigation
  162. // In a storyboard-based application, you will often want to do a little preparation before navigation
  163. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  164. // Get the new view controller using [segue destinationViewController].
  165. // Pass the selected object to the new view controller.
  166. }
  167. */
  168. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  169. if( self.modellist.count==0)
  170. return 0;
  171. return self.modellist.count+1;
  172. }
  173. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  174. if(indexPath.row==self.modellist.count)
  175. return 44;
  176. else
  177. {
  178. // return 166.0f;
  179. UILabel * label = [UILabel new];
  180. NSMutableArray* arr = self.modellist;
  181. NSMutableDictionary* model = arr[indexPath.row];
  182. label.frame = CGRectMake(0, 0, tableView.frame.size.width-32, 0);
  183. label.text = model[@"description"];
  184. label.font = [UIFont systemFontOfSize:15];
  185. label.numberOfLines=0;
  186. label.lineBreakMode = NSLineBreakByWordWrapping;
  187. // double s = label.font.pointSize;
  188. // NSLog(label.text);
  189. // CGSize s=[label sizeThatFits:CGSizeMake(tableView.frame.size.width-32, 0)];
  190. [label sizeToFit];
  191. return label.frame.size.height+109.5;
  192. }
  193. }
  194. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  195. {
  196. if(indexPath.row==self.modellist.count)
  197. {
  198. UITableViewCell *moreCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mores"];
  199. UILabel *moreDataLabel=[[UILabel alloc] init];
  200. moreDataLabel.tag=200;
  201. moreDataLabel.text=self.load_more_hint;
  202. [moreDataLabel setFont:[UIFont systemFontOfSize:14.0f]];
  203. [moreDataLabel setTextAlignment:NSTextAlignmentCenter];
  204. moreDataLabel.frame=CGRectMake(0, 10, self.tableview.bounds.size.width, 20);
  205. [moreDataLabel setBackgroundColor:[UIColor clearColor]];
  206. moreDataLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  207. // [moreDataLabel addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
  208. [moreCell.contentView addSubview:moreDataLabel];
  209. if([self.load_more_hint isEqualToString:@"Load more..."])
  210. [self performSelector:@selector(loadMore) withObject:nil afterDelay:1];
  211. // DebugLog(@"更多...");
  212. moreCell.backgroundColor = [UIColor whiteColor];
  213. return moreCell;
  214. }
  215. else
  216. {
  217. ScanListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ScanListCell"];
  218. if (!cell) {
  219. cell = [[ScanListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ScanListCell"];
  220. }
  221. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  222. [cell setSeparatorInset:UIEdgeInsetsMake(1, 0, 50, 0)];
  223. }
  224. NSMutableArray* arr = self.modellist;
  225. [cell setModelJson:arr[indexPath.row]];
  226. cell.labelStock.text = [RADataProvider queryStock:cell.labelModel.text];
  227. // cell.labelDescription.frame.size;
  228. return cell;
  229. }
  230. }
  231. -(void) loadMore {
  232. __weak typeof(self) weakself = self;
  233. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  234. if (weakself) {
  235. __strong typeof(weakself) strongself = weakself;
  236. [strongself operation_loadMore];
  237. }
  238. }];
  239. [self.dataOperationQueue addOperation:operation];
  240. }
  241. -(void) operation_loadMore
  242. {
  243. if (self.dataOperationQueue.operationCount > 1) {
  244. return;
  245. }
  246. dispatch_async(dispatch_get_main_queue(), ^{
  247. self.isrefreshing=true;
  248. [RADataProvider request_scansearch:self.offset limit:PAGE_ITEM keywords:self.keywords completionHandler:^(NSMutableDictionary *result) {
  249. NSMutableDictionary* category_more=result;
  250. // [self.mum stopAnimating];
  251. self.isrefreshing=false;
  252. if (self.dataOperationQueue.operationCount > 1) {
  253. return;
  254. }
  255. // NSDictionary* more_items=[category_more objectForKey:@"items"];
  256. if([[category_more valueForKey:@"result"] intValue]==2)
  257. {
  258. // self.category_data = category_data;
  259. // NSMutableDictionary* items_json= [[category_more objectForKey:@"items"] mutableCopy];
  260. int count =[[category_more valueForKey:@"count"] intValue];
  261. for(int i=0;i<count;i++)
  262. {
  263. NSMutableDictionary* item = [category_more[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  264. [self.modellist addObject:item];
  265. }
  266. // long newcount=count;
  267. // [items_json setValue: [NSString stringWithFormat:@"%ld",newcount] forKey:@"count"];
  268. // [self.category_data setObject:items_json forKey:@"items"];
  269. self.offset+=count;
  270. if(count<PAGE_ITEM)
  271. {
  272. // int i =self.btnrefresh.state;
  273. self.load_more_hint=@"All loaded";
  274. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"no_more", nil);
  275. // [self.btnrefresh setTitle:@"No more" forState:UIControlStateNormal];
  276. }
  277. else
  278. {
  279. self.load_more_hint=@"Load more...";
  280. // self.btnrefresh.enabled = true;
  281. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"load_more",nil);
  282. // [self.btnrefresh setTitle:@"Load more" forState:UIControlStateNormal];
  283. }
  284. // NSDictionary * item_json = [self.category_data objectForKey:@"items"];
  285. // DebugLog(@"%@",self.category_data);
  286. [self.tableview reloadData];
  287. }
  288. else
  289. {
  290. [RAUtils message_box:@"Search" message:[category_more valueForKey:@"err_msg"] completion:nil];
  291. }
  292. self.isrefreshing=false;
  293. }];
  294. return;
  295. });
  296. }
  297. -(void) reload {
  298. __weak typeof(self) weakself = self;
  299. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  300. if (weakself) {
  301. __strong typeof(weakself) strongself = weakself;
  302. [strongself operation_reload];
  303. }
  304. }];
  305. [self.dataOperationQueue addOperation:operation];
  306. }
  307. -(void) operation_reload
  308. {
  309. if (self.dataOperationQueue.operationCount > 1) {
  310. return;
  311. }
  312. dispatch_async(dispatch_get_main_queue(), ^{
  313. if(self.isrefreshing)
  314. return;
  315. // self.showalert=true;
  316. self.isrefreshing=true;
  317. // self.loadall = false;
  318. UIRefreshControl *reF = (UIRefreshControl *)[self.tableview viewWithTag:201];
  319. [reF endRefreshing];
  320. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  321. // self.label_net_err.hidden=true;
  322. // self.collectionview.hidden=true;
  323. // self.mum.center = self.view.center;
  324. // self.mum.hidden = false;
  325. // [self.mum startAnimating];
  326. // self.loadmore_matchfull = self.switch_matchfull.isOn;
  327. [RADataProvider request_scansearch:0 limit:PAGE_ITEM keywords:self.keywords completionHandler:^(NSMutableDictionary *result) {
  328. NSMutableDictionary* category_data=result;
  329. // [self.mum stopAnimating];
  330. self.isrefreshing=false;
  331. if (self.dataOperationQueue.operationCount > 1) {
  332. return;
  333. }
  334. // self.category_data = [category_data mutableCopy];
  335. // [self.collectionview reloadData];
  336. if([[category_data valueForKey:@"result"] intValue]==2)
  337. {
  338. [self.modellist removeAllObjects];
  339. // self.collectionview.hidden=false;
  340. // NSDictionary * item_json = [category_data objectForKey:@"items"];
  341. int c=[[category_data valueForKey:@"count"] intValue];
  342. if(c==0)
  343. {
  344. [RAUtils message_box:@"Search" message:@"No results found" completion:nil];
  345. return;
  346. }
  347. self.offset = c;
  348. for (int i=0;i<c;i++)
  349. {
  350. NSMutableDictionary* item = [category_data[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  351. [self.modellist addObject:item];
  352. }
  353. if(c<PAGE_ITEM)
  354. {
  355. // int i =self.btnrefresh.state;
  356. self.load_more_hint=@"All loaded";
  357. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"no_more", nil);
  358. // [self.btnrefresh setTitle:@"No more" forState:UIControlStateNormal];
  359. }
  360. else
  361. {
  362. self.load_more_hint=@"Load more...";
  363. // self.btnrefresh.enabled = true;
  364. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"load_more",nil);
  365. // [self.btnrefresh setTitle:@"Load more" forState:UIControlStateNormal];
  366. }
  367. [self.tableview reloadData];
  368. }
  369. else
  370. {
  371. [RAUtils message_box:@"Search" message:[category_data valueForKey:@"err_msg"] completion:nil];
  372. }
  373. self.isrefreshing=false;
  374. }];
  375. return;
  376. });
  377. }
  378. #pragma mark - searchBar delegate;
  379. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  380. {
  381. self.keywords=searchBar.text;
  382. self.offset = 0;
  383. if(self.modellist.count>0)
  384. {
  385. NSIndexPath *indexPathOne = [NSIndexPath indexPathForRow:0 inSection:0];
  386. [self.tableview scrollToRowAtIndexPath:indexPathOne atScrollPosition:UITableViewScrollPositionTop animated:YES];
  387. }
  388. [self.modellist removeAllObjects];
  389. [self.tableview reloadData];
  390. // [self.content_data removeAllObjects];
  391. [self reload];
  392. }
  393. - (void)onCartUpdatedRefreshHighlight {
  394. [self.tableview reloadData];
  395. }
  396. -(void) refresh_ui
  397. {
  398. [self.tableview reloadData];
  399. }
  400. //-(void) clear_data
  401. //{
  402. // [self.modellist removeAllObjects];
  403. // self.offset = 0;
  404. // self.keywords = nil;
  405. // self.searchBar.text = nil;
  406. // [self.tableview reloadData];
  407. //}
  408. @end