CategoryViewController.m 76 KB

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