SearchViewController.m 78 KB

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