SearchViewController.m 82 KB

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