SearchViewController.m 77 KB

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