CategoryViewController.m 90 KB

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