ItemSearchViewController.m 80 KB

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