SearchViewController.m 77 KB

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