CategoryViewController.m 92 KB

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