ItemSearchViewController.m 75 KB

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