CategoryViewController.m 72 KB

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