ItemSearchViewController.m 75 KB

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