CategoryViewController.m 86 KB

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