CategoryViewController.m 72 KB

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