SavedDetailViewController.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // FavoritesViewController.m
  3. // Apex Mobile
  4. //
  5. // Created by Ray on 14-4-25.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "SavedDetailViewController.h"
  9. #import "RAConvertor.h"
  10. #import "RASingleton.h"
  11. #import "const.h"
  12. #import "DetailTabBarController.h"
  13. @interface SavedDetailViewController ()
  14. @end
  15. @implementation SavedDetailViewController
  16. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  17. {
  18. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  19. if (self) {
  20. // Custom initialization
  21. }
  22. return self;
  23. }
  24. - (BOOL)shouldAutorotate
  25. {
  26. return YES;
  27. }
  28. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  29. {
  30. return UIInterfaceOrientationMaskPortrait;
  31. }
  32. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  33. {
  34. return UIInterfaceOrientationPortrait;
  35. }
  36. - (void)viewDidLoad
  37. {
  38. [super viewDidLoad];
  39. NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  40. NSData* json_user =[NSData dataWithContentsOfFile:[docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@.json",@"saved_detail",RASingleton.sharedInstance.user]]];
  41. NSMutableArray* array;
  42. if(json_user==nil)
  43. {
  44. array = [NSMutableArray new];
  45. }
  46. else
  47. {
  48. array = [[RAConvertor data2arr:json_user] mutableCopy] ;
  49. }
  50. self.data =array;
  51. [self.tableview reloadData];
  52. self.tableview.tableFooterView = [UIView new];
  53. UIBarButtonItem *clearItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"bar_clear"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector(clearClick:)];
  54. clearItem.tintColor = UIColor.whiteColor;
  55. self.navigationItem.rightBarButtonItem = clearItem;
  56. }
  57. -(void)viewWillAppear:(BOOL)animated
  58. {
  59. }
  60. - (void)didReceiveMemoryWarning
  61. {
  62. [super didReceiveMemoryWarning];
  63. // Dispose of any resources that can be recreated.
  64. }
  65. - (void)clearClick:(id)sender {
  66. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle: @"Warning"
  67. message: @"Are you sure to clear the list?"
  68. preferredStyle: UIAlertControllerStyleAlert];
  69. UIAlertAction *action_ok = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  70. [self.data removeAllObjects];
  71. NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  72. NSData* json_user =[RAConvertor arr2data:self.data];
  73. bool result =[json_user writeToFile:[docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@.json",@"saved_detail",RASingleton.sharedInstance.user]] atomically:true];
  74. [self.tableview reloadData];
  75. }];
  76. UIAlertAction *action_cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  77. }];
  78. [alertVC addAction:action_ok];
  79. [alertVC addAction:action_cancel];
  80. [self presentViewController:alertVC animated:YES completion:nil];
  81. }
  82. /*
  83. #pragma mark - Navigation
  84. // In a storyboard-based application, you will often want to do a little preparation before navigation
  85. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  86. {
  87. // Get the new view controller using [segue destinationViewController].
  88. // Pass the selected object to the new view controller.
  89. }
  90. */
  91. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  92. {
  93. return 1;
  94. }
  95. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  96. {
  97. return self.data.count;
  98. }
  99. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  100. {
  101. DebugLog(@"cellForRowAtIndexPath");
  102. NSString *CellIdentifier = @"cell_item_history";
  103. CellItemHistory *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  104. NSString* name = [self.data[indexPath.row] valueForKey:@"name"];
  105. NSString* create_time = [self.data[indexPath.row] valueForKey:@"current_date"];
  106. NSString* company_name = [self.data[indexPath.row] valueForKey:@"company_name"];
  107. NSString *module_name = [self.data[indexPath.row] valueForKey:@"module"];
  108. // Cargo Tracking
  109. NSString* type =@"";
  110. if ([module_name isEqualToString:@"isf"]) {
  111. type=@"ISF";
  112. cell.icon.image = [[UIImage imageNamed:@"flag_us"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  113. } else if ([module_name isEqualToString:@"acem1"]) {
  114. type=@"ACE M1";
  115. cell.icon.image = [[UIImage imageNamed:@"flag_us"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  116. } else if ([module_name isEqualToString:@"aci"]) {
  117. type=@"ACI";
  118. cell.icon.image = [[UIImage imageNamed:@"flag_ca"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  119. } else if ([module_name isEqualToString:@"emanifest"]) {
  120. type=@"eManifest";
  121. cell.icon.image = [[UIImage imageNamed:@"flag_ca"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  122. }
  123. else if ([module_name isEqualToString:@"customer"]) {
  124. type=@"Customer";
  125. cell.icon.image = [[UIImage imageNamed:@"ic_customer"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  126. }
  127. cell.name.text = name;
  128. // [cell.name sizeToFit];
  129. cell.time.text =create_time;
  130. // [cell.time sizeToFit];
  131. cell.type.text =type;
  132. cell.company.text = company_name;
  133. // cell.imageView.image = [UIImage imageNamed:[self.toolsinfo[indexPath.row] valueForKey:@"img"]];
  134. return cell;
  135. }
  136. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  137. {
  138. // NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:self.params copyItems:true];
  139. NSMutableDictionary* params = [self.data[indexPath.row] valueForKey:@"param"];
  140. NSString * str_actions =[self.data[indexPath.row] valueForKey:@"actions"];
  141. NSArray* actions = [str_actions componentsSeparatedByString:@","];
  142. NSString* function_name = [self.data[indexPath.row] valueForKey:@"module"];
  143. DetailTabBarController *detailViewController=[[DetailTabBarController alloc] init:function_name actions:actions params:params];
  144. // DetailTabBarController *detailViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"DetailTabBarController"];
  145. // detailViewController.function_name = self.function_name;
  146. // detailViewController.actions = self.actions;
  147. // detailViewController.params =params;
  148. [self.navigationController pushViewController:detailViewController animated:YES];
  149. }
  150. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  151. return UITableViewCellEditingStyleDelete;
  152. //不能是UITableViewCellEditingStyleNone
  153. }
  154. - (void)tableView:(UITableView *)tableView
  155. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  156. [self.data removeObjectAtIndex:indexPath.row];
  157. NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  158. NSData* json_user =[RAConvertor arr2data:self.data];
  159. bool result =[json_user writeToFile:[docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@.json",@"saved_detail",RASingleton.sharedInstance.user]] atomically:true];
  160. // sqlite3* db = [ApexMobileDB get_db];
  161. // NSString* sql = [NSString stringWithFormat:@"delete from favorites where _id=%@",[self.data.pagedata[indexPath.row] valueForKey:@"_id"]];
  162. // [ApexMobileDB execSql:sql db:db];
  163. // sqlite3_close(db);
  164. //
  165. // [self.data.pagedata removeObjectAtIndex:indexPath.row];
  166. [tableView reloadData];
  167. }
  168. @end