GridResultViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. //
  2. // ResultViewController.m
  3. // Apex Mobile
  4. //
  5. // Created by Ray on 14-3-4.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "GridResultViewController.h"
  9. #import "TabBarController.h"
  10. @interface GridResultViewController ()
  11. -(void) getHeader: (NSMutableArray*)header_name header_aname:(NSMutableArray*)header_aname;
  12. -(void) loadpage;
  13. -(NSArray*) getActions;
  14. @end
  15. @implementation GridResultViewController
  16. -(void) loadpage
  17. {
  18. [self.params setValue:[NSString stringWithFormat:@"%ld",self.offset] forKey:@"offset"];
  19. self.mum.center = self.view.center;
  20. NSTimeInterval animationDuration = 0.30f;
  21. [UIView beginAnimations:@"ResizeView" context:nil];
  22. [UIView setAnimationDuration:animationDuration];
  23. self.prepageButton.enabled=false;
  24. self.nextpageButton.enabled=false;
  25. self.mum.hidden = false;
  26. self.grid.hidden = true;
  27. [UIView commitAnimations];
  28. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  29. // PageData* pageData=nil;
  30. if(self.recordCount<0)
  31. self.recordCount=[RANetwork get_recordcount: self.params ];
  32. if(self.recordCount>0)
  33. {
  34. self.pageData=[RANetwork get_pagedata:self.params];
  35. // self.pageData=[RANetwork]
  36. }
  37. dispatch_async(dispatch_get_main_queue(), ^{
  38. NSTimeInterval animationDuration = 0.30f;
  39. [UIView beginAnimations:@"ResizeView" context:nil];
  40. [UIView setAnimationDuration:animationDuration];
  41. if(self.recordCount>=0)
  42. {
  43. if(self.recordCount<2000)
  44. self.countView.title = [NSString stringWithFormat:@"%ld records",self.recordCount];
  45. else
  46. self.countView.title = @"2000+ records";
  47. }
  48. else
  49. {
  50. self.countView.title = @"Net error!";
  51. }
  52. if(self.offset<self.pageData.get_count||self.offset==0)
  53. self.prepageButton.enabled=false;
  54. else
  55. self.prepageButton.enabled=true;
  56. if(self.offset+self.pageData.get_count>=self.recordCount)
  57. self.nextpageButton.enabled=false;
  58. else
  59. self.nextpageButton.enabled=true;
  60. [self.grid initgrid :self.pageData];
  61. // CGPoint p = self.grid.frame.origin;
  62. self.mum.hidden = true;
  63. self.grid.hidden = false;
  64. [UIView commitAnimations];
  65. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  66. bool autologin =[defaults boolForKey:@"autologin"];
  67. if(!autologin)
  68. {
  69. TabBarController * viewController =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginTabBar"];
  70. [self presentViewController:viewController animated:YES completion:^{
  71. }];
  72. }
  73. });
  74. });
  75. }
  76. - (void) showDetail:(UIMenuItem*) sender
  77. {
  78. DebugLog(@"show detail id = %@",self.detail_id);
  79. NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:self.params copyItems:true];
  80. [params setValue:self.detail_id forKey:@"id"];
  81. DetailTabBarController *detailViewController=[[DetailTabBarController alloc] init:self.function_name actions:self.actions params:params];
  82. // DetailTabBarController *detailViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"DetailTabBarController"];
  83. // detailViewController.function_name = self.function_name;
  84. // detailViewController.actions = self.actions;
  85. // detailViewController.params =params;
  86. [self.navigationController pushViewController:detailViewController animated:YES];
  87. }
  88. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
  89. {
  90. if (action == @selector(showDetail:))
  91. return YES;//显示
  92. else
  93. return NO;//不显示
  94. }
  95. -(void)menuItemSelected:(NSIndexPath *)indexPath
  96. {
  97. NSLog(@"%ld",indexPath.item);
  98. [pulldownMenu animateDropDown];
  99. if(indexPath.item==0)
  100. {
  101. CustomizeFieldViewController *ViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CustomizeFieldViewController"];
  102. ViewController.function_name = self.function_name;
  103. ViewController.behavior =BEHAVIOR_RESULT;
  104. self.dirty=true;
  105. // ViewController.dirty = self.dirty;
  106. [self.navigationController pushViewController:ViewController animated:YES];
  107. }
  108. else
  109. {
  110. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"alert_title_create_tag", nil) message:NSLocalizedString(@"alert_msg_savehint", nil) preferredStyle:UIAlertControllerStyleAlert];
  111. __block UITextField *inputTextField;
  112. [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  113. inputTextField = textField;
  114. }];
  115. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  116. }];
  117. UIAlertAction *saveAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"save", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  118. UITextField * txt= inputTextField;
  119. NSString* name = txt.text;
  120. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self.params
  121. options:0
  122. error:nil];
  123. NSString* param = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  124. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  125. NSString* user = appDelegate.user;
  126. sqlite3 *db =[ApexMobileDB get_db];
  127. [ApexMobileDB execSql: [NSString stringWithFormat: @"insert into history(params,name,module_name,user) values('%@','%@','%@','%@')",param,name,self.function_name,user] db:db];
  128. sqlite3_close(db);
  129. }];
  130. [alertVC addAction:cancelAction];
  131. [alertVC addAction:saveAction];
  132. [self presentViewController:alertVC animated:YES completion:nil];
  133. }
  134. }
  135. -(void)pullDownAnimated:(BOOL)open
  136. {
  137. if (open)
  138. {
  139. NSLog(@"Pull down menu open!");
  140. }
  141. else
  142. {
  143. NSLog(@"Pull down menu closed!");
  144. }
  145. }
  146. - (IBAction)onCustomize:(UIBarButtonItem *)sender {
  147. // CGRect ff = self.navigationController.navigationBar.frame;
  148. // : CGPointMake(0.0f,self.navigationController.navigationBar.frame.size.height)
  149. [pulldownMenu animateDropDown:CGPointMake(0.0f,self.navigationController.navigationBar.frame.size.height)];
  150. // CustomizeFieldViewController *ViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CustomizeFieldViewController"];
  151. // ViewController.function_name = self.function_name;
  152. // ViewController.behavior =BEHAVIOR_RESULT;
  153. // self.dirty=true;
  154. // // ViewController.dirty = self.dirty;
  155. //
  156. // [self.navigationController pushViewController:ViewController animated:YES];
  157. }
  158. - (BOOL)canBecomeFirstResponder{
  159. return YES;
  160. }
  161. - (void) Tap:(UIView*)view url:(NSString*) url
  162. {
  163. WebViewController *ViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"WebViewController"];
  164. ViewController.url = url;
  165. // ViewController. = self.function_name;
  166. // ViewController.behavior =BEHAVIOR_SEARCH;
  167. [self.navigationController pushViewController:ViewController animated:YES];
  168. }
  169. - (void) LongPress:(UIView*)view _id:(NSString*) _id
  170. {
  171. DebugLog(@"!!!!!!!!!!!!!!!!!");
  172. self.actions = [self getActions];
  173. if(self.actions.count<1)
  174. return;
  175. self.detail_id = _id;
  176. UIMenuItem *detail = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"menuitem_showdetail", nil) action:@selector(showDetail:)];
  177. // [detail setValue:_id forKey:@"_id"];
  178. UIMenuController *menu = [UIMenuController sharedMenuController];
  179. [menu setMenuItems:[NSArray arrayWithObjects:detail, nil]];
  180. [menu setTargetRect:view.frame inView:view.superview];
  181. [menu setMenuVisible:YES animated:YES];
  182. }
  183. - (IBAction)onPrePageClick:(UIButton *)sender {
  184. self.offset -=self.prepageCount;
  185. [self loadpage];
  186. }
  187. - (IBAction)onNextPageClick:(UIButton *)sender {
  188. self.offset +=self.pageData.get_count;
  189. self.prepageCount=self.pageData.get_count;
  190. [self loadpage];
  191. }
  192. -(NSArray*) getActions
  193. {
  194. DebugLog(@"getActions");
  195. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  196. NSMutableArray* actions = [[NSMutableArray alloc]init];
  197. sqlite3 *db =[ApexMobileDB get_db];
  198. NSString *quary = [NSString stringWithFormat:@"select name from actions_info where function_name='%@' and user='%@'order by priority",self.function_name, appDelegate.user];
  199. sqlite3_stmt *stmt;
  200. if (sqlite3_prepare_v2(db, [quary UTF8String], -1, &stmt, nil) == SQLITE_OK) {
  201. DebugLog(@"sql:%@",quary);
  202. DebugLog(@"=======================show tabs===================");
  203. while (sqlite3_step(stmt)==SQLITE_ROW)
  204. {
  205. NSString *name = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(stmt, 0)];
  206. [actions addObject:name];
  207. DebugLog(@"action name: %@",name);
  208. }
  209. DebugLog(@"=======================show tabs===================");
  210. sqlite3_finalize(stmt);
  211. }
  212. //用完了一定记得关闭,释放内存
  213. sqlite3_close(db);
  214. return actions;
  215. }
  216. -(void) getHeader: (NSMutableArray*)header_name header_aname:(NSMutableArray*)header_aname
  217. {
  218. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  219. // [header setValue:@"aaa" forKey:@"bbb"];
  220. DebugLog(@"initHeader");
  221. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  222. sqlite3 *db =[ApexMobileDB get_db];
  223. NSString *quary = [NSString stringWithFormat:@"select aname,name from fields_info where function_name='%@' and user='%@' and behavior=%d and show=1 order by priority,aname",self.function_name, appDelegate.user,BEHAVIOR_RESULT];
  224. sqlite3_stmt *stmt;
  225. if (sqlite3_prepare_v2(db, [quary UTF8String], -1, &stmt, nil) == SQLITE_OK) {
  226. DebugLog(@"sql:%@",quary);
  227. DebugLog(@"=======================show fields===================");
  228. while (sqlite3_step(stmt)==SQLITE_ROW)
  229. {
  230. NSString *aname = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(stmt, 0)];
  231. NSString *name = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(stmt, 1)];
  232. // [header setValue:aname forKey:name];
  233. [header_name addObject:name];
  234. [header_aname addObject:aname];
  235. DebugLog(@"%@",aname);
  236. }
  237. DebugLog(@"=======================show fields===================");
  238. sqlite3_finalize(stmt);
  239. }
  240. //用完了一定记得关闭,释放内存
  241. sqlite3_close(db);
  242. // return header;
  243. }
  244. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  245. {
  246. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  247. if (self) {
  248. // self.pageData=[[PageData alloc] init];
  249. }
  250. return self;
  251. }
  252. //- (BOOL)shouldAutorotate
  253. //{
  254. // return YES;
  255. //}
  256. //
  257. //- (NSUInteger)supportedInterfaceOrientations
  258. //{
  259. // return UIInterfaceOrientationMaskLandscapeRight;
  260. //}
  261. //
  262. //- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  263. //{
  264. // return UIInterfaceOrientationLandscapeRight;
  265. //}
  266. - (void)viewDidLoad
  267. {
  268. [super viewDidLoad];
  269. NSString* title=@"";
  270. if([self.function_name isEqualToString:@"Ocean Booking"])
  271. title = @"Booking Result";
  272. else if([self.function_name isEqualToString:@"Ocean B/L info."])
  273. title = @"B/L info. Result";
  274. else if([self.function_name isEqualToString:@"Container detail"])
  275. title = @"Container Result";
  276. else if([self.function_name isEqualToString:@"Download Document"])
  277. title = @"Document Result";
  278. self.navigationItem.title = title;
  279. // UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
  280. // backButton.title = @"Back";
  281. //
  282. // self.navigationItem.backBarButtonItem = backButton;
  283. // self.navigationController.view.frame.size.height;
  284. // NSMutableArray * items = [[NSMutableArray alloc]init];
  285. // UIBarButtonItem *item1 = [[UIBarButtonItem alloc] init];
  286. // item1.title = @"item1";
  287. // item1.image = [UIImage imageNamed:@"rect_setting"];
  288. // UIBarButtonItem *item2 = [[UIBarButtonItem alloc] init];
  289. // item2.image = [UIImage imageNamed:@"rect_about"];
  290. // item2.title = @"item2";
  291. // [items addObject:item1];
  292. // [items addObject:item2];
  293. // self.navigationItem.rightBarButtonItems=items;
  294. // self.navigationItem.backBarButtonItem = backButton;
  295. self.dirty = @"true";
  296. if([self.function_name isEqualToString:@"Download Document"])
  297. self.fullrow_select =false;
  298. else
  299. self.fullrow_select=true;
  300. pulldownMenu = [[PulldownMenu alloc] initWithView:self.view];
  301. [self.view addSubview:pulldownMenu];
  302. [pulldownMenu insertButton:NSLocalizedString(@"menuitem_setting", nil)];
  303. [pulldownMenu insertButton:NSLocalizedString(@"menuitem_savesearch", nil)];
  304. pulldownMenu.delegate = self;
  305. // self.navigationItem.rightBarButtonItems
  306. [pulldownMenu loadMenu];
  307. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  308. ref.tag = 200 ;
  309. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"drag to refresh"];
  310. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  311. // ref.hidden = true;
  312. [ref addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
  313. [self.grid addSubview:ref];
  314. }
  315. -(void)changeCell
  316. {
  317. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  318. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
  319. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  320. DebugLog(@"refresh!!!!!!!!");
  321. }
  322. -(void)ReloadData
  323. {
  324. // Count ++ ;
  325. // [mytabelview reloadData];
  326. [self loadpage];
  327. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  328. [reF endRefreshing];
  329. }
  330. - (void)viewWillAppear:(BOOL)animated
  331. {
  332. DebugLog(@"viewWillAppear");
  333. if(self.dirty ==true)
  334. {
  335. self.prepageCount=0;
  336. self.recordCount = -1;
  337. self.offset = 0;
  338. NSMutableArray* header_name =[[NSMutableArray alloc]init];
  339. NSMutableArray* header_aname =[[NSMutableArray alloc]init];
  340. [self getHeader:header_name header_aname:header_aname];
  341. // self.header = [self getHeader:header_name];
  342. // self.grid.header = self.header;
  343. self.grid.fullrowselect = self.fullrow_select;
  344. self.grid.griddelegate = self;
  345. self.grid.header_name=header_name;
  346. self.grid.header_aname=header_aname;
  347. [self.params setValue:self.function_name forKey:@"module_name"];
  348. NSString* fields =@"";
  349. for(int i=0;i<header_name.count;i++)
  350. {
  351. fields=[fields stringByAppendingFormat:@"%@,",header_name[i]];
  352. }
  353. fields=[fields substringToIndex: fields.length-1];
  354. [self.params setValue:fields forKey:@"columns"];
  355. [self loadpage];
  356. self.dirty = false;
  357. // [pulldownMenu loadMenu:self.grid.frame.origin];
  358. }
  359. }
  360. //-(NSUInteger)supportedInterfaceOrientations
  361. //{
  362. // DebugLog(@"myViewController::supportedInterfaceOrientations");
  363. // return UIInterfaceOrientationMaskLandscape;
  364. // // return [[self topViewController] supportedInterfaceOrientations];
  365. //}
  366. //- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  367. //{
  368. // DebugLog(@"myViewController::preferredInterfaceOrientationForPresentation");
  369. // return UIInterfaceOrientationLandscapeRight;
  370. //}
  371. //-(BOOL)shouldAutorotate
  372. //{
  373. // DebugLog(@"myViewController::shouldAutorotate");
  374. // return YES;
  375. //}
  376. //
  377. //- (void)viewWillAppear:(BOOL)animated
  378. //{
  379. //// [self presentViewController:[[UIViewController alloc]init] animated:NO completion:^
  380. //// {
  381. //// DebugLog(@"dismiss viewcontroller");
  382. //// [self dismissViewControllerAnimated:NO completion:nil];
  383. //// }];
  384. //
  385. //
  386. //// [self presentViewController:nil animated:NO completion:nil];
  387. //
  388. // // [self presentViewController];
  389. //
  390. //}
  391. // - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  392. // {
  393. // return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
  394. // }
  395. // - (NSUInteger)supportedInterfaceOrientations
  396. // {
  397. // // return UIInterfaceOrientationMaskLandscapeLeft;
  398. // return UIInterfaceOrientationMaskLandscapeRight;
  399. // }
  400. // - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  401. // return UIInterfaceOrientationLandscapeRight;
  402. // // return UIInterfaceOrientationPortrait;
  403. // }
  404. - (void)didReceiveMemoryWarning
  405. {
  406. [super didReceiveMemoryWarning];
  407. // Dispose of any resources that can be recreated.
  408. }
  409. @end