CategoryViewController.m 77 KB

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