ItemSearchViewController.m 85 KB

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