DetailViewController.m 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  1. //
  2. // DetailViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 7/25/15.
  6. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "DetailViewController.h"
  9. #import "DetailHeaderCell.h"
  10. #import "DetailImageCell.h"
  11. #import "iSalesNetwork.h"
  12. #import "RTLabel.h"
  13. #import "DetailKVCell.h"
  14. #import "LineView.h"
  15. //#import "SelectorViewController.h"
  16. #import "RAUtils.h"
  17. #import "MainViewController.h"
  18. #import "QRCodeGenerator.h"
  19. #import "DefaultTableHeaderView.h"
  20. #import "DefaultAppearance.h"
  21. #import "JKMessageBoxController.h"
  22. #define DEF_CELL_HEIGHT 44
  23. #define DEF_TABLE_HEIGHT 44
  24. #define LINE_WIDTH 0
  25. #define CELL_MARGIN 0
  26. #define LABEL_MARGIN 10
  27. #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
  28. #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
  29. #define NavColor ([UIColor colorWithRed:237/255.0 green:20/255.0f blue:91/255.0f alpha:1.0f])
  30. @interface DetailViewController ()
  31. @property (nonatomic,strong) UIBezierPath *path;
  32. @end
  33. @implementation DetailViewController
  34. {
  35. CALayer *_layer;
  36. }
  37. -(void) reload_container_getdata:(bool) update_data
  38. {
  39. [super reload_container_getdata:update_data];
  40. if(update_data)
  41. [self reload];
  42. else
  43. {
  44. [self.detailTable reloadData];
  45. // [self refresh_total];
  46. }
  47. }
  48. -(void)manually_refresh
  49. {
  50. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:300];
  51. if(self.isrefreshing)
  52. {
  53. [reF endRefreshing];
  54. return;
  55. }
  56. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  57. if ([self respondsToSelector:@selector(ReloadData)])
  58. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  59. // DebugLog(@"refresh!!!!!!!!");
  60. }
  61. -(void) ReloadData
  62. {
  63. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:300];
  64. [reF endRefreshing];
  65. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  66. [self reload];
  67. }
  68. -(void) reload
  69. {
  70. dispatch_async(dispatch_get_main_queue(), ^{
  71. if(self.isrefreshing)
  72. return;
  73. self.label_net_err.hidden=true;
  74. self.detailTable.hidden = true;
  75. self.isrefreshing=true;
  76. self.mum.center = self.view.center;
  77. self.mum.hidden = false;
  78. [self.mum startAnimating];
  79. //self.detailTable.hidden=true;
  80. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  81. NSDictionary* detail_json = [iSalesNetwork request_ItemDetail:self.product_id model_name:self.model_name category_id:self.category_id use_name:self.use_model_name];
  82. dispatch_async(dispatch_get_main_queue(), ^{
  83. [self.mum stopAnimating];
  84. self.detail_data = [detail_json mutableCopy];
  85. self.product_id = [NSString stringWithFormat:@"%d",[[detail_json valueForKey:@"product_id"] intValue] ];
  86. NSDictionary* imgsection = [detail_json objectForKey:@"img_section"];
  87. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  88. if(/*appDelegate.user_type==USER_ROLE_CUSTOMER*/true)
  89. self.quantity =[[imgsection valueForKey:@"stockUom"] intValue];
  90. else
  91. self.quantity =1;
  92. if(self.quantity==0)
  93. self.quantity=1;
  94. self.step=self.quantity;
  95. [self.detailTable reloadData];
  96. if([[detail_json valueForKey:@"result"] intValue]==2)
  97. {
  98. self.detailTable.hidden = false;
  99. NSString* model_name = self.detail_data[@"img_section"][@"model_name"];
  100. NSString* img_url = self.detail_data[@"img_section"][@"model_s_img"];
  101. NSMutableDictionary* item = [[NSMutableDictionary alloc]init];
  102. [item setValue:self.product_id forKey:@"product_id"];
  103. [item setValue:self.category_id forKey:@"category"];
  104. [item setValue:model_name forKey:@"fash_name"];
  105. [item setValue:img_url forKey:@"picture_path"];
  106. [RAUtils add_recent_model:item];
  107. NSUInteger ii[2] = {0,0};
  108. [self.detailTable scrollToRowAtIndexPath:[NSIndexPath indexPathWithIndexes:ii length:2] atScrollPosition:UITableViewScrollPositionTop animated:YES];
  109. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  110. // int count=[appDelegate.recent_model[@"count"] intValue];
  111. // bool bexist = false;
  112. // for(int i=0;i<count;i++)
  113. // {
  114. // NSMutableDictionary * mitem= appDelegate.recent_model[[NSString stringWithFormat:@"item_%d",i]];
  115. // if([mitem[@"product_id"] isEqualToString:self.product_id])
  116. // {
  117. // bexist=true;
  118. // break;
  119. // }
  120. // }
  121. //
  122. // if(!bexist)
  123. // {
  124. // [appDelegate.recent_model setObject:item forKey:[NSString stringWithFormat:@"item_%d",count]];
  125. // appDelegate.recent_model[@"count"]=[NSString stringWithFormat:@"%d",count+1];
  126. // }
  127. }
  128. else
  129. if([[detail_json valueForKey:@"result"] intValue]==RESULT_NET_ERROR)
  130. {
  131. self.detailTable.hidden = true;
  132. self.label_net_err.hidden=false;
  133. }
  134. else if([[detail_json valueForKey:@"result"] intValue]==RESULT_NO_MODEL)
  135. {
  136. self.detailTable.hidden = true;
  137. [RAUtils message_alert:[detail_json valueForKey:@"err_msg"] title:@"Model Detail"/*@"Loading Model Detail Failed."*/ controller:self] ;
  138. if(self.ispush)
  139. [ self.navigationController popViewControllerAnimated:true];
  140. }
  141. else
  142. {
  143. [RAUtils message_alert:[detail_json valueForKey:@"err_msg"] title:@"Model Detail"/*@"Loading Model Detail Failed."*/ controller:self] ;
  144. }
  145. self.isrefreshing=false;
  146. });
  147. });
  148. });
  149. }
  150. - (void)onBackClick:(UIButton *)sender {
  151. if(self.OnBack)
  152. {
  153. self.OnBack(self.add_cart,self.add_wish,self.update_data);
  154. }
  155. [self.navigationController popViewControllerAnimated:(false)];
  156. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  157. // ;
  158. // }];
  159. }
  160. - (void)viewDidLoad {
  161. [super viewDidLoad];
  162. [self registeObserver];
  163. self.refresh_type = REFRESH_VIEW;
  164. UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  165. style:UIBarButtonItemStylePlain
  166. target:self
  167. action:@selector( onBackClick:)];
  168. self.navigationItem.leftBarButtonItem = backButton;
  169. // self.detailTable.style = UITableViewStylePlain;
  170. self.edgesForExtendedLayout = UIRectEdgeNone;
  171. if(self.ispush)
  172. {
  173. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.000001)];
  174. [line setBackgroundColor:[UIColor clearColor]];
  175. self.detailTable.tableHeaderView = line;
  176. }
  177. //
  178. //
  179. // self.automaticallyAdjustsScrollViewInsets = false;
  180. self.step=1;
  181. self.quantity=1;
  182. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  183. ref.tag = 300 ;
  184. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  185. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  186. // ref.hidden = true;
  187. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  188. [self.detailTable addSubview:ref];
  189. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  190. self.label_net_err.layer.borderWidth = 2.0;
  191. self.label_net_err.layer.cornerRadius=15;
  192. self.label_net_err.layer.masksToBounds=true;
  193. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  194. // tap.minimumPressDuration = 0.8; //定义按的时间
  195. [self.label_net_err addGestureRecognizer:tap];
  196. // Do any additional setup after loading the view.
  197. }
  198. - (void)viewWillAppear:(BOOL)animated
  199. {
  200. if(self.ispush)
  201. {
  202. [super viewWillAppear:animated];
  203. [[self navigationController] setNavigationBarHidden:NO animated:NO];
  204. }
  205. }
  206. - (void) viewWillDisappear:(BOOL)animated
  207. {
  208. [super viewWillDisappear:animated];
  209. if(self.ispush)
  210. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  211. }
  212. - (void)didReceiveMemoryWarning {
  213. [super didReceiveMemoryWarning];
  214. // Dispose of any resources that can be recreated.
  215. }
  216. /*
  217. #pragma mark - Navigation
  218. // In a storyboard-based application, you will often want to do a little preparation before navigation
  219. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  220. // Get the new view controller using [segue destinationViewController].
  221. // Pass the selected object to the new view controller.
  222. }
  223. */
  224. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  225. //将page2设定成Storyboard Segue的目标UIViewController
  226. id page2 = segue.destinationViewController;
  227. //将值透过Storyboard Segue带给页面2的string变数
  228. [page2 setValue: _selector forKey:@"selector"];
  229. [page2 setValue: self forKey:@"selectordelegate"];
  230. }
  231. //- (IBAction)OnAddPortfolio:(id)sender {
  232. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  233. ////
  234. //// [appDelegate test_onDecodedDataResult:@"371252"];
  235. //// [self onDecodedData:@"3712524-235"];
  236. // return;
  237. //}
  238. -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  239. {
  240. // [self.detailTable reloadData];
  241. // return;
  242. [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  243. // NSMutableIndexSet *sectionToReload = [[NSMutableIndexSet alloc] init];
  244. // int section_count =[[self.detail_data valueForKey:@"detail_section_count"] intValue];
  245. //
  246. // for(int i=0;i<section_count;i++)
  247. // {
  248. // NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)i]];
  249. //
  250. //
  251. //
  252. // NSString* type=[section_json valueForKey:@"type"] ;
  253. //
  254. // if([type isEqualToString:@"kv"])
  255. // {
  256. //
  257. // [sectionToReload addIndex:i+1];
  258. // }
  259. // }
  260. //
  261. //
  262. // [self.detailTable reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationNone];
  263. //
  264. [self.detailTable reloadData];
  265. }
  266. #pragma mark - Selector delegate
  267. -(void) selected:(NSString*) detail_id category:(NSString*) category
  268. {
  269. self.product_id = detail_id;
  270. self.use_model_name = false;
  271. [self reload];
  272. DebugLog(@"color selected");
  273. }
  274. #pragma mark - Header cell delegate
  275. -(void) SelectorClicked:(UIView*) trigger
  276. {
  277. // int selcount = [[self.selector valueForKey:@"count"] intValue];
  278. int selector_count = [[self.selector valueForKey:@"count"] intValue];
  279. int current_sel = -1;
  280. for(int i=0;i<selector_count;i++)
  281. {
  282. NSDictionary* params_json=[[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] objectForKey:@"params"] ;
  283. // NSString* aaa=[[[params_json objectForKey:@"param_0"] valueForKey:@"val"] stringValue];
  284. if([[[[params_json objectForKey:@"param_0"] valueForKey:@"val"] stringValue] isEqualToString:self.product_id])
  285. {
  286. current_sel = i;
  287. }
  288. }
  289. // NSArray * arr = [[NSArray alloc] init];
  290. // arr = [NSArray arrayWithObjects:@"Hello 0", @"Hello 1", @"Hello 2", @"Hello 3", @"Hello 4", @"Hello 5", @"Hello 6", @"Hello 7", @"Hello 8", @"Hello 9",nil];
  291. // NSArray * arrImage = [[NSArray alloc] init];
  292. // arrImage = [NSArray arrayWithObjects:[UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], [UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], [UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], [UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], [UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], nil];
  293. if(self.dropDown == nil)
  294. {
  295. CGFloat height = 70*selector_count;
  296. self.dropDown =[[NIDropDown alloc] showDropDown:self.detailTable based:(id)trigger height:height data:self.selector direction:@"down" current_sel:current_sel];
  297. //Capturing strongly warring;
  298. __block DetailViewController *brself= self;
  299. self.dropDown.selectChanged =^(long index){
  300. NSDictionary* item = [brself.selector objectForKey:[NSString stringWithFormat:@"item_%ld",index]];
  301. NSDictionary* params = [item objectForKey:@"params"];
  302. NSString* detail_id=[[[params objectForKey:@"param_0"] valueForKey:@"val" ] stringValue];
  303. brself.product_id = detail_id;
  304. brself.use_model_name = false;
  305. [brself reload];
  306. DebugLog(@"color selected");
  307. brself.dropDown = nil;
  308. };
  309. //[[NIDropDown alloc]showDropDown:(id)trigger :&f :arr :arrImage :@"down"];
  310. // self.dropDown.delegate = self;
  311. }
  312. else {
  313. [self.dropDown hideDropDown:(id)trigger];
  314. self.dropDown= nil;
  315. // [self rel];
  316. }
  317. return;
  318. /*
  319. // CGRect rect = trigger.frame;
  320. //
  321. // DebugLog(@"%@",NSStringFromCGRect(rect));
  322. //
  323. //
  324. //
  325. // DebugLog(@"%@",NSStringFromCGPoint (trigger.center));
  326. //
  327. //
  328. // CGRect new=[trigger convertRect:rect toView:self.view];
  329. //
  330. // DebugLog(@"convert %@",NSStringFromCGRect(new));
  331. //
  332. CGRect cellrect_screen = [RAUtils relativeFrameForScreenWithView:trigger];
  333. CGRect rect1=[self.view convertRect:cellrect_screen fromView:[[[UIApplication sharedApplication] delegate] window]];
  334. DebugLog(@"convert1 %@",NSStringFromCGRect(rect1));
  335. SelectorViewController *menu = [ self.storyboard instantiateViewControllerWithIdentifier:@"SelectorViewController"];
  336. NSString * name = [self.selector valueForKey:@"name"];
  337. menu.title=name;
  338. menu.selector = self.selector;
  339. menu.selectordelegate = self;
  340. // 1.创建一个UIPopover
  341. UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:[[UINavigationController alloc] initWithRootViewController:menu]];
  342. // 2.设置尺寸
  343. // popover.popoverContentSize = CGSizeMake(320, 44 * 5);
  344. // 3.从哪里显示出来 --> 指向item
  345. [popover presentPopoverFromRect:rect1 inView:self.view permittedArrowDirections:0 animated:YES];
  346. // [popover presentPopoverFromBarButtonItem:item permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  347. self.popover = popover;
  348. // [self performSegueWithIdentifier:@"selector_popover" sender:self];
  349. */
  350. }
  351. -(void) stepClicked:(int) value
  352. {
  353. self.quantity = value;
  354. }
  355. #pragma mark - Topic cell delegate
  356. -(void) TopicItemClicked:(NSString*) product_id category:(NSString*) category
  357. {
  358. // DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  359. // // dvc
  360. //
  361. //
  362. // dvc.OnBack=^(bool add_cart,bool add_wish,bool update_data)
  363. // {
  364. //
  365. //
  366. // [self reload_container_getdata:false]
  367. // };
  368. //
  369. //
  370. // dvc.product_id=product_id;
  371. // dvc.category_id=category;
  372. // dvc.ispush=true;
  373. // [dvc reload];
  374. // [self.navigationController pushViewController:dvc animated:true];
  375. // self.model_name = value;
  376. self.product_id = product_id;
  377. self.category_id=category;
  378. [self reload];
  379. DebugLog(@"color selected");
  380. }
  381. #pragma mark - Table view data source
  382. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  383. //{
  384. // return 0;
  385. //}
  386. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  387. //{
  388. // return 0;
  389. //}
  390. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  391. //{
  392. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  393. // return myView;
  394. //
  395. //}
  396. //- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  397. //{
  398. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  399. //// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  400. //// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
  401. //// titleLabel.textColor=[UIColor whiteColor];
  402. //// titleLabel.backgroundColor = [UIColor clearColor];
  403. //// if(section==0)
  404. //// titleLabel.text=NSLocalizedString(@"display_items", nil);
  405. //// else
  406. //// titleLabel.text=NSLocalizedString(@"hide_items", nil);
  407. //// [titleLabel sizeToFit];
  408. //// [myView addSubview:titleLabel];
  409. ////
  410. // return myView;
  411. //}
  412. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  413. if(section==0)
  414. return nil;
  415. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  416. if(related_json==nil)
  417. {
  418. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)(section-1)]];
  419. return [section_json valueForKey:@"title"] ;
  420. }
  421. else
  422. {
  423. if(section==1)
  424. return @"related model";
  425. else
  426. {
  427. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)section-2]];
  428. return [section_json valueForKey:@"title"] ;
  429. }
  430. }
  431. }
  432. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  433. //{
  434. // return 33;
  435. //}
  436. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  437. {
  438. NSString* value =[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  439. if(value==nil)
  440. value=@"";
  441. unsigned long color = strtoul([value UTF8String],0,16);
  442. NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
  443. if (sectionTitle == nil) {
  444. return nil;
  445. }
  446. DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  447. // myView.backgroundColor = UIColorFromRGB(0x996633);
  448. myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath;
  449. myView.layer.shadowColor = [UIColor blackColor].CGColor;
  450. myView.layer.shadowOffset = CGSizeMake(0, 0);
  451. myView.layer.shadowOpacity = 0.5;
  452. myView.layer.shadowRadius = 2.0;
  453. UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5.5, 90, 22)];
  454. titlelabel.textColor=UIColorFromRGB(color);
  455. // titlelabel.backgroundColor = [UIColor clearColor];
  456. titlelabel.text=sectionTitle;
  457. [titlelabel sizeToFit];
  458. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)(section-1)]];
  459. bool refresh= [[section_json valueForKey:@"partial_refresh"] boolValue] ;
  460. if(refresh)
  461. {
  462. NSString* btntitle=@"Refresh" ;
  463. // NSString* value =[DefaultAppearance get_noneappearance_value:@"DetailTableHeaderView" valuename:@"refresh_button_color"];
  464. //
  465. // if(value==nil)
  466. // value=@"";
  467. // unsigned long color = strtoul([value UTF8String],0,16);
  468. UIButton * btn =[[UIButton alloc] initWithFrame:CGRectMake(tableView.bounds.size.width-100-15, 0, 100, 33)];
  469. // [btn setTitleColor:UIColorFromRGB(color) forState:UIControlStateNormal];
  470. btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
  471. btn.tag=section;
  472. [btn addTarget:self action:@selector(RefreshSection:) forControlEvents:UIControlEventTouchUpInside];
  473. [btn setTitle:btntitle forState:UIControlStateNormal];
  474. [myView addSubview:btn];
  475. }
  476. [myView addSubview:titlelabel];
  477. return myView;
  478. }
  479. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  480. {
  481. int section_count =[[self.detail_data valueForKey:@"detail_section_count"] intValue];
  482. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  483. if(related_json==nil)
  484. return section_count+1;
  485. else
  486. return section_count+2;
  487. }
  488. //- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  489. //{
  490. // DebugLog(@"canMoveRowAtIndexPath");
  491. //
  492. // if([tableView numberOfRowsInSection:indexPath.section]>1)
  493. // return true;
  494. // else
  495. // return false;
  496. //}
  497. //- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
  498. //{
  499. // NSMutableArray * from;
  500. // NSMutableArray * to ;
  501. // if(fromIndexPath.section==0)
  502. // from = self.displayfields;
  503. // else
  504. // from = self.hidefields;
  505. // if(toIndexPath.section==0)
  506. // to = self.displayfields;
  507. // else
  508. // to = self.hidefields;
  509. // id content=from[fromIndexPath.row];
  510. // [from removeObjectAtIndex:fromIndexPath.row];
  511. // [to insertObject:content atIndex:toIndexPath.row];
  512. //
  513. // [tableView reloadData];
  514. //
  515. //}
  516. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  517. {
  518. if(section==0)
  519. return 1;
  520. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  521. if(related_json==nil)
  522. {
  523. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)section-1]];
  524. NSString* type=[section_json valueForKey:@"type"] ;
  525. if([type isEqualToString:@"content"])
  526. return 1;
  527. return [[section_json valueForKey:@"count"] intValue];
  528. }
  529. else
  530. {
  531. if(section==1)
  532. return 1;
  533. else
  534. {
  535. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)section-2]];
  536. NSString* type=[section_json valueForKey:@"type"] ;
  537. if([type isEqualToString:@"content"])
  538. return 1;
  539. return [[section_json valueForKey:@"count"] intValue];
  540. }
  541. }
  542. }
  543. //- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  544. //{
  545. // return UITableViewCellEditingStyleNone;
  546. //}
  547. //- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  548. //{
  549. // //如果两次点击的时间间隔小于1秒,则断定为双击事件
  550. //
  551. // if(tableView.editing==false)
  552. // return;
  553. //
  554. //
  555. // if([tableView numberOfRowsInSection:indexPath.section]<=1)
  556. // return;
  557. // // int between = 1;
  558. // NSUInteger curr = [[NSDate date] timeIntervalSince1970];
  559. //
  560. // if(indexPath.row==self.lasttap.row&&indexPath.section==self.lasttap.section)
  561. // {
  562. // if (curr-self.taptime<1) {
  563. // DebugLog(@"double click!");
  564. // self.lasttap =nil;
  565. //
  566. // NSMutableArray * from;
  567. // NSMutableArray * to ;
  568. // int section;
  569. // if(indexPath.section==0)
  570. // {
  571. // from = self.displayfields;
  572. // to = self.hidefields;
  573. // section = 1;
  574. // }
  575. // else
  576. // {
  577. //
  578. // from = self.hidefields;
  579. // to = self.displayfields;
  580. // section = 0;
  581. // }
  582. // NSIndexPath * toIndexPath = [NSIndexPath indexPathForRow:0 inSection:section];
  583. // // NSIndexPath * headerIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
  584. // // NSIndexPath * headerIndexPath1 = toIndexPath;
  585. //
  586. // id content=from[indexPath.row];
  587. // [tableView beginUpdates];
  588. // [from removeObjectAtIndex:indexPath.row];
  589. // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  590. //
  591. // [to insertObject:content atIndex:0];
  592. // [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:toIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  593. // [tableView endUpdates];
  594. //
  595. // // [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:headerIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  596. // // [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:headerIndexPath1] withRowAnimation:UITableViewRowAnimationFade];
  597. // [tableView reloadData];
  598. //
  599. //
  600. // }
  601. // self.taptime = curr;
  602. //
  603. //
  604. // }
  605. // else
  606. // {
  607. // self.taptime = curr;
  608. // self.lasttap = indexPath;
  609. // }
  610. //}
  611. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  612. {
  613. if(section ==0)
  614. return 0;
  615. else
  616. return 33;
  617. }
  618. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  619. {
  620. if(indexPath.section ==0)
  621. return 550;
  622. NSDictionary* nsdic = self.detail_data;
  623. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  624. if(related_json==nil)
  625. {
  626. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)indexPath.section-1]];
  627. NSDictionary* item_json =[section_json objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row ]];
  628. NSString* type=[section_json valueForKey:@"type"] ;
  629. if([type isEqualToString:@"kv"])
  630. {
  631. float width = tableView.frame.size.width;
  632. width-=CELL_MARGIN*2;
  633. CGSize constraintkey = CGSizeMake(width*0.4-2*LABEL_MARGIN, 20000.0f);//key label width is 40% cell width;
  634. CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
  635. // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
  636. NSString* key =[item_json valueForKey:@"key"] ;
  637. // [cell.keyLabel sizeToFit];
  638. NSString* val=[item_json valueForKey:@"val"] ;
  639. if([val isEqual:[NSNull null]])
  640. val=@"";
  641. if(val==nil)
  642. val=@"";
  643. if([val isEqualToString:@"null"])
  644. val=@"";
  645. CGRect frame;
  646. frame.size = constraintval;
  647. frame.origin.x=0;
  648. frame.origin.y=0;
  649. RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
  650. [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  651. // rtlabel.lineSpacing = 20.0;
  652. [rtlabel setText: val];
  653. CGSize sizeval=rtlabel.optimumSize;
  654. CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  655. // CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping];
  656. float height = MAX(sizekey.height,sizeval.height);
  657. height = MAX(height+LINE_WIDTH, DEF_CELL_HEIGHT);
  658. return height;
  659. }
  660. else if([type isEqualToString:@"img"])
  661. {
  662. int img_count = [[item_json valueForKey:@"count"] intValue];
  663. double ret = 0;
  664. for(int j=0;j<img_count;j++)
  665. {
  666. NSDictionary* img_json =[item_json objectForKey:[NSString stringWithFormat:@"image_%d",j ]];
  667. float width = [[img_json valueForKey:@"width"]floatValue];
  668. float height = [[img_json valueForKey:@"height"]floatValue];
  669. ret+=height*tableView.bounds.size.width / width;
  670. }
  671. return ret;
  672. }
  673. else if([type isEqualToString:@"content"])
  674. return 270;
  675. return 44;
  676. }
  677. else
  678. {
  679. if(indexPath.section==1)
  680. return 270;
  681. else
  682. {
  683. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)indexPath.section-2]];
  684. NSDictionary* item_json =[section_json objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row ]];
  685. NSString* type=[section_json valueForKey:@"type"] ;
  686. if([type isEqualToString:@"kv"])
  687. {
  688. float width = tableView.frame.size.width;
  689. width-=CELL_MARGIN*2;
  690. CGSize constraintkey = CGSizeMake(width*0.4-2*LABEL_MARGIN, 20000.0f);//key label width is 40% cell width;
  691. CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
  692. // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
  693. NSString* key =[item_json valueForKey:@"key"] ;
  694. // [cell.keyLabel sizeToFit];
  695. NSString* val=[item_json valueForKey:@"val"] ;
  696. if([val isEqual:[NSNull null]])
  697. val=@"";
  698. if(val==nil)
  699. val=@"";
  700. if([val isEqualToString:@"null"])
  701. val=@"";
  702. CGRect frame;
  703. frame.size = constraintval;
  704. frame.origin.x=0;
  705. frame.origin.y=0;
  706. RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
  707. [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  708. // rtlabel.lineSpacing = 20.0;
  709. [rtlabel setText: val];
  710. CGSize sizeval=rtlabel.optimumSize;
  711. CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  712. // CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping];
  713. float height = MAX(sizekey.height,sizeval.height);
  714. height = MAX(height+LINE_WIDTH, DEF_CELL_HEIGHT);
  715. return height;
  716. }
  717. else if([type isEqualToString:@"img"])
  718. {
  719. int img_count = [[item_json valueForKey:@"count"] intValue];
  720. double ret = 0;
  721. for(int j=0;j<img_count;j++)
  722. {
  723. NSDictionary* img_json =[item_json objectForKey:[NSString stringWithFormat:@"image_%d",j ]];
  724. float width = [[img_json valueForKey:@"width"]floatValue];
  725. float height = [[img_json valueForKey:@"height"]floatValue];
  726. ret+=height*tableView.bounds.size.width / width;
  727. }
  728. return ret;
  729. }
  730. return 44;
  731. }
  732. }
  733. }
  734. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  735. {
  736. // DebugLog(@"cellForRowAtIndexPath");
  737. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  738. if(indexPath.section==0)
  739. {
  740. NSString *CellIdentifier = @"DetailHeaderCell";
  741. DetailHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  742. NSMutableDictionary* section_json = [[self.detail_data objectForKey:@"img_section"] mutableCopy];
  743. if (appDelegate.user && appDelegate.user_type == USER_ROLE_CUSTOMER) {
  744. cell.btnaddPortfolio.hidden = YES;
  745. // CGRect wishListFrame = cell.btnaddWish.frame;
  746. // CGRect cartFrame = cell.btnaddCart.frame;
  747. // cartFrame.origin.x = wishListFrame.origin.x;
  748. // cell.btnaddCart.frame = cartFrame;
  749. __weak typeof(self) weakself = self;
  750. cell.notifyMeBlock = ^{
  751. NSString *emailAddr = [appDelegate.customerInfo valueForKey:@"customer_email"];
  752. if (!emailAddr.length) {
  753. [self showEmailAddrBox];
  754. } else {
  755. UIAlertView *waitting_alert = [RAUtils waiting_alert:@"Sending Email" title:@"Waitting"];
  756. dispatch_async(dispatch_get_global_queue(0,0), ^{
  757. NSDictionary *dic = [iSalesNetwork notifyModel:self.product_id emailAddr:nil];
  758. dispatch_async(dispatch_get_main_queue(), ^{
  759. [waitting_alert dismissWithClickedButtonIndex:0 animated:YES];
  760. if ([[dic valueForKey:@"result"] integerValue] != RESULT_TRUE) {
  761. UIAlertController *errorAlertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"The email send to %@ failed",emailAddr] preferredStyle:UIAlertControllerStyleAlert];
  762. UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  763. }];
  764. [errorAlertVC addAction:action];
  765. [weakself presentViewController:errorAlertVC animated:YES completion:nil];
  766. } else {
  767. }
  768. });
  769. });
  770. }
  771. };
  772. cell.btnNotifyMe.layer.borderColor = [UIColor blackColor].CGColor;
  773. cell.btnNotifyMe.layer.borderWidth = 1.0f;
  774. cell.btnNotifyMe.layer.cornerRadius = 3.0f;
  775. cell.btnNotifyMe.layer.masksToBounds = YES;
  776. NSString *qtyStr = [section_json valueForKey:@"Availability"];
  777. if ([qtyStr isEqualToString:@"In Production"] || [qtyStr isEqualToString:@"0"] ) {
  778. cell.btnaddCart.enabled = NO;
  779. }
  780. } else {
  781. cell.btnNotifyMe.hidden = YES;
  782. }
  783. self.anchor_button.frame=cell.selector_Button.frame;
  784. cell.celldelegate=self;
  785. cell.quantity_text.text = [NSString stringWithFormat:@"%d",self.quantity];
  786. cell.step = self.step;
  787. cell.quantity = self.quantity;
  788. // cell.stepper.stepValue = self.step;
  789. // cell.stepper.value = self.quantity;
  790. NSString* price = [section_json valueForKey:@"price"];
  791. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  792. if(appDelegate.contact_id ==nil)
  793. {
  794. // appDelegate.bLogin = false;
  795. // cell.btnaddCart.enabled = false;
  796. }
  797. else
  798. {
  799. if ([price isEqualToString:@"No Price."]) {
  800. // cell.btnaddCart.enabled = false;
  801. }
  802. else
  803. {
  804. // cell.btnaddCart.enabled = true;
  805. }
  806. }
  807. int cqty = [[section_json valueForKey:@"Model QTY in cart"] intValue];
  808. cell.cqty = cqty;
  809. cell.set_cqty=^(int cqty)
  810. {
  811. [section_json setValue:[NSString stringWithFormat:@"%d",cqty] forKey:@"Model QTY in cart"];
  812. [self.detail_data setObject:section_json forKey:@"img_section"];
  813. [self.detailTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  814. };
  815. cell.set_update_data=^(bool bupdate)
  816. {
  817. //self.update_data = bupdate;
  818. if(bupdate)
  819. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  820. else
  821. [((MainViewController*)appDelegate.main_vc) reloadCategory:false immediately:false];
  822. };
  823. if(cqty>0)
  824. {
  825. cell.cqyt_label.hidden=false;
  826. cell.cqyt_label.text=[NSString stringWithFormat:@"Model QTY in Cart:%d",cqty];
  827. }
  828. else
  829. {
  830. cell.cqyt_label.hidden=true;
  831. cell.cqyt_label.text=nil;
  832. }
  833. cell.descriptionRTLabel.text =[NSString stringWithFormat:@"%@\n\n",[section_json valueForKey:@"model_descrition"]];
  834. // cell.descriptionLabel.text =[NSString stringWithFormat:@"%@\n\n",[section_json valueForKey:@"model_descrition"]];
  835. cell.model_label.text =[section_json valueForKey:@"model_name"];
  836. // UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:80];
  837. // cell.qrcode_imageview.image = qrimg;
  838. if(appDelegate.bLogin==true)
  839. {
  840. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  841. {
  842. }
  843. else
  844. {
  845. price=nil;
  846. }
  847. }
  848. cell.priceLabel.text =price;
  849. // cell.shipLabel.text = [section_json valueForKey:@"ship"];
  850. cell.count_Label.text =[section_json valueForKey:@"Availability"] ;
  851. NSString* eta =[section_json valueForKey:@"ETA"] ;
  852. if(eta.length>0)
  853. {
  854. cell.eta_label.hidden = false;
  855. cell.etaval_label.text = eta;
  856. cell.etaval_label.hidden = false;
  857. cell.incoming_stock_label.hidden=false;
  858. cell.incomingcount_Label.text =[section_json valueForKey:@"incoming_stock"] ;
  859. cell.incomingcount_Label.hidden = false;
  860. }
  861. else
  862. {
  863. cell.eta_label.hidden = true;
  864. cell.etaval_label.text = nil;
  865. cell.etaval_label.hidden = true;
  866. cell.incoming_stock_label.hidden = true;
  867. cell.incomingcount_Label.text = nil;
  868. cell.incomingcount_Label.hidden = true;
  869. }
  870. self.selector = [section_json objectForKey:@"selector"];
  871. if(self.selector!=nil)
  872. {
  873. [cell Hide_selector:false];
  874. cell.selector_nameLabel.text =[self.selector valueForKey:@"name"];
  875. int selector_count = [[self.selector valueForKey:@"count"] intValue];
  876. cell.selector_imageView.image = [UIImage imageNamed:@"loading_s"];
  877. for(int i=0;i<selector_count;i++)
  878. {
  879. NSDictionary* params_json=[[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] objectForKey:@"params"] ;
  880. // NSString* aaa=[[[params_json objectForKey:@"param_0"] valueForKey:@"val"] stringValue];
  881. if([[[[params_json objectForKey:@"param_0"] valueForKey:@"val"] stringValue] isEqualToString:self.product_id])
  882. {
  883. cell.selector_valLabel.text=[[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] valueForKey:@"title"];
  884. NSString* selectstr=[NSString stringWithFormat:@"%@: %@",[self.selector valueForKey:@"name"],[[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] valueForKey:@"title"]];
  885. if([[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] valueForKey:@"title"] ==nil)
  886. selectstr=[NSString stringWithFormat:@"%@:",[self.selector valueForKey:@"name"]];
  887. cell.selector_label.text = selectstr;
  888. NSString* img_url = [[self.selector objectForKey:[NSString stringWithFormat:@"item_%d",i]] valueForKey:@"pic_url"];
  889. NSString* file_name=[img_url lastPathComponent];
  890. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  891. if(img_data!=nil)
  892. {
  893. dispatch_async(dispatch_get_main_queue(), ^{
  894. UIImage * img =[UIImage imageWithData:img_data];
  895. cell.selector_imageView.image=img;
  896. });
  897. }
  898. else
  899. {
  900. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  901. dispatch_async(dispatch_get_main_queue(), ^{
  902. if(downloadimg_data!=nil)
  903. {
  904. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  905. UIImage * img =[UIImage imageWithData:downloadimg_data];
  906. cell.selector_imageView.image=img;
  907. }
  908. else
  909. cell.selector_imageView.image = [UIImage imageNamed:@"notfound_s"];
  910. });
  911. }
  912. }
  913. }
  914. }
  915. else
  916. {
  917. [cell Hide_selector:true];
  918. }
  919. // cell.selector_valLabel.text =[[section_json objectForKey:@"selector"] valueForKey:@"name"];
  920. NSDictionary* property_json = [section_json objectForKey:@"property"];
  921. // int pcount = [[property_json valueForKey:@"count"]intValue];
  922. cell.property_nameLabel.text=[[property_json objectForKey:@"item_0"] valueForKey:@"key"];
  923. cell.property_valLabel.text=[[property_json objectForKey:@"item_0"] valueForKey:@"val"];
  924. cell.product_id = self.product_id;
  925. // cell.itemIDLabel.text = [NSString stringWithFormat:@"Item id:%d",3947105];
  926. [cell setup];
  927. [cell ClearPhotos];
  928. NSDictionary* imgs_json = [section_json objectForKey:@"images"];
  929. int count = [[imgs_json valueForKey:@"img_count"]intValue];
  930. if(count<2-1)//1 for qrcode;
  931. cell.pageControl.hidden = true;
  932. else
  933. cell.pageControl.hidden = false;
  934. // NSMutableArray* urls= [[NSMutableArray alloc]init];
  935. // cell.urls = urls;
  936. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  937. for(int i=0;i<count;i++)
  938. {
  939. NSDictionary* img_json=[imgs_json objectForKey:[NSString stringWithFormat:@"img_%d",i]];
  940. NSString* img_url = [img_json valueForKey:@"s"];
  941. // NSString* img_url_l = [img_json valueForKey:@"l"];
  942. // if(img_url_l.length>0)
  943. // [urls addObject:img_url_l];
  944. NSString* file_name=[img_url lastPathComponent];
  945. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  946. if(img_data!=nil)
  947. {
  948. dispatch_async(dispatch_get_main_queue(), ^{
  949. UIImage * img =[UIImage imageWithData:img_data];
  950. [cell AddPhoto:img];
  951. if(i==count-1)
  952. {
  953. UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:350];
  954. if(qrimg!=nil)
  955. [cell AddPhoto:qrimg];
  956. }
  957. // if(i==0)
  958. // cell.imageView.image = img;
  959. });
  960. }
  961. else
  962. {
  963. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  964. dispatch_async(dispatch_get_main_queue(), ^{
  965. if(downloadimg_data!=nil)
  966. {
  967. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  968. UIImage * img =[UIImage imageWithData:downloadimg_data];
  969. [cell AddPhoto:img];
  970. if(i==count-1)
  971. {
  972. UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:350];
  973. if(qrimg!=nil)
  974. [cell AddPhoto:qrimg];
  975. }
  976. // if(i==0)
  977. // cell.imageView.image = img;
  978. }
  979. else
  980. {
  981. UIImage * img =[UIImage imageNamed:@"notfound_l"];
  982. [cell AddPhoto:img];
  983. if(i==count-1)
  984. {
  985. UIImage* qrimg=[QRCodeGenerator qrImageForString:[section_json valueForKey:@"model_name"] imageSize:350];
  986. if(qrimg!=nil)
  987. [cell AddPhoto:qrimg];
  988. }
  989. // if(i==0)
  990. // cell.imageView.image = img;
  991. }
  992. });
  993. }
  994. }
  995. });
  996. // cell.backgroundColor = [UIColor whiteColor];
  997. cell.shopCartBlock = ^(UIImageView *imageView){
  998. self.add_cart=true;
  999. CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
  1000. rect.origin.y = rect.origin.y - [self.detailTable contentOffset].y;
  1001. CGRect headRect = imageView.frame;
  1002. headRect.origin.y = rect.origin.y+headRect.origin.y;
  1003. [self startAnimationWithRect:headRect ImageView:imageView];
  1004. [self.navigationController popToRootViewControllerAnimated:true];
  1005. // [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  1006. [((MainViewController*)appDelegate.main_vc) switchToCart];
  1007. };
  1008. cell.WatchlistBlock = ^(UIImageView *imageView){
  1009. self.add_wish=true;
  1010. CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
  1011. rect.origin.y = rect.origin.y - [self.detailTable contentOffset].y;
  1012. CGRect headRect = imageView.frame;
  1013. headRect.origin.y = rect.origin.y+headRect.origin.y;
  1014. [self startAnimationWithRect:headRect ImageView:imageView];
  1015. };
  1016. cell.PortfolioBlock = ^(UIImageView *imageView){
  1017. self.add_portfolio=true;
  1018. CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
  1019. rect.origin.y = rect.origin.y - [self.detailTable contentOffset].y;
  1020. CGRect headRect = imageView.frame;
  1021. headRect.origin.y = rect.origin.y+headRect.origin.y;
  1022. [self startAnimationWithRect:headRect ImageView:imageView];
  1023. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1024. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  1025. };
  1026. return cell;
  1027. }
  1028. // NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  1029. // if(related_json==nil)
  1030. else
  1031. {
  1032. NSDictionary* section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)indexPath.section-1]];
  1033. NSDictionary* item_json =[section_json objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row ]];
  1034. NSString *CellIdentifier = @"DetailInfoCell";
  1035. NSString* type=[section_json valueForKey:@"type"] ;
  1036. if([type isEqualToString:@"kv"])
  1037. {
  1038. float width = tableView.frame.size.width;
  1039. width-=CELL_MARGIN*2;
  1040. CGSize constraintkey = CGSizeMake(width*0.4-2*LABEL_MARGIN, 20000.0f);//key label width is 40% cell width;
  1041. CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
  1042. // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
  1043. NSString* key =[item_json valueForKey:@"key"] ;
  1044. NSString* val=[item_json valueForKey:@"val"] ;
  1045. NSString* type=[item_json valueForKey:@"type"] ;
  1046. if([type isEqualToString:@"price"])
  1047. {
  1048. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  1049. {
  1050. }
  1051. else
  1052. {
  1053. val=nil;
  1054. }
  1055. }
  1056. if([val isEqual:[NSNull null]])
  1057. val=@"";
  1058. if(val==nil)
  1059. val=@"";
  1060. if([val isEqualToString:@"null"])
  1061. val=@"";
  1062. CGRect frame;
  1063. frame.size = constraintval;
  1064. frame.origin.x=0;
  1065. frame.origin.y=0;
  1066. RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
  1067. [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  1068. // rtlabel.lineSpacing = 20.0;
  1069. [rtlabel setText: val];
  1070. CGSize sizeval=rtlabel.optimumSize;
  1071. CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  1072. float height = MAX(sizekey.height,sizeval.height);
  1073. height = MAX(height, DEF_CELL_HEIGHT-LINE_WIDTH);
  1074. sizekey.height = height;
  1075. sizekey.width = constraintkey.width;
  1076. // sizeval.height = height;
  1077. sizeval.width =constraintval.width;
  1078. // NSString *CellIdentifier = @"detail_item_kv";
  1079. DetailKVCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  1080. if(cell==nil)
  1081. DebugLog(@"cell is nil.........................");
  1082. CGRect framekey = cell.keyLabel.frame;
  1083. framekey.origin.x=CELL_MARGIN+LABEL_MARGIN;
  1084. framekey.origin.y=LINE_WIDTH;
  1085. framekey.size = sizekey;
  1086. cell.keyLabel.frame = framekey;
  1087. cell.keyLabel.text=key;
  1088. CGRect frameval = cell.valLabel.frame;
  1089. frameval.origin.x=constraintkey.width+CELL_MARGIN+3*LABEL_MARGIN; //2 margin for key 1 margin for value self
  1090. frameval.origin.y=LINE_WIDTH;
  1091. frameval.size = sizeval;
  1092. cell.valLabel.frame = frameval;
  1093. // [cell.valLabel setTextAlignment:RTTextAlignmentCenter];
  1094. cell.valLabel.text= val;
  1095. CGPoint rcenter;
  1096. rcenter.x = width * 0.4+width * 0.6/2;
  1097. rcenter.y = height /2;
  1098. cell.valLabel.center=rcenter;
  1099. // cell.valLabel.backgroundColor =[UIColor redColor];
  1100. // cell.contentView.superview.backgroundColor = [UIColor clearColor];
  1101. // cell.backgroundColor = [UIColor whiteColor];
  1102. UIView * lineview = [[LineView alloc] initWithFrame:cell.contentView.frame];
  1103. lineview.userInteractionEnabled = NO;// 不设为NO会屏蔽cell的点击事件
  1104. lineview.backgroundColor = [UIColor clearColor];// 设为透明从而使得cell.backgroundColor有效.
  1105. lineview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  1106. [cell.contentView addSubview:lineview];// cell.contentView是个readonly属性,所以别想着替换contentView了.
  1107. cell.backgroundColor = [UIColor whiteColor];
  1108. return cell;
  1109. }
  1110. else if ([type isEqualToString:@"img"])
  1111. {
  1112. DetailImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailImageCell" forIndexPath:indexPath];
  1113. int img_count = [[item_json valueForKey:@"count"] intValue];
  1114. double posy = 0;
  1115. for(int j=0;j<img_count;j++)
  1116. {
  1117. NSDictionary* img_json =[item_json objectForKey:[NSString stringWithFormat:@"image_%d",j ]];
  1118. float width = [[img_json valueForKey:@"width"]floatValue];
  1119. float height = [[img_json valueForKey:@"height"]floatValue];
  1120. //
  1121. UIImageView * imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, posy, tableView.bounds.size.width , height*tableView.bounds.size.width / width)];
  1122. NSString* img_url = [img_json valueForKey:@"img_url"];
  1123. posy+=height*tableView.bounds.size.width / width;
  1124. NSString* file_name=[img_url lastPathComponent];
  1125. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  1126. if(img_data!=nil)
  1127. {
  1128. UIImage * img =[UIImage imageWithData:img_data];
  1129. imgview.image = img;
  1130. [cell addSubview:imgview];
  1131. }
  1132. else
  1133. {
  1134. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1135. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  1136. dispatch_async(dispatch_get_main_queue(), ^{
  1137. if(downloadimg_data!=nil)
  1138. {
  1139. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  1140. UIImage * img =[UIImage imageWithData:downloadimg_data];
  1141. imgview.image = img;
  1142. [cell addSubview:imgview];
  1143. }
  1144. else
  1145. {
  1146. imgview.image = [UIImage imageNamed:@"notfound_l"];
  1147. [cell addSubview:imgview];
  1148. }
  1149. });
  1150. });
  1151. }
  1152. }
  1153. cell.backgroundColor = [UIColor whiteColor];
  1154. return cell;
  1155. }
  1156. else if ([type isEqualToString:@"content"])
  1157. {
  1158. NSString *CellIdentifier = @"DetailTopicCell";
  1159. NSString* data=[section_json valueForKey:@"data"] ;
  1160. if([data isEqualToString:@"local"])
  1161. {
  1162. [appDelegate.recent_model addEntriesFromDictionary:section_json];
  1163. // NSDictionary* section_json = [self.detail_data objectForKey:@"related_model"];
  1164. DetailTopicCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  1165. cell.itemdelegate = self;
  1166. cell.related_data = appDelegate.recent_model;
  1167. [cell.topicCollectionView reloadData];
  1168. cell.backgroundColor = [UIColor whiteColor];
  1169. return cell;
  1170. }
  1171. else
  1172. {
  1173. // NSDictionary* section_json = [self.detail_data objectForKey:@"related_model"];
  1174. DetailTopicCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  1175. cell.itemdelegate = self;
  1176. cell.related_data = section_json;
  1177. [cell.topicCollectionView reloadData];
  1178. cell.backgroundColor = [UIColor whiteColor];
  1179. return cell;
  1180. }
  1181. }
  1182. return nil;
  1183. }
  1184. }
  1185. -(void)startAnimationWithRect:(CGRect)rect ImageView:(UIImageView *)imageView
  1186. {
  1187. if (!_layer) {
  1188. // _btn.enabled = NO;
  1189. _layer = [CALayer layer];
  1190. _layer.contents = (id)imageView.layer.contents;
  1191. _layer.contentsGravity = kCAGravityResizeAspectFill;
  1192. _layer.bounds = rect;
  1193. [_layer setCornerRadius:CGRectGetHeight([_layer bounds]) / 2];
  1194. _layer.masksToBounds = YES;
  1195. // 导航64
  1196. _layer.position = CGPointMake(imageView.center.x, CGRectGetMidY(rect)+64);
  1197. // [_tableView.layer addSublayer:_layer];
  1198. [self.view.layer addSublayer:_layer];
  1199. self.path = [UIBezierPath bezierPath];
  1200. // [_path moveToPoint:_layer.position];
  1201. [_path moveToPoint:imageView.center];
  1202. [_path addLineToPoint:CGPointMake(imageView.center.x, imageView.center.y-100)];
  1203. // (SCREEN_WIDTH - 60), 0, 50, 50)
  1204. // [_path addQuadCurveToPoint:CGPointMake(SCREEN_WIDTH - 40, SCREEN_HEIGHT-40) controlPoint:CGPointMake(SCREEN_WIDTH/2,rect.origin.y-80)];
  1205. // [_path addLineToPoint:CGPointMake(SCREEN_WIDTH-40, 30)];
  1206. }
  1207. [self groupAnimation];
  1208. }
  1209. -(void)groupAnimation
  1210. {
  1211. self.detailTable.userInteractionEnabled = NO;
  1212. CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  1213. animation.path = _path.CGPath;
  1214. // animation.rotationMode = kCAAnimationRotateAuto;
  1215. //
  1216. CABasicAnimation *expandAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  1217. expandAnimation.duration = 0.3f;
  1218. expandAnimation.fromValue = [NSNumber numberWithFloat:1];
  1219. expandAnimation.toValue = [NSNumber numberWithFloat:2.0f];
  1220. expandAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
  1221. CABasicAnimation *narrowAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  1222. narrowAnimation.beginTime = 0.3;
  1223. narrowAnimation.fromValue = [NSNumber numberWithFloat:2.0f];
  1224. narrowAnimation.duration = 0.9f;
  1225. narrowAnimation.toValue = [NSNumber numberWithFloat:0.3f];
  1226. //
  1227. // narrowAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  1228. CAAnimationGroup *groups = [CAAnimationGroup animation];
  1229. groups.animations = @[animation,expandAnimation,narrowAnimation];
  1230. groups.duration = 1.0f;
  1231. groups.removedOnCompletion=NO;
  1232. groups.fillMode=kCAFillModeForwards;
  1233. groups.delegate = self;
  1234. [_layer addAnimation:groups forKey:@"group"];
  1235. }
  1236. -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
  1237. {
  1238. // [anim def];
  1239. if (anim == [_layer animationForKey:@"group"]) {
  1240. self.detailTable.userInteractionEnabled = YES;
  1241. // _btn.enabled = YES;
  1242. [_layer removeFromSuperlayer];
  1243. _layer = nil;
  1244. // _cnt++;
  1245. // if (_cnt) {
  1246. // _cntLabel.hidden = NO;
  1247. // }
  1248. // CATransition *animation = [CATransition animation];
  1249. // animation.duration = 0.25f;
  1250. // _cntLabel.text = [NSString stringWithFormat:@"%d",_cnt];
  1251. // [_cntLabel.layer addAnimation:animation forKey:nil];
  1252. // CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
  1253. // shakeAnimation.duration = 0.25f;
  1254. // shakeAnimation.fromValue = [NSNumber numberWithFloat:-5];
  1255. // shakeAnimation.toValue = [NSNumber numberWithFloat:5];
  1256. // shakeAnimation.autoreverses = YES;
  1257. // [_btn.layer addAnimation:shakeAnimation forKey:nil];
  1258. }
  1259. }
  1260. #pragma mark - hide section button clicked
  1261. - (void)RefreshSection:(UIButton *)sender {
  1262. sender.enabled=false;
  1263. NSDictionary* section_json = nil;
  1264. NSDictionary* related_json = [self.detail_data objectForKey:@"related_model"];
  1265. if(related_json==nil)
  1266. {
  1267. section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)(sender.tag-1)]];
  1268. }
  1269. else
  1270. {
  1271. if(sender.tag==1)
  1272. {
  1273. section_json = related_json ;
  1274. }
  1275. else
  1276. {
  1277. section_json = [self.detail_data objectForKey:[NSString stringWithFormat:@"detail_%ld",(long)sender.tag-2]];
  1278. }
  1279. }
  1280. NSString* url = section_json[@"data_interface"];
  1281. NSMutableDictionary* params = [section_json[@"params"] mutableCopy];
  1282. if(params==nil)
  1283. params = [[NSMutableDictionary alloc] init];
  1284. NSUInteger ii[2] = {sender.tag,0};
  1285. NSIndexPath* indexpath= [NSIndexPath indexPathWithIndexes:ii length:2];
  1286. DetailTopicCell * cell=[self.detailTable cellForRowAtIndexPath:indexpath];
  1287. [cell begin_refresh];
  1288. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..."title:@"Refreshing"];
  1289. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1290. NSDictionary* editor_json = [iSalesNetwork commoneditor_partialrefresh:params url:url];
  1291. dispatch_async(dispatch_get_main_queue(), ^{
  1292. [cell end_refresh ];
  1293. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1294. sender.enabled=true;
  1295. if([[editor_json valueForKey:@"result"] intValue]==2)
  1296. {
  1297. [self.detail_data addEntriesFromDictionary:editor_json];
  1298. NSRange range = NSMakeRange(sender.tag, 1);
  1299. NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
  1300. [self.detailTable reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationAutomatic];
  1301. }
  1302. else
  1303. {
  1304. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:nil controller:self] ;
  1305. }
  1306. });
  1307. });
  1308. // self.showMore=!self.showMore;
  1309. // NSRange range = NSMakeRange(1, 1);
  1310. // NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
  1311. // [self.detailTable reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationAutomatic];
  1312. }
  1313. #pragma mark - Support scanner
  1314. -(void) onDecodedData:(NSString*) value
  1315. {
  1316. // UIApplication * app = [UIApplication sharedApplication];
  1317. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1318. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  1319. //
  1320. self.model_name = value;
  1321. self.product_id = nil;
  1322. self.use_model_name = true;
  1323. [self reload];
  1324. DebugLog(@"color selected");
  1325. }
  1326. #pragma mark - Handle Notification
  1327. - (void)dealloc {
  1328. [[NSNotificationCenter defaultCenter] removeObserver:self];
  1329. }
  1330. - (void)registeObserver {
  1331. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNoRightMsg:) name:No_Rights_Notification object:nil];
  1332. }
  1333. - (void)handleNoRightMsg:(NSNotification *)notification {
  1334. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Warning" message:@"You have no rights to do that." preferredStyle:UIAlertControllerStyleAlert];
  1335. UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  1336. [alertController dismissViewControllerAnimated:YES completion:nil];
  1337. }];
  1338. [alertController addAction:action];
  1339. [self presentViewController:alertController animated:YES completion:nil];
  1340. }
  1341. #pragma mark - action
  1342. - (void)showEmailAddrBox {
  1343. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1344. JKMessageBoxController *emailAddrVC = [JKMessageBoxController messageBoxControllerWithTip:@"Please enter email address"];
  1345. emailAddrVC.textFiled.secureTextEntry = NO;
  1346. emailAddrVC.yesButtonTitle = @"send";
  1347. __weak typeof(emailAddrVC) weakVC = emailAddrVC;
  1348. __weak typeof(self) weakself = self;
  1349. emailAddrVC.textHandler = ^(NSString *text){
  1350. // 验证邮箱格式是否正确
  1351. NSString *match = @"^[A-Za-zd]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$";
  1352. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",match];
  1353. BOOL isEmailAddr = [predicate evaluateWithObject:text];
  1354. if (isEmailAddr) {
  1355. // 验证是邮件地址,发送邮件
  1356. [weakVC dismissViewControllerAnimated:YES completion:^{
  1357. UIAlertView *waitting_alert = [RAUtils waiting_alert:@"Sending Email" title:@"Waitting"];
  1358. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  1359. NSDictionary *result = [iSalesNetwork notifyModel:weakself.product_id emailAddr:text];
  1360. DebugLog(@"email cart result: %@",result);
  1361. dispatch_async(dispatch_get_main_queue(), ^{
  1362. [waitting_alert dismissWithClickedButtonIndex:0 animated:YES];
  1363. if ([[result objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  1364. UIAlertController *errorAlertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"The email send to %@ failed",text] preferredStyle:UIAlertControllerStyleAlert];
  1365. UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  1366. }];
  1367. [errorAlertVC addAction:action];
  1368. [weakself presentViewController:errorAlertVC animated:YES completion:nil];
  1369. }else {
  1370. }
  1371. });
  1372. });
  1373. }];
  1374. } else {
  1375. // 非邮件地址,警告
  1376. [weakVC warning:@"Please enter right email address"];
  1377. }
  1378. };
  1379. [self presentViewController:emailAddrVC animated:YES completion:nil];
  1380. }
  1381. @end