ContactListViewController.m 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. //
  2. // ContactListViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 8/29/15.
  6. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ContactListViewController.h"
  9. #import "RANetwork.h"
  10. #import "ContactListTableViewCell.h"
  11. #import "CustomerEditViewController.h"
  12. #import "ContactAdvanceSearchViewController.h"
  13. #define DEF_CELL_HEIGHT 44
  14. //#define DEF_TABLE_HEIGHT 44
  15. //#define LINE_WIDTH 0
  16. //#define CELL_MARGIN 0
  17. #define LABEL_MARGIN 15
  18. @interface ContactListViewController ()
  19. @property (nonatomic,strong) NSOperationQueue *dataOperationQueue;
  20. @end
  21. @implementation ContactListViewController
  22. - (NSOperationQueue *)dataOperationQueue {
  23. if (!_dataOperationQueue) {
  24. _dataOperationQueue = [[NSOperationQueue alloc] init];
  25. _dataOperationQueue.maxConcurrentOperationCount = 1;
  26. }
  27. return _dataOperationQueue;
  28. }
  29. - (void)awakeFromNib {
  30. [super awakeFromNib];
  31. self.edit_icon = true;
  32. self.assig_icon = true;
  33. self.reset_icon=false;
  34. }
  35. -(void) logout
  36. {
  37. self.offset = 0;
  38. self.keywords = nil;
  39. self.searchBar.text = nil;
  40. [self.content_data removeAllObjects];
  41. [self refresh_ui];
  42. NSLog(@"%s removeAllObjects",__func__);
  43. }
  44. - (void)viewDidLoad {
  45. [super viewDidLoad];
  46. // self.toolbar.hidden=true;
  47. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  48. self.label_net_err.layer.borderWidth = 2.0;
  49. self.label_net_err.layer.cornerRadius=15;
  50. self.label_net_err.layer.masksToBounds=true;
  51. // self.edgesForExtendedLayout = UIRectEdgeNone;
  52. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  53. // tap.minimumPressDuration = 0.8; //定义按的时间
  54. [self.label_net_err addGestureRecognizer:tap];
  55. // Do any additional setup after loading the view.
  56. self.offset = 0;
  57. self.limit = 25;
  58. self.content_data = [[NSMutableArray alloc]init];
  59. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  60. ref.tag = 201 ;
  61. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  62. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  63. // ref.hidden = true;
  64. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  65. [self.table addSubview:ref];
  66. self.reset = false;
  67. //// UILabel* titleLabel= [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 100
  68. //
  69. // , 44)];
  70. // titleLabel.text=@"Contacts";
  71. // // titleLabel.backgroundColor =[UIColor yellowColor];
  72. // titleLabel.textAlignment=NSTextAlignmentCenter;
  73. // // [titleLabel sizeToFit];
  74. //
  75. // //将搜索条放在一个UIView上
  76. // // UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1768, 44)];
  77. //
  78. //
  79. //
  80. //
  81. // searchView.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth;
  82. //
  83. // titleLabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  84. // titleLabel.center=searchView.center;
  85. //
  86. //
  87. //
  88. // self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(searchView.frame.size.width-200.0f-8.0f,0.0f,200.0f,44.0f)];
  89. // self.searchBar.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  90. // self.searchBar.delegate = self;
  91. // [self.searchBar setPlaceholder:@"name/address"];
  92. // //searchBar.barTintColor=[UIColor clearColor];
  93. // self.searchBar.searchBarStyle=UISearchBarStyleMinimal;
  94. // [searchView addSubview:titleLabel];
  95. // [searchView addSubview:self.searchBar];
  96. //
  97. // self.navigationItem.titleView = searchView;
  98. // NSMutableArray * items = [[NSMutableArray alloc]init];
  99. //
  100. //
  101. // UIBarButtonItem *Btnback = nil;//[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onSaveClick:)];
  102. //
  103. // Btnback = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  104. // style:UIBarButtonItemStylePlain
  105. // target:self
  106. // action:@selector(onBack:)];
  107. //
  108. //
  109. // UIBarButtonItem *btnAS = nil;
  110. // btnAS =[[UIBarButtonItem alloc] initWithTitle:@"Advanced search"
  111. // style:UIBarButtonItemStylePlain
  112. // target:self
  113. // action:@selector(onASearch:)];
  114. //
  115. //
  116. //
  117. //
  118. // //savebtn setBackgroundImage:(nullable UIImage *) forState:<#(UIControlState)#> style:<#(UIBarButtonItemStyle)#> barMetrics:<#(UIBarMetrics)#>
  119. // //[savebtn setImage:[UIImage imageNamed:@"save"] ];
  120. //
  121. //
  122. // /*
  123. // UIBarMetricsDefault,
  124. // UIBarMetricsCompact,
  125. // UIBarMetricsDefaultPrompt = 101, // Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar
  126. // UIBarMetricsCompactPrompt
  127. //
  128. // */
  129. // // [savebtn setBackgroundImage:[UIImage imageNamed:@"save"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  130. // // UIBarButtonItem *item1 = [[UIBarButtonItem alloc] init];
  131. // // item1.title = @"item1";
  132. // //// item1.image = [UIImage imageNamed:@"rect_setting"];
  133. // // UIBarButtonItem *item2 = [[UIBarButtonItem alloc] init];
  134. // //// item2.image = [UIImage imageNamed:@"rect_about"];
  135. // // item2.title = @"item2";
  136. //
  137. //
  138. // UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  139. // fixedItem.width = 20.0f;
  140. //
  141. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  142. ////
  143. //// if(appDelegate.user_type ==USER_ROLE_EMPLOYEE)
  144. //
  145. // [items addObject:Btnback];
  146. //
  147. // [items addObject:fixedItem];
  148. //
  149. // [items addObject:btnAS];
  150. // // [items addObject:item2];
  151. // self.navigationItem.leftBarButtonItems=items;
  152. self.offset = 0;
  153. [self.content_data removeAllObjects];
  154. NSLog(@"%s removeAllObjects",__func__);
  155. // [self loadpage];
  156. }
  157. -(void)manually_refresh
  158. {
  159. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:201];
  160. if(self.isrefreshing)
  161. {
  162. [reF endRefreshing];
  163. return;
  164. }
  165. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  166. if ([self respondsToSelector:@selector(ReloadData)])
  167. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  168. }
  169. -(void)ReloadData
  170. {
  171. self.keywords=self.searchBar.text;
  172. self.offset = 0;
  173. [self.content_data removeAllObjects];
  174. NSLog(@"%s removeAllObjects",__func__);
  175. [self.table reloadData];
  176. NSLog(@"%s reloadData",__FUNCTION__);
  177. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:201];
  178. [reF endRefreshing];
  179. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  180. [self loadpage];
  181. // [self loadpage];
  182. }
  183. - (void)didReceiveMemoryWarning {
  184. [super didReceiveMemoryWarning];
  185. // Dispose of any resources that can be recreated.
  186. }
  187. -(void) viewWillAppear:(BOOL)animated
  188. {
  189. [super viewWillAppear:animated];
  190. // [[self navigationController] setNavigationBarHidden:NO animated:NO];
  191. // self.offset = 0;
  192. // [self.content_data removeAllObjects];
  193. // [self loadpage];
  194. [[self navigationController] setNavigationBarHidden:!self.showNavibar animated:NO];
  195. ;
  196. }
  197. #ifndef RA_NOTIFICATION
  198. -(void) reload_container_getdata:(bool) update_data
  199. {
  200. [super reload_container_getdata:update_data];
  201. if(update_data)
  202. {
  203. self.offset = 0;
  204. [self.content_data removeAllObjects];
  205. NSLog(@"%s removeAllObjects",__func__);
  206. [self reload_data];
  207. }
  208. else
  209. {
  210. [self.table reloadData];
  211. NSLog(@"%s reloadData",__FUNCTION__);
  212. }
  213. }
  214. #endif
  215. - (void)reRefreshView {
  216. [self.table reloadData];
  217. }
  218. -(void) operation_loadpage
  219. {
  220. if (self.dataOperationQueue.operationCount > 1) {
  221. return;
  222. }
  223. dispatch_async(dispatch_get_main_queue(), ^{
  224. if(self.isrefreshing)
  225. return;
  226. self.label_net_err.hidden=true;
  227. self.isrefreshing=true;
  228. if(self.offset==0)
  229. {
  230. self.table.hidden = true;
  231. // [self.params setValue:[NSString stringWithFormat:@"%ld",self.offset] forKey:@"offset"];
  232. self.mum.center = self.view.center;
  233. self.mum.hidden = false;
  234. [self.mum startAnimating];
  235. }
  236. // self.btnrefresh.enabled = false;
  237. //
  238. // NSTimeInterval animationDuration = 0.30f;
  239. // [UIView beginAnimations:@"ResizeView" context:nil];
  240. // [UIView setAnimationDuration:animationDuration];
  241. // self.prepageButton.enabled=false;
  242. // self.nextpageButton.enabled=false;
  243. // self.mum.hidden = false;
  244. // self.grid.hidden = true;
  245. // self.mum.hidden = false;
  246. // self.tableview.hidden = true;
  247. // [UIView commitAnimations];
  248. [RANetwork request_contactlist:self.offset limit:self.limit keywords:self.keywords type:self.contact_type adv_search:self.adv_search completionHandler:^(NSMutableDictionary *result) {
  249. NSMutableDictionary* content=result;
  250. [self.mum stopAnimating];
  251. self.isrefreshing=false;
  252. if (self.dataOperationQueue.operationCount > 1) {
  253. return ;
  254. }
  255. if([[content valueForKey:@"result"] intValue]==2)
  256. {
  257. self.table.hidden =false;
  258. int count = [[content valueForKey:@"count" ] intValue] ;
  259. self.offset += count;
  260. for(int i=0;i<count;i++)
  261. {
  262. NSDictionary* objmsg = [content objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  263. [self.content_data addObject:[NSMutableDictionary dictionaryWithDictionary:objmsg]];
  264. }
  265. if(count<self.limit)
  266. {
  267. // int i =self.btnrefresh.state;
  268. self.load_more_hint=@"All loaded";
  269. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"no_more", nil);
  270. // [self.btnrefresh setTitle:@"No more" forState:UIControlStateNormal];
  271. }
  272. else
  273. {
  274. self.load_more_hint=@"Load more...";
  275. // self.btnrefresh.enabled = true;
  276. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"load_more",nil);
  277. // [self.btnrefresh setTitle:@"Load more" forState:UIControlStateNormal];
  278. }
  279. [self.table reloadData ];
  280. NSLog(@"%s reloadData",__FUNCTION__);
  281. }
  282. else if([[content valueForKey:@"result"] intValue]==RESULT_NET_ERROR &&self.offset==0)
  283. {
  284. self.label_net_err.hidden=false;
  285. self.table.hidden = true;
  286. }
  287. else
  288. {
  289. [RAUtils message_alert:[content valueForKey:@"err_msg"] title:@"Loading Contact List" controller:self] ;
  290. }
  291. self.isrefreshing=false;
  292. }];
  293. return;
  294. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  295. //
  296. //
  297. // NSMutableDictionary* content=nil;
  298. // content=[[RANetwork request_ContactList:self.offset limit : self.limit keywords:self.keywords type:self.contact_type adv_search:self.adv_search] mutableCopy];
  299. // dispatch_async(dispatch_get_main_queue(), ^{
  300. //
  301. // [self.mum stopAnimating];
  302. // self.isrefreshing=false;
  303. // if (self.dataOperationQueue.operationCount > 1) {
  304. // return ;
  305. // }
  306. //
  307. //
  308. // if([[content valueForKey:@"result"] intValue]==2)
  309. // {
  310. // self.table.hidden =false;
  311. // int count = [[content valueForKey:@"count" ] intValue] ;
  312. //
  313. // self.offset += count;
  314. // for(int i=0;i<count;i++)
  315. // {
  316. //
  317. // NSDictionary* objmsg = [content objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  318. // [self.content_data addObject:[NSMutableDictionary dictionaryWithDictionary:objmsg]];
  319. //
  320. // }
  321. // if(count<self.limit)
  322. // {
  323. //
  324. // // int i =self.btnrefresh.state;
  325. // self.load_more_hint=@"All loaded";
  326. // // self.btnrefresh.titleLabel.text =NSLocalizedString(@"no_more", nil);
  327. // // [self.btnrefresh setTitle:@"No more" forState:UIControlStateNormal];
  328. // }
  329. // else
  330. // {
  331. // self.load_more_hint=@"Load more...";
  332. // // self.btnrefresh.enabled = true;
  333. // // self.btnrefresh.titleLabel.text =NSLocalizedString(@"load_more",nil);
  334. // // [self.btnrefresh setTitle:@"Load more" forState:UIControlStateNormal];
  335. // }
  336. //
  337. //
  338. //
  339. // [self.table reloadData ];
  340. // NSLog(@"%s reloadData",__FUNCTION__);
  341. //
  342. // }
  343. // else if([[content valueForKey:@"result"] intValue]==RESULT_NET_ERROR &&self.offset==0)
  344. // {
  345. // self.label_net_err.hidden=false;
  346. // self.table.hidden = true;
  347. // }
  348. // else
  349. // {
  350. // [RAUtils message_alert:[content valueForKey:@"err_msg"] title:@"Loading Contact List" controller:self] ;
  351. // }
  352. //
  353. //
  354. //
  355. //
  356. //
  357. // self.isrefreshing=false;
  358. //
  359. // });
  360. // });
  361. });
  362. }
  363. -(void) loadpage {
  364. __weak typeof(self) weakself = self;
  365. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  366. if (weakself) {
  367. __strong typeof(weakself) strongself = weakself;
  368. [strongself operation_loadpage];
  369. }
  370. }];
  371. [self.dataOperationQueue addOperation:operation];
  372. }
  373. - (void)loadmore
  374. {
  375. // self.btnrefresh.enabled = NO;
  376. // self.btnrefresh.titleLabel.text =NSLocalizedString(@"loading",nil);
  377. // [self.btnrefresh setTitle:@"Loading..." forState:UIControlStateNormal];
  378. [self loadpage];
  379. }
  380. - (IBAction)onBack:(UIBarButtonItem *)sender {
  381. if(self.onCancel)
  382. self.onCancel();
  383. [self.navigationController popViewControllerAnimated:(false)];
  384. }
  385. - (IBAction)onASearch:(UIBarButtonItem *)sender {
  386. ContactAdvanceSearchViewController * vc = [[ContactAdvanceSearchViewController alloc] initWithNibName:@"CommonEditor.iPad" bundle:nil];
  387. vc.url_type = URL_REMOTE;
  388. vc.request_url=URL_CUSTOMER_ADV_SEARCH;
  389. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  390. vc.params = params;
  391. vc.data_init = self.adv_search;
  392. // vc.update_order = true;
  393. // vc.contactId=contactid;
  394. // vc.delegate = self;
  395. vc.returnValue = ^(NSMutableDictionary* value){
  396. self.adv_search = value;
  397. self.keywords=nil;
  398. self.offset = 0;
  399. [self.content_data removeAllObjects];
  400. NSLog(@"%s removeAllObjects",__func__);
  401. [self loadpage];
  402. // appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  403. // appDelegate.customerInfo = value;
  404. };
  405. [self.navigationController pushViewController:vc animated:true];
  406. }
  407. - (IBAction)onNewCustomerClick:(id)sender {
  408. NSLog(@"new click");
  409. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  410. CustomerEditViewController * cuseditVC = [[CustomerEditViewController alloc] initWithNibName:@"CommonEditor.iPad" bundle:nil];
  411. cuseditVC.bnewcustomer=true;
  412. cuseditVC.url_type = URL_REMOTE;
  413. cuseditVC.request_url=URL_NEW_CUSTOMER;
  414. cuseditVC.params = params;
  415. // cuseditVC.delegate=self;
  416. //
  417. // if(checked.count==count)
  418. // {
  419. // orderinfoVC.have_tail = true
  420. // }
  421. [[self navigationController] setNavigationBarHidden:NO animated:NO];
  422. [self.navigationController pushViewController:cuseditVC animated:false];
  423. }
  424. - (IBAction)resetContactId:(id)sender {
  425. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  426. appDelegate.contact_id=nil;
  427. appDelegate.customerInfo = nil;
  428. // appDelegate.contact_name =nil;
  429. if(self.onReset)
  430. self.onReset();
  431. [self.navigationController popViewControllerAnimated:(false)];
  432. }
  433. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  434. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  435. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  436. // what ever you want to prepare
  437. } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  438. DebugLog(@"!!!!!!!!!!!!!!!!!!!!!route!!!!!!!!!!!!!!!!!!!!!");
  439. [self.table reloadData];
  440. NSLog(@"%s reloadData",__FUNCTION__);
  441. }];
  442. }
  443. //- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  444. //{
  445. // DebugLog(@"!!!!!!!!!!!!!!!!!!!!!route!!!!!!!!!!!!!!!!!!!!!");
  446. // [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  447. // [self.table reloadData];
  448. // NSLog(@"%s reloadData",__FUNCTION__);
  449. // // CGSize contentsize = self.scroll.contentSize;
  450. // // contentsize.height =330+2 * 230+29+20+20 +1 * 230+29+20+20 ;
  451. // // UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  452. // // if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight)
  453. // // {
  454. // // contentsize.height+=250;
  455. // // }
  456. // //
  457. // // self.scroll.contentSize = contentsize;
  458. // // // CGRect frame = self.view.frame;
  459. // // // [self.carouselController.view removeFromSuperview];
  460. // // // wait(2000);
  461. // // // self.carouselController = [[FPCarouselNonXIBViewController alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 400)];
  462. // // // [self.view addSubview:self.carouselController.view];
  463. // // // int i = self.view.subviews.count;
  464. // // // int b = 0;
  465. // // // [self.carouselController updateFrame:CGRectMake(0, 0, self.view.frame.size.width, 400)];
  466. //}
  467. //
  468. //-(void)changeCell
  469. //{
  470. // // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  471. // // reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
  472. // [self performSelector:@selector(loadmore) withObject:nil afterDelay:1];
  473. // DebugLog(@"refresh!!!!!!!!");
  474. //
  475. //}
  476. - (IBAction)onAssignClick:(id)sender {
  477. UIButton* btn = (UIButton*)sender;
  478. UITableViewCell *cell = (UITableViewCell *)btn.superview.superview;;
  479. NSIndexPath *indexPath = [self.table indexPathForCell:cell];
  480. DebugLog(@"indexPath is = %ld",indexPath.row);
  481. NSString* contactid=[self.content_data[indexPath.row] valueForKey:@"contact_id"] ;
  482. __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Getting Contact information" completion:^{
  483. [RANetwork request_contactinfo:contactid completionHandler:^(NSMutableDictionary *result) {
  484. NSMutableDictionary* editor_json = result;
  485. [waitalert dismissViewControllerAnimated:YES completion:^{
  486. if([[editor_json valueForKey:@"result"] intValue]==2)
  487. {
  488. __block NSMutableDictionary* data_init = [[editor_json objectForKey:@"customerInfo" ] mutableCopy];
  489. //---------------- construct customer_contact----------------------
  490. NSString* customer_first_name = [data_init valueForKey:@"customer_first_name"];
  491. if(customer_first_name==nil)
  492. customer_first_name=@"";
  493. NSString* customer_last_name= [data_init valueForKey:@"customer_last_name"];
  494. if(customer_last_name==nil)
  495. customer_last_name=@"";
  496. NSString* customer_contact = [NSString stringWithFormat:@"%@ %@",customer_first_name,customer_last_name];
  497. customer_contact=[customer_contact stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  498. [data_init setValue:customer_contact forKey:@"customer_contact"];
  499. data_init[@"is_subaction"]=self.is_subaction;
  500. data_init[@"subaction_tag"]= self.subaction_tag;
  501. [self.table reloadData];
  502. NSLog(@"%s reloadData",__FUNCTION__);
  503. [self.navigationController popToRootViewControllerAnimated:false];
  504. if(self.returnValue)
  505. self.returnValue(data_init,self.from);
  506. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  507. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  508. // NSDictionary* editor_json = [iSalesNetwork update_OrderCustomer:appDelegate.order_code customerinfo:data_init];
  509. //
  510. // dispatch_async(dispatch_get_main_queue(), ^{
  511. //
  512. //
  513. // if([[editor_json valueForKey:@"result"] intValue]==2)
  514. // {
  515. //
  516. //// [self.navigationController popViewControllerAnimated:false];
  517. //// if(self.returnValue)
  518. //// {
  519. ////
  520. //// self.returnValue(data_init);
  521. //// }
  522. //
  523. //
  524. //
  525. // }
  526. // else
  527. // {
  528. // [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Update Order Customer Infomation" controller:self] ;
  529. // }
  530. //
  531. //
  532. //
  533. // });
  534. // });
  535. }
  536. else
  537. {
  538. [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Loading Contact Information" controller:self] ;
  539. // self.btn_assign.enabled=false;
  540. }
  541. }];
  542. }];
  543. }];
  544. return ;
  545. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  546. //
  547. // NSDictionary* editor_json = [RANetwork request_CustomerInfo:contactid];
  548. //
  549. // dispatch_async(dispatch_get_main_queue(), ^{
  550. //
  551. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  552. //
  553. // if([[editor_json valueForKey:@"result"] intValue]==2)
  554. // {
  555. // __block NSMutableDictionary* data_init = [[editor_json objectForKey:@"customerInfo" ] mutableCopy];
  556. //
  557. // //---------------- construct customer_contact----------------------
  558. // NSString* customer_first_name = [data_init valueForKey:@"customer_first_name"];
  559. // if(customer_first_name==nil)
  560. // customer_first_name=@"";
  561. //
  562. // NSString* customer_last_name= [data_init valueForKey:@"customer_last_name"];
  563. // if(customer_last_name==nil)
  564. // customer_last_name=@"";
  565. // NSString* customer_contact = [NSString stringWithFormat:@"%@ %@",customer_first_name,customer_last_name];
  566. // customer_contact=[customer_contact stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  567. // [data_init setValue:customer_contact forKey:@"customer_contact"];
  568. //
  569. // data_init[@"is_subaction"]=self.is_subaction;
  570. // data_init[@"subaction_tag"]= self.subaction_tag;
  571. // [self.navigationController popToRootViewControllerAnimated:false];
  572. //
  573. //
  574. // [self.table reloadData];
  575. // NSLog(@"%s reloadData",__FUNCTION__);
  576. // if(self.returnValue)
  577. // self.returnValue(data_init,self.from);
  578. //
  579. // // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  580. // // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  581. // // NSDictionary* editor_json = [iSalesNetwork update_OrderCustomer:appDelegate.order_code customerinfo:data_init];
  582. // //
  583. // // dispatch_async(dispatch_get_main_queue(), ^{
  584. // //
  585. // //
  586. // // if([[editor_json valueForKey:@"result"] intValue]==2)
  587. // // {
  588. // //
  589. // //// [self.navigationController popViewControllerAnimated:false];
  590. // //// if(self.returnValue)
  591. // //// {
  592. // ////
  593. // //// self.returnValue(data_init);
  594. // //// }
  595. // //
  596. // //
  597. // //
  598. // // }
  599. // // else
  600. // // {
  601. // // [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Update Order Customer Infomation" controller:self] ;
  602. // // }
  603. // //
  604. // //
  605. // //
  606. // // });
  607. // // });
  608. //
  609. // }
  610. // else
  611. // {
  612. // [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Loading Contact Information" controller:self] ;
  613. // // self.btn_assign.enabled=false;
  614. // }
  615. //
  616. //
  617. // });
  618. // });
  619. }
  620. #pragma mark - Table view data source
  621. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  622. {
  623. if(indexPath.row==self.content_data.count)
  624. return 44;
  625. NSString* value = [self.content_data[indexPath.row] valueForKey:@"name"];
  626. float width = tableView.frame.size.width;
  627. // 40+15+8
  628. width-=63;
  629. CGSize constraintkey = CGSizeMake(width-2*LABEL_MARGIN, 10.0f);//key label width is 40% cell width;
  630. // CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
  631. // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
  632. // NSString* key =[self.content_data valueForKey:@"order_info"];
  633. // [cell.keyLabel sizeToFit];
  634. // NSString* val=[item_json valueForKey:@"val"] ;
  635. // if([val isEqual:[NSNull null]])
  636. // val=@"";
  637. // if(val==nil)
  638. // val=@"";
  639. // if([val isEqualToString:@"null"])
  640. // val=@"";
  641. CGRect frame;
  642. frame.size = constraintkey;
  643. frame.origin.x=0;
  644. frame.origin.y=0;
  645. RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
  646. // [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  647. // rtlabel.lineSpacing = 20.0;
  648. [rtlabel setText: value];
  649. CGSize optimumSize = [rtlabel optimumSize];
  650. // DebugLog(@"%@",key);
  651. // [rtlabel sizeThatFits:constraintkey];
  652. // rtlabel frameHeight:<#(CTFrameRef)#>
  653. // rtlabel
  654. // [rtlabel sizeToFit];
  655. //CGSize newsize= rtlabel.frame.size;
  656. // CGSize sizeval=rtlabel.optimumSize;
  657. // CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  658. // CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping];
  659. float height = optimumSize.height;
  660. height = MAX(height+2*LABEL_MARGIN, DEF_CELL_HEIGHT);
  661. return height;
  662. }
  663. //- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  664. //{
  665. // NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  666. // if([item_json objectForKey:@"combine"] == nil)
  667. // {
  668. // return 120;
  669. // }
  670. // else
  671. // return 150;
  672. //}
  673. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  674. //{
  675. // return 0;
  676. //}
  677. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  678. //{
  679. // return 0;
  680. //}
  681. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  682. //{
  683. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  684. // return myView;
  685. //
  686. //}
  687. //- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  688. //{
  689. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  690. //// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  691. //// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
  692. //// titleLabel.textColor=[UIColor whiteColor];
  693. //// titleLabel.backgroundColor = [UIColor clearColor];
  694. //// if(section==0)
  695. //// titleLabel.text=NSLocalizedString(@"display_items", nil);
  696. //// else
  697. //// titleLabel.text=NSLocalizedString(@"hide_items", nil);
  698. //// [titleLabel sizeToFit];
  699. //// [myView addSubview:titleLabel];
  700. ////
  701. // return myView;
  702. //}
  703. //
  704. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  705. // if(section==0)
  706. // return nil;
  707. // else
  708. // return @"detail section";
  709. //}
  710. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  711. {
  712. return 1;
  713. }
  714. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  715. {
  716. // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
  717. if( self.content_data.count==0 || /*self.refresh_type == REFRESH_DATA ||*/ self.isrefreshing) //
  718. return 0;
  719. return self.content_data.count+1;
  720. }
  721. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  722. {
  723. // if(tableView==self.itemListTable)
  724. // {
  725. // DebugLog(@"row %ld",(long)indexPath.row);
  726. if(indexPath.row==self.content_data.count)
  727. {
  728. UITableViewCell *moreCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mores"];
  729. UILabel *moreDataLabel=[[UILabel alloc] init];
  730. moreDataLabel.tag=200;
  731. moreDataLabel.text=self.load_more_hint;
  732. [moreDataLabel setFont:[UIFont systemFontOfSize:14.0f]];
  733. [moreDataLabel setTextAlignment:NSTextAlignmentCenter];
  734. moreDataLabel.frame=CGRectMake(0, 10, self.table.bounds.size.width, 20);
  735. [moreDataLabel setBackgroundColor:[UIColor clearColor]];
  736. // [moreDataLabel addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
  737. moreCell.backgroundColor = [UIColor whiteColor];
  738. [moreCell.contentView addSubview:moreDataLabel];
  739. if([self.load_more_hint isEqualToString:@"Load more..."])
  740. [self performSelector:@selector(loadmore) withObject:nil afterDelay:1];
  741. // DebugLog(@"更多...");
  742. return moreCell;
  743. }
  744. else
  745. {
  746. // UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ContactListCell"];
  747. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  748. NSString *CellIdentifier = @"ContactListTableViewCell";
  749. ContactListTableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  750. NSString* value = [self.content_data[indexPath.row] valueForKey:@"name"];
  751. cell.rtlabel.text = value;
  752. cell.btn_assign.hidden = !(self.assig_icon&&(appDelegate.contact_id.length==0));
  753. /* NSString* name= [self.content_data[indexPath.row] valueForKey:@"name"];
  754. NSString* address= [self.content_data[indexPath.row] valueForKey:@"address"];
  755. cell.detailTextLabel.text=address;
  756. cell.textLabel.text=name;
  757. //cell.sep
  758. // [UIColor rg]*/
  759. cell.backgroundColor = [UIColor whiteColor];
  760. // cell.separator.backgroundColor = [UIColor grayColor];
  761. return cell;
  762. }
  763. }
  764. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  765. {
  766. if (indexPath.row>self.content_data.count-1) {
  767. return;
  768. }
  769. NSString* contactid=[self.content_data[indexPath.row] valueForKey:@"contact_id"] ;
  770. bool editable =[[self.content_data[indexPath.row] valueForKey:@"can_update"]boolValue] ;
  771. CustomerInfoViewController * vc=[[CustomerInfoViewController alloc] initWithNibName:@"CommonEditor.iPad" bundle:nil];
  772. vc.url_type = URL_LOCAL;
  773. vc.request_url=LOCAL_URL_CUSTOMER_INFO_EDITOR;
  774. vc.update_order = self.update_order;
  775. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  776. if(self.ui_type==CONTACT_LIST)
  777. {
  778. vc.edit_icon = self.edit_icon&&editable&&appDelegate.can_update_contact_info;
  779. vc.assig_icon = self.assig_icon&&(appDelegate.contact_id.length==0);
  780. vc.reset_icon = self.reset_icon&&(appDelegate.contact_id.length!=0);
  781. }
  782. else
  783. {
  784. vc.edit_icon = self.edit_icon&&editable&&appDelegate.can_update_contact_info;
  785. vc.assig_icon = self.assig_icon;
  786. vc.reset_icon = self.reset_icon;
  787. }
  788. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  789. // // NSString * string = [checked componentsJoinedByString:@","];
  790. // // [params setValue:string forKey:@"cart2Checkbox"];
  791. //
  792. //
  793. //
  794. //
  795. // addressVC.params = params;
  796. vc.contactId=contactid;
  797. // vc.delegate = self;
  798. vc.from = indexPath;
  799. // orderinfoVC.params = params;
  800. vc.returnValue = ^(NSMutableDictionary* value){
  801. value[@"is_subaction"]=self.is_subaction;
  802. value[@"subaction_tag"]= self.subaction_tag;
  803. [self.navigationController popViewControllerAnimated:false];
  804. if(self.returnValue)
  805. self.returnValue(value,self.from);
  806. };
  807. [self.navigationController pushViewController:vc animated:true];
  808. }
  809. /*
  810. #pragma mark - Navigation
  811. // In a storyboard-based application, you will often want to do a little preparation before navigation
  812. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  813. // Get the new view controller using [segue destinationViewController].
  814. // Pass the selected object to the new view controller.
  815. }
  816. */
  817. #pragma mark - searchBar delegate;
  818. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  819. {
  820. self.keywords=searchBar.text;
  821. self.offset = 0;
  822. [self.content_data removeAllObjects];
  823. NSLog(@"%s removeAllObjects",__func__);
  824. self.adv_search=nil;
  825. [self loadpage];
  826. DebugLog(@"search");
  827. }
  828. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  829. {
  830. if([searchText isEqualToString:@""] && !self.reset)
  831. {
  832. DebugLog(@"clear");
  833. self.reset=true;
  834. self.keywords=nil;
  835. self.offset = 0;
  836. [self.content_data removeAllObjects];
  837. NSLog(@"%s removeAllObjects",__func__);
  838. self.adv_search=nil;
  839. [self loadpage];
  840. // [self loadpage];
  841. }
  842. else
  843. self.reset = false;
  844. }
  845. #pragma mark - RA_NOTIFICAITON
  846. -(void) refresh_ui
  847. {
  848. [self.table reloadData];
  849. }
  850. -(void) reload_data
  851. {
  852. self.offset = 0;
  853. [self.content_data removeAllObjects];
  854. [self.table reloadData];
  855. [self loadpage];
  856. }
  857. @end