EnumSelectOnlineViewController.m 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. //
  2. // EnumSelectOnlineViewController.m
  3. // RA TradeFiling
  4. //
  5. // Created by Rui Zhang on 5/14/21.
  6. //
  7. #import "EnumSelectOnlineViewController.h"
  8. #import "EnumSelectorCell.h"
  9. #import "RAUtils.h"
  10. #import "AppDelegate.h"
  11. #import "RADataProvider.h"
  12. #import "ResultPresenter.h"
  13. #import "JLRefreshHeader.h"
  14. #import "JLRefreshFooter.h"
  15. #define DEF_CELL_HEIGHT 44
  16. //#define DEF_TABLE_HEIGHT 44
  17. //#define LINE_WIDTH 0
  18. //#define CELL_MARGIN 0
  19. #define LABEL_MARGIN 15
  20. @interface EnumSelectOnlineViewController ()<JLRefreshDelegate>
  21. @property (nonatomic,weak) JLRefreshBasis *curRefresh;
  22. @property (nonatomic,weak) RAProgressHUD *curHUD;
  23. @property (strong,nonatomic) NSMutableDictionary* cadedate;
  24. @property int offset ;
  25. @property int limit ;
  26. //@property (nonatomic,strong) NSArray<NSMutableDictionary *> *dataArray;
  27. @end
  28. @implementation EnumSelectOnlineViewController
  29. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  30. {
  31. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  32. if (self) {
  33. // Custom initialization
  34. }
  35. return self;
  36. }
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. // self.automaticallyAdjustsScrollViewInsets = NO;
  41. // self.max_select = 0;
  42. self.navi_item.title = self.title;
  43. // self.navigationItem.title = self.title;
  44. // Do any additional setup after loading the view.
  45. self.auto_close = true;
  46. if(self.is_dialog)
  47. {
  48. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  49. style:UIBarButtonItemStylePlain
  50. target:self
  51. action:@selector( onCloseClick:)];
  52. // [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
  53. self.navi_item.rightBarButtonItem = closeButton;
  54. }
  55. else
  56. {
  57. UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  58. style:UIBarButtonItemStylePlain
  59. target:self
  60. action:@selector( onBackClick:)];
  61. // [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onBackClick:)];
  62. self.navigationItem.leftBarButtonItem = backButton;
  63. }
  64. self.dirty = false;
  65. NSString *model = [UIDevice currentDevice].model;
  66. if ([model isEqualToString:@"iPhone"]) {
  67. } else {
  68. UILabel* titleLabel= [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 200
  69. , 44)];
  70. titleLabel.text=self.title;
  71. // titleLabel.backgroundColor =[UIColor yellowColor];
  72. titleLabel.textAlignment=NSTextAlignmentCenter;
  73. //将搜索条放在一个UIView上
  74. UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1768, 44)];
  75. searchView.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth;
  76. titleLabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  77. titleLabel.center=searchView.center;
  78. UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(searchView.frame.size.width-200.0f-8.0f,0.0f,200.0f,44.0f)];
  79. searchBar.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  80. searchBar.delegate = self;
  81. [searchBar setPlaceholder:@"filter"];
  82. //searchBar.barTintColor=[UIColor clearColor];
  83. searchBar.searchBarStyle=UISearchBarStyleMinimal;
  84. [searchView addSubview:titleLabel];
  85. [searchView addSubview:searchBar];
  86. self.navigationItem.titleView = searchView;
  87. }
  88. [[self navigationController] setNavigationBarHidden:self.hide_navibar animated:NO];
  89. JLRefreshHeader *header = [[JLRefreshHeader alloc] init];
  90. header.refreshDelegate = self;
  91. self.tableEnum.jl_header = header;
  92. JLRefreshFooter *footer = [[JLRefreshFooter alloc] init];
  93. footer.refreshDelegate = self;
  94. self.tableEnum.jl_footer = footer;
  95. [self loadData];
  96. }
  97. - (void)onBackClick:(UIButton *)sender {
  98. [self.navigationController popViewControllerAnimated:(false)];
  99. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  100. // ;
  101. // }];
  102. }
  103. - (void)onCloseClick:(UIButton *)sender {
  104. [self dismissViewControllerAnimated:YES
  105. completion:^{
  106. }];
  107. }
  108. - (void)viewWillDisappear:(BOOL)animated
  109. {
  110. [super viewWillDisappear:animated];
  111. if(self.dirty == false)
  112. return;
  113. if (self.delegate && [self.delegate respondsToSelector:@selector(EnumValueChanged:indexPath:)]) {
  114. [self.delegate EnumValueChanged:self.cadedate indexPath:self.updatePosition];
  115. }
  116. if(self.returnValue)
  117. self.returnValue(self.cadedate);
  118. }
  119. - (void)didReceiveMemoryWarning
  120. {
  121. [super didReceiveMemoryWarning];
  122. // Dispose of any resources that can be recreated.
  123. }
  124. //-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  125. //{
  126. //
  127. // [self.tableEnum reloadData];
  128. ////
  129. //// UIApplication * app = [UIApplication sharedApplication];
  130. //// AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  131. //// [appDelegate didRotated];
  132. //
  133. //
  134. //
  135. //}
  136. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  137. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  138. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  139. // what ever you want to prepare
  140. } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  141. [self.tableEnum reloadData];
  142. }];
  143. }
  144. #pragma mark - Table view data source
  145. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  146. {
  147. return 1;
  148. }
  149. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  150. {
  151. // if(self.canbeEmpty)
  152. // return [[self.cadedate valueForKey:@"count"] intValue]+1;
  153. // else
  154. if(self.keywords==nil || self.keywords.length==0)
  155. {
  156. self.filter_count = 0;
  157. return [[self.cadedate valueForKey:@"count"] intValue];
  158. }
  159. else
  160. {
  161. int count = 0;
  162. for (int i=0; i<[[self.cadedate valueForKey:@"count"] intValue]; i++)
  163. {
  164. NSDictionary* val_json = [self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i ]];
  165. NSString* value = [val_json valueForKey:@"value"];
  166. NSUInteger location=[[value uppercaseString] rangeOfString:self.keywords].location;
  167. if(location!= NSNotFound)
  168. count++;
  169. }
  170. self.filter_count = count;
  171. return count;
  172. }
  173. }
  174. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  175. {
  176. NSDictionary* val_json =nil;
  177. if(self.keywords==nil || self.keywords.length==0)
  178. {
  179. val_json=[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row ]];
  180. }
  181. else
  182. {
  183. int count = 0;
  184. for (int i=0; i<[[self.cadedate valueForKey:@"count"] intValue]; i++)
  185. {
  186. if(count>= self.filter_count)
  187. break;
  188. NSDictionary* search_json = [self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i ]];
  189. NSString* value = [search_json valueForKey:@"value"];
  190. NSUInteger location=[[value uppercaseString] rangeOfString:self.keywords].location;
  191. if(location!= NSNotFound)
  192. count++;
  193. if(count-1==indexPath.row)
  194. val_json=search_json;
  195. }
  196. }
  197. NSString* value = [val_json valueForKey:@"value"];
  198. float width = tableView.frame.size.width;
  199. width-=40;
  200. CGSize constraintkey = CGSizeMake(width-2*LABEL_MARGIN, 10.0f);//key label width is 40% cell width;
  201. // CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
  202. // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
  203. // NSString* key =[self.content_data valueForKey:@"order_info"];
  204. // [cell.keyLabel sizeToFit];
  205. // NSString* val=[item_json valueForKey:@"val"] ;
  206. // if([val isEqual:[NSNull null]])
  207. // val=@"";
  208. // if(val==nil)
  209. // val=@"";
  210. // if([val isEqualToString:@"null"])
  211. // val=@"";
  212. CGRect frame;
  213. frame.size = constraintkey;
  214. frame.origin.x=0;
  215. frame.origin.y=0;
  216. RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
  217. // rtlabel.lineSpacing = 10;
  218. // [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  219. // rtlabel.lineSpacing = 20.0;
  220. [rtlabel setText: value];
  221. CGSize optimumSize = [rtlabel optimumSize];
  222. // DebugLog(@"%@",key);
  223. // [rtlabel sizeThatFits:constraintkey];
  224. // rtlabel frameHeight:<#(CTFrameRef)#>
  225. // rtlabel
  226. // [rtlabel sizeToFit];
  227. //CGSize newsize= rtlabel.frame.size;
  228. // CGSize sizeval=rtlabel.optimumSize;
  229. // CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  230. // CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping];
  231. float height = optimumSize.height;
  232. height = MAX(height+2*LABEL_MARGIN, DEF_CELL_HEIGHT);
  233. return height;
  234. }
  235. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  236. {
  237. NSDictionary* val_json =nil;
  238. if(self.keywords==nil || self.keywords.length==0)
  239. {
  240. val_json=[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row ]];
  241. }
  242. else
  243. {
  244. int count = 0;
  245. for (int i=0; i<[[self.cadedate valueForKey:@"count"] intValue]; i++)
  246. {
  247. if(count>= self.filter_count)
  248. break;
  249. NSDictionary* search_json = [self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i ]];
  250. NSString* value = [search_json valueForKey:@"value"];
  251. NSUInteger location=[[value uppercaseString] rangeOfString:self.keywords].location;
  252. if(location!= NSNotFound)
  253. count++;
  254. if(count-1==indexPath.row)
  255. {
  256. val_json=search_json;
  257. break;
  258. }
  259. }
  260. }
  261. NSString* CellIdentifier = @"EnumSelectorCell";
  262. EnumSelectorCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  263. // NSDictionary* val_json = [self.cadedate objectForKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row ]];
  264. NSString* value = [val_json valueForKey:@"value"];
  265. // cell.rtLabel.lineSpacing=10;
  266. cell.rtLabel.text = value;
  267. // cell.labelValue.text = value;
  268. int check = [[val_json valueForKey:@"check" ] intValue];
  269. if(check==1)
  270. cell.img_checkmark.hidden=false;
  271. else
  272. cell.img_checkmark.hidden=true;
  273. return cell;
  274. }
  275. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  276. {
  277. EnumSelectorCell* cell = (EnumSelectorCell*)[tableView cellForRowAtIndexPath:indexPath];
  278. cell.selected = false;
  279. self.dirty = true;
  280. if(self.single_select)
  281. {
  282. if(self.keywords==nil || self.keywords.length==0)
  283. {
  284. for(int i=0;i<[[self.cadedate valueForKey:@"count"] intValue];i++)
  285. {
  286. NSMutableDictionary* val_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i]] mutableCopy];
  287. if(i==indexPath.row)
  288. [val_json setValue:@"1" forKey:@"check"];
  289. else
  290. [val_json setValue:@"0" forKey:@"check"];
  291. [self.cadedate setObject:val_json forKey:[NSString stringWithFormat:@"val_%d",i]];
  292. }
  293. }
  294. else
  295. {
  296. NSMutableDictionary* val_json =nil;
  297. int count = 0;
  298. for (int cc=0; cc<[[self.cadedate valueForKey:@"count"] intValue]; cc++)
  299. {
  300. if(count>= self.filter_count)
  301. break;
  302. NSMutableDictionary* search_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",cc ]] mutableCopy];
  303. NSString* value = [search_json valueForKey:@"value"];
  304. NSUInteger location=[[value uppercaseString] rangeOfString:self.keywords].location;
  305. if(location!= NSNotFound)
  306. count++;
  307. if(count-1==indexPath.row)
  308. val_json=search_json;
  309. }
  310. for (int sc=0;sc<[[self.cadedate valueForKey:@"count"] intValue];sc++)
  311. {
  312. NSMutableDictionary* sc_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",sc ]] mutableCopy];
  313. if ([[sc_json valueForKey:@"value" ] isEqual:[val_json valueForKey:@"value"]] )
  314. [sc_json setValue:@"1" forKey:@"check"];
  315. else
  316. [sc_json setValue:@"0" forKey:@"check"];
  317. [self.cadedate setObject:sc_json forKey:[NSString stringWithFormat:@"val_%d",sc]];
  318. }
  319. }
  320. if(self.auto_close)
  321. {
  322. if(self.navigationController!=nil)
  323. [self.navigationController popViewControllerAnimated:YES];
  324. // [self.navigationController dismissViewControllerAnimated:YES
  325. // completion:^{
  326. //
  327. //
  328. // }];
  329. else
  330. [self dismissViewControllerAnimated:YES
  331. completion:^{
  332. }];
  333. }
  334. }
  335. else
  336. {
  337. NSMutableDictionary* val_json =nil;
  338. if(self.keywords==nil || self.keywords.length==0)
  339. {
  340. val_json=[[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row ]] mutableCopy];
  341. }
  342. else
  343. {
  344. int count = 0;
  345. for (int i=0; i<[[self.cadedate valueForKey:@"count"] intValue]; i++)
  346. {
  347. if(count>= self.filter_count)
  348. break;
  349. NSMutableDictionary* search_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i ]] mutableCopy];
  350. NSString* value = [search_json valueForKey:@"value"];
  351. NSUInteger location=[[value uppercaseString] rangeOfString:self.keywords].location;
  352. if(location!= NSNotFound)
  353. count++;
  354. if(count-1==indexPath.row)
  355. val_json=search_json;
  356. }
  357. }
  358. // NSMutableDictionary* val_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row]] mutableCopy];
  359. int check = [[val_json valueForKey:@"check"] intValue];
  360. if(check==1)
  361. [val_json setValue:@"0" forKey:@"check"];
  362. else
  363. {
  364. if([self check_count]>=self.max_select && self.max_select>0)
  365. {
  366. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: message:[NSString stringWithFormat: delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  367. // // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
  368. // [alert show];
  369. [RAUtils message_alert:[NSString stringWithFormat:@"%d items at most for this field.",self.max_select ] title:@"Max count reached" controller:self];
  370. // [RAUtils alert_view:[NSString stringWithFormat:@"%d items at most for this field.",self.max_select ] title:@"Max count reached"];
  371. return;
  372. }
  373. [val_json setValue:@"1" forKey:@"check"];
  374. }
  375. [self.cadedate setObject:val_json forKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row]];
  376. }
  377. [self.tableEnum reloadData];
  378. }
  379. -(int) check_count
  380. {
  381. int check_count=0;
  382. // int count = 0;
  383. for (int i=0; i<[[self.cadedate valueForKey:@"count"] intValue]; i++)
  384. {
  385. NSMutableDictionary* val_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i ]] mutableCopy];
  386. int check = [[val_json valueForKey:@"check"] intValue];
  387. if(check==1)
  388. check_count++;
  389. }
  390. return check_count;
  391. }
  392. #pragma mark - searchBar delegate;
  393. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  394. {
  395. self.keywords=[searchBar.text uppercaseString];
  396. [self loadData];
  397. // [self.tableEnum reloadData];
  398. // self.offset = 0;
  399. // [self.content_data removeAllObjects];
  400. // [self loadpage];
  401. // DebugLog(@"search");
  402. }
  403. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  404. {
  405. if([searchText isEqualToString:@""] && !self.reset)
  406. {
  407. self.keywords=nil;
  408. [self.tableEnum reloadData];
  409. self.reset=true;
  410. }
  411. else
  412. self.reset = false;
  413. }
  414. /*
  415. #pragma mark - Navigation
  416. // In a storyboard-based application, you will often want to do a little preparation before navigation
  417. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  418. {
  419. // Get the new view controller using [segue destinationViewController].
  420. // Pass the selected object to the new view controller.
  421. }
  422. */
  423. - (void)loadData:(RAResultFetchDataType)option {
  424. [self onStartLoading];
  425. __weak typeof(self) weakSelf = self;
  426. NSMutableDictionary *params = [self.online_params mutableCopy];
  427. [params setObject:[NSNumber numberWithInteger:self.offset] forKey:@"offset"];
  428. [params setObject:[NSNumber numberWithInteger:self.limit] forKey:@"limit"];
  429. if(self.online_keywords.length>0)
  430. self.online_params[@"keywords"]=self.online_keywords;
  431. [RADataProvider GetCadedate:self.online_params completionHandler:^(NSMutableDictionary *result) {
  432. NSMutableDictionary * json = result;
  433. __strong typeof(weakSelf) strongSelf = weakSelf;
  434. int resultcode = [[json objectForKey:@"result"] intValue];
  435. if (resultcode == RESULT_TRUE) {
  436. // NSMutableArray *modelsArr = [NSMutableArray array];
  437. // if (option == RAResultFetchDataTypeLoadMore && strongSelf.dataArray != nil && strongSelf.dataArray.count > 0) {
  438. // [modelsArr addObjectsFromArray:strongSelf.dataArray];
  439. // }
  440. if (option != RAResultFetchDataTypeLoadMore) {
  441. self.cadedate = [NSMutableDictionary new];
  442. }
  443. int count = [json[@"count"] intValue];
  444. for (int i=0;i<count;i++)
  445. {
  446. NSMutableDictionary* item = [json[[NSString stringWithFormat:@"val_%d",i]] mutableCopy];
  447. self.cadedate[[NSString stringWithFormat:@"val_%d",i]]=item;
  448. }
  449. self.cadedate[@"count"]= @([self.cadedate[@"count"] intValue]+count);
  450. strongSelf.offset = [self.cadedate[@"count"] intValue];
  451. NSString *title = [json objectForKey:@"title"];
  452. // refresh UI
  453. dispatch_async(dispatch_get_main_queue(), ^{
  454. if (weakSelf) {
  455. __strong typeof(weakSelf) strongSelf = weakSelf;
  456. // strongSelf.dataArray = [modelsArr copy];
  457. [strongSelf onStopLoading];
  458. [strongSelf onSuccess:title];
  459. if (count < self.limit) {
  460. [strongSelf onNoMoreData];
  461. }
  462. }
  463. });
  464. } else {
  465. NSString *msg = [json objectForKey:@"err_msg"];
  466. if(msg.length==0)
  467. {
  468. msg=@"Sorry, can not connect to the server, please try again later.";
  469. }
  470. __weak typeof(self) weakSelf = self;
  471. dispatch_async(dispatch_get_main_queue(), ^{
  472. if (weakSelf) {
  473. __strong typeof(weakSelf) strongSelf = weakSelf;
  474. [strongSelf onStopLoading];
  475. [strongSelf onFailed:msg];
  476. }
  477. });
  478. }
  479. }];
  480. return;
  481. //
  482. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  483. //
  484. // NSMutableDictionary *params = [weakSelf.params mutableCopy];
  485. // [params setObject:[NSNumber numberWithInteger:offset] forKey:@"offset"];
  486. // [params setObject:[NSNumber numberWithInteger:detal] forKey:@"limit"];
  487. // [params setObject:displayFields forKey:@"columns"];
  488. //
  489. // NSDictionary *json = [RANetwork fetchResultParameters:params];
  490. // if (weakSelf) {
  491. // __strong typeof(weakSelf) strongSelf = weakSelf;
  492. //
  493. // int result = [[json objectForKey:@"result"] intValue];
  494. // if (result == RESULT_TRUE) {
  495. //
  496. // NSMutableArray *modelsArr = [NSMutableArray array];
  497. // if (option == ApexResultFetchDataTypeLoadMore && strongSelf.dataArray != nil && strongSelf.dataArray.count > 0) {
  498. // [modelsArr addObjectsFromArray:strongSelf.dataArray];
  499. // }
  500. //
  501. // NSArray *items = [json objectForKey:@"items"];
  502. // for (NSDictionary *item in items) {
  503. //
  504. // NSInteger type = [[item objectForKey:@"type"] integerValue];
  505. //
  506. // switch (type) {
  507. // case ApexResultTypeBooking: {
  508. // ApexResultBookingModel *model = [ApexResultBookingModel new];
  509. // [model setValuesForKeysWithDictionary:item];
  510. // [modelsArr addObject:model];
  511. // }
  512. // break;
  513. // case ApexResultTypeBLInfo: {
  514. // ApexResultBLInfoModel *model = [ApexResultBLInfoModel new];
  515. // [model setValuesForKeysWithDictionary:item];
  516. // [modelsArr addObject:model];
  517. // }
  518. // break;
  519. // case ApexResultTypeContainer: {
  520. // ApexResultContainerModel *model = [ApexResultContainerModel new];
  521. // [model setValuesForKeysWithDictionary:item];
  522. // [modelsArr addObject:model];
  523. // }
  524. // break;
  525. // case ApexResultTypeDocument: {
  526. // ApexResultDocumentModel *model = [ApexResultDocumentModel new];
  527. // [model setValuesForKeysWithDictionary:item];
  528. // [modelsArr addObject:model];
  529. // }
  530. // break;
  531. // default:
  532. // break;
  533. // }
  534. //
  535. // } // for
  536. // strongSelf.offset = modelsArr.count;
  537. //
  538. // NSString *title = [json objectForKey:@"title"];
  539. // strongSelf.rowActions = [json objectForKey:@"row_actions"];
  540. // strongSelf.actions = [json objectForKey:@"actions"];
  541. //
  542. // // menu
  543. // NSArray *menuArr = [json objectForKey:@"menu"];
  544. // if (menuArr) {
  545. // NSMutableArray *tmpArr = [NSMutableArray array];
  546. // for (NSDictionary *item in menuArr) {
  547. //
  548. // ApexResultMenuItem *menuItem = [ApexResultMenuItem new];
  549. // [menuItem setValuesForKeysWithDictionary:item];
  550. // [tmpArr addObject:menuItem];
  551. // }
  552. // strongSelf.menuItems = [tmpArr copy];
  553. // }
  554. //
  555. // // refresh UI
  556. // [strongSelf loadDataSuccessWithTitle:title itemsArray:modelsArr itemCount:items.count];
  557. //
  558. // } else {
  559. // [strongSelf loadDataFailed];
  560. // }
  561. // }
  562. //
  563. // });
  564. }
  565. - (void)loadData {
  566. if (self.dirty) {
  567. self.dirty = NO;
  568. }
  569. self.offset = 0;
  570. [self loadData:RAResultFetchDataTypeInitial];
  571. }
  572. - (void)refreshData {
  573. self.offset = 0;
  574. [self loadData: RAResultFetchDataTypeRefresh];
  575. }
  576. - (void)loadMoreData {
  577. [self loadData: RAResultFetchDataTypeLoadMore];
  578. }
  579. #pragma mark - Presenter
  580. - (void)onStartLoading {
  581. self.curHUD = [RAProgressHUD showHUDOnView:self.view];
  582. }
  583. - (void)onStopLoading {
  584. [self.curHUD dismiss];
  585. }
  586. - (void)onNoMoreData {
  587. [(JLRefreshFooter *)self.tableEnum.jl_footer noMoreData];
  588. }
  589. - (void)onSuccess:(NSString *)title {
  590. [self.curRefresh endRefresh];
  591. [self.tableEnum reloadData];
  592. if([self.cadedate[@"count"] intValue]==0)
  593. self.labelNoRecord.hidden=false;
  594. else
  595. self.labelNoRecord.hidden=true;
  596. self.title = title;
  597. }
  598. - (void)onFailed:(NSString *)errMsg {
  599. [self.curRefresh endRefresh];
  600. // [RAUtils message_alert:errMsg title:@"Warning" controller:self];
  601. [RAUtils ra_showAlertTitle:@"Warning" message:errMsg withViewController:self];
  602. }
  603. #pragma mark - Refresh Delegate
  604. - (void)jl_pullRefresh:(JLRefreshBasis *)refresh state:(JLRefreshState)state percentage:(float)percentage {
  605. }
  606. - (void)jl_beginRefresh:(JLRefreshBasis *)refresh {
  607. self.curRefresh = refresh;
  608. if (refresh == self.tableEnum.jl_header) {
  609. [self refreshData];
  610. [(JLRefreshFooter *)self.tableEnum.jl_footer enableLoadMore];
  611. } else if (refresh == self.tableEnum.jl_footer) {
  612. [self loadMoreData];
  613. }
  614. }
  615. - (void)jl_endRefresh:(JLRefreshBasis *)refresh {
  616. }
  617. @end