ItemSearchViewController.m 85 KB

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