SearchViewController.m 82 KB

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