EnumSelectOnlineViewController.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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);
  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. cell.img_checkmark.hidden=false;
  273. else
  274. cell.img_checkmark.hidden=true;
  275. return cell;
  276. }
  277. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  278. {
  279. EnumSelectorCell* cell = (EnumSelectorCell*)[tableView cellForRowAtIndexPath:indexPath];
  280. cell.selected = false;
  281. self.dirty = true;
  282. if(self.single_select)
  283. {
  284. if(self.keywords==nil || self.keywords.length==0)
  285. {
  286. NSMutableDictionary* val_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%ld",indexPath.row]] mutableCopy];
  287. [val_json setValue:@true forKey:@"check"];
  288. self.online_cadedate = [NSMutableDictionary new];
  289. self.online_cadedate[@"count"]=@1;
  290. self.online_cadedate[@"val_0"]=val_json;
  291. // for(int i=0;i<[[self.cadedate valueForKey:@"count"] intValue];i++)
  292. // {
  293. //
  294. //
  295. //
  296. //
  297. // NSMutableDictionary* val_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i]] mutableCopy];
  298. // if(i==indexPath.row)
  299. // [val_json setValue:@"1" forKey:@"check"];
  300. // else
  301. // [val_json setValue:@"0" forKey:@"check"];
  302. // [self.cadedate setObject:val_json forKey:[NSString stringWithFormat:@"val_%d",i]];
  303. // }
  304. }
  305. else
  306. {
  307. NSAssert(true, @"NOT IMPL");
  308. NSMutableDictionary* val_json =nil;
  309. int count = 0;
  310. for (int cc=0; cc<[[self.cadedate valueForKey:@"count"] intValue]; cc++)
  311. {
  312. if(count>= self.filter_count)
  313. break;
  314. NSMutableDictionary* search_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",cc ]] mutableCopy];
  315. NSString* value = [search_json valueForKey:@"value"];
  316. NSUInteger location=[[value uppercaseString] rangeOfString:self.keywords].location;
  317. if(location!= NSNotFound)
  318. count++;
  319. if(count-1==indexPath.row)
  320. val_json=search_json;
  321. }
  322. for (int sc=0;sc<[[self.cadedate valueForKey:@"count"] intValue];sc++)
  323. {
  324. NSMutableDictionary* sc_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",sc ]] mutableCopy];
  325. if ([[sc_json valueForKey:@"value" ] isEqual:[val_json valueForKey:@"value"]] )
  326. [sc_json setValue:@"1" forKey:@"check"];
  327. else
  328. [sc_json setValue:@"0" forKey:@"check"];
  329. [self.cadedate setObject:sc_json forKey:[NSString stringWithFormat:@"val_%d",sc]];
  330. }
  331. }
  332. if(self.auto_close)
  333. {
  334. if(self.navigationController!=nil)
  335. [self.navigationController popViewControllerAnimated:YES];
  336. // [self.navigationController dismissViewControllerAnimated:YES
  337. // completion:^{
  338. //
  339. //
  340. // }];
  341. else
  342. [self dismissViewControllerAnimated:YES
  343. completion:^{
  344. }];
  345. }
  346. }
  347. else
  348. {
  349. NSMutableDictionary* val_json =nil;
  350. if(self.keywords==nil || self.keywords.length==0)
  351. {
  352. val_json=[[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row ]] mutableCopy];
  353. }
  354. else
  355. {
  356. int count = 0;
  357. for (int i=0; i<[[self.cadedate valueForKey:@"count"] intValue]; i++)
  358. {
  359. if(count>= self.filter_count)
  360. break;
  361. NSMutableDictionary* search_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i ]] mutableCopy];
  362. NSString* value = [search_json valueForKey:@"value"];
  363. NSUInteger location=[[value uppercaseString] rangeOfString:self.keywords].location;
  364. if(location!= NSNotFound)
  365. count++;
  366. if(count-1==indexPath.row)
  367. val_json=search_json;
  368. }
  369. }
  370. // NSMutableDictionary* val_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row]] mutableCopy];
  371. int check = [[val_json valueForKey:@"check"] intValue];
  372. if(check==1)
  373. [val_json setValue:@"0" forKey:@"check"];
  374. else
  375. {
  376. if([self check_count]>=self.max_select && self.max_select>0)
  377. {
  378. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: message:[NSString stringWithFormat: delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  379. // // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
  380. // [alert show];
  381. [RAUtils message_alert:[NSString stringWithFormat:@"%d items at most for this field.",self.max_select ] title:@"Max count reached" controller:self];
  382. // [RAUtils alert_view:[NSString stringWithFormat:@"%d items at most for this field.",self.max_select ] title:@"Max count reached"];
  383. return;
  384. }
  385. [val_json setValue:@"1" forKey:@"check"];
  386. }
  387. [self.cadedate setObject:val_json forKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row]];
  388. }
  389. [self.tableEnum reloadData];
  390. }
  391. -(int) check_count
  392. {
  393. int check_count=0;
  394. // int count = 0;
  395. for (int i=0; i<[[self.cadedate valueForKey:@"count"] intValue]; i++)
  396. {
  397. NSMutableDictionary* val_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i ]] mutableCopy];
  398. int check = [[val_json valueForKey:@"check"] intValue];
  399. if(check==1)
  400. check_count++;
  401. }
  402. return check_count;
  403. }
  404. #pragma mark - searchBar delegate;
  405. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  406. {
  407. self.online_keywords=[searchBar.text uppercaseString];
  408. [self loadData];
  409. // [self.tableEnum reloadData];
  410. // self.offset = 0;
  411. // [self.content_data removeAllObjects];
  412. // [self loadpage];
  413. // DebugLog(@"search");
  414. }
  415. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  416. {
  417. if([searchText isEqualToString:@""] && !self.reset)
  418. {
  419. self.keywords=nil;
  420. [self.tableEnum reloadData];
  421. self.reset=true;
  422. }
  423. else
  424. self.reset = false;
  425. }
  426. /*
  427. #pragma mark - Navigation
  428. // In a storyboard-based application, you will often want to do a little preparation before navigation
  429. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  430. {
  431. // Get the new view controller using [segue destinationViewController].
  432. // Pass the selected object to the new view controller.
  433. }
  434. */
  435. - (void)loadData:(RAResultFetchDataType)option {
  436. [self onStartLoading];
  437. __weak typeof(self) weakSelf = self;
  438. NSMutableDictionary *params = [self.online_params mutableCopy];
  439. if(params==nil)
  440. params=[NSMutableDictionary new];
  441. [params setObject:[NSNumber numberWithInteger:self.offset] forKey:@"offset"];
  442. [params setObject:[NSNumber numberWithInteger:self.limit] forKey:@"limit"];
  443. if(self.online_keywords.length>0)
  444. params[@"keywords"]=self.online_keywords;
  445. [RADataProvider GetCadedate:params completionHandler:^(NSMutableDictionary *result) {
  446. NSMutableDictionary * json = result;
  447. __strong typeof(weakSelf) strongSelf = weakSelf;
  448. int resultcode = [[json objectForKey:@"result"] intValue];
  449. if (resultcode == RESULT_TRUE) {
  450. // NSMutableArray *modelsArr = [NSMutableArray array];
  451. // if (option == RAResultFetchDataTypeLoadMore && strongSelf.dataArray != nil && strongSelf.dataArray.count > 0) {
  452. // [modelsArr addObjectsFromArray:strongSelf.dataArray];
  453. // }
  454. if (option != RAResultFetchDataTypeLoadMore) {
  455. self.cadedate = [NSMutableDictionary new];
  456. }
  457. int count = [json[@"count"] intValue];
  458. for (int i=0;i<count;i++)
  459. {
  460. NSMutableDictionary* item = [json[[NSString stringWithFormat:@"val_%d",i]] mutableCopy];
  461. self.cadedate[[NSString stringWithFormat:@"val_%d",i]]=item;
  462. }
  463. self.cadedate[@"count"]= @([self.cadedate[@"count"] intValue]+count);
  464. strongSelf.offset = [self.cadedate[@"count"] intValue];
  465. NSString *title = [json objectForKey:@"title"];
  466. // refresh UI
  467. dispatch_async(dispatch_get_main_queue(), ^{
  468. if (weakSelf) {
  469. __strong typeof(weakSelf) strongSelf = weakSelf;
  470. // strongSelf.dataArray = [modelsArr copy];
  471. [strongSelf onStopLoading];
  472. [strongSelf onSuccess:title];
  473. if (count < self.limit) {
  474. [strongSelf onNoMoreData];
  475. }
  476. }
  477. });
  478. } else {
  479. NSString *msg = [json objectForKey:@"err_msg"];
  480. if(msg.length==0)
  481. {
  482. msg=@"Sorry, can not connect to the server, please try again later.";
  483. }
  484. __weak typeof(self) weakSelf = self;
  485. dispatch_async(dispatch_get_main_queue(), ^{
  486. if (weakSelf) {
  487. __strong typeof(weakSelf) strongSelf = weakSelf;
  488. [strongSelf onStopLoading];
  489. [strongSelf onFailed:msg];
  490. }
  491. });
  492. }
  493. }];
  494. return;
  495. //
  496. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  497. //
  498. // NSMutableDictionary *params = [weakSelf.params mutableCopy];
  499. // [params setObject:[NSNumber numberWithInteger:offset] forKey:@"offset"];
  500. // [params setObject:[NSNumber numberWithInteger:detal] forKey:@"limit"];
  501. // [params setObject:displayFields forKey:@"columns"];
  502. //
  503. // NSDictionary *json = [RANetwork fetchResultParameters:params];
  504. // if (weakSelf) {
  505. // __strong typeof(weakSelf) strongSelf = weakSelf;
  506. //
  507. // int result = [[json objectForKey:@"result"] intValue];
  508. // if (result == RESULT_TRUE) {
  509. //
  510. // NSMutableArray *modelsArr = [NSMutableArray array];
  511. // if (option == ApexResultFetchDataTypeLoadMore && strongSelf.dataArray != nil && strongSelf.dataArray.count > 0) {
  512. // [modelsArr addObjectsFromArray:strongSelf.dataArray];
  513. // }
  514. //
  515. // NSArray *items = [json objectForKey:@"items"];
  516. // for (NSDictionary *item in items) {
  517. //
  518. // NSInteger type = [[item objectForKey:@"type"] integerValue];
  519. //
  520. // switch (type) {
  521. // case ApexResultTypeBooking: {
  522. // ApexResultBookingModel *model = [ApexResultBookingModel new];
  523. // [model setValuesForKeysWithDictionary:item];
  524. // [modelsArr addObject:model];
  525. // }
  526. // break;
  527. // case ApexResultTypeBLInfo: {
  528. // ApexResultBLInfoModel *model = [ApexResultBLInfoModel new];
  529. // [model setValuesForKeysWithDictionary:item];
  530. // [modelsArr addObject:model];
  531. // }
  532. // break;
  533. // case ApexResultTypeContainer: {
  534. // ApexResultContainerModel *model = [ApexResultContainerModel new];
  535. // [model setValuesForKeysWithDictionary:item];
  536. // [modelsArr addObject:model];
  537. // }
  538. // break;
  539. // case ApexResultTypeDocument: {
  540. // ApexResultDocumentModel *model = [ApexResultDocumentModel new];
  541. // [model setValuesForKeysWithDictionary:item];
  542. // [modelsArr addObject:model];
  543. // }
  544. // break;
  545. // default:
  546. // break;
  547. // }
  548. //
  549. // } // for
  550. // strongSelf.offset = modelsArr.count;
  551. //
  552. // NSString *title = [json objectForKey:@"title"];
  553. // strongSelf.rowActions = [json objectForKey:@"row_actions"];
  554. // strongSelf.actions = [json objectForKey:@"actions"];
  555. //
  556. // // menu
  557. // NSArray *menuArr = [json objectForKey:@"menu"];
  558. // if (menuArr) {
  559. // NSMutableArray *tmpArr = [NSMutableArray array];
  560. // for (NSDictionary *item in menuArr) {
  561. //
  562. // ApexResultMenuItem *menuItem = [ApexResultMenuItem new];
  563. // [menuItem setValuesForKeysWithDictionary:item];
  564. // [tmpArr addObject:menuItem];
  565. // }
  566. // strongSelf.menuItems = [tmpArr copy];
  567. // }
  568. //
  569. // // refresh UI
  570. // [strongSelf loadDataSuccessWithTitle:title itemsArray:modelsArr itemCount:items.count];
  571. //
  572. // } else {
  573. // [strongSelf loadDataFailed];
  574. // }
  575. // }
  576. //
  577. // });
  578. }
  579. - (void)loadData {
  580. if (self.dirty) {
  581. self.dirty = NO;
  582. }
  583. self.offset = 0;
  584. [self loadData:RAResultFetchDataTypeInitial];
  585. }
  586. - (void)refreshData {
  587. self.offset = 0;
  588. [self loadData: RAResultFetchDataTypeRefresh];
  589. }
  590. - (void)loadMoreData {
  591. [self loadData: RAResultFetchDataTypeLoadMore];
  592. }
  593. #pragma mark - Presenter
  594. - (void)onStartLoading {
  595. self.curHUD = [RAProgressHUD showHUDOnView:self.view];
  596. }
  597. - (void)onStopLoading {
  598. [self.curHUD dismiss];
  599. }
  600. - (void)onNoMoreData {
  601. [(JLRefreshFooter *)self.tableEnum.jl_footer noMoreData];
  602. }
  603. - (void)onSuccess:(NSString *)title {
  604. [self.curRefresh endRefresh];
  605. [self.tableEnum reloadData];
  606. if([self.cadedate[@"count"] intValue]==0)
  607. self.labelNoRecord.hidden=false;
  608. else
  609. self.labelNoRecord.hidden=true;
  610. self.title = title;
  611. }
  612. - (void)onFailed:(NSString *)errMsg {
  613. [self.curRefresh endRefresh];
  614. // [RAUtils message_alert:errMsg title:@"Warning" controller:self];
  615. [RAUtils ra_showAlertTitle:@"Warning" message:errMsg withViewController:self];
  616. }
  617. #pragma mark - Refresh Delegate
  618. - (void)jl_pullRefresh:(JLRefreshBasis *)refresh state:(JLRefreshState)state percentage:(float)percentage {
  619. }
  620. - (void)jl_beginRefresh:(JLRefreshBasis *)refresh {
  621. self.curRefresh = refresh;
  622. if (refresh == self.tableEnum.jl_header) {
  623. [self refreshData];
  624. [(JLRefreshFooter *)self.tableEnum.jl_footer enableLoadMore];
  625. } else if (refresh == self.tableEnum.jl_footer) {
  626. [self loadMoreData];
  627. }
  628. }
  629. - (void)jl_endRefresh:(JLRefreshBasis *)refresh {
  630. }
  631. @end