EnumSelectOnlineViewController.m 29 KB

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