SearchViewController.m 83 KB

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