EnumSelectOnlineViewController.m 29 KB

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