ScanSearchViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. #ifndef RA_NOTIFICATION
  26. -(void) reload_container_getdata:(bool)update_data
  27. {
  28. if(self.disable_refresh)
  29. return;
  30. [super reload_container_getdata:update_data];
  31. if(update_data)
  32. {
  33. self.offset = 0;
  34. // [self.content_data removeAllObjects];
  35. [self reload];
  36. }
  37. else
  38. {
  39. [self.tableview reloadData];
  40. }
  41. return;
  42. // if(!self.showDetail)
  43. // return;
  44. // NSArray* a= self.childViewControllers;
  45. //
  46. // for (int i=0;i<a.count;i++)
  47. // {
  48. // if([a[i] isKindOfClass:[DetailViewController class]])
  49. // {
  50. // DetailViewController *detailvc=a[i];
  51. // [detailvc reload];
  52. // }
  53. // }
  54. }
  55. #endif
  56. - (void)viewDidLoad {
  57. [super viewDidLoad];
  58. self.modellist = [NSMutableArray new];
  59. // Do any additional setup after loading the view.
  60. //添加四个边阴影
  61. // self.headerView.layer.shadowColor = [UIColor grayColor].CGColor;
  62. // self.headerView.layer.shadowOffset = CGSizeMake(0, 0);
  63. // self.headerView.layer.shadowOpacity = 0.5;
  64. // self.headerView.layer.shadowRadius = 2.0;
  65. //添加两个边阴影
  66. self.tableview.layer.masksToBounds = false;
  67. self.tableview.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.tableview.bounds].CGPath;
  68. self.tableview.layer.shadowColor = [UIColor grayColor].CGColor;
  69. self.tableview.layer.shadowOffset = CGSizeMake(0, 0);
  70. self.tableview.layer.shadowOpacity = 0.5;
  71. self.tableview.layer.shadowRadius = 2.0;
  72. //刷新
  73. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  74. ref.tag = 201 ;
  75. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  76. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  77. // ref.hidden = true;
  78. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  79. [self.tableview addSubview:ref];
  80. self.tableview.alwaysBounceVertical = YES;
  81. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clear) name:@"Clear" object:nil];
  82. }
  83. -(void)clear
  84. {
  85. [self.modellist removeAllObjects];
  86. [self.tableview reloadData];
  87. }
  88. - (void)dealloc {
  89. [[NSNotificationCenter defaultCenter] removeObserver:self];
  90. }
  91. -(void)manually_refresh
  92. {
  93. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:201];
  94. if(self.isrefreshing)
  95. {
  96. [reF endRefreshing];
  97. return;
  98. }
  99. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  100. if ([self respondsToSelector:@selector(reload)])
  101. [self performSelector:@selector(reload) withObject:nil afterDelay:1];
  102. }
  103. -(void)viewWillLayoutSubviews
  104. {
  105. // if(self.showDetail==true)
  106. // {
  107. // UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  108. // int width=175;
  109. // // int height;
  110. // if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight)
  111. // {
  112. // width = 370;
  113. // // height = 400;
  114. // }
  115. // else
  116. // {
  117. // // height = 300;
  118. // width = 175;
  119. // }
  120. //
  121. //
  122. // CGRect frame = CGRectMake(0, 64, width, self.view.bounds.size.height-64);
  123. //
  124. // self.collectionview.frame=frame;
  125. //
  126. //
  127. //
  128. // CGRect framedetail = CGRectMake(width,64 ,self.view.bounds.size.width-width,self.view.bounds.size.height-64);
  129. // self.detailView.frame = framedetail;
  130. // }
  131. self.headerView.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.headerView.bounds].CGPath;
  132. self.tableview.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.tableview.bounds].CGPath;
  133. }
  134. /*
  135. #pragma mark - Navigation
  136. // In a storyboard-based application, you will often want to do a little preparation before navigation
  137. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  138. // Get the new view controller using [segue destinationViewController].
  139. // Pass the selected object to the new view controller.
  140. }
  141. */
  142. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  143. if( self.modellist.count==0)
  144. return 0;
  145. return self.modellist.count+1;
  146. }
  147. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  148. if(indexPath.row==self.modellist.count)
  149. return 44;
  150. else
  151. return 136.0f;
  152. }
  153. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  154. {
  155. if(indexPath.row==self.modellist.count)
  156. {
  157. UITableViewCell *moreCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mores"];
  158. UILabel *moreDataLabel=[[UILabel alloc] init];
  159. moreDataLabel.tag=200;
  160. moreDataLabel.text=self.load_more_hint;
  161. [moreDataLabel setFont:[UIFont systemFontOfSize:14.0f]];
  162. [moreDataLabel setTextAlignment:NSTextAlignmentCenter];
  163. moreDataLabel.frame=CGRectMake(0, 10, self.tableview.bounds.size.width, 20);
  164. [moreDataLabel setBackgroundColor:[UIColor clearColor]];
  165. moreDataLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  166. // [moreDataLabel addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
  167. [moreCell.contentView addSubview:moreDataLabel];
  168. if([self.load_more_hint isEqualToString:@"Load more..."])
  169. [self performSelector:@selector(loadMore) withObject:nil afterDelay:1];
  170. // DebugLog(@"更多...");
  171. moreCell.backgroundColor = [UIColor whiteColor];
  172. return moreCell;
  173. }
  174. else
  175. {
  176. ScanListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ScanListCell"];
  177. if (!cell) {
  178. cell = [[ScanListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ScanListCell"];
  179. }
  180. NSMutableArray* arr = self.modellist;
  181. [cell setModelJson:arr[indexPath.row]];
  182. return cell;
  183. }
  184. }
  185. -(void) loadMore {
  186. __weak typeof(self) weakself = self;
  187. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  188. if (weakself) {
  189. __strong typeof(weakself) strongself = weakself;
  190. [strongself operation_loadMore];
  191. }
  192. }];
  193. [self.dataOperationQueue addOperation:operation];
  194. }
  195. -(void) operation_loadMore
  196. {
  197. if (self.dataOperationQueue.operationCount > 1) {
  198. return;
  199. }
  200. dispatch_async(dispatch_get_main_queue(), ^{
  201. // if(self.loadall)
  202. // {
  203. //// if(self.showalert)
  204. // {
  205. //// self.showalert=false;
  206. // [RAUtils message_alert:@"All loaded" title:@"Loading items" controller:self] ;
  207. // }
  208. // return;
  209. // }
  210. //
  211. // self.mum.hidden = false;
  212. // [self.mum startAnimating];
  213. // self.mum.center = self.view.center;
  214. self.isrefreshing=true;
  215. [RADataProvider request_scansearch:self.offset limit:PAGE_ITEM keywords:self.keywords completionHandler:^(NSMutableDictionary *result) {
  216. NSMutableDictionary* category_more=result;
  217. // [self.mum stopAnimating];
  218. self.isrefreshing=false;
  219. if (self.dataOperationQueue.operationCount > 1) {
  220. return;
  221. }
  222. // NSDictionary* more_items=[category_more objectForKey:@"items"];
  223. if([[category_more valueForKey:@"result"] intValue]==2)
  224. {
  225. // self.category_data = category_data;
  226. // NSMutableDictionary* items_json= [[category_more objectForKey:@"items"] mutableCopy];
  227. int count =[[category_more valueForKey:@"count"] intValue];
  228. for(int i=0;i<count;i++)
  229. {
  230. NSMutableDictionary* item = [category_more[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  231. [self.modellist addObject:item];
  232. }
  233. // long newcount=count;
  234. // [items_json setValue: [NSString stringWithFormat:@"%ld",newcount] forKey:@"count"];
  235. // [self.category_data setObject:items_json forKey:@"items"];
  236. self.offset+=count;
  237. if(count<PAGE_ITEM)
  238. {
  239. // int i =self.btnrefresh.state;
  240. self.load_more_hint=@"All loaded";
  241. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"no_more", nil);
  242. // [self.btnrefresh setTitle:@"No more" forState:UIControlStateNormal];
  243. }
  244. else
  245. {
  246. self.load_more_hint=@"Load more...";
  247. // self.btnrefresh.enabled = true;
  248. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"load_more",nil);
  249. // [self.btnrefresh setTitle:@"Load more" forState:UIControlStateNormal];
  250. }
  251. // NSDictionary * item_json = [self.category_data objectForKey:@"items"];
  252. // DebugLog(@"%@",self.category_data);
  253. [self.tableview reloadData];
  254. }
  255. else
  256. {
  257. [RAUtils message_alert:[category_more valueForKey:@"err_msg"] title:@"Search" controller:self] ;
  258. }
  259. self.isrefreshing=false;
  260. }];
  261. return;
  262. });
  263. }
  264. -(void) reload {
  265. __weak typeof(self) weakself = self;
  266. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  267. if (weakself) {
  268. __strong typeof(weakself) strongself = weakself;
  269. [strongself operation_reload];
  270. }
  271. }];
  272. [self.dataOperationQueue addOperation:operation];
  273. }
  274. -(void) operation_reload
  275. {
  276. if (self.dataOperationQueue.operationCount > 1) {
  277. return;
  278. }
  279. dispatch_async(dispatch_get_main_queue(), ^{
  280. if(self.isrefreshing)
  281. return;
  282. // self.showalert=true;
  283. self.isrefreshing=true;
  284. // self.loadall = false;
  285. UIRefreshControl *reF = (UIRefreshControl *)[self.tableview viewWithTag:201];
  286. [reF endRefreshing];
  287. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  288. // self.label_net_err.hidden=true;
  289. // self.collectionview.hidden=true;
  290. // self.mum.center = self.view.center;
  291. // self.mum.hidden = false;
  292. // [self.mum startAnimating];
  293. // self.loadmore_matchfull = self.switch_matchfull.isOn;
  294. [RADataProvider request_scansearch:0 limit:PAGE_ITEM keywords:self.keywords completionHandler:^(NSMutableDictionary *result) {
  295. NSMutableDictionary* category_data=result;
  296. // [self.mum stopAnimating];
  297. self.isrefreshing=false;
  298. if (self.dataOperationQueue.operationCount > 1) {
  299. return;
  300. }
  301. // self.category_data = [category_data mutableCopy];
  302. // [self.collectionview reloadData];
  303. if([[category_data valueForKey:@"result"] intValue]==2)
  304. {
  305. // self.collectionview.hidden=false;
  306. // NSDictionary * item_json = [category_data objectForKey:@"items"];
  307. int c=[[category_data valueForKey:@"count"] intValue];
  308. if(c==0)
  309. {
  310. [RAUtils message_alert:@"No results found" title:@"Search" controller:self];
  311. return;
  312. }
  313. self.offset = c;
  314. for (int i=0;i<c;i++)
  315. {
  316. NSMutableDictionary* item = [category_data[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  317. [self.modellist addObject:item];
  318. }
  319. if(c<PAGE_ITEM)
  320. {
  321. // int i =self.btnrefresh.state;
  322. self.load_more_hint=@"All loaded";
  323. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"no_more", nil);
  324. // [self.btnrefresh setTitle:@"No more" forState:UIControlStateNormal];
  325. }
  326. else
  327. {
  328. self.load_more_hint=@"Load more...";
  329. // self.btnrefresh.enabled = true;
  330. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"load_more",nil);
  331. // [self.btnrefresh setTitle:@"Load more" forState:UIControlStateNormal];
  332. }
  333. [self.tableview reloadData];
  334. // int total = [[self.category_data valueForKey:@"item_total_count"]intValue];
  335. //
  336. // if(total<2)
  337. // {
  338. // self.labelTotal.text = [NSString stringWithFormat:@"Total %d item",total];
  339. // if(total==1)
  340. // {
  341. // NSDictionary * detail_item = [item_json objectForKey:@"item_0"];
  342. // NSString* detail_id= [detail_item valueForKey:@"product_id"] ;
  343. //
  344. // [self showDetailat:detail_id category_id:nil name:[detail_item objectForKey:@"fash_name"] index:0];
  345. // }
  346. // }
  347. // else
  348. // self.labelTotal.text = [NSString stringWithFormat:@"Total %d items",total];
  349. }
  350. else
  351. {
  352. [RAUtils message_alert:[category_data valueForKey:@"err_msg"] title:@"Search" controller:self] ;
  353. }
  354. self.isrefreshing=false;
  355. }];
  356. return;
  357. });
  358. }
  359. #pragma mark - searchBar delegate;
  360. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  361. {
  362. self.keywords=searchBar.text;
  363. self.offset = 0;
  364. [self.modellist removeAllObjects];
  365. [self.tableview reloadData];
  366. // [self.content_data removeAllObjects];
  367. [self reload];
  368. }
  369. @end