CacheViewController.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // testTableViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-8-5.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "CacheViewController.h"
  9. #import "RAUtils.h"
  10. #import "AppDelegate.h"
  11. #define AL_CACHE 1234
  12. #define AL_CLEAN 1235
  13. @interface CacheViewController ()
  14. @end
  15. @implementation CacheViewController
  16. - (id)initWithStyle:(UITableViewStyle)style
  17. {
  18. self = [super initWithStyle:style];
  19. if (self) {
  20. // Custom initialization
  21. }
  22. return self;
  23. }
  24. - (void)onCloseClick:(id)sender {
  25. [self dismissViewControllerAnimated:true completion:nil];
  26. }
  27. - (void)viewDidLoad
  28. {
  29. [super viewDidLoad];
  30. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  31. //
  32. //
  33. if( appDelegate.bEnable_Cache)
  34. {
  35. self.catchSwitch.on=true;
  36. self.sizeLabel.text = @"Calculating";
  37. // 创建
  38. NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(calculate) object:nil];
  39. // 启动
  40. [thread start];
  41. }
  42. else
  43. {
  44. self.catchSwitch.on=false;
  45. self.sizeLabel.text = @"";
  46. [self.mum stopAnimating];
  47. }
  48. UIBarButtonItem* closeBTN = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  49. style:UIBarButtonItemStylePlain
  50. target:self
  51. action:@selector(onCloseClick:)];
  52. self.navigationItem.rightBarButtonItem = closeBTN;
  53. // Uncomment the following line to preserve selection between presentations.
  54. // self.clearsSelectionOnViewWillAppear = NO;
  55. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  56. // self.navigationItem.rightBarButtonItem = self.editButtonItem;
  57. }
  58. - (void)calculate
  59. {
  60. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  61. NSString *cachefolder = [paths objectAtIndex:0];
  62. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  63. // DebugLog(@"%@",cachefolder);
  64. // DebugLog(@"%@",img_cache);
  65. float size=[RAUtils fileSizeForDir:img_cache];
  66. // float size1=[RAUtils fileSizeForDir:img_cache];
  67. self.sizeLabel.text = [NSString stringWithFormat:@" %.2f MB",size];
  68. [self.mum stopAnimating];
  69. }
  70. - (void)clear
  71. {
  72. UIAlertController * alert = [RAUtils waiting_alert:self message:@"Deleting..." title:nil];
  73. // UIAlertView * alert=[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Deleting", nil) message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
  74. // alert.tag = AL_CACHE;
  75. // [alert show];
  76. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  77. NSString *cachefolder = [paths objectAtIndex:0];
  78. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  79. [RAUtils deletefiles:img_cache];
  80. [RAUtils deletefiles: cachefolder];
  81. // DebugLog(@"%@",cachefolder);
  82. // DebugLog(@"%@",img_cache);
  83. // float size=[RAUtils fileSizeForDir:cachefolder];
  84. // // float size1=[RAUtils fileSizeForDir:img_cache];
  85. //
  86. // self.sizeLabel.text = [NSString stringWithFormat:@"Cache size: %.2f MB",size];
  87. //
  88. // [self.mum stopAnimating];
  89. [alert dismissViewControllerAnimated:YES completion:nil];
  90. // [alert dismissWithClickedButtonIndex:0 animated:FALSE];
  91. [self.mum startAnimating];
  92. float size=[RAUtils fileSizeForDir:cachefolder];
  93. // float size1=[RAUtils fileSizeForDir:img_cache];
  94. self.sizeLabel.text = [NSString stringWithFormat:@"Cache size: %.2f MB",size];
  95. [self.mum stopAnimating];
  96. }
  97. - (void)didReceiveMemoryWarning
  98. {
  99. [super didReceiveMemoryWarning];
  100. // Dispose of any resources that can be recreated.
  101. }
  102. - (IBAction)cacheSwitchChanged:(UISwitch *)sender {
  103. if(!sender.on)
  104. {
  105. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Disable Cache Confirm", nil) message:NSLocalizedString(@"Disable cache will save some diskspace but cost more network traffic and also slower. Are you sure to Disable cache?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  106. alert.tag = AL_CACHE;
  107. [alert show];
  108. }
  109. else
  110. {
  111. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  112. [defaults setValue:@"true" forKey:@"enable_cache"];
  113. [defaults synchronize];
  114. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  115. appDelegate.bEnable_Cache = true;
  116. }
  117. }
  118. #pragma mark - UIAlertViewDelegate
  119. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  120. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  121. {
  122. if(alertView.tag==AL_CACHE)
  123. {
  124. if(buttonIndex!=alertView.cancelButtonIndex)
  125. {
  126. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  127. [defaults setValue:@"false" forKey:@"enable_cache"];
  128. [defaults synchronize];
  129. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  130. appDelegate.bEnable_Cache = false;
  131. // 创建
  132. NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(clear) object:nil];
  133. // 启动
  134. [thread start];
  135. return;
  136. }
  137. else
  138. self.catchSwitch.on=YES;
  139. }
  140. if(buttonIndex!=alertView.cancelButtonIndex&&alertView.tag==AL_CLEAN)
  141. {
  142. // 创建
  143. NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(clear) object:nil];
  144. // 启动
  145. [thread start];
  146. return;
  147. }
  148. }
  149. #pragma mark - Table view delegate
  150. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  151. {
  152. if(indexPath.row==2)
  153. {
  154. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Clear Cache Confirm", nil) message:NSLocalizedString(@"Clear cache will take some time, click YES to confirm clear.", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  155. alert.tag = AL_CLEAN;
  156. [alert show];
  157. }
  158. }
  159. @end