ItemSearchViewController.m 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. //
  2. // ItemSearchViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 10/26/15.
  6. // Copyright © 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ItemSearchViewController.h"
  9. #import "const.h"
  10. #import "iSalesNetwork.h"
  11. #import "CategoryCellNPD.h"
  12. #import "DetailViewController.h"
  13. #import "ItemSearchFilterViewController.h"
  14. #import "ContactListViewController.h"
  15. #import "MainViewController.h"
  16. #define DEF_CELL_HEIGHT 44
  17. #define DEF_TABLE_HEIGHT 44
  18. #define LINE_WIDTH 0
  19. #define CELL_MARGIN 0
  20. #define LABEL_MARGIN 5
  21. @interface ItemSearchViewController ()
  22. @end
  23. @implementation ItemSearchViewController
  24. -(void) reload_container_getdata:(bool)update_data
  25. {
  26. if(self.disable_refresh)
  27. return;
  28. [super reload_container_getdata:update_data];
  29. // self.keywords=nil;
  30. if(update_data)
  31. {
  32. self.offset = 0;
  33. // [self.content_data removeAllObjects];
  34. [self reload];
  35. }
  36. else
  37. [self.collectionview reloadData];
  38. return;
  39. // if(!self.showDetail)
  40. // return;
  41. // NSArray* a= self.childViewControllers;
  42. //
  43. // for (int i=0;i<a.count;i++)
  44. // {
  45. // if([a[i] isKindOfClass:[DetailViewController class]])
  46. // {
  47. // DetailViewController *detailvc=a[i];
  48. // [detailvc reload];
  49. // }
  50. // }
  51. }
  52. -(void)begin_select
  53. {
  54. self.addCartBtn.enabled=false;
  55. self.addWishBtn.enabled=false;
  56. self.addPortfolioBtn.enabled=false;
  57. self.selectToolbar.hidden=false;
  58. // self.collectionview.allowsMultipleSelection = YES;
  59. self.isSelectionMode=true;
  60. self.select_count=0;
  61. }
  62. -(void)end_select:(int) type
  63. {
  64. self.select_count=0;
  65. self.selectToolbar.hidden=true;
  66. // self.collectionview.allowsMultipleSelection = NO;
  67. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  68. int count = [[items valueForKey:@"count"] intValue];
  69. for(int i=0;i<count;i++)
  70. {
  71. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  72. bool check = [[item valueForKey:@"checked"] boolValue];
  73. if(check)
  74. {
  75. switch (type) {
  76. case 1:
  77. item[@"wish_exists"]=@"true";
  78. break;
  79. case 2:
  80. item[@"cart_exists"]=@"true";
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. [item setValue:@"false" forKey:@"checked"];
  87. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  88. }
  89. [self.category_data setObject:items forKey:@"items"];
  90. self.selectallBtn.tag = 0;
  91. [self.selectallBtn setTitle:@"Select all"];
  92. self.isSelectionMode=false;
  93. if(self.disable_refresh)
  94. {
  95. self.disable_refresh = false;
  96. [self reload_container_getdata:true];
  97. }
  98. else
  99. [self.collectionview reloadData];
  100. }
  101. - (IBAction)onSelectClick:(id)sender {
  102. [self begin_select];
  103. }
  104. - (IBAction)onCancelSelectClick:(id)sender {
  105. [self end_select:0];
  106. }
  107. - (IBAction)onSelectAllClick:(id)sender {
  108. // int tag =self.selectallBtn.tag;
  109. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  110. int count = [[items valueForKey:@"count"] intValue];
  111. NSString* checked;
  112. if (self.selectallBtn.tag == 0)
  113. {
  114. self.selectallBtn.tag = 1;
  115. [self.selectallBtn setTitle:@"Deselect all"];
  116. if(count>0)
  117. {
  118. self.addWishBtn.enabled = true;
  119. self.addCartBtn.enabled = true;
  120. self.addPortfolioBtn.enabled=true;
  121. }
  122. self.select_count=count;
  123. checked=@"true";
  124. }
  125. else
  126. {
  127. self.selectallBtn.tag = 0;
  128. [self.selectallBtn setTitle:@"Select all"];
  129. self.addWishBtn.enabled = false;
  130. self.addCartBtn.enabled = false;
  131. self.addPortfolioBtn.enabled=false;
  132. checked=@"false";
  133. }
  134. for(int i=0;i<count;i++)
  135. {
  136. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  137. [item setValue:checked forKey:@"checked"];
  138. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  139. }
  140. [self.category_data setObject:items forKey:@"items"];
  141. [self.collectionview reloadData];
  142. }
  143. - (void)viewDidLoad {
  144. [super viewDidLoad];
  145. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  146. self.label_net_err.layer.borderWidth = 2.0;
  147. self.label_net_err.layer.cornerRadius=15;
  148. self.label_net_err.layer.masksToBounds=true;
  149. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  150. // tap.minimumPressDuration = 0.8; //定义按的时间
  151. [self.label_net_err addGestureRecognizer:tap];
  152. self.categoryViewType = CATEGORY_VIEWTYPE_SMALL;
  153. UICollectionViewFlowLayout* layout =(UICollectionViewFlowLayout*)self.collectionview.collectionViewLayout;
  154. CGSize cellsize = CGSizeMake(175, 250);
  155. layout.itemSize =cellsize;
  156. layout.minimumLineSpacing = 15;
  157. self.collectionview.collectionViewLayout = layout;
  158. //
  159. self.display_type = [[NSMutableDictionary alloc] init];
  160. [self.display_type setValue:@3 forKey:@"count"];
  161. NSMutableDictionary* val_0 = [[NSMutableDictionary alloc] init];
  162. [val_0 setValue:@1 forKey:@"check"];
  163. [val_0 setValue:@"small icon" forKey:@"value"];
  164. [val_0 setValue:@"" forKey:@"img"];
  165. [self.display_type setObject:val_0 forKey:@"val_0"];
  166. NSMutableDictionary* val_1 = [[NSMutableDictionary alloc] init];
  167. [val_1 setValue:@0 forKey:@"check"];
  168. [val_1 setValue:@"large icon" forKey:@"value"];
  169. [val_1 setValue:@"" forKey:@"img"];
  170. [self.display_type setObject:val_1 forKey:@"val_1"];
  171. NSMutableDictionary* val_2 = [[NSMutableDictionary alloc] init];
  172. [val_2 setValue:@2 forKey:@"check"];
  173. [val_2 setValue:@"detail" forKey:@"value"];
  174. [val_2 setValue:@"" forKey:@"img"];
  175. [self.display_type setObject:val_2 forKey:@"val_2"];
  176. // self.collectionview.
  177. // self.cagegoryLabel.text = self.categoryString;
  178. self.showDetail = false;
  179. //添加边框
  180. // CALayer *layer = [self.collectionview layer];
  181. // layer.borderColor = [[UIColor redColor] CGColor];
  182. // layer.borderWidth = 5.0f;
  183. self.headerView.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.headerView.bounds].CGPath;
  184. self.headerView.layer.masksToBounds = false;
  185. //添加四个边阴影
  186. self.headerView.layer.shadowColor = [UIColor grayColor].CGColor;
  187. self.headerView.layer.shadowOffset = CGSizeMake(0, 0);
  188. self.headerView.layer.shadowOpacity = 0.5;
  189. self.headerView.layer.shadowRadius = 2.0;
  190. //添加两个边阴影
  191. self.collectionview.layer.masksToBounds = false;
  192. self.collectionview.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.collectionview.bounds].CGPath;
  193. self.collectionview.layer.shadowColor = [UIColor grayColor].CGColor;
  194. self.collectionview.layer.shadowOffset = CGSizeMake(0, 0);
  195. self.collectionview.layer.shadowOpacity = 0.5;
  196. self.collectionview.layer.shadowRadius = 2.0;
  197. // Do any additional setup after loading the view.
  198. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  199. ref.tag = 200 ;
  200. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  201. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  202. // ref.hidden = true;
  203. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  204. [self.collectionview addSubview:ref];
  205. }
  206. -(void)manually_refresh
  207. {
  208. UIRefreshControl *reF = (UIRefreshControl *)[self.collectionview viewWithTag:200];
  209. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  210. [self performSelector:@selector(reload) withObject:nil afterDelay:1];
  211. // DebugLog(@"refresh!!!!!!!!");
  212. }
  213. - (IBAction)onFilterClicked:(id)sender {
  214. ItemSearchFilterViewController * filterVC =[ self.storyboard instantiateViewControllerWithIdentifier:@"ItemSearchFilterViewController"];
  215. filterVC.categoryMenu = [[self.category_data objectForKey:@"filter"] objectForKey:@"category"];
  216. filterVC.alert_cadedate =[[self.category_data objectForKey:@"filter"] objectForKey:@"alert"];
  217. filterVC.modelname=[[self.category_data objectForKey:@"filter"] valueForKey:@"model_name"];
  218. filterVC.modeldescrip=[[self.category_data objectForKey:@"filter"] valueForKey:@"model_description"];
  219. filterVC.SearchClick =^(NSString* ids,NSString* modelname,NSString* modeldescrip,NSString* alert)
  220. {
  221. self.ctgid = ids;
  222. self.modelname = modelname;
  223. self.modeldescrip = modeldescrip;
  224. self.alert = alert;
  225. self.covertype = nil;
  226. self.offset = 0;
  227. // [self.content_data removeAllObjects];
  228. [self reload];
  229. NSLog(@"search");
  230. };
  231. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:filterVC] ;
  232. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  233. [self presentViewController:navi animated:YES completion:^{
  234. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  235. NSLog(@"bundle present.........");
  236. // self.btop = false;
  237. // <#code#>
  238. }];
  239. }
  240. -(void)viewWillLayoutSubviews
  241. {
  242. // if(self.showDetail==true)
  243. // {
  244. // UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  245. // int width=175;
  246. // // int height;
  247. // if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight)
  248. // {
  249. // width = 370;
  250. // // height = 400;
  251. // }
  252. // else
  253. // {
  254. // // height = 300;
  255. // width = 175;
  256. // }
  257. //
  258. //
  259. // CGRect frame = CGRectMake(0, 64, width, self.view.bounds.size.height-64);
  260. //
  261. // self.collectionview.frame=frame;
  262. //
  263. //
  264. //
  265. // CGRect framedetail = CGRectMake(width,64 ,self.view.bounds.size.width-width,self.view.bounds.size.height-64);
  266. // self.detailView.frame = framedetail;
  267. // }
  268. self.headerView.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.headerView.bounds].CGPath;
  269. self.collectionview.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.collectionview.bounds].CGPath;
  270. }
  271. - (void)viewWillAppear:(BOOL)animated
  272. {
  273. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  274. }
  275. - (void)didReceiveMemoryWarning {
  276. [super didReceiveMemoryWarning];
  277. // Dispose of any resources that can be recreated.
  278. }
  279. -(void) loadMore
  280. {
  281. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  282. self.isrefreshing=true;
  283. NSDictionary* category_more=[iSalesNetwork ItemSearch:self.offset limit:20 covertype:self.covertype ctgid:self.ctgid modelname:self.modelname modeldescrip:self.modeldescrip alert:self.alert available:self.available
  284. ];
  285. dispatch_async(dispatch_get_main_queue(), ^{
  286. NSDictionary* more_items=[category_more objectForKey:@"items"];
  287. if([[category_more valueForKey:@"result"] intValue]==2)
  288. {
  289. // self.category_data = category_data;
  290. NSMutableDictionary* items_json= [[self.category_data objectForKey:@"items"] mutableCopy];
  291. int count =[[more_items valueForKey:@"count"] intValue];
  292. for(int i=0;i<count;i++)
  293. {
  294. NSDictionary * item_json = [more_items objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  295. [items_json setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",i+self.offset]];
  296. }
  297. long newcount=[[[self.category_data objectForKey:@"items"] valueForKey:@"count"] intValue]+count;
  298. [items_json setValue: [NSString stringWithFormat:@"%ld",newcount] forKey:@"count"];
  299. [self.category_data setObject:items_json forKey:@"items"];
  300. self.offset+=count;
  301. // NSDictionary * item_json = [self.category_data objectForKey:@"items"];
  302. NSLog(@"%@",self.category_data);
  303. [self.collectionview reloadData];
  304. }
  305. else
  306. {
  307. [RAUtils message_alert:[category_more valueForKey:@"err_msg"] title:@"Search" controller:self] ;
  308. }
  309. self.isrefreshing=false;
  310. });
  311. // dispatch_async(dispatch_get_main_queue(), ^{
  312. // // self.category_data = category_data;
  313. //
  314. //
  315. // if([[category_more valueForKey:@"result"] intValue]==2)
  316. // {
  317. // NSMutableDictionary* items_json= [[self.category_data objectForKey:@"items"] mutableCopy];
  318. //
  319. // int count =[[category_more valueForKey:@"count"] intValue];
  320. // for(int i=0;i<count;i++)
  321. // {
  322. //
  323. // NSDictionary * item_json = [category_more objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  324. // [items_json setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",i+self.offset]];
  325. //
  326. //
  327. // }
  328. // long newcount=[[[self.category_data objectForKey:@"items"] valueForKey:@"count"] intValue]+count;
  329. // [items_json setValue: [NSString stringWithFormat:@"%ld",newcount] forKey:@"count"];
  330. // [self.category_data setObject:items_json forKey:@"items"];
  331. // self.offset+=count;
  332. // // NSDictionary * item_json = [self.category_data objectForKey:@"items"];
  333. //
  334. //
  335. //
  336. //
  337. // NSLog(@"%@",self.category_data);
  338. //
  339. // [self.collectionview reloadData];
  340. // }
  341. // else
  342. // {
  343. // [RAUtils error_alert:[category_more valueForKey:@"err_msg"]] ;
  344. // }
  345. //
  346. //
  347. //
  348. //
  349. // });
  350. });
  351. }
  352. -(void) reload
  353. {
  354. dispatch_async(dispatch_get_main_queue(), ^{
  355. if(self.isrefreshing)
  356. return;
  357. self.isrefreshing=true;
  358. UIRefreshControl *reF = (UIRefreshControl *)[self.collectionview viewWithTag:200];
  359. [reF endRefreshing];
  360. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  361. self.label_net_err.hidden=true;
  362. self.collectionview.hidden = true;
  363. self.mum.center = self.view.center;
  364. self.mum.hidden = false;
  365. [self.mum startAnimating];
  366. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  367. NSDictionary* category_data=[iSalesNetwork ItemSearch:0 limit:20 covertype:self.covertype ctgid:self.ctgid modelname:self.modelname modeldescrip:self.modeldescrip alert:self.alert available:self.available
  368. ];
  369. dispatch_async(dispatch_get_main_queue(), ^{
  370. [self.mum stopAnimating];
  371. self.category_data = [category_data mutableCopy];
  372. [self.collectionview reloadData];
  373. if([[category_data valueForKey:@"result"] intValue]==2)
  374. {
  375. self.collectionview.hidden = false;
  376. NSDictionary * item_json = [self.category_data objectForKey:@"items"];
  377. self.offset =[[item_json valueForKey:@"count"] intValue];
  378. int total = [[self.category_data valueForKey:@"item_total_count"]intValue];
  379. if(total<2)
  380. self.labelTotal.text = [NSString stringWithFormat:@"Total %d item",total];
  381. else
  382. self.labelTotal.text = [NSString stringWithFormat:@"Total %d items",total];
  383. }
  384. else if([[category_data valueForKey:@"result"] intValue]==RESULT_NET_ERROR)
  385. {
  386. self.label_net_err.hidden=false;
  387. self.collectionview.hidden = true;
  388. }
  389. else
  390. {
  391. [RAUtils message_alert:[category_data valueForKey:@"err_msg"] title:@"Search" controller:self] ;
  392. }
  393. self.isrefreshing=false;
  394. });
  395. });
  396. });
  397. }
  398. /*
  399. #pragma mark - Navigation
  400. // In a storyboard-based application, you will often want to do a little preparation before navigation
  401. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  402. // Get the new view controller using [segue destinationViewController].
  403. // Pass the selected object to the new view controller.
  404. }
  405. */
  406. //- (void)reload_container
  407. //{
  408. //
  409. //
  410. // // self.keywords=nil;
  411. // self.offset = 0;
  412. // // [self.content_data removeAllObjects];
  413. // [self reload];
  414. // NSLog(@"search");
  415. //}
  416. - (IBAction)onChangeDisplay:(UIButton *)sender {
  417. UICollectionViewFlowLayout *flowLayout =(UICollectionViewFlowLayout*)[self.collectionview collectionViewLayout];
  418. flowLayout.scrollDirection= UICollectionViewScrollDirectionVertical;
  419. if(sender.tag==2&&self.categoryViewType != CATEGORY_VIEWTYPE_SMALL)
  420. {
  421. self.categoryViewType = CATEGORY_VIEWTYPE_SMALL;
  422. // [self.collectionview.collectionViewLayout invalidateLayout];
  423. NSLog(@"frame %f",self.collectionview.frame.size.height);
  424. NSLog(@"offset %f",self.self.collectionview.contentOffset.y);
  425. NSLog(@"content %f",self.collectionview.contentSize.height);
  426. // float pos =self.collectionview.contentOffset.y/self.collectionview.contentSize.height;
  427. [self.collectionview reloadData];
  428. // CGPoint offset = self.collectionview.contentOffset;
  429. // offset.y=pos*self.collectionview.contentSize.height;
  430. // self.collectionview.contentOffset=offset;
  431. }
  432. else if(sender.tag==3&&self.categoryViewType != CATEGORY_VIEWTYPE_LARGE)
  433. {
  434. self.categoryViewType = CATEGORY_VIEWTYPE_LARGE;
  435. NSLog(@"frame %f",self.collectionview.frame.size.height);
  436. NSLog(@"offset %f",self.self.collectionview.contentOffset.y);
  437. NSLog(@"content %f",self.collectionview.contentSize.height);
  438. [self.collectionview reloadData];
  439. }
  440. else if(sender.tag==4&&self.categoryViewType != CATEGORY_VIEWTYPE_LIST)
  441. {
  442. self.categoryViewType = CATEGORY_VIEWTYPE_LIST;
  443. NSLog(@"frame %f",self.collectionview.frame.size.height);
  444. NSLog(@"offset %f",self.self.collectionview.contentOffset.y);
  445. NSLog(@"content %f",self.collectionview.contentSize.height);
  446. [self.collectionview reloadData];
  447. }
  448. [self showCategory];
  449. }
  450. - (IBAction)onAddCartClick:(id)sender {
  451. [RAUtils add_to_cart:self selector:@selector(addtocart)];
  452. return;
  453. UIApplication * app = [UIApplication sharedApplication];
  454. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  455. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  456. if(appDelegate.bLogin==false)
  457. {
  458. LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  459. // loginvc.delegate = self;
  460. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  461. loginvc.returnValue = ^(bool blogin){
  462. // UIApplication * app = [UIApplication sharedApplication];
  463. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  464. if(blogin)
  465. {
  466. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  467. {
  468. [main_vc checklogin:false];
  469. if(appDelegate.can_create_order)
  470. {
  471. NSString* msg =@"";
  472. if(appDelegate.contact_id.length>0)
  473. {
  474. msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  475. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  476. }
  477. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  478. // alert.
  479. [alert show];
  480. }
  481. else
  482. {
  483. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil), nil];
  484. // alert.
  485. [alert show];
  486. }
  487. }
  488. else
  489. {
  490. [main_vc checklogin:false];
  491. [self addtocart];
  492. }
  493. }
  494. // self.offset = 0;
  495. // [self.content_data removeAllObjects];
  496. // [self loadpage];
  497. //
  498. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  499. };
  500. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  501. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  502. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  503. [self presentViewController:navi animated:YES completion:^{
  504. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  505. NSLog(@"LoginViewController present.........");
  506. // self.btop = false;
  507. // <#code#>
  508. }];
  509. }
  510. else
  511. {
  512. if(appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil)
  513. {
  514. [main_vc checklogin:false];
  515. if(appDelegate.can_create_order)
  516. {
  517. NSString* msg =@"";
  518. if(appDelegate.contact_id.length>0)
  519. {
  520. msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  521. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  522. }
  523. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  524. // alert.
  525. [alert show];
  526. }
  527. else
  528. {
  529. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil), nil];
  530. // alert.
  531. [alert show];
  532. }
  533. }
  534. else
  535. {
  536. if(appDelegate.order_code==nil)
  537. [ self neworder];
  538. else
  539. [self addtocart];
  540. }
  541. }
  542. }
  543. -(void) neworder
  544. {
  545. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  546. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  547. NSDictionary* return_json = [iSalesNetwork new_Order];
  548. dispatch_async(dispatch_get_main_queue(), ^{
  549. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  550. if([[return_json valueForKey:@"result"] intValue]==2)
  551. {
  552. int result=[[return_json valueForKey:@"result"] intValue];
  553. if(result==2)
  554. {
  555. //successed.
  556. NSString* order_code = [return_json valueForKey:@"orderCode"];
  557. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  558. appDelegate.order_code = order_code;
  559. [self addtocart];
  560. // [self ReloadData];
  561. // if(self.shopCartBlock!=nil)
  562. // {
  563. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  564. //
  565. //
  566. // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  567. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  568. // iv.image = img;
  569. // // [self.contentView addSubview:iv];
  570. // self.shopCartBlock(iv);
  571. //
  572. // }
  573. }
  574. }
  575. else
  576. {
  577. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  578. }
  579. });
  580. });
  581. }
  582. - (IBAction)onAddPortfolioClick:(id)sender {
  583. UIApplication * app = [UIApplication sharedApplication];
  584. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  585. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  586. if(appDelegate.bLogin==false)
  587. {
  588. LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  589. // loginvc.delegate = self;
  590. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  591. loginvc.returnValue = ^(bool blogin){
  592. if(blogin)
  593. {
  594. [main_vc checklogin:false];
  595. [self addtoportfolio];
  596. }
  597. };
  598. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  599. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  600. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  601. [self presentViewController:navi animated:YES completion:^{
  602. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  603. NSLog(@"LoginViewController present.........");
  604. // self.btop = false;
  605. // <#code#>
  606. }];
  607. }
  608. else
  609. {
  610. [self addtoportfolio];
  611. }
  612. }
  613. - (IBAction)onAddWishlistClick:(id)sender {
  614. UIApplication * app = [UIApplication sharedApplication];
  615. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  616. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  617. if(appDelegate.bLogin==false)
  618. {
  619. LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  620. // loginvc.delegate = self;
  621. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  622. loginvc.returnValue = ^(bool blogin){
  623. if(blogin)
  624. {
  625. [main_vc checklogin:false];
  626. [self addtowish];
  627. }
  628. };
  629. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  630. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  631. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  632. [self presentViewController:navi animated:YES completion:^{
  633. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  634. NSLog(@"LoginViewController present.........");
  635. // self.btop = false;
  636. // <#code#>
  637. }];
  638. }
  639. else
  640. {
  641. [self addtowish];
  642. }
  643. }
  644. -(void) addtocart
  645. {
  646. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  647. int count = [[items valueForKey:@"count"] intValue];
  648. NSMutableArray* checked = [[NSMutableArray alloc] init];
  649. for(int i=0;i<count;i++)
  650. {
  651. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  652. bool check = [[item valueForKey:@"checked"] boolValue];
  653. if(check)
  654. {
  655. // [checked addObject:[NSNumber numberWithInt:[[item valueForKey:@"product_id"] intValue]]];
  656. [checked addObject:[item valueForKey:@"fash_name"]];
  657. }
  658. }
  659. if(checked.count==0)
  660. {
  661. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Add to cart." controller:self] ;
  662. return;
  663. }
  664. NSString * ids = [checked componentsJoinedByString:@","];
  665. self.addCartBtn.enabled = false;
  666. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Add Models To Cart"];
  667. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  668. NSDictionary* return_json = [iSalesNetwork add_toCart_byName:ids];
  669. dispatch_async(dispatch_get_main_queue(), ^{
  670. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  671. self.addCartBtn.enabled = true;
  672. if([[return_json valueForKey:@"result"] intValue]==2)
  673. {
  674. // int result=[[return_json valueForKey:@"result"] intValue];
  675. // if(result==2)
  676. // {
  677. // //successed.
  678. // NSString* order_code = [return_json valueForKey:@"order_code"];
  679. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  680. // appDelegate.order_code = order_code;
  681. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  682. // appDelegate.order_code = order_code;
  683. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  684. [RAUtils message_alert:nil title:@" Multiple items added to Cart" controller:self] ;
  685. [self end_select:2];
  686. // }
  687. }
  688. else if([[return_json valueForKey:@"result"] intValue]==8)
  689. {
  690. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  691. // appDelegate.order_code = order_code;
  692. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  693. [self end_select:2];
  694. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  695. }
  696. else
  697. {
  698. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  699. }
  700. });
  701. });
  702. }
  703. -(void) addtoportfolio
  704. {
  705. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  706. int count = [[items valueForKey:@"count"] intValue];
  707. NSMutableArray* checked = [[NSMutableArray alloc] init];
  708. for(int i=0;i<count;i++)
  709. {
  710. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  711. bool check = [[item valueForKey:@"checked"] boolValue];
  712. if(check)
  713. {
  714. [checked addObject:[NSNumber numberWithInt:[[item valueForKey:@"product_id"] intValue]]];
  715. }
  716. }
  717. if(checked.count==0)
  718. {
  719. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Add to portfolio." controller:self] ;
  720. return;
  721. }
  722. NSString * ids = [checked componentsJoinedByString:@","];
  723. self.addPortfolioBtn.enabled =false;
  724. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Add Models To Portfolio"];
  725. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  726. NSDictionary* return_json = [iSalesNetwork add_toPortfolio:ids ];
  727. dispatch_async(dispatch_get_main_queue(), ^{
  728. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  729. self.addPortfolioBtn.enabled =true;
  730. if([[return_json valueForKey:@"result"] intValue]==2)
  731. {
  732. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  733. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  734. [RAUtils message_alert:nil title:@" Multiple items added to Portfolio" controller:self] ;
  735. [self end_select:3];
  736. }
  737. else
  738. {
  739. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Portfolio" controller:self] ;
  740. }
  741. });
  742. });
  743. }
  744. -(void) addtowish
  745. {
  746. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  747. int count = [[items valueForKey:@"count"] intValue];
  748. NSMutableArray* checked = [[NSMutableArray alloc] init];
  749. for(int i=0;i<count;i++)
  750. {
  751. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  752. bool check = [[item valueForKey:@"checked"] boolValue];
  753. if(check)
  754. {
  755. [checked addObject:[NSNumber numberWithInt:[[item valueForKey:@"product_id"] intValue]]];
  756. }
  757. }
  758. if(checked.count==0)
  759. {
  760. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Add to wishlist." controller:self] ;
  761. return;
  762. }
  763. NSString * ids = [checked componentsJoinedByString:@","];
  764. self.addWishBtn.enabled =false;
  765. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Add Models To Wishlist"];
  766. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  767. NSDictionary* return_json = [iSalesNetwork add_toWatchList:ids ];
  768. dispatch_async(dispatch_get_main_queue(), ^{
  769. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  770. self.addWishBtn.enabled =true;
  771. if([[return_json valueForKey:@"result"] intValue]==2)
  772. {
  773. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  774. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  775. [RAUtils message_alert:nil title:@" Multiple items added to Wishlist" controller:self] ;
  776. [self end_select:1];
  777. }
  778. else
  779. {
  780. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Wishlist" controller:self] ;
  781. }
  782. });
  783. });
  784. }
  785. -(void) showCategory
  786. {
  787. if(self.showDetail==false)
  788. return;
  789. CGRect frame = CGRectMake(0, 48, self.view.bounds.size.width
  790. , self.view.bounds.size.height-48);
  791. [UIView animateWithDuration:0.5 animations:^{ self.collectionview.frame=frame;
  792. }];
  793. self.collectionview.autoresizingMask = self.collectionview.autoresizingMask | UIViewAutoresizingFlexibleWidth;
  794. self.showDetail = false;
  795. // self.cagegoryLabel.hidden = false;
  796. }
  797. //#pragma mark - searchBar delegate;
  798. //- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  799. //{
  800. //
  801. //
  802. // // self.keywords=searchBar.text;
  803. // self.offset = 0;
  804. // // [self.content_data removeAllObjects];
  805. // [self reload];
  806. // NSLog(@"search");
  807. //}
  808. /*
  809. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  810. {
  811. if([searchText isEqualToString:@""] && !self.reset)
  812. {
  813. NSLog(@"clear");
  814. self.reset=true;
  815. self.keywords=nil;
  816. self.offset = 0;
  817. [self.content_data removeAllObjects];
  818. [self loadpage];
  819. [self loadpage];
  820. }
  821. else
  822. self.reset = false;
  823. }*/
  824. -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  825. {
  826. [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  827. UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  828. if (UIInterfaceOrientationIsLandscape(orientation)|| UIInterfaceOrientationIsPortrait(orientation))
  829. {
  830. // self.orientation = orientation;
  831. [self.collectionview.collectionViewLayout invalidateLayout];
  832. NSLog(@"routed");
  833. }
  834. }
  835. #pragma mark -- UICollectionViewDataSource
  836. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  837. {
  838. // 每个Section的item个数
  839. NSDictionary * item_json = [self.category_data objectForKey:@"items"];
  840. int count =[[item_json valueForKey:@"count"] intValue];
  841. return count;
  842. }
  843. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  844. {
  845. return 1;
  846. }
  847. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  848. {
  849. if(self.categoryViewType == CATEGORY_VIEWTYPE_SMALL)
  850. {
  851. static NSString * CellIdentifier = @"CategoryCellNPD";
  852. CategoryCellNPD * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
  853. NSDictionary * item_json =[[self.category_data objectForKey:@"items" ] objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  854. NSString* img_url =[item_json valueForKey:@"img"];
  855. NSString* description =[item_json valueForKey:@"description"];
  856. NSString* name =[item_json valueForKey:@"fash_name"];
  857. bool wish_exist = [[item_json valueForKey:@"wish_exists"]boolValue];
  858. bool cart_exists = [[item_json valueForKey:@"cart_exists"]boolValue];
  859. if(wish_exist)
  860. cell.mark_wish.hidden=false;
  861. else
  862. cell.mark_wish.hidden=true;
  863. if(cart_exists)
  864. cell.mark_order.hidden=false;
  865. else
  866. cell.mark_order.hidden=true;
  867. // NSString* old_price =[item_json valueForKey:@"old_price"];
  868. // NSString* price =[item_json valueForKey:@"price"];
  869. bool checked =[[item_json valueForKey:@"checked"] boolValue];
  870. if(!checked)
  871. {
  872. cell.checkImageView.hidden = true;
  873. [cell setBackgroundColor: UIColorFromRGB(0x996633)];
  874. }
  875. else
  876. {
  877. cell.checkImageView.hidden = false;
  878. [cell setBackgroundColor: UIColorFromRGB(0x009966)];
  879. }
  880. cell.nameLabel.text = description;
  881. cell.modelNoLabel.text=name;
  882. // cell.cellDescription.text = description;
  883. // cell.oldPrice.text = old_price;
  884. // cell.Price.text = price;
  885. cell.cellImageView.image=[UIImage imageNamed:@"loading_s"];
  886. NSString* file_name=[img_url lastPathComponent];
  887. NSData* img_data=[iSalesDB load_cached_img:file_name];
  888. if(img_data!=nil)
  889. {
  890. UIImage * img =[UIImage imageWithData:img_data];
  891. cell.cellImageView.image = img;
  892. }
  893. else
  894. {
  895. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  896. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  897. dispatch_async(dispatch_get_main_queue(), ^{
  898. if(downloadimg_data!=nil)
  899. {
  900. [iSalesDB cache_img:downloadimg_data :file_name ];
  901. UIImage * img =[UIImage imageWithData:downloadimg_data];
  902. cell.cellImageView.image = img;
  903. }
  904. else
  905. {
  906. UIImage * img =[UIImage imageNamed:@"notfound_s"];
  907. cell.cellImageView.image = img;
  908. }
  909. });
  910. });
  911. }
  912. return cell;
  913. }
  914. else if(self.categoryViewType == CATEGORY_VIEWTYPE_LARGE)
  915. {
  916. static NSString * CellIdentifier = @"CategoryCellNPDLarge";
  917. CategoryCellNPD * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
  918. NSDictionary * item_json =[[self.category_data objectForKey:@"items" ] objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  919. NSString* img_url =[item_json valueForKey:@"img"];
  920. NSString* description =[item_json valueForKey:@"description"];
  921. NSString* modelid =[item_json valueForKey:@"fash_name"];
  922. bool checked =[[item_json valueForKey:@"checked"] boolValue];
  923. bool wish_exist = [[item_json valueForKey:@"wish_exists"]boolValue];
  924. bool cart_exists = [[item_json valueForKey:@"cart_exists"]boolValue];
  925. if(wish_exist)
  926. cell.mark_wish.hidden=false;
  927. else
  928. cell.mark_wish.hidden=true;
  929. if(cart_exists)
  930. cell.mark_order.hidden=false;
  931. else
  932. cell.mark_order.hidden=true;
  933. if(!checked)
  934. {
  935. cell.checkImageView.hidden = true;
  936. [cell setBackgroundColor: UIColorFromRGB(0x996633)];
  937. }
  938. else
  939. {
  940. cell.checkImageView.hidden = false;
  941. [cell setBackgroundColor: UIColorFromRGB(0x009966)];
  942. }
  943. // NSString* name =[item_json valueForKey:@"name"];
  944. // NSString* old_price =[item_json valueForKey:@"old_price"];
  945. // NSString* price =[item_json valueForKey:@"price"];
  946. cell.nameLabel.text = description;
  947. cell.modelNoLabel.text=modelid;
  948. cell.cellImageView.image=[UIImage imageNamed:@"loading_l"];
  949. // cell.cellDescription.text = description;
  950. // cell.oldPrice.text = old_price;
  951. // cell.Price.text = price;
  952. NSString* file_name=[img_url lastPathComponent];
  953. NSData* img_data=[iSalesDB load_cached_img:file_name];
  954. if(img_data!=nil)
  955. {
  956. UIImage * img =[UIImage imageWithData:img_data];
  957. cell.cellImageView.image = img;
  958. }
  959. else
  960. {
  961. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  962. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  963. dispatch_async(dispatch_get_main_queue(), ^{
  964. if(downloadimg_data!=nil)
  965. {
  966. [iSalesDB cache_img:downloadimg_data :file_name ];
  967. UIImage * img =[UIImage imageWithData:downloadimg_data];
  968. cell.cellImageView.image = img;
  969. }
  970. cell.cellImageView.image=[UIImage imageNamed:@"notfound_l"];
  971. });
  972. });
  973. }
  974. return cell;
  975. }
  976. else
  977. {
  978. static NSString * CellIdentifier = @"CategoryCellNPDList";
  979. CategoryCellNPD * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
  980. NSDictionary * item_json =[[self.category_data objectForKey:@"items" ] objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  981. NSString* img_url =[item_json valueForKey:@"img"];
  982. NSString* description =[item_json valueForKey:@"description"];
  983. NSString* name =[item_json valueForKey:@"fash_name"];
  984. bool wish_exist = [[item_json valueForKey:@"wish_exists"]boolValue];
  985. bool cart_exists = [[item_json valueForKey:@"cart_exists"]boolValue];
  986. if(wish_exist)
  987. cell.mark_wish.hidden=false;
  988. else
  989. cell.mark_wish.hidden=true;
  990. if(cart_exists)
  991. cell.mark_order.hidden=false;
  992. else
  993. cell.mark_order.hidden=true;
  994. // NSString* old_price =[item_json valueForKey:@"old_price"];
  995. // NSString* price =[item_json valueForKey:@"price"];
  996. bool checked =[[item_json valueForKey:@"checked"] boolValue];
  997. if(!checked)
  998. {
  999. cell.checkImageView.hidden = true;
  1000. [cell setBackgroundColor: UIColorFromRGB(0xF2EEEA)];
  1001. }
  1002. else
  1003. {
  1004. cell.checkImageView.hidden = false;
  1005. [cell setBackgroundColor: UIColorFromRGB(0x009966)];
  1006. }
  1007. cell.nameLabel.text = description;
  1008. cell.modelNoLabel.text=name;
  1009. cell.cellImageView.image = [UIImage imageNamed:@"loading_l"];
  1010. // cell.cellDescription.text = description;
  1011. // cell.oldPrice.text = old_price;
  1012. // cell.Price.text = price;
  1013. NSString* file_name=[img_url lastPathComponent];
  1014. NSData* img_data=[iSalesDB load_cached_img:file_name];
  1015. if(img_data!=nil)
  1016. {
  1017. UIImage * img =[UIImage imageWithData:img_data];
  1018. cell.cellImageView.image = img;
  1019. }
  1020. else
  1021. {
  1022. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1023. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  1024. dispatch_async(dispatch_get_main_queue(), ^{
  1025. if(downloadimg_data!=nil)
  1026. {
  1027. [iSalesDB cache_img:downloadimg_data :file_name ];
  1028. UIImage * img =[UIImage imageWithData:downloadimg_data];
  1029. cell.cellImageView.image = img;
  1030. }
  1031. else
  1032. cell.cellImageView.image = [UIImage imageNamed:@"notfound_l"];
  1033. });
  1034. });
  1035. }
  1036. return cell;
  1037. }
  1038. }
  1039. #pragma mark --UICollectionViewDelegateFlowLayout
  1040. ////定义每个UICollectionView 的大小
  1041. //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  1042. //{
  1043. // return CGSizeMake(96, 100);
  1044. //}
  1045. //定义每个UICollectionView 的 margin
  1046. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  1047. {
  1048. UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  1049. if (UIInterfaceOrientationIsLandscape(orientation))
  1050. {
  1051. // NSLog(@"横");
  1052. // if(self.showDetail)
  1053. // return CGSizeMake(96, 100);
  1054. if(self.categoryViewType ==CATEGORY_VIEWTYPE_LARGE)
  1055. return CGSizeMake(328, 391);
  1056. else if(self.categoryViewType ==CATEGORY_VIEWTYPE_LIST)
  1057. return CGSizeMake(1024, 120);
  1058. return CGSizeMake(193, 269);
  1059. }
  1060. else if(UIInterfaceOrientationIsPortrait(orientation))
  1061. {
  1062. // NSLog(@"竖");
  1063. // if(self.showDetail)
  1064. // return CGSizeMake(96, 100);
  1065. if(self.categoryViewType ==CATEGORY_VIEWTYPE_LARGE)
  1066. return CGSizeMake(369, 440);
  1067. else if(self.categoryViewType ==CATEGORY_VIEWTYPE_LIST)
  1068. return CGSizeMake(768, 120);
  1069. return CGSizeMake(180, 251);
  1070. }
  1071. NSLog(@"error , %ld",(long)orientation );
  1072. return CGSizeMake(96, 100);
  1073. }
  1074. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  1075. {
  1076. if(self.categoryViewType ==CATEGORY_VIEWTYPE_LIST)
  1077. return 1;
  1078. return 15;
  1079. }
  1080. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  1081. {
  1082. if(self.showDetail)
  1083. return UIEdgeInsetsMake(10, 5, 10, 5);
  1084. // if(self.categoryViewType ==CATEGORY_VIEWTYPE_LARGE)
  1085. // return UIEdgeInsetsMake(10, 10, 10, 10);
  1086. return UIEdgeInsetsMake(10, 10, 10, 10);
  1087. }
  1088. #pragma mark --UICollectionViewDelegate
  1089. -(void) showDetailat:(NSString*) item_id category_id:(NSString*) category_id index:(long)index
  1090. {
  1091. DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  1092. // dvc
  1093. dvc.OnBack=^(bool add_cart,bool add_wish,bool update_data)
  1094. {
  1095. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  1096. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%ld",index]] mutableCopy];
  1097. if(add_cart)
  1098. item[@"cart_exists"]=@"true";
  1099. if(add_wish)
  1100. item[@"wish_exists"]=@"true";
  1101. items[[NSString stringWithFormat:@"item_%ld",index]] = item;
  1102. self.category_data[@"items"]=items;
  1103. [self.collectionview reloadData];
  1104. };
  1105. // dvc.use_model_name = true;
  1106. // dvc.model_name=[transformed stringValue];
  1107. dvc.product_id = item_id;
  1108. dvc.category_id=category_id;
  1109. dvc.ispush = true;
  1110. [dvc reload];
  1111. [self.navigationController pushViewController:dvc animated:true];
  1112. return;
  1113. //一下为detail嵌入当前窗口的方式
  1114. self.categoryViewType = CATEGORY_VIEWTYPE_SMALL;
  1115. //[self.collectionview.collectionViewLayout invalidateLayout];
  1116. [self.collectionview reloadData];
  1117. UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  1118. int width=175;
  1119. // int height;
  1120. if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight)
  1121. {
  1122. width = 370;
  1123. // height = 400;
  1124. }
  1125. else
  1126. {
  1127. UICollectionViewFlowLayout *flowLayout =(UICollectionViewFlowLayout*)[self.collectionview collectionViewLayout];
  1128. flowLayout.scrollDirection= UICollectionViewScrollDirectionHorizontal;
  1129. // [self.collectionview ] set
  1130. // height = 251;
  1131. width = 180;
  1132. }
  1133. CGRect frame = CGRectMake(0, 48, self.view.bounds.size.width, 271);
  1134. // CGRect framedetail = CGRectMake(0,271 ,self.view.bounds.size.width,self.view.bounds.size.height-271);
  1135. [UIView animateWithDuration:0.5 animations:^{
  1136. self.collectionview.frame=frame;
  1137. // self.detailView.frame = framedetail;
  1138. // self.cagegoryLabel.hidden = true;
  1139. }];
  1140. self.collectionview.autoresizingMask = self.collectionview.autoresizingMask ^ UIViewAutoresizingFlexibleWidth;
  1141. self.showDetail = true;
  1142. NSArray* a= self.childViewControllers;
  1143. for (int i=0;i<a.count;i++)
  1144. {
  1145. if([a[i] isKindOfClass:[DetailViewController class]])
  1146. {
  1147. DetailViewController *detailvc=a[i];
  1148. detailvc.category_id = nil;
  1149. detailvc.product_id = item_id;
  1150. detailvc.ispush=false;
  1151. [detailvc reload];
  1152. }
  1153. }
  1154. }
  1155. //UICollectionView被选中时调用的方法
  1156. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  1157. {
  1158. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  1159. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1160. if(self.isSelectionMode)
  1161. {
  1162. bool checked =[[item valueForKey:@"checked"] boolValue];
  1163. CategoryCellNPD *currentSelecteCell = (CategoryCellNPD *)[self.collectionview cellForItemAtIndexPath:indexPath];
  1164. if(checked)
  1165. {
  1166. self.select_count--;
  1167. if(self.select_count==0)
  1168. {
  1169. self.addWishBtn.enabled = false;
  1170. self.addCartBtn.enabled = false;
  1171. self.addPortfolioBtn.enabled=false;
  1172. }
  1173. [item setValue:@"false" forKey:@"checked"];
  1174. currentSelecteCell.checkImageView.hidden = true;
  1175. if(self.categoryViewType != CATEGORY_VIEWTYPE_LIST)
  1176. [currentSelecteCell setBackgroundColor: UIColorFromRGB(0x996633)];
  1177. else
  1178. [currentSelecteCell setBackgroundColor: UIColorFromRGB(0xF2EEEA)];
  1179. }
  1180. else
  1181. {
  1182. self.select_count++;
  1183. self.addWishBtn.enabled = true;
  1184. self.addCartBtn.enabled = true;
  1185. self.addPortfolioBtn.enabled=true;
  1186. [item setValue:@"true" forKey:@"checked"];
  1187. currentSelecteCell.checkImageView.hidden = false;
  1188. [currentSelecteCell setBackgroundColor: UIColorFromRGB(0x009966)];
  1189. }
  1190. [items setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1191. [self.category_data setObject:items forKey:@"items"];
  1192. }
  1193. else
  1194. {
  1195. NSString* detail_id= [item valueForKey:@"product_id"] ;
  1196. [self showDetailat:detail_id category_id:nil index:indexPath.row];
  1197. }
  1198. }
  1199. #pragma mark Deferred image loading (UIScrollViewDelegate)
  1200. //// Load images for all onscreen rows when scrolling is finished
  1201. //- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  1202. //{
  1203. // if (!decelerate)
  1204. // {
  1205. // NSLog(@"scrollViewDidEndDragging LOADMORE");
  1206. // }
  1207. //}
  1208. //
  1209. //- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  1210. //{
  1211. // NSLog(@"scrollViewDidEndDecelerating LOADMORE");
  1212. //}
  1213. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  1214. // [refreshHeaderViewegoRefreshScrollViewDidScroll:scrollView];//下拉刷新
  1215. CGPoint contentOffsetPoint = self.collectionview.contentOffset;
  1216. CGRect frame =self.collectionview.frame;
  1217. UICollectionViewFlowLayout *flowLayout =(UICollectionViewFlowLayout*)[self.collectionview collectionViewLayout];
  1218. if(flowLayout.scrollDirection== UICollectionViewScrollDirectionVertical)
  1219. {
  1220. NSLog(@"contentOffsetPoint %f",contentOffsetPoint.y);
  1221. NSLog(@"frame.size.height %f",frame.size.height);
  1222. NSLog(@"self.collectionview.contentSize.height %f",self.collectionview.contentSize.height);
  1223. if (contentOffsetPoint.y == (self.collectionview.contentSize.height - frame.size.height) /*|| self.collectionview.contentSize.height < frame.size.height*/)
  1224. {
  1225. if(self.isrefreshing)
  1226. return;
  1227. NSLog(@"scroll to the end,load more......");
  1228. [self loadMore];
  1229. // self.btnLoad.hidden = NO;
  1230. }
  1231. }
  1232. else if(flowLayout.scrollDirection== UICollectionViewScrollDirectionHorizontal)
  1233. {
  1234. NSLog(@"contentOffsetPoint %f",contentOffsetPoint.y);
  1235. NSLog(@"frame.size.height %f",frame.size.height);
  1236. NSLog(@"self.collectionview.contentSize.height %f",self.collectionview.contentSize.height);
  1237. if (contentOffsetPoint.x == (self.collectionview.contentSize.width - frame.size.width) /*|| self.collectionview.contentSize.width < frame.size.width*/)
  1238. {
  1239. if(self.isrefreshing)
  1240. return;
  1241. NSLog(@"scroll to the end,load more......");
  1242. [self loadMore];
  1243. // self.btnLoad.hidden = NO;
  1244. }
  1245. }
  1246. }
  1247. #pragma mark - UIAlertViewDelegate
  1248. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  1249. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  1250. {
  1251. UIApplication * app = [UIApplication sharedApplication];
  1252. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1253. // if(alertView.tag==1234)
  1254. // {
  1255. //
  1256. // // availability warring;
  1257. //
  1258. //
  1259. // if(buttonIndex!=alertView.cancelButtonIndex)
  1260. // {
  1261. // if(appDelegate.order_code==nil)
  1262. // {
  1263. // [self neworder];
  1264. // }
  1265. // else
  1266. // {
  1267. // [self addtocart];
  1268. // }
  1269. // }
  1270. // return;
  1271. // }
  1272. // int count=[self.quantity_text.text intValue];
  1273. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  1274. if(buttonIndex==alertView.cancelButtonIndex)
  1275. {
  1276. }
  1277. else if(buttonIndex==1)
  1278. {
  1279. //open exist
  1280. self.disable_refresh = true;
  1281. OrderListViewController* ovc =[ self.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  1282. ovc.showNavibar = true;
  1283. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  1284. if(appDelegate.order_code==nil)
  1285. {
  1286. [self neworder];
  1287. }
  1288. else
  1289. {
  1290. [self addtocart];
  1291. [main_vc checklogin:true];
  1292. }
  1293. };
  1294. ovc.init_style = OL_OPEN;
  1295. ovc.onCancel = ^(){
  1296. self.disable_refresh = false;
  1297. // [main_vc checklogin:true];
  1298. };
  1299. [self.navigationController pushViewController:ovc animated:true];
  1300. }
  1301. else
  1302. {
  1303. //create new;
  1304. self.disable_refresh = true;
  1305. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  1306. {
  1307. ContactListViewController* cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  1308. cvc.showNavibar = true;
  1309. cvc.contact_type = @"Sales_Order_Customer";
  1310. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  1311. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  1312. appDelegate.customerInfo = value;
  1313. if(appDelegate.order_code==nil)
  1314. [self neworder];
  1315. [main_vc checklogin:true];
  1316. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  1317. //
  1318. // if(self.returnValue)
  1319. // self.returnValue(value);
  1320. };
  1321. cvc.onCancel = ^(){
  1322. [RAUtils message_alert:@"Can ot create order without cursomer infomation." title:@"" controller:self];
  1323. self.disable_refresh = false;
  1324. };
  1325. cvc.onReset = ^(){
  1326. [main_vc checklogin:true];
  1327. };
  1328. [self.navigationController pushViewController:cvc animated:true];
  1329. }
  1330. else
  1331. {
  1332. [self neworder];
  1333. }
  1334. }
  1335. }
  1336. /*
  1337. #pragma mark - Navigation
  1338. // In a storyboard-based application, you will often want to do a little preparation before navigation
  1339. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  1340. // Get the new view controller using [segue destinationViewController].
  1341. // Pass the selected object to the new view controller.
  1342. }
  1343. */
  1344. @end