FavoritesViewController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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:@"ic_clear"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(clearClick:)];
  54. self.navigationItem.rightBarButtonItem = clearItem;
  55. }
  56. -(void)viewWillAppear:(BOOL)animated
  57. {
  58. }
  59. - (void)didReceiveMemoryWarning
  60. {
  61. [super didReceiveMemoryWarning];
  62. // Dispose of any resources that can be recreated.
  63. }
  64. - (void)clearClick:(id)sender {
  65. //
  66. //
  67. // NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  68. //
  69. //
  70. //
  71. // NSFileManager* fm = [NSFileManager defaultManager];
  72. // [fm removeItemAtPath:[docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@.json",@"saved_detail",RASingleton.sharedInstance.user]] error:nil];
  73. //
  74. [self.data removeAllObjects];
  75. NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  76. NSData* json_user =[RAConvertor arr2data:self.data];
  77. bool result =[json_user writeToFile:[docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@.json",@"saved_detail",RASingleton.sharedInstance.user]] atomically:true];
  78. [self.tableview reloadData];
  79. // __weak typeof(self) weakSelf = self;
  80. // [self.data deleteAll:^(BOOL success){
  81. // if (success) {
  82. // dispatch_async(dispatch_get_main_queue(), ^{
  83. // [weakSelf.tableview reloadData];
  84. // });
  85. // }
  86. // }];
  87. }
  88. /*
  89. #pragma mark - Navigation
  90. // In a storyboard-based application, you will often want to do a little preparation before navigation
  91. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  92. {
  93. // Get the new view controller using [segue destinationViewController].
  94. // Pass the selected object to the new view controller.
  95. }
  96. */
  97. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  98. {
  99. return 1;
  100. }
  101. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  102. {
  103. return self.data.count;
  104. }
  105. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  106. {
  107. DebugLog(@"cellForRowAtIndexPath");
  108. NSString *CellIdentifier = @"cell_item_history";
  109. CellItemHistory *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  110. NSString* name = [self.data[indexPath.row] valueForKey:@"name"];
  111. NSString* create_time = [self.data[indexPath.row] valueForKey:@"create_time"];
  112. NSString *module_name = [self.data[indexPath.row] valueForKey:@"module_name"];
  113. // Cargo Tracking
  114. if ([module_name isEqualToString:@"ifs"]) {
  115. cell.icon.image = [[UIImage imageNamed:@"mode_booking"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  116. } else if ([module_name isEqualToString:@"acem1"]) {
  117. cell.icon.image = [[UIImage imageNamed:@"mode_container"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  118. } else if ([module_name isEqualToString:@"aci"]) {
  119. cell.icon.image = [[UIImage imageNamed:@"mode_bl"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  120. } else if ([module_name isEqualToString:@"emanifest"]) {
  121. cell.icon.image = [[UIImage imageNamed:@"mode_cargo_tracking"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  122. }
  123. cell.name.text = name;
  124. [cell.name sizeToFit];
  125. cell.time.text =create_time;
  126. [cell.time sizeToFit];
  127. // cell.imageView.image = [UIImage imageNamed:[self.toolsinfo[indexPath.row] valueForKey:@"img"]];
  128. return cell;
  129. }
  130. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  131. {
  132. // NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:self.params copyItems:true];
  133. NSMutableDictionary* params = [self.data[indexPath.row] valueForKey:@"params"];
  134. NSString * str_actions =[self.data[indexPath.row] valueForKey:@"action"];
  135. NSArray* actions = [str_actions componentsSeparatedByString:@","];
  136. NSString* function_name = [self.data[indexPath.row] valueForKey:@"module_name"];
  137. DetailTabBarController *detailViewController=[[DetailTabBarController alloc] init:function_name actions:actions params:params];
  138. // DetailTabBarController *detailViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"DetailTabBarController"];
  139. // detailViewController.function_name = self.function_name;
  140. // detailViewController.actions = self.actions;
  141. // detailViewController.params =params;
  142. [self.navigationController pushViewController:detailViewController animated:YES];
  143. }
  144. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  145. return UITableViewCellEditingStyleDelete;
  146. //不能是UITableViewCellEditingStyleNone
  147. }
  148. - (void)tableView:(UITableView *)tableView
  149. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  150. [self.data removeObjectAtIndex:indexPath.row];
  151. NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  152. NSData* json_user =[RAConvertor arr2data:self.data];
  153. bool result =[json_user writeToFile:[docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@.json",@"saved_detail",RASingleton.sharedInstance.user]] atomically:true];
  154. // sqlite3* db = [ApexMobileDB get_db];
  155. // NSString* sql = [NSString stringWithFormat:@"delete from favorites where _id=%@",[self.data.pagedata[indexPath.row] valueForKey:@"_id"]];
  156. // [ApexMobileDB execSql:sql db:db];
  157. // sqlite3_close(db);
  158. //
  159. // [self.data.pagedata removeObjectAtIndex:indexPath.row];
  160. [tableView reloadData];
  161. }
  162. @end