SearchViewController.m 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874
  1. //
  2. // SearchViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 9/1/15.
  6. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "SearchViewController.h"
  9. #import "const.h"
  10. #import "RANetwork.h"
  11. #import "CategoryCellNPD.h"
  12. #import "DetailViewController.h"
  13. #import "ContactListViewController.h"
  14. #import "MainViewController.h"
  15. #import "DefaultAppearance.h"
  16. #import "UIColor+JK_HEX.h"
  17. #import "CartUtils.h"
  18. #define DEF_CELL_HEIGHT 44
  19. #define DEF_TABLE_HEIGHT 44
  20. #define LINE_WIDTH 0
  21. #define CELL_MARGIN 0
  22. #define LABEL_MARGIN 5
  23. #define END_SELECT_PORTFOLIO 5
  24. @interface SearchViewController ()
  25. @property (nonatomic,assign) BOOL addAll;
  26. @property (nonatomic,assign) UIInterfaceOrientation toOrientation;///<屏幕将要旋转到的方向
  27. @property (nonatomic,strong) NSOperationQueue *dataOperationQueue;
  28. @end
  29. @implementation SearchViewController
  30. -(void)checkSearchBar
  31. {
  32. if (self.searchBar.superview.frame.origin.y > 0) {
  33. CGRect rect = self.searchBar.superview.frame;
  34. rect.origin.y = 0;
  35. self.searchBar.superview.frame = rect;
  36. }
  37. }
  38. - (NSOperationQueue *)dataOperationQueue {
  39. if (!_dataOperationQueue) {
  40. _dataOperationQueue = [[NSOperationQueue alloc] init];
  41. _dataOperationQueue.maxConcurrentOperationCount = 1;
  42. }
  43. return _dataOperationQueue;
  44. }
  45. #ifndef RA_NOTIFICATION
  46. -(void) reload_container_getdata:(bool)update_data
  47. {
  48. if(self.disable_refresh)
  49. return;
  50. [super reload_container_getdata:update_data];
  51. if(update_data)
  52. {
  53. self.offset = 0;
  54. // [self.content_data removeAllObjects];
  55. [self reload];
  56. }
  57. else
  58. {
  59. [self.collectionview reloadData];
  60. }
  61. return;
  62. // if(!self.showDetail)
  63. // return;
  64. // NSArray* a= self.childViewControllers;
  65. //
  66. // for (int i=0;i<a.count;i++)
  67. // {
  68. // if([a[i] isKindOfClass:[DetailViewController class]])
  69. // {
  70. // DetailViewController *detailvc=a[i];
  71. // [detailvc reload];
  72. // }
  73. // }
  74. }
  75. #endif
  76. -(void)begin_select
  77. {
  78. self.addWishBtn.enabled = true;
  79. self.addCartBtn.enabled = true;
  80. self.addPortfolioBtn.enabled=true;
  81. // self.addCartBtn.enabled=false;
  82. // self.addWishBtn.enabled=false;
  83. // self.addPortfolioBtn.enabled=false;
  84. self.selectToolbar.hidden=false;
  85. // self.collectionview.allowsMultipleSelection = YES;
  86. self.isSelectionMode=true;
  87. self.select_count=0;
  88. }
  89. -(void)end_select:(int) type
  90. {
  91. self.select_count=0;
  92. self.selectToolbar.hidden=true;
  93. // self.collectionview.allowsMultipleSelection = NO;
  94. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  95. if (items == nil) {
  96. items = [NSMutableDictionary dictionary];
  97. [items setObject:@(0) forKey:@"count"];
  98. }
  99. int count = [[items valueForKey:@"count"] intValue];
  100. for(int i=0;i<count;i++)
  101. {
  102. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  103. bool check = [[item valueForKey:@"checked"] boolValue];
  104. if(check)
  105. {
  106. switch (type) {
  107. case 1:
  108. item[@"wish_exists"]=@"true";
  109. break;
  110. case 2:
  111. item[@"cart_exists"]=@"true";
  112. break;
  113. default:
  114. break;
  115. }
  116. }
  117. else
  118. {
  119. switch (type) {
  120. case 3:
  121. item[@"wish_exists"]=@"true";
  122. break;
  123. case 4:
  124. item[@"cart_exists"]=@"true";
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. [item setValue:@"false" forKey:@"checked"];
  131. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  132. }
  133. [self.category_data setObject:items forKey:@"items"];
  134. self.selectallBtn.tag = 0;
  135. [self.selectallBtn setTitle:@"Select"];
  136. self.isSelectionMode=false;
  137. if(self.disable_refresh)
  138. {
  139. self.disable_refresh = false;
  140. // [self reload_container_getdata:true];
  141. [self reload_data];
  142. }
  143. else
  144. [self.collectionview reloadData];
  145. }
  146. - (IBAction)onSelectClick:(id)sender {
  147. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  148. //
  149. // [appDelegate test_onDecodedDataResult:@"108530-HS01"];
  150. // return;
  151. [self begin_select];
  152. }
  153. - (IBAction)onCancelSelectClick:(id)sender {
  154. [self end_select:0];
  155. }
  156. - (void)checkToolBar {
  157. // customer 隐藏add to portfolio
  158. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  159. NSMutableArray *items = [self.selectToolbar.items mutableCopy];
  160. if (appDelegate.user && appDelegate.user_type == USER_ROLE_CUSTOMER && appDelegate.customer_type == CustomerTypeStore) {
  161. if ([items containsObject:self.addPortfolioBtn]) {
  162. [items removeObject:self.addPortfolioBtn];
  163. self.selectToolbar.items = items;
  164. }
  165. } else {
  166. if (![items containsObject:self.addPortfolioBtn]) {
  167. [items insertObject:self.addPortfolioBtn atIndex:4];
  168. self.selectToolbar.items = items;
  169. }
  170. }
  171. }
  172. - (void)viewWillAppear:(BOOL)animated
  173. {
  174. [super viewWillAppear:animated];
  175. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  176. [self checkToolBar];
  177. [self checkSearchBar];
  178. self.toOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  179. [self.collectionview layoutIfNeeded];
  180. }
  181. - (void)viewWillDisappear:(BOOL)animated {
  182. [super viewWillDisappear:animated];
  183. }
  184. - (void)viewDidLoad {
  185. [super viewDidLoad];
  186. #ifdef RA_NOTIFICATION
  187. self.support_scanner = true;
  188. #endif
  189. #if defined(BUILD_HOMER) || defined(BUILD_GATIT) || defined(BUILD_UWAVER)
  190. self.switch_matchfull.hidden = true;
  191. self.label_matchfull.hidden = true;
  192. #else
  193. self.switch_matchfull.hidden = false;
  194. self.label_matchfull.hidden = false;
  195. #endif
  196. self.mum.hidden = true;
  197. CGPoint center = self.mum.center;
  198. self.mum.frame = CGRectMake(0, 0, 100, 100);
  199. self.mum.center = center;
  200. // self.mum.backgroundColor = [UIColor colorWithHEX:0xA5A5A5 alpha:0.5];
  201. self.mum.layer.cornerRadius = 5.0f;
  202. self.mum.layer.masksToBounds = YES;
  203. self.addWishBtn.enabled = true;
  204. self.addCartBtn.enabled = true;
  205. self.addPortfolioBtn.enabled=true;
  206. self.addAll = NO;
  207. [self.addAllCheckBtn setImage:[[UIImage imageNamed:@"check_0_24"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  208. self.selectallBtn.possibleTitles = [NSSet setWithObjects:@"Select", @"Deselect", nil];
  209. #ifdef BUILD_UWAVER
  210. // self.addPortfolioBtn.title = @"Add to Hang Tag";
  211. self.btn_select.hidden = true;
  212. #endif
  213. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  214. self.categoryViewType = CATEGORY_VIEWTYPE_LARGE;
  215. #else
  216. self.categoryViewType = CATEGORY_VIEWTYPE_SMALL;
  217. #endif
  218. UICollectionViewFlowLayout* layout =(UICollectionViewFlowLayout*)self.collectionview.collectionViewLayout;
  219. CGSize cellsize = CGSizeMake(175, 250);
  220. layout.itemSize =cellsize;
  221. layout.minimumLineSpacing = 15;
  222. self.collectionview.collectionViewLayout = layout;
  223. //
  224. self.display_type = [[NSMutableDictionary alloc] init];
  225. [self.display_type setValue:@3 forKey:@"count"];
  226. NSMutableDictionary* val_0 = [[NSMutableDictionary alloc] init];
  227. [val_0 setValue:@1 forKey:@"check"];
  228. [val_0 setValue:@"small icon" forKey:@"value"];
  229. [val_0 setValue:@"" forKey:@"img"];
  230. [self.display_type setObject:val_0 forKey:@"val_0"];
  231. NSMutableDictionary* val_1 = [[NSMutableDictionary alloc] init];
  232. [val_1 setValue:@0 forKey:@"check"];
  233. [val_1 setValue:@"large icon" forKey:@"value"];
  234. [val_1 setValue:@"" forKey:@"img"];
  235. [self.display_type setObject:val_1 forKey:@"val_1"];
  236. NSMutableDictionary* val_2 = [[NSMutableDictionary alloc] init];
  237. [val_2 setValue:@2 forKey:@"check"];
  238. [val_2 setValue:@"detail" forKey:@"value"];
  239. [val_2 setValue:@"" forKey:@"img"];
  240. [self.display_type setObject:val_2 forKey:@"val_2"];
  241. // self.collectionview.
  242. // self.cagegoryLabel.text = self.categoryString;
  243. self.showDetail = false;
  244. //添加边框
  245. // CALayer *layer = [self.collectionview layer];
  246. // layer.borderColor = [[UIColor redColor] CGColor];
  247. // layer.borderWidth = 5.0f;
  248. self.headerView.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.headerView.bounds].CGPath;
  249. self.headerView.layer.masksToBounds = false;
  250. //添加四个边阴影
  251. self.headerView.layer.shadowColor = [UIColor grayColor].CGColor;
  252. self.headerView.layer.shadowOffset = CGSizeMake(0, 0);
  253. self.headerView.layer.shadowOpacity = 0.5;
  254. self.headerView.layer.shadowRadius = 2.0;
  255. //添加两个边阴影
  256. self.collectionview.layer.masksToBounds = false;
  257. self.collectionview.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.collectionview.bounds].CGPath;
  258. self.collectionview.layer.shadowColor = [UIColor grayColor].CGColor;
  259. self.collectionview.layer.shadowOffset = CGSizeMake(0, 0);
  260. self.collectionview.layer.shadowOpacity = 0.5;
  261. self.collectionview.layer.shadowRadius = 2.0;
  262. // Do any additional setup after loading the view.
  263. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  264. ref.tag = 200 ;
  265. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  266. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  267. // ref.hidden = true;
  268. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  269. [self.collectionview addSubview:ref];
  270. self.collectionview.alwaysBounceVertical = YES;
  271. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  272. self.label_net_err.layer.borderWidth = 2.0;
  273. self.label_net_err.layer.cornerRadius=15;
  274. self.label_net_err.layer.masksToBounds=true;
  275. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  276. // tap.minimumPressDuration = 0.8; //定义按的时间
  277. [self.label_net_err addGestureRecognizer:tap];
  278. self.refresh_type = REFRESH_VIEW;
  279. self.notification_refreshLevel = NotificationRefreshLevelView;
  280. }
  281. -(void)manually_refresh
  282. {
  283. UIRefreshControl *reF = (UIRefreshControl *)[self.collectionview viewWithTag:200];
  284. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  285. [self performSelector:@selector(reload) withObject:nil afterDelay:1];
  286. // DebugLog(@"refresh!!!!!!!!");
  287. }
  288. -(void)viewWillLayoutSubviews
  289. {
  290. // if(self.showDetail==true)
  291. // {
  292. // UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  293. // int width=175;
  294. // // int height;
  295. // if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight)
  296. // {
  297. // width = 370;
  298. // // height = 400;
  299. // }
  300. // else
  301. // {
  302. // // height = 300;
  303. // width = 175;
  304. // }
  305. //
  306. //
  307. // CGRect frame = CGRectMake(0, 64, width, self.view.bounds.size.height-64);
  308. //
  309. // self.collectionview.frame=frame;
  310. //
  311. //
  312. //
  313. // CGRect framedetail = CGRectMake(width,64 ,self.view.bounds.size.width-width,self.view.bounds.size.height-64);
  314. // self.detailView.frame = framedetail;
  315. // }
  316. self.headerView.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.headerView.bounds].CGPath;
  317. self.collectionview.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.collectionview.bounds].CGPath;
  318. }
  319. - (IBAction)onSelectAllClick:(id)sender {
  320. // int tag =self.selectallBtn.tag;
  321. if(self.category_data==nil)
  322. return;
  323. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  324. if(items==nil)
  325. return;
  326. int count = [[items valueForKey:@"count"] intValue];
  327. NSString* checked;
  328. if (self.selectallBtn.tag == 0)
  329. {
  330. self.selectallBtn.tag = 1;
  331. [self.selectallBtn setTitle:@"Deselect"];
  332. // if(count>0)
  333. // {
  334. // self.addWishBtn.enabled = true;
  335. // self.addCartBtn.enabled = true;
  336. // self.addPortfolioBtn.enabled=true;
  337. // }
  338. self.select_count=count;
  339. checked=@"true";
  340. }
  341. else
  342. {
  343. self.selectallBtn.tag = 0;
  344. [self.selectallBtn setTitle:@"Select"];
  345. // self.addWishBtn.enabled = false;
  346. // self.addCartBtn.enabled = false;
  347. // self.addPortfolioBtn.enabled=false;
  348. checked=@"false";
  349. }
  350. for(int i=0;i<count;i++)
  351. {
  352. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  353. [item setValue:checked forKey:@"checked"];
  354. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  355. }
  356. [self.category_data setObject:items forKey:@"items"];
  357. [self.collectionview reloadData];
  358. }
  359. - (IBAction)onChangeDisplay:(UIButton *)sender {
  360. UICollectionViewFlowLayout *flowLayout =(UICollectionViewFlowLayout*)[self.collectionview collectionViewLayout];
  361. flowLayout.scrollDirection= UICollectionViewScrollDirectionVertical;
  362. if(sender.tag==2&&self.categoryViewType != CATEGORY_VIEWTYPE_SMALL)
  363. {
  364. self.categoryViewType = CATEGORY_VIEWTYPE_SMALL;
  365. // [self.collectionview.collectionViewLayout invalidateLayout];
  366. DebugLog(@"frame %f",self.collectionview.frame.size.height);
  367. DebugLog(@"offset %f",self.self.collectionview.contentOffset.y);
  368. DebugLog(@"content %f",self.collectionview.contentSize.height);
  369. // float pos =self.collectionview.contentOffset.y/self.collectionview.contentSize.height;
  370. [self.collectionview reloadData];
  371. // CGPoint offset = self.collectionview.contentOffset;
  372. // offset.y=pos*self.collectionview.contentSize.height;
  373. // self.collectionview.contentOffset=offset;
  374. }
  375. else if(sender.tag==3&&self.categoryViewType != CATEGORY_VIEWTYPE_LARGE)
  376. {
  377. self.categoryViewType = CATEGORY_VIEWTYPE_LARGE;
  378. DebugLog(@"frame %f",self.collectionview.frame.size.height);
  379. DebugLog(@"offset %f",self.self.collectionview.contentOffset.y);
  380. DebugLog(@"content %f",self.collectionview.contentSize.height);
  381. // float pos =self.collectionview.contentOffset.y/self.collectionview.contentSize.height;
  382. // [self.collectionview reloadData];
  383. // [self.collectionview.collectionViewLayout invalidateLayout];
  384. [self.collectionview reloadData];
  385. // CGPoint offset = self.collectionview.contentOffset;
  386. // offset.y=pos*self.collectionview.contentSize.height;
  387. // self.collectionview.contentOffset=offset;
  388. //
  389. // self.collectionview move
  390. }
  391. else if(sender.tag==4&&self.categoryViewType != CATEGORY_VIEWTYPE_LIST)
  392. {
  393. self.categoryViewType = CATEGORY_VIEWTYPE_LIST;
  394. DebugLog(@"frame %f",self.collectionview.frame.size.height);
  395. DebugLog(@"offset %f",self.self.collectionview.contentOffset.y);
  396. DebugLog(@"content %f",self.collectionview.contentSize.height);
  397. // float pos =self.collectionview.contentOffset.y/self.collectionview.contentSize.height;
  398. // [self.collectionview reloadData];
  399. // [self.collectionview.collectionViewLayout invalidateLayout];
  400. [self.collectionview reloadData];
  401. // CGPoint offset = self.collectionview.contentOffset;
  402. // offset.y=pos*self.collectionview.contentSize.height;
  403. // self.collectionview.contentOffset=offset;
  404. //
  405. // self.collectionview move
  406. }
  407. // UICollectionViewFlowLayout* layout =(UICollectionViewFlowLayout*)self.collectionview.collectionViewLayout;
  408. // CGSize cellsize = CGSizeMake(383, 450);
  409. // layout.itemSize =cellsize;
  410. // layout.minimumInteritemSpacing=10;
  411. // layout.minimumLineSpacing = 10;
  412. // self.collectionview.collectionViewLayout = layout;
  413. // [self.collectionview.collectionViewLayout invalidateLayout];
  414. [self showCategory];
  415. }
  416. -(void) showCategory
  417. {
  418. if(self.showDetail==false)
  419. return;
  420. CGRect frame = CGRectMake(0, 48, self.view.bounds.size.width
  421. , self.view.bounds.size.height-48);
  422. [UIView animateWithDuration:0.5 animations:^{ self.collectionview.frame=frame;
  423. }];
  424. self.collectionview.autoresizingMask = self.collectionview.autoresizingMask | UIViewAutoresizingFlexibleWidth;
  425. self.showDetail = false;
  426. // self.cagegoryLabel.hidden = false;
  427. }
  428. - (void)didReceiveMemoryWarning {
  429. [super didReceiveMemoryWarning];
  430. // Dispose of any resources that can be recreated.
  431. }
  432. - (IBAction)onAddCartClick:(id)sender {
  433. [CartUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
  434. return;
  435. // {
  436. // UIApplication * app = [UIApplication sharedApplication];
  437. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  438. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  439. // if(appDelegate.bLogin==false)
  440. // {
  441. //
  442. // LoginViewController * loginvc =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"];
  443. // // loginvc.delegate = self;
  444. // // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  445. //
  446. // loginvc.returnValue = ^(bool blogin){
  447. //
  448. //
  449. // // UIApplication * app = [UIApplication sharedApplication];
  450. // // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  451. //
  452. //
  453. //
  454. //
  455. // if(blogin)
  456. // {
  457. // if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE*/ true)
  458. // {
  459. //
  460. // [main_vc checklogin:false];
  461. // if(appDelegate.can_create_order)
  462. // {
  463. // NSString* msg =@"";
  464. // if(appDelegate.contact_id.length>0)
  465. // {
  466. // msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  467. // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  468. //
  469. // }
  470. // 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];
  471. //
  472. //
  473. //
  474. // // alert.
  475. // [alert show];
  476. // }
  477. // else
  478. // {
  479. // 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];
  480. //
  481. // // alert.
  482. // [alert show];
  483. // }
  484. // }
  485. //// else
  486. //// {
  487. //// [main_vc checklogin:false];
  488. //// [self addtocart];
  489. //// }
  490. // }
  491. //
  492. // // self.offset = 0;
  493. // // [self.content_data removeAllObjects];
  494. // // [self loadpage];
  495. // //
  496. // // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  497. //
  498. // };
  499. //
  500. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  501. //
  502. //
  503. //
  504. //
  505. //
  506. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  507. //
  508. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  509. // [self presentViewController:navi animated:YES completion:^{
  510. //
  511. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  512. //
  513. // DebugLog(@"LoginViewController present.........");
  514. //
  515. // // self.btop = false;
  516. // // <#code#>
  517. // }];
  518. // }
  519. // else
  520. // {
  521. //// if(appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&appDelegate.order_code==nil)
  522. // if(appDelegate.order_code==nil)
  523. // {
  524. // // [main_vc checklogin:false];
  525. // if(appDelegate.can_create_order)
  526. // {
  527. // NSString* msg =@"";
  528. // if(appDelegate.contact_id.length>0)
  529. // {
  530. // msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  531. // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  532. //
  533. // }
  534. // 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];
  535. //
  536. // // alert.
  537. // [alert show];
  538. // }
  539. // else
  540. // {
  541. // 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];
  542. //
  543. // // alert.
  544. // [alert show];
  545. // }
  546. // }
  547. // else
  548. // {
  549. //
  550. // if(appDelegate.order_code==nil)
  551. // [ self neworder];
  552. // else
  553. // [self addtocart];
  554. //
  555. //
  556. //
  557. // }
  558. // }
  559. //
  560. //
  561. //
  562. //
  563. //}
  564. }
  565. -(void) neworder
  566. {
  567. __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Create Order" completion:^{
  568. [RANetwork request_create_order:^(NSMutableDictionary *result) {
  569. NSMutableDictionary* return_json = result;
  570. [waitalert dismissViewControllerAnimated:YES completion:^{
  571. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  572. if([[return_json valueForKey:@"result"] intValue]==2)
  573. {
  574. int result=[[return_json valueForKey:@"result"] intValue];
  575. if(result==2)
  576. {
  577. //successed.
  578. NSString* order_code = [return_json valueForKey:@"orderCode"];
  579. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  580. appDelegate.order_code = order_code;
  581. appDelegate.order_status = [[return_json valueForKey:@"orderStatus"] intValue];
  582. [self addtocart];
  583. // [self ReloadData];
  584. // if(self.shopCartBlock!=nil)
  585. // {
  586. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  587. //
  588. //
  589. // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  590. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  591. // iv.image = img;
  592. // // [self.contentView addSubview:iv];
  593. // self.shopCartBlock(iv);
  594. //
  595. // }
  596. }
  597. }
  598. else
  599. {
  600. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  601. }
  602. }];
  603. }];
  604. }];
  605. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  606. //
  607. // NSDictionary* return_json = [RANetwork new_Order];
  608. //
  609. // dispatch_async(dispatch_get_main_queue(), ^{
  610. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  611. //
  612. //
  613. // if([[return_json valueForKey:@"result"] intValue]==2)
  614. // {
  615. // int result=[[return_json valueForKey:@"result"] intValue];
  616. // if(result==2)
  617. // {
  618. // //successed.
  619. //
  620. // NSString* order_code = [return_json valueForKey:@"orderCode"];
  621. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  622. // appDelegate.order_code = order_code;
  623. // appDelegate.order_status = [[return_json valueForKey:@"orderStatus"] intValue];
  624. //
  625. // [self addtocart];
  626. //
  627. //
  628. // // [self ReloadData];
  629. //
  630. //
  631. // // if(self.shopCartBlock!=nil)
  632. // // {
  633. // // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  634. // //
  635. // //
  636. // // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  637. // // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  638. // // iv.image = img;
  639. // // // [self.contentView addSubview:iv];
  640. // // self.shopCartBlock(iv);
  641. // //
  642. // // }
  643. //
  644. // }
  645. // }
  646. // else
  647. // {
  648. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  649. // }
  650. //
  651. //
  652. //
  653. //
  654. // });
  655. // });
  656. }
  657. - (IBAction)onAddPortfolioClick:(id)sender {
  658. UIApplication * app = [UIApplication sharedApplication];
  659. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  660. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  661. if(appDelegate.bLogin==false)
  662. {
  663. LoginViewController * loginvc =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"];
  664. // loginvc.delegate = self;
  665. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  666. loginvc.returnValue = ^(bool blogin){
  667. if(blogin)
  668. {
  669. [main_vc checklogin:false];
  670. [self checkToolBar];
  671. [self addtoportfolio];
  672. }
  673. };
  674. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  675. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  676. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  677. [self presentViewController:navi animated:YES completion:^{
  678. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  679. DebugLog(@"LoginViewController present.........");
  680. // self.btop = false;
  681. // <#code#>
  682. }];
  683. }
  684. else
  685. {
  686. [self addtoportfolio];
  687. }
  688. }
  689. - (IBAction)onAddWishlistClick:(id)sender {
  690. UIApplication * app = [UIApplication sharedApplication];
  691. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  692. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  693. if(appDelegate.bLogin==false)
  694. {
  695. LoginViewController * loginvc =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"];
  696. // loginvc.delegate = self;
  697. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  698. loginvc.returnValue = ^(bool blogin){
  699. if(blogin)
  700. {
  701. [main_vc checklogin:false];
  702. [self addtowish];
  703. }
  704. };
  705. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  706. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  707. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  708. [self presentViewController:navi animated:YES completion:^{
  709. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  710. DebugLog(@"LoginViewController present.........");
  711. // self.btop = false;
  712. // <#code#>
  713. }];
  714. }
  715. else
  716. {
  717. [self addtowish];
  718. }
  719. }
  720. -(void) addtocart
  721. {
  722. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  723. int count = [[items valueForKey:@"count"] intValue];
  724. NSMutableArray* checked = [[NSMutableArray alloc] init];
  725. for(int i=0;i<count;i++)
  726. {
  727. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  728. bool check = [[item valueForKey:@"checked"] boolValue];
  729. if(check)
  730. {
  731. // [checked addObject:[NSNumber numberWithInt:[[item valueForKey:@"product_id"] intValue]]];
  732. [checked addObject:[item valueForKey:@"fash_name"]];
  733. }
  734. }
  735. if (!count) {
  736. [RAUtils message_alert:@"There is no model." title:@"Cannot Add to cart." controller:self] ;
  737. return;
  738. }
  739. if(!self.addAll && checked.count==0)
  740. {
  741. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Add to cart." controller:self] ;
  742. return;
  743. }
  744. NSString * ids = [checked componentsJoinedByString:@","];
  745. self.addCartBtn.enabled = false;
  746. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  747. __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Add Models To Cart" completion:^{
  748. if (self.addAll)
  749. {
  750. [RANetwork request_addall_search:self.offset limit:20 keywords:self.keywords matchfull:self.loadmore_matchfull orderCode:appDelegate.order_code addTo:@"cart" completionHandler:^(NSMutableDictionary *result) {
  751. {
  752. NSDictionary* return_json=result;
  753. [waitalert dismissViewControllerAnimated:YES completion:^{
  754. self.addCartBtn.enabled = true;
  755. if([[return_json valueForKey:@"result"] intValue]==2)
  756. {
  757. #ifdef RA_NOTIFICATION
  758. [ActiveViewController Notify:@"CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  759. #else
  760. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  761. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  762. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  763. #endif
  764. NSString * msg = nil;
  765. if (self.addAll) {
  766. unsigned long checkedCount = [return_json[@"count"] integerValue];
  767. msg=[NSString stringWithFormat:@"%lu items added to Cart",checkedCount];
  768. } else {
  769. if(checked.count==1)
  770. {
  771. msg=@"1 item added to Cart";
  772. }
  773. else
  774. {
  775. msg=[NSString stringWithFormat:@"%lu items added to Cart",(unsigned long)checked.count];
  776. }
  777. }
  778. [RAUtils message_alert:nil title:msg controller:self] ;
  779. [self end_select:4];
  780. // }
  781. }
  782. else if([[return_json valueForKey:@"result"] intValue]==8)
  783. {
  784. #ifdef RA_NOTIFICATION
  785. [ActiveViewController Notify:@"CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  786. #else
  787. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  788. // appDelegate.order_code = order_code;
  789. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  790. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  791. #endif
  792. [self end_select:4];
  793. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  794. }
  795. else
  796. {
  797. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  798. }
  799. }];
  800. };
  801. }];
  802. }
  803. else
  804. {
  805. [RANetwork request_addto_cart_byname:ids withScreen:ScreenCodeSearch completionHandler:^(NSMutableDictionary *result) {
  806. NSDictionary* return_json=result;
  807. [waitalert dismissViewControllerAnimated:YES completion:^{
  808. self.addCartBtn.enabled = true;
  809. if([[return_json valueForKey:@"result"] intValue]==2)
  810. {
  811. #ifdef RA_NOTIFICATION
  812. [ActiveViewController Notify:@"CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  813. #else
  814. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  815. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  816. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  817. #endif
  818. NSString * msg = nil;
  819. if (self.addAll) {
  820. unsigned long checkedCount = [return_json[@"count"] integerValue];
  821. msg=[NSString stringWithFormat:@"%lu items added to Cart",checkedCount];
  822. } else {
  823. if(checked.count==1)
  824. {
  825. msg=@"1 item added to Cart";
  826. }
  827. else
  828. {
  829. msg=[NSString stringWithFormat:@"%lu items added to Cart",(unsigned long)checked.count];
  830. }
  831. }
  832. [RAUtils message_alert:nil title:msg controller:self] ;
  833. [self end_select:2];
  834. // }
  835. }
  836. else if([[return_json valueForKey:@"result"] intValue]==8)
  837. {
  838. #ifdef RA_NOTIFICATION
  839. [ActiveViewController Notify:@"CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  840. #else
  841. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  842. // appDelegate.order_code = order_code;
  843. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  844. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  845. #endif
  846. [self end_select:2];
  847. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  848. }
  849. else
  850. {
  851. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  852. }
  853. }];
  854. }];
  855. }
  856. }];
  857. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  858. //
  859. // NSDictionary* return_json = nil;
  860. // if (self.addAll) {
  861. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  862. // return_json = [RANetwork Search_addall:self.offset limit:20 keywords:self.keywords matchfull:self.loadmore_matchfull orderCode:appDelegate.order_code addTo:@"cart"];
  863. // } else {
  864. // return_json = [RANetwork add_toCart_byName:ids withScreen:ScreenCodeSearch];
  865. // }
  866. //
  867. //
  868. // dispatch_async(dispatch_get_main_queue(), ^{
  869. //
  870. //// [waitalert dismissViewControllerAnimated:YES completion:nil];
  871. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  872. //
  873. //
  874. // self.addCartBtn.enabled = true;
  875. //
  876. // if([[return_json valueForKey:@"result"] intValue]==2)
  877. // {
  878. //
  879. //
  880. //
  881. //#ifdef RA_NOTIFICATION
  882. // [ActiveViewController Notify:@"CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  883. //#else
  884. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  885. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  886. // [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  887. //#endif
  888. // NSString * msg = nil;
  889. //
  890. // if (self.addAll) {
  891. //
  892. // unsigned long checkedCount = [return_json[@"count"] integerValue];
  893. //
  894. // msg=[NSString stringWithFormat:@"%lu items added to Cart",checkedCount];
  895. // } else {
  896. //
  897. // if(checked.count==1)
  898. // {
  899. // msg=@"1 item added to Cart";
  900. // }
  901. // else
  902. // {
  903. // msg=[NSString stringWithFormat:@"%lu items added to Cart",(unsigned long)checked.count];
  904. // }
  905. // }
  906. //
  907. // [RAUtils message_alert:nil title:msg controller:self] ;
  908. //
  909. // [self end_select:2];
  910. //
  911. //
  912. //
  913. //// }
  914. // }
  915. // else if([[return_json valueForKey:@"result"] intValue]==8)
  916. // {
  917. //
  918. //
  919. //#ifdef RA_NOTIFICATION
  920. // [ActiveViewController Notify:@"CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  921. //#else
  922. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  923. // // appDelegate.order_code = order_code;
  924. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  925. // [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  926. //#endif
  927. // [self end_select:2];
  928. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  929. // }
  930. // else
  931. // {
  932. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  933. // }
  934. //
  935. //
  936. //
  937. //
  938. // });
  939. // });
  940. }
  941. -(void) addtoportfolio
  942. {
  943. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  944. // if (appDelegate.offline_mode) {
  945. //
  946. // [RAUtils message_alert:@"offline mode does not support this function." title:@"Add to Portfolio" controller:self] ;
  947. // return;
  948. // }
  949. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  950. int count = [[items valueForKey:@"count"] intValue];
  951. NSMutableArray* checked = [[NSMutableArray alloc] init];
  952. for(int i=0;i<count;i++)
  953. {
  954. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  955. bool check = [[item valueForKey:@"checked"] boolValue];
  956. if(check)
  957. {
  958. [checked addObject:[NSNumber numberWithInt:[[item valueForKey:@"product_id"] intValue]]];
  959. }
  960. }
  961. // if(checked.count==0)
  962. // {
  963. // [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Add to portfolio." controller:self] ;
  964. // return;
  965. // }
  966. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  967. if (appDelegate.user && appDelegate.user_type == USER_ROLE_CUSTOMER && appDelegate.customer_type == CustomerTypeStore) {
  968. [RAUtils message_alert:@"Cannot Add to portfolio." title:@"Warning" controller:self] ;
  969. return;
  970. }
  971. if (!count) {
  972. [RAUtils message_alert:@"There is no model." title:@"Cannot Add to portfolio." controller:self] ;
  973. return;
  974. }
  975. if(!self.addAll && checked.count==0)
  976. {
  977. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Add to portfolio." controller:self] ;
  978. return;
  979. }
  980. NSString * ids = [checked componentsJoinedByString:@","];
  981. self.addPortfolioBtn.enabled =false;
  982. __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Add Models To Portfolio" completion:^{
  983. if (self.addAll) {
  984. [RANetwork request_addall_search:self.offset limit:20 keywords:self.keywords matchfull:self.loadmore_matchfull orderCode:nil addTo:@"portfolio" completionHandler:^(NSMutableDictionary *result) {
  985. NSMutableDictionary* return_json = result;
  986. [waitalert dismissViewControllerAnimated:YES completion:^{
  987. self.addPortfolioBtn.enabled =true;
  988. if([[return_json valueForKey:@"result"] intValue]==2)
  989. {
  990. #ifdef RA_NOTIFICATION
  991. [ActiveViewController Notify:@"PortfolioViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  992. #else
  993. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  994. [((MainViewController*)appDelegate.main_vc) reloadPortfolio :true immediately:false];
  995. #endif
  996. NSString * msg = nil;
  997. if (self.addAll) {
  998. unsigned long checkedCount = [return_json[@"count"] integerValue];
  999. msg=[NSString stringWithFormat:@"%lu items added to Portfolio",checkedCount];
  1000. } else {
  1001. if(checked.count==1)
  1002. {
  1003. msg=@"1 item added to Portfolio";
  1004. }
  1005. else
  1006. {
  1007. msg=[NSString stringWithFormat:@"%lu items added to Portfolio",(unsigned long)checked.count];
  1008. }
  1009. }
  1010. [RAUtils message_alert:nil title:msg controller:self] ;
  1011. [self end_select:END_SELECT_PORTFOLIO];
  1012. }
  1013. else
  1014. {
  1015. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Portfolio" controller:self] ;
  1016. }
  1017. }];
  1018. }];
  1019. } else {
  1020. [RANetwork request_addto_portfolio:ids withScreen:ScreenCodeSearch completionHandler:^(NSMutableDictionary *result) {
  1021. NSMutableDictionary* return_json = result;
  1022. [waitalert dismissViewControllerAnimated:YES completion:^{
  1023. self.addPortfolioBtn.enabled =true;
  1024. if([[return_json valueForKey:@"result"] intValue]==2)
  1025. {
  1026. #ifdef RA_NOTIFICATION
  1027. [ActiveViewController Notify:@"PortfolioViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1028. #else
  1029. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1030. [((MainViewController*)appDelegate.main_vc) reloadPortfolio :true immediately:false];
  1031. #endif
  1032. NSString * msg = nil;
  1033. if (self.addAll) {
  1034. unsigned long checkedCount = [return_json[@"count"] integerValue];
  1035. msg=[NSString stringWithFormat:@"%lu items added to Portfolio",checkedCount];
  1036. } else {
  1037. if(checked.count==1)
  1038. {
  1039. msg=@"1 item added to Portfolio";
  1040. }
  1041. else
  1042. {
  1043. msg=[NSString stringWithFormat:@"%lu items added to Portfolio",(unsigned long)checked.count];
  1044. }
  1045. }
  1046. [RAUtils message_alert:nil title:msg controller:self] ;
  1047. [self end_select:END_SELECT_PORTFOLIO];
  1048. }
  1049. else
  1050. {
  1051. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Portfolio" controller:self] ;
  1052. }
  1053. }];
  1054. }];
  1055. }
  1056. }];
  1057. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1058. //
  1059. // NSDictionary* return_json = nil;
  1060. // if (self.addAll) {
  1061. //// AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  1062. // return_json = [RANetwork Search_addall:self.offset limit:20 keywords:self.keywords matchfull:self.loadmore_matchfull orderCode:nil addTo:@"portfolio"];
  1063. // } else {
  1064. // return_json = [RANetwork add_toPortfolio:ids withScreen:ScreenCodeSearch];
  1065. // }
  1066. //
  1067. //
  1068. // dispatch_async(dispatch_get_main_queue(), ^{
  1069. //// [waitalert dismissViewControllerAnimated:YES completion:nil];
  1070. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1071. //
  1072. // self.addPortfolioBtn.enabled =true;
  1073. //
  1074. // if([[return_json valueForKey:@"result"] intValue]==2)
  1075. // {
  1076. //
  1077. //
  1078. //
  1079. //
  1080. //
  1081. //
  1082. //#ifdef RA_NOTIFICATION
  1083. // [ActiveViewController Notify:@"PortfolioViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1084. //#else
  1085. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1086. // [((MainViewController*)appDelegate.main_vc) reloadPortfolio :true immediately:false];
  1087. //#endif
  1088. //
  1089. // NSString * msg = nil;
  1090. //
  1091. // if (self.addAll) {
  1092. //
  1093. // unsigned long checkedCount = [return_json[@"count"] integerValue];
  1094. //
  1095. // msg=[NSString stringWithFormat:@"%lu items added to Portfolio",checkedCount];
  1096. // } else {
  1097. //
  1098. // if(checked.count==1)
  1099. // {
  1100. // msg=@"1 item added to Portfolio";
  1101. // }
  1102. // else
  1103. // {
  1104. // msg=[NSString stringWithFormat:@"%lu items added to Portfolio",(unsigned long)checked.count];
  1105. // }
  1106. // }
  1107. //
  1108. // [RAUtils message_alert:nil title:msg controller:self] ;
  1109. //
  1110. //
  1111. // [self end_select:3];
  1112. //
  1113. // }
  1114. // else
  1115. // {
  1116. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Portfolio" controller:self] ;
  1117. // }
  1118. //
  1119. //
  1120. //
  1121. //
  1122. // });
  1123. // });
  1124. }
  1125. -(void) addtowish
  1126. {
  1127. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  1128. int count = [[items valueForKey:@"count"] intValue];
  1129. NSMutableArray* checked = [[NSMutableArray alloc] init];
  1130. for(int i=0;i<count;i++)
  1131. {
  1132. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  1133. bool check = [[item valueForKey:@"checked"] boolValue];
  1134. if(check)
  1135. {
  1136. [checked addObject:[NSNumber numberWithInt:[[item valueForKey:@"product_id"] intValue]]];
  1137. }
  1138. }
  1139. // if(checked.count==0)
  1140. // {
  1141. // [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Add To Wish List." controller:self] ;
  1142. // return;
  1143. // }
  1144. if (!count) {
  1145. [RAUtils message_alert:@"There is no model." title:@"Cannot Add to Wish List." controller:self] ;
  1146. return;
  1147. }
  1148. if(!self.addAll && checked.count==0)
  1149. {
  1150. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Add to Wish List." controller:self] ;
  1151. return;
  1152. }
  1153. NSString * ids = [checked componentsJoinedByString:@","];
  1154. self.addWishBtn.enabled =false;
  1155. __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Add Models To Wish List" completion:^{
  1156. if (self.addAll) {
  1157. [RANetwork request_addall_search:self.offset limit:20 keywords:self.keywords matchfull:self.loadmore_matchfull orderCode:nil addTo:@"wishlist" completionHandler:^(NSMutableDictionary *result) {
  1158. NSDictionary* return_json =result;
  1159. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1160. [waitalert dismissViewControllerAnimated:YES completion:^{
  1161. self.addWishBtn.enabled =true;
  1162. if([[return_json valueForKey:@"result"] intValue]==2)
  1163. {
  1164. #ifdef RA_NOTIFICATION
  1165. [ActiveViewController Notify:@"WatchListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1166. #else
  1167. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1168. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  1169. #endif
  1170. NSString * msg = nil;
  1171. if (self.addAll) {
  1172. unsigned long checkedCount = [return_json[@"count"] integerValue];
  1173. msg=[NSString stringWithFormat:@"%lu items added to \nWish List",checkedCount];
  1174. } else {
  1175. if(checked.count==1)
  1176. {
  1177. msg=@"1 item added to \nWish List";
  1178. }
  1179. else
  1180. {
  1181. msg=[NSString stringWithFormat:@"%lu items added to \nWish List",(unsigned long)checked.count];
  1182. }
  1183. }
  1184. [RAUtils message_alert:nil title:@" Multiple items added to \nWish List" controller:self] ;
  1185. [self end_select:3];
  1186. }
  1187. else
  1188. {
  1189. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Wish List" controller:self] ;
  1190. }
  1191. }];
  1192. }];
  1193. } else {
  1194. [RANetwork request_addto_wishlist:ids withScreen:ScreenCodeSearch completionHandler:^(NSMutableDictionary *result) {
  1195. NSDictionary* return_json =result;
  1196. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1197. [waitalert dismissViewControllerAnimated:YES completion:^{
  1198. self.addWishBtn.enabled =true;
  1199. if([[return_json valueForKey:@"result"] intValue]==2)
  1200. {
  1201. #ifdef RA_NOTIFICATION
  1202. [ActiveViewController Notify:@"WatchListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1203. #else
  1204. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1205. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  1206. #endif
  1207. NSString * msg = nil;
  1208. if (self.addAll) {
  1209. unsigned long checkedCount = [return_json[@"count"] integerValue];
  1210. msg=[NSString stringWithFormat:@"%lu items added to \nWish List",checkedCount];
  1211. } else {
  1212. if(checked.count==1)
  1213. {
  1214. msg=@"1 item added to \nWish List";
  1215. }
  1216. else
  1217. {
  1218. msg=[NSString stringWithFormat:@"%lu items added to \nWish List",(unsigned long)checked.count];
  1219. }
  1220. }
  1221. [RAUtils message_alert:nil title:@" Multiple items added to \nWish List" controller:self] ;
  1222. [self end_select:1];
  1223. }
  1224. else
  1225. {
  1226. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Wish List" controller:self] ;
  1227. }
  1228. }];
  1229. }];
  1230. }
  1231. }];
  1232. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1233. //
  1234. // NSDictionary* return_json = nil;
  1235. // if (self.addAll) {
  1236. //// AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  1237. // return_json = [RANetwork Search_addall:self.offset limit:20 keywords:self.keywords matchfull:self.loadmore_matchfull orderCode:nil addTo:@"wishlist"];
  1238. // } else {
  1239. // return_json = [RANetwork add_toWatchList:ids withScreen:ScreenCodeSearch];
  1240. // }
  1241. //
  1242. //
  1243. // dispatch_async(dispatch_get_main_queue(), ^{
  1244. //
  1245. //// [waitalert dismissViewControllerAnimated:YES completion:nil];
  1246. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1247. //
  1248. // self.addWishBtn.enabled =true;
  1249. //
  1250. // if([[return_json valueForKey:@"result"] intValue]==2)
  1251. // {
  1252. //
  1253. //
  1254. //
  1255. //
  1256. //#ifdef RA_NOTIFICATION
  1257. // [ActiveViewController Notify:@"WatchListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1258. //#else
  1259. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1260. //
  1261. // [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  1262. //#endif
  1263. //
  1264. // NSString * msg = nil;
  1265. //
  1266. // if (self.addAll) {
  1267. //
  1268. // unsigned long checkedCount = [return_json[@"count"] integerValue];
  1269. //
  1270. // msg=[NSString stringWithFormat:@"%lu items added to \nWish List",checkedCount];
  1271. // } else {
  1272. //
  1273. // if(checked.count==1)
  1274. // {
  1275. // msg=@"1 item added to \nWish List";
  1276. // }
  1277. // else
  1278. // {
  1279. // msg=[NSString stringWithFormat:@"%lu items added to \nWish List",(unsigned long)checked.count];
  1280. // }
  1281. // }
  1282. //
  1283. // [RAUtils message_alert:nil title:@" Multiple items added to \nWish List" controller:self] ;
  1284. //
  1285. //
  1286. // [self end_select:1];
  1287. //
  1288. // }
  1289. // else
  1290. // {
  1291. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Wish List" controller:self] ;
  1292. // }
  1293. //
  1294. //
  1295. //
  1296. //
  1297. // });
  1298. // });
  1299. }
  1300. -(void) operation_loadMore
  1301. {
  1302. if (self.dataOperationQueue.operationCount > 1) {
  1303. return;
  1304. }
  1305. dispatch_async(dispatch_get_main_queue(), ^{
  1306. if(self.loadall)
  1307. {
  1308. if(self.showalert)
  1309. {
  1310. self.showalert=false;
  1311. [RAUtils message_alert:@"All loaded" title:@"Loading items" controller:self] ;
  1312. }
  1313. return;
  1314. }
  1315. self.mum.hidden = false;
  1316. [self.mum startAnimating];
  1317. self.mum.center = self.view.center;
  1318. self.isrefreshing=true;
  1319. [RANetwork request_search:self.offset limit:20 keywords:self.keywords matchfull:self.loadmore_matchfull completionHandler:^(NSMutableDictionary *result) {
  1320. NSMutableDictionary* category_more=result;
  1321. [self.mum stopAnimating];
  1322. self.isrefreshing=false;
  1323. if (self.dataOperationQueue.operationCount > 1) {
  1324. return;
  1325. }
  1326. NSDictionary* more_items=[category_more objectForKey:@"items"];
  1327. if([[category_more valueForKey:@"result"] intValue]==2)
  1328. {
  1329. // self.category_data = category_data;
  1330. NSMutableDictionary* items_json= [[self.category_data objectForKey:@"items"] mutableCopy];
  1331. int count =[[more_items valueForKey:@"count"] intValue];
  1332. for(int i=0;i<count;i++)
  1333. {
  1334. NSDictionary * item_json = [more_items objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  1335. [items_json setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",i+self.offset]];
  1336. }
  1337. long newcount=[[[self.category_data objectForKey:@"items"] valueForKey:@"count"] intValue]+count;
  1338. [items_json setValue: [NSString stringWithFormat:@"%ld",newcount] forKey:@"count"];
  1339. [self.category_data setObject:items_json forKey:@"items"];
  1340. self.offset+=count;
  1341. if(count<20)
  1342. {
  1343. self.loadall=true;
  1344. }
  1345. // NSDictionary * item_json = [self.category_data objectForKey:@"items"];
  1346. // DebugLog(@"%@",self.category_data);
  1347. [self.collectionview reloadData];
  1348. }
  1349. else
  1350. {
  1351. [RAUtils message_alert:[category_more valueForKey:@"err_msg"] title:@"Search" controller:self] ;
  1352. }
  1353. self.isrefreshing=false;
  1354. }];
  1355. return;
  1356. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1357. //
  1358. //
  1359. //
  1360. // NSDictionary* category_more=[RANetwork Search:self.offset limit:20 keywords:self.keywords matchfull:self.loadmore_matchfull];
  1361. // dispatch_async(dispatch_get_main_queue(), ^{
  1362. // [self.mum stopAnimating];
  1363. // self.isrefreshing=false;
  1364. // if (self.dataOperationQueue.operationCount > 1) {
  1365. // return;
  1366. // }
  1367. //
  1368. // NSDictionary* more_items=[category_more objectForKey:@"items"];
  1369. // if([[category_more valueForKey:@"result"] intValue]==2)
  1370. // {
  1371. // // self.category_data = category_data;
  1372. // NSMutableDictionary* items_json= [[self.category_data objectForKey:@"items"] mutableCopy];
  1373. //
  1374. // int count =[[more_items valueForKey:@"count"] intValue];
  1375. // for(int i=0;i<count;i++)
  1376. // {
  1377. //
  1378. // NSDictionary * item_json = [more_items objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  1379. // [items_json setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",i+self.offset]];
  1380. //
  1381. //
  1382. // }
  1383. // long newcount=[[[self.category_data objectForKey:@"items"] valueForKey:@"count"] intValue]+count;
  1384. // [items_json setValue: [NSString stringWithFormat:@"%ld",newcount] forKey:@"count"];
  1385. // [self.category_data setObject:items_json forKey:@"items"];
  1386. // self.offset+=count;
  1387. // if(count<20)
  1388. // {
  1389. // self.loadall=true;
  1390. //
  1391. // }
  1392. // // NSDictionary * item_json = [self.category_data objectForKey:@"items"];
  1393. //
  1394. //
  1395. //
  1396. //
  1397. // // DebugLog(@"%@",self.category_data);
  1398. //
  1399. // [self.collectionview reloadData];
  1400. //
  1401. // }
  1402. // else
  1403. // {
  1404. // [RAUtils message_alert:[category_more valueForKey:@"err_msg"] title:@"Search" controller:self] ;
  1405. // }
  1406. // self.isrefreshing=false;
  1407. //
  1408. // });
  1409. //// dispatch_async(dispatch_get_main_queue(), ^{
  1410. //// // self.category_data = category_data;
  1411. ////
  1412. ////
  1413. //// if([[category_more valueForKey:@"result"] intValue]==2)
  1414. //// {
  1415. //// NSMutableDictionary* items_json= [[self.category_data objectForKey:@"items"] mutableCopy];
  1416. ////
  1417. //// int count =[[category_more valueForKey:@"count"] intValue];
  1418. //// for(int i=0;i<count;i++)
  1419. //// {
  1420. ////
  1421. //// NSDictionary * item_json = [category_more objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  1422. //// [items_json setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",i+self.offset]];
  1423. ////
  1424. ////
  1425. //// }
  1426. //// long newcount=[[[self.category_data objectForKey:@"items"] valueForKey:@"count"] intValue]+count;
  1427. //// [items_json setValue: [NSString stringWithFormat:@"%ld",newcount] forKey:@"count"];
  1428. //// [self.category_data setObject:items_json forKey:@"items"];
  1429. //// self.offset+=count;
  1430. //// // NSDictionary * item_json = [self.category_data objectForKey:@"items"];
  1431. ////
  1432. ////
  1433. ////
  1434. ////
  1435. //// DebugLog(@"%@",self.category_data);
  1436. ////
  1437. //// [self.collectionview reloadData];
  1438. //// }
  1439. //// else
  1440. //// {
  1441. //// [RAUtils error_alert:[category_more valueForKey:@"err_msg"]] ;
  1442. //// }
  1443. ////
  1444. ////
  1445. ////
  1446. ////
  1447. //// });
  1448. // });
  1449. });
  1450. }
  1451. -(void) loadMore {
  1452. __weak typeof(self) weakself = self;
  1453. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  1454. if (weakself) {
  1455. __strong typeof(weakself) strongself = weakself;
  1456. [strongself operation_loadMore];
  1457. }
  1458. }];
  1459. [self.dataOperationQueue addOperation:operation];
  1460. }
  1461. -(void) operation_reload
  1462. {
  1463. if (self.dataOperationQueue.operationCount > 1) {
  1464. return;
  1465. }
  1466. dispatch_async(dispatch_get_main_queue(), ^{
  1467. if(self.isrefreshing)
  1468. return;
  1469. self.showalert=true;
  1470. self.isrefreshing=true;
  1471. self.loadall = false;
  1472. UIRefreshControl *reF = (UIRefreshControl *)[self.collectionview viewWithTag:200];
  1473. [reF endRefreshing];
  1474. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  1475. self.label_net_err.hidden=true;
  1476. self.collectionview.hidden=true;
  1477. self.mum.center = self.view.center;
  1478. self.mum.hidden = false;
  1479. [self.mum startAnimating];
  1480. self.loadmore_matchfull = self.switch_matchfull.isOn;
  1481. [RANetwork request_search:0 limit:20 keywords:self.keywords
  1482. matchfull:self.loadmore_matchfull completionHandler:^(NSMutableDictionary *result) {
  1483. NSMutableDictionary* category_data=result;
  1484. [self.mum stopAnimating];
  1485. self.isrefreshing=false;
  1486. if (self.dataOperationQueue.operationCount > 1) {
  1487. return;
  1488. }
  1489. self.category_data = [category_data mutableCopy];
  1490. [self.collectionview reloadData];
  1491. if([[category_data valueForKey:@"result"] intValue]==2)
  1492. {
  1493. self.collectionview.hidden=false;
  1494. NSDictionary * item_json = [self.category_data objectForKey:@"items"];
  1495. self.offset =[[item_json valueForKey:@"count"] intValue];
  1496. int total = [[self.category_data valueForKey:@"item_total_count"]intValue];
  1497. if(total<2)
  1498. {
  1499. self.labelTotal.text = [NSString stringWithFormat:@"Total %d item",total];
  1500. if(total==1)
  1501. {
  1502. NSDictionary * detail_item = [item_json objectForKey:@"item_0"];
  1503. NSString* detail_id= [detail_item valueForKey:@"product_id"] ;
  1504. [self showDetailat:detail_id category_id:nil name:[detail_item objectForKey:@"fash_name"] index:0];
  1505. }
  1506. }
  1507. else
  1508. self.labelTotal.text = [NSString stringWithFormat:@"Total %d items",total];
  1509. }
  1510. else if([[category_data valueForKey:@"result"] intValue]==RESULT_NET_ERROR)
  1511. {
  1512. self.collectionview.hidden=true;
  1513. self.label_net_err.hidden=false;
  1514. }
  1515. else
  1516. {
  1517. [RAUtils message_alert:[category_data valueForKey:@"err_msg"] title:@"Search" controller:self] ;
  1518. }
  1519. self.isrefreshing=false;
  1520. }];
  1521. return;
  1522. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1523. //
  1524. // NSDictionary* category_data=[RANetwork Search:0 limit:20 keywords:self.keywords matchfull:self.loadmore_matchfull];
  1525. //
  1526. // dispatch_async(dispatch_get_main_queue(), ^{
  1527. //
  1528. // [self.mum stopAnimating];
  1529. // self.isrefreshing=false;
  1530. // if (self.dataOperationQueue.operationCount > 1) {
  1531. // return;
  1532. // }
  1533. //
  1534. // self.category_data = [category_data mutableCopy];
  1535. // [self.collectionview reloadData];
  1536. //
  1537. // if([[category_data valueForKey:@"result"] intValue]==2)
  1538. // {
  1539. // self.collectionview.hidden=false;
  1540. // NSDictionary * item_json = [self.category_data objectForKey:@"items"];
  1541. // self.offset =[[item_json valueForKey:@"count"] intValue];
  1542. //
  1543. // int total = [[self.category_data valueForKey:@"item_total_count"]intValue];
  1544. //
  1545. // if(total<2)
  1546. // {
  1547. // self.labelTotal.text = [NSString stringWithFormat:@"Total %d item",total];
  1548. // if(total==1)
  1549. // {
  1550. // NSDictionary * detail_item = [item_json objectForKey:@"item_0"];
  1551. // NSString* detail_id= [detail_item valueForKey:@"product_id"] ;
  1552. //
  1553. // [self showDetailat:detail_id category_id:nil name:[detail_item objectForKey:@"fash_name"] index:0];
  1554. // }
  1555. // }
  1556. // else
  1557. // self.labelTotal.text = [NSString stringWithFormat:@"Total %d items",total];
  1558. // }
  1559. // else if([[category_data valueForKey:@"result"] intValue]==RESULT_NET_ERROR)
  1560. // {
  1561. // self.collectionview.hidden=true;
  1562. // self.label_net_err.hidden=false;
  1563. // }
  1564. // else
  1565. // {
  1566. // [RAUtils message_alert:[category_data valueForKey:@"err_msg"] title:@"Search" controller:self] ;
  1567. // }
  1568. //
  1569. // self.isrefreshing=false;
  1570. //
  1571. // });
  1572. // });
  1573. });
  1574. }
  1575. -(void) reload {
  1576. __weak typeof(self) weakself = self;
  1577. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  1578. if (weakself) {
  1579. __strong typeof(weakself) strongself = weakself;
  1580. [strongself operation_reload];
  1581. }
  1582. }];
  1583. [self.dataOperationQueue addOperation:operation];
  1584. }
  1585. //-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  1586. //{
  1587. // [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  1588. // UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  1589. // if (UIInterfaceOrientationIsLandscape(orientation)|| UIInterfaceOrientationIsPortrait(orientation))
  1590. // {
  1591. // // self.orientation = orientation;
  1592. // [self.collectionview.collectionViewLayout invalidateLayout];
  1593. // DebugLog(@"routed");
  1594. // }
  1595. //
  1596. //
  1597. //
  1598. //
  1599. //}
  1600. //- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  1601. //
  1602. // [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
  1603. //
  1604. // self.toOrientation = toInterfaceOrientation;
  1605. // [self.collectionview.collectionViewLayout invalidateLayout];
  1606. //
  1607. //}
  1608. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  1609. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  1610. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  1611. // what ever you want to prepare
  1612. } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  1613. self.headerView.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.headerView.bounds].CGPath;
  1614. self.toOrientation = [UIApplication sharedApplication].statusBarOrientation;
  1615. [self.collectionview.collectionViewLayout invalidateLayout];
  1616. }];
  1617. }
  1618. /*
  1619. #pragma mark - Navigation
  1620. // In a storyboard-based application, you will often want to do a little preparation before navigation
  1621. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  1622. // Get the new view controller using [segue destinationViewController].
  1623. // Pass the selected object to the new view controller.
  1624. }
  1625. */
  1626. - (IBAction)onMatchFullChanged:(id)sender {
  1627. // NSDictionary * item_json = [self.category_data objectForKey:@"items"];
  1628. // int count =[[item_json valueForKey:@"count"] intValue];
  1629. // if(count!=0)
  1630. // {
  1631. // self.offset = 0;
  1632. // [self reload];
  1633. // }
  1634. //
  1635. }
  1636. #pragma mark - searchBar delegate;
  1637. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  1638. {
  1639. self.keywords=searchBar.text;
  1640. self.offset = 0;
  1641. // [self.content_data removeAllObjects];
  1642. [self reload];
  1643. DebugLog(@"search");
  1644. }
  1645. /*
  1646. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  1647. {
  1648. if([searchText isEqualToString:@""] && !self.reset)
  1649. {
  1650. DebugLog(@"clear");
  1651. self.reset=true;
  1652. self.keywords=nil;
  1653. self.offset = 0;
  1654. [self.content_data removeAllObjects];
  1655. [self loadpage];
  1656. [self loadpage];
  1657. }
  1658. else
  1659. self.reset = false;
  1660. }*/
  1661. #pragma mark -- UICollectionViewDataSource
  1662. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  1663. {
  1664. // 每个Section的item个数
  1665. NSDictionary * item_json = [self.category_data objectForKey:@"items"];
  1666. int count =[[item_json valueForKey:@"count"] intValue];
  1667. return count;
  1668. }
  1669. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  1670. {
  1671. return 1;
  1672. }
  1673. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  1674. {
  1675. NSString* value =[DefaultAppearance get_noneappearance_value:@"CategoryViewController" valuename:@"cell_border_color"];
  1676. if(value==nil)
  1677. value=@"";
  1678. unsigned long color = strtoul([value UTF8String],0,16);
  1679. if(self.categoryViewType == CATEGORY_VIEWTYPE_SMALL)
  1680. {
  1681. static NSString * CellIdentifier = @"CategoryCellNPD";
  1682. CategoryCellNPD * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
  1683. NSDictionary * item_json =[[self.category_data objectForKey:@"items" ] objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1684. NSString* description =[item_json valueForKey:@"description"];
  1685. NSString* name =[item_json valueForKey:@"fash_name"];
  1686. bool wish_exist = [[item_json valueForKey:@"wish_exists"]boolValue];
  1687. bool cart_exists = [[item_json valueForKey:@"cart_exists"]boolValue];
  1688. bool closeout = [[item_json valueForKey:@"is_closeout"]boolValue];
  1689. BOOL more_color = [[item_json valueForKey:@"more_color"] boolValue];
  1690. cell.mark_moreColor.hidden = !more_color;
  1691. if(wish_exist)
  1692. cell.mark_wish.hidden=false;
  1693. else
  1694. cell.mark_wish.hidden=true;
  1695. if(cart_exists)
  1696. cell.mark_order.hidden=false;
  1697. else
  1698. cell.mark_order.hidden=true;
  1699. if(closeout)
  1700. cell.mark_closeout.hidden=false;
  1701. else
  1702. cell.mark_closeout.hidden=true;
  1703. // NSString* old_price =[item_json valueForKey:@"old_price"];
  1704. // NSString* price =[item_json valueForKey:@"price"];
  1705. bool checked =[[item_json valueForKey:@"checked"] boolValue];
  1706. if(!checked)
  1707. {
  1708. cell.checkImageView.hidden = true;
  1709. [cell setBackgroundColor:[UIColor whiteColor]/* UIColorFromRGB(0x996633)*/];
  1710. }
  1711. else
  1712. {
  1713. cell.checkImageView.hidden = false;
  1714. [cell setBackgroundColor: UIColorFromRGB(0x009966)];
  1715. }
  1716. cell.nameLabel.text = description;
  1717. cell.modelNoLabel.text=name;
  1718. // cell.cellDescription.text = description;
  1719. // cell.oldPrice.text = old_price;
  1720. // cell.Price.text = price;
  1721. cell.layer.borderColor = UIColorFromRGB(color).CGColor;
  1722. cell.layer.borderWidth = 0.5;
  1723. //cell.layer.cornerRadius=15;
  1724. cell.layer.masksToBounds=true;
  1725. #ifdef BUILD_UWAVER
  1726. cell.mark_wish.hidden = YES;
  1727. cell.mark_order.hidden = YES;
  1728. cell.mark_closeout.hidden = YES;
  1729. cell.mark_moreColor.hidden = YES;
  1730. #endif
  1731. return cell;
  1732. }
  1733. else if(self.categoryViewType == CATEGORY_VIEWTYPE_LARGE)
  1734. {
  1735. static NSString * CellIdentifier = @"CategoryCellNPDLarge";
  1736. CategoryCellNPD * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
  1737. NSDictionary * item_json =[[self.category_data objectForKey:@"items" ] objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1738. NSString* description =[item_json valueForKey:@"description"];
  1739. NSString* name =[item_json valueForKey:@"fash_name"];
  1740. bool wish_exist = [[item_json valueForKey:@"wish_exists"]boolValue];
  1741. bool cart_exists = [[item_json valueForKey:@"cart_exists"]boolValue];
  1742. bool closeout = [[item_json valueForKey:@"is_closeout"]boolValue];
  1743. BOOL more_color = [[item_json valueForKey:@"more_color"] boolValue];
  1744. cell.mark_moreColor.hidden = !more_color;
  1745. if(wish_exist)
  1746. cell.mark_wish.hidden=false;
  1747. else
  1748. cell.mark_wish.hidden=true;
  1749. if(cart_exists)
  1750. cell.mark_order.hidden=false;
  1751. else
  1752. cell.mark_order.hidden=true;
  1753. if(closeout)
  1754. cell.mark_closeout.hidden=false;
  1755. else
  1756. cell.mark_closeout.hidden=true;
  1757. // NSString* old_price =[item_json valueForKey:@"old_price"];
  1758. // NSString* price =[item_json valueForKey:@"price"];
  1759. bool checked =[[item_json valueForKey:@"checked"] boolValue];
  1760. if(!checked)
  1761. {
  1762. cell.checkImageView.hidden = true;
  1763. [cell setBackgroundColor:[UIColor whiteColor]/* UIColorFromRGB(0x996633)*/];
  1764. }
  1765. else
  1766. {
  1767. cell.checkImageView.hidden = false;
  1768. [cell setBackgroundColor: UIColorFromRGB(0x009966)];
  1769. }
  1770. cell.nameLabel.text = description;
  1771. cell.modelNoLabel.text=name;
  1772. // cell.cellDescription.text = description;
  1773. // cell.oldPrice.text = old_price;
  1774. // cell.Price.text = price;
  1775. cell.layer.borderColor = UIColorFromRGB(color).CGColor;
  1776. cell.layer.borderWidth = 0.5;
  1777. //cell.layer.cornerRadius=15;
  1778. cell.layer.masksToBounds=true;
  1779. #ifdef BUILD_UWAVER
  1780. cell.mark_wish.hidden = YES;
  1781. cell.mark_order.hidden = YES;
  1782. cell.mark_closeout.hidden = YES;
  1783. cell.mark_moreColor.hidden = YES;
  1784. #endif
  1785. return cell;
  1786. }
  1787. else
  1788. {
  1789. static NSString * CellIdentifier = @"CategoryCellNPDList";
  1790. CategoryCellNPD * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
  1791. NSDictionary * item_json =[[self.category_data objectForKey:@"items" ] objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1792. NSString* description =[item_json valueForKey:@"description"];
  1793. NSString* name =[item_json valueForKey:@"fash_name"];
  1794. bool wish_exist = [[item_json valueForKey:@"wish_exists"]boolValue];
  1795. bool cart_exists = [[item_json valueForKey:@"cart_exists"]boolValue];
  1796. bool closeout = [[item_json valueForKey:@"is_closeout"]boolValue];
  1797. BOOL more_color = [[item_json valueForKey:@"more_color"] boolValue];
  1798. cell.mark_moreColor.hidden = !more_color;
  1799. if(wish_exist)
  1800. cell.mark_wish.hidden=false;
  1801. else
  1802. cell.mark_wish.hidden=true;
  1803. if(cart_exists)
  1804. cell.mark_order.hidden=false;
  1805. else
  1806. cell.mark_order.hidden=true;
  1807. if(closeout)
  1808. cell.mark_closeout.hidden=false;
  1809. else
  1810. cell.mark_closeout.hidden=true;
  1811. // NSString* old_price =[item_json valueForKey:@"old_price"];
  1812. // NSString* price =[item_json valueForKey:@"price"];
  1813. bool checked =[[item_json valueForKey:@"checked"] boolValue];
  1814. if(!checked)
  1815. {
  1816. cell.checkImageView.hidden = true;
  1817. [cell setBackgroundColor: [UIColor whiteColor]];
  1818. }
  1819. else
  1820. {
  1821. cell.checkImageView.hidden = false;
  1822. [cell setBackgroundColor: UIColorFromRGB(0x009966)];
  1823. }
  1824. cell.nameLabel.text = description;
  1825. cell.modelNoLabel.text=name;
  1826. // cell.cellDescription.text = description;
  1827. // cell.oldPrice.text = old_price;
  1828. // cell.Price.text = price;
  1829. cell.layer.borderColor = UIColorFromRGB(color).CGColor;
  1830. cell.layer.borderWidth = 0.5;
  1831. //cell.layer.cornerRadius=15;
  1832. cell.layer.masksToBounds=true;
  1833. #ifdef BUILD_UWAVER
  1834. cell.mark_wish.hidden = YES;
  1835. cell.mark_order.hidden = YES;
  1836. cell.mark_closeout.hidden = YES;
  1837. cell.mark_moreColor.hidden = YES;
  1838. #endif
  1839. return cell;
  1840. }
  1841. }
  1842. - (void)collectionCell:(CategoryCellNPD *)cell setImageForIndexPath:(NSIndexPath *)indexPath {
  1843. NSDictionary * item_json =[[self.category_data objectForKey:@"items" ] objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  1844. NSString* img_url =[item_json valueForKey:@"img"];
  1845. NSString *loading = @"loading_l";
  1846. NSString *notFound = @"notfound_l";
  1847. if (self.categoryViewType == CATEGORY_VIEWTYPE_LARGE) {
  1848. loading = @"loading_l";
  1849. notFound = @"notfound_l";
  1850. img_url =[item_json valueForKey:@"img_big"]; // large 大图
  1851. } else if (self.categoryViewType == CATEGORY_VIEWTYPE_SMALL) {
  1852. loading = @"loading_s";
  1853. notFound = @"notfound_s";
  1854. img_url =[item_json valueForKey:@"img"]; // large 大图
  1855. }
  1856. NSString* file_name=[img_url lastPathComponent];
  1857. if (![cell.imageName isEqualToString:img_url]) {
  1858. cell.imageName = img_url;
  1859. cell.cellImageView.image = [UIImage imageNamed:loading];
  1860. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  1861. if(img_data!=nil)
  1862. {
  1863. UIImage * img =[UIImage imageWithData:img_data];
  1864. cell.cellImageView.image = img;
  1865. }
  1866. else
  1867. {
  1868. NSLog(@"load image");
  1869. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1870. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  1871. dispatch_async(dispatch_get_main_queue(), ^{
  1872. if(downloadimg_data!=nil)
  1873. {
  1874. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  1875. UIImage * img =[UIImage imageWithData:downloadimg_data];
  1876. cell.cellImageView.image = img;
  1877. }
  1878. else
  1879. cell.cellImageView.image = [UIImage imageNamed:notFound];
  1880. });
  1881. });
  1882. }
  1883. }
  1884. }
  1885. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  1886. CategoryCellNPD * son_cell = (CategoryCellNPD *)cell;
  1887. son_cell.imageName = nil;
  1888. [self collectionCell:son_cell setImageForIndexPath:indexPath];
  1889. }
  1890. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  1891. CategoryCellNPD * son_cell = (CategoryCellNPD *)cell;
  1892. NSString *loading = @"loading_l";
  1893. if (self.categoryViewType == CATEGORY_VIEWTYPE_LARGE) {
  1894. loading = @"loading_l";
  1895. } else if (self.categoryViewType == CATEGORY_VIEWTYPE_SMALL) {
  1896. loading = @"loading_s";
  1897. }
  1898. son_cell.cellImageView.image = [UIImage imageNamed:loading];
  1899. }
  1900. #pragma mark --UICollectionViewDelegateFlowLayout
  1901. ////定义每个UICollectionView 的大小
  1902. //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  1903. //{
  1904. // return CGSizeMake(96, 100);
  1905. //}
  1906. //定义每个UICollectionView 的 margin
  1907. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  1908. {
  1909. UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  1910. orientation = self.toOrientation;
  1911. if (UIInterfaceOrientationIsLandscape(orientation))
  1912. {
  1913. // DebugLog(@"横");
  1914. // if(self.showDetail)
  1915. // return CGSizeMake(96, 100);
  1916. if(self.categoryViewType ==CATEGORY_VIEWTYPE_LARGE)
  1917. return CGSizeMake(328, 391);
  1918. else if(self.categoryViewType ==CATEGORY_VIEWTYPE_LIST)
  1919. return CGSizeMake(1024, 120);
  1920. return CGSizeMake(193, 269);
  1921. }
  1922. else if(UIInterfaceOrientationIsPortrait(orientation))
  1923. {
  1924. // DebugLog(@"竖");
  1925. // if(self.showDetail)
  1926. // return CGSizeMake(96, 100);
  1927. if(self.categoryViewType ==CATEGORY_VIEWTYPE_LARGE)
  1928. return CGSizeMake(369, 440);
  1929. else if(self.categoryViewType ==CATEGORY_VIEWTYPE_LIST)
  1930. return CGSizeMake(768, 120);
  1931. return CGSizeMake(180, 251);
  1932. }
  1933. DebugLog(@"error , %ld",(long)orientation );
  1934. return CGSizeMake(96, 100);
  1935. }
  1936. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  1937. {
  1938. if(self.categoryViewType ==CATEGORY_VIEWTYPE_LIST)
  1939. return 1;
  1940. return 15;
  1941. }
  1942. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  1943. {
  1944. if(self.showDetail)
  1945. return UIEdgeInsetsMake(10, 5, 10, 5);
  1946. // if(self.categoryViewType ==CATEGORY_VIEWTYPE_LARGE)
  1947. // return UIEdgeInsetsMake(10, 10, 10, 10);
  1948. if(self.categoryViewType ==CATEGORY_VIEWTYPE_LARGE)
  1949. return UIEdgeInsetsMake(10, 10, 10, 10);
  1950. else if(self.categoryViewType ==CATEGORY_VIEWTYPE_LIST)
  1951. return UIEdgeInsetsMake(10, 0, 10, 0);
  1952. return UIEdgeInsetsMake(10, 10, 10, 10);
  1953. return UIEdgeInsetsMake(10, 10, 10, 10);
  1954. }
  1955. #pragma mark --UICollectionViewDelegate
  1956. -(void) showDetailat:(NSString*) item_id category_id:(NSString*) category_id name:(NSString *)name index:(long)index
  1957. {
  1958. DetailViewController* dvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Model" bundle:nil] instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  1959. // dvc
  1960. dvc.OnBack=^(bool add_cart,bool add_wish,bool update_data)
  1961. {
  1962. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  1963. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%ld",index]] mutableCopy];
  1964. if(add_cart)
  1965. item[@"cart_exists"]=@"true";
  1966. if(add_wish)
  1967. item[@"wish_exists"]=@"true";
  1968. items[[NSString stringWithFormat:@"item_%ld",index]] = item;
  1969. self.category_data[@"items"]=items;
  1970. [self.collectionview reloadData];
  1971. };
  1972. // dvc.use_model_name = true;
  1973. // dvc.model_name=[transformed stringValue];
  1974. dvc.product_id = item_id;
  1975. dvc.category_id=category_id;
  1976. dvc.ispush = true;
  1977. dvc.model_name = name;
  1978. [dvc reload];
  1979. [self.navigationController pushViewController:dvc animated:true];
  1980. return;
  1981. // //一下为detail嵌入当前窗口的方式
  1982. // self.categoryViewType = CATEGORY_VIEWTYPE_SMALL;
  1983. // // [self.collectionview.collectionViewLayout invalidateLayout];
  1984. // [self.collectionview reloadData];
  1985. // UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  1986. // int width=175;
  1987. // // int height;
  1988. // if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight)
  1989. // {
  1990. // width = 370;
  1991. // // height = 400;
  1992. // }
  1993. // else
  1994. // {
  1995. // UICollectionViewFlowLayout *flowLayout =(UICollectionViewFlowLayout*)[self.collectionview collectionViewLayout];
  1996. //
  1997. // flowLayout.scrollDirection= UICollectionViewScrollDirectionHorizontal;
  1998. // // [self.collectionview ] set
  1999. // // height = 251;
  2000. // width = 180;
  2001. // }
  2002. //
  2003. //
  2004. // CGRect frame = CGRectMake(0, 48, self.view.bounds.size.width, 271);
  2005. //
  2006. //
  2007. //// CGRect framedetail = CGRectMake(0,271 ,self.view.bounds.size.width,self.view.bounds.size.height-271);
  2008. //
  2009. // [UIView animateWithDuration:0.5 animations:^{
  2010. // self.collectionview.frame=frame;
  2011. // // self.detailView.frame = framedetail;
  2012. // // self.cagegoryLabel.hidden = true;
  2013. // }];
  2014. // self.collectionview.autoresizingMask = self.collectionview.autoresizingMask ^ UIViewAutoresizingFlexibleWidth;
  2015. //
  2016. // self.showDetail = true;
  2017. //
  2018. //
  2019. // NSArray* a= self.childViewControllers;
  2020. //
  2021. // for (int i=0;i<a.count;i++)
  2022. // {
  2023. // if([a[i] isKindOfClass:[DetailViewController class]])
  2024. // {
  2025. // DetailViewController *detailvc=a[i];
  2026. // detailvc.category_id = nil;
  2027. // detailvc.product_id = item_id;
  2028. // detailvc.ispush=false;
  2029. // [detailvc reload];
  2030. // }
  2031. // }
  2032. //
  2033. }
  2034. //UICollectionView被选中时调用的方法
  2035. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  2036. {
  2037. NSMutableDictionary* items =[[self.category_data objectForKey:@"items"] mutableCopy];
  2038. NSMutableDictionary* item = [[items objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  2039. if(self.isSelectionMode)
  2040. {
  2041. bool checked =[[item valueForKey:@"checked"] boolValue];
  2042. CategoryCellNPD *currentSelecteCell = (CategoryCellNPD *)[self.collectionview cellForItemAtIndexPath:indexPath];
  2043. if(checked)
  2044. {
  2045. self.select_count--;
  2046. if(self.select_count==0)
  2047. {
  2048. // self.addWishBtn.enabled = false;
  2049. // self.addCartBtn.enabled = false;
  2050. // self.addPortfolioBtn.enabled=false;
  2051. }
  2052. [item setValue:@"false" forKey:@"checked"];
  2053. currentSelecteCell.checkImageView.hidden = true;
  2054. if(self.categoryViewType != CATEGORY_VIEWTYPE_LIST)
  2055. [currentSelecteCell setBackgroundColor: [UIColor whiteColor]];
  2056. else
  2057. [currentSelecteCell setBackgroundColor: [UIColor whiteColor]];
  2058. }
  2059. else
  2060. {
  2061. self.select_count++;
  2062. // self.addWishBtn.enabled = true;
  2063. // self.addCartBtn.enabled = true;
  2064. // self.addPortfolioBtn.enabled=true;
  2065. [item setValue:@"true" forKey:@"checked"];
  2066. currentSelecteCell.checkImageView.hidden = false;
  2067. [currentSelecteCell setBackgroundColor: UIColorFromRGB(0x009966)];
  2068. }
  2069. [items setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  2070. [self.category_data setObject:items forKey:@"items"];
  2071. }
  2072. else
  2073. {
  2074. NSString* detail_id = [NSString stringWithFormat:@"%@",[item valueForKey:@"product_id"]];
  2075. [self showDetailat:detail_id category_id:nil name:[item objectForKey:@"fash_name"] index:indexPath.row];
  2076. }
  2077. }
  2078. #pragma mark Deferred image loading (UIScrollViewDelegate)
  2079. //// Load images for all onscreen rows when scrolling is finished
  2080. //- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  2081. //{
  2082. // if (!decelerate)
  2083. // {
  2084. // DebugLog(@"scrollViewDidEndDragging LOADMORE");
  2085. // }
  2086. //}
  2087. //
  2088. //- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  2089. //{
  2090. // DebugLog(@"scrollViewDidEndDecelerating LOADMORE");
  2091. //}
  2092. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  2093. // [refreshHeaderViewegoRefreshScrollViewDidScroll:scrollView];//下拉刷新
  2094. CGPoint contentOffsetPoint = self.collectionview.contentOffset;
  2095. CGRect frame =self.collectionview.frame;
  2096. UICollectionViewFlowLayout *flowLayout =(UICollectionViewFlowLayout*)[self.collectionview collectionViewLayout];
  2097. if(flowLayout.scrollDirection== UICollectionViewScrollDirectionVertical)
  2098. {
  2099. DebugLog(@"contentOffsetPoint %f",contentOffsetPoint.y);
  2100. DebugLog(@"frame.size.height %f",frame.size.height);
  2101. DebugLog(@"self.collectionview.contentSize.height %f",self.collectionview.contentSize.height);
  2102. if (contentOffsetPoint.y == (self.collectionview.contentSize.height - frame.size.height) /*|| self.collectionview.contentSize.height < frame.size.height*/)
  2103. {
  2104. if(self.isrefreshing)
  2105. return;
  2106. DebugLog(@"scroll to the end,load more......");
  2107. [self loadMore];
  2108. // self.btnLoad.hidden = NO;
  2109. }
  2110. }
  2111. else if(flowLayout.scrollDirection== UICollectionViewScrollDirectionHorizontal)
  2112. {
  2113. DebugLog(@"contentOffsetPoint %f",contentOffsetPoint.y);
  2114. DebugLog(@"frame.size.height %f",frame.size.height);
  2115. DebugLog(@"self.collectionview.contentSize.height %f",self.collectionview.contentSize.height);
  2116. if (contentOffsetPoint.x == (self.collectionview.contentSize.width - frame.size.width) /*|| self.collectionview.contentSize.width < frame.size.width*/)
  2117. {
  2118. if(self.isrefreshing)
  2119. return;
  2120. DebugLog(@"scroll to the end,load more......");
  2121. [self loadMore];
  2122. // self.btnLoad.hidden = NO;
  2123. }
  2124. }
  2125. }
  2126. //#pragma mark - UIAlertViewDelegate
  2127. //// Called when a button is clicked. The view will be automatically dismissed after this call returns
  2128. //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  2129. //{
  2130. // UIApplication * app = [UIApplication sharedApplication];
  2131. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2132. // // if(alertView.tag==1234)
  2133. // // {
  2134. // //
  2135. // // // availability warring;
  2136. // //
  2137. // //
  2138. // // if(buttonIndex!=alertView.cancelButtonIndex)
  2139. // // {
  2140. // // if(appDelegate.order_code==nil)
  2141. // // {
  2142. // // [self neworder];
  2143. // // }
  2144. // // else
  2145. // // {
  2146. // // [self addtocart];
  2147. // // }
  2148. // // }
  2149. // // return;
  2150. // // }
  2151. // // int count=[self.quantity_text.text intValue];
  2152. //
  2153. // // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  2154. // if(buttonIndex==alertView.cancelButtonIndex)
  2155. // {
  2156. //
  2157. //
  2158. //
  2159. // }
  2160. // else if(buttonIndex==1)
  2161. // {
  2162. // //open exist
  2163. // self.disable_refresh = true;
  2164. // OrderListViewController* ovc =[ [UIStoryboard storyboardWithName:@"ERP_Mobile_Order" bundle:nil] instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  2165. // ovc.showNavibar = true;
  2166. // ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  2167. // if(appDelegate.order_code==nil)
  2168. // {
  2169. // [self neworder];
  2170. // }
  2171. // else
  2172. // {
  2173. // [self addtocart];
  2174. // // [main_vc checklogin:true];
  2175. //
  2176. // }
  2177. //
  2178. // };
  2179. // ovc.init_style = OL_OPEN;
  2180. // ovc.onCancel = ^(){
  2181. // self.disable_refresh = false;
  2182. // // [main_vc checklogin:true];
  2183. //
  2184. // };
  2185. //
  2186. // [self.navigationController pushViewController:ovc animated:true];
  2187. // }
  2188. // else
  2189. // {
  2190. // //create new;
  2191. // self.disable_refresh = true;
  2192. // if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  2193. // {
  2194. // ContactListViewController* cvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  2195. // cvc.showNavibar = true;
  2196. // cvc.contact_type = @"Sales_Order_Customer";
  2197. // cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  2198. //
  2199. // appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  2200. // appDelegate.customerInfo = value;
  2201. //
  2202. //
  2203. // if(appDelegate.order_code==nil)
  2204. // [self neworder];
  2205. //
  2206. // // [main_vc checklogin:true];
  2207. // // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  2208. //
  2209. // //
  2210. // // if(self.returnValue)
  2211. // // self.returnValue(value);
  2212. // };
  2213. //
  2214. // cvc.onCancel = ^(){
  2215. //
  2216. // [RAUtils message_alert:@"Can ot create order without cursomer infomation." title:@"" controller:self];
  2217. //
  2218. // self.disable_refresh = false;
  2219. //
  2220. // };
  2221. // cvc.onReset = ^(){
  2222. // // [main_vc checklogin:true];
  2223. // };
  2224. //
  2225. // [self.navigationController pushViewController:cvc animated:true];
  2226. // }
  2227. // else
  2228. // {
  2229. // [self neworder];
  2230. // }
  2231. //
  2232. // }
  2233. //}
  2234. #pragma mark - Support scanner
  2235. -(void) onDecodedData:(NSString*) value
  2236. {
  2237. // UIApplication * app = [UIApplication sharedApplication];
  2238. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2239. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  2240. //
  2241. self.searchBar.text = value;
  2242. self.keywords=self.searchBar.text;
  2243. self.offset = 0;
  2244. // [self.content_data removeAllObjects];
  2245. [self reload];
  2246. }
  2247. #pragma mark Jack
  2248. - (void)addALlItem {
  2249. self.addAll = !self.addAll;
  2250. if (self.addAll) {
  2251. [self.addAllCheckBtn setImage:[[UIImage imageNamed:@"check_1_24"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  2252. } else {
  2253. [self.addAllCheckBtn setImage:[[UIImage imageNamed:@"check_0_24"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  2254. }
  2255. }
  2256. - (IBAction)addAllCheckBtnClick:(id)sender {
  2257. [self addALlItem];
  2258. }
  2259. - (IBAction)addAllClick:(id)sender {
  2260. [self addALlItem];
  2261. }
  2262. - (void)reRefreshView {
  2263. [self.collectionview reloadData];
  2264. }
  2265. #pragma mark - RA_NOTIFICAITON
  2266. -(void) clear_data
  2267. {
  2268. self.category_data = nil;
  2269. self.offset = 0;
  2270. self.keywords = nil;
  2271. self.searchBar.text = nil;
  2272. [self.collectionview reloadData];
  2273. }
  2274. -(void) refresh_ui
  2275. {
  2276. [self.collectionview reloadData];
  2277. }
  2278. -(void) reload_data
  2279. {
  2280. if(self.disable_refresh)
  2281. return;
  2282. self.offset = 0;
  2283. [self reload];
  2284. }
  2285. @end