CacheViewController.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // __block UIAlertController * alert = [RAUtils waiting_alert:self message:@"Deleting..." title:nil completion:^{
  73. PopWaitAlert* pop = [RAUtils waiting_pop:@"Deleting..." completion:nil];
  74. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  75. NSString *cachefolder = [paths objectAtIndex:0];
  76. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  77. [RAUtils deletefiles:img_cache];
  78. [RAUtils deletefiles: cachefolder];
  79. // [alert dismissViewControllerAnimated:YES completion:^{
  80. [pop hide];
  81. // [alert dismissWithClickedButtonIndex:0 animated:FALSE];
  82. [self.mum startAnimating];
  83. float size=[RAUtils fileSizeForDir:cachefolder];
  84. // float size1=[RAUtils fileSizeForDir:img_cache];
  85. self.sizeLabel.text = [NSString stringWithFormat:@"Cache size: %.2f MB",size];
  86. [self.mum stopAnimating];
  87. // }];
  88. // }];
  89. }
  90. - (void)didReceiveMemoryWarning
  91. {
  92. [super didReceiveMemoryWarning];
  93. // Dispose of any resources that can be recreated.
  94. }
  95. - (IBAction)cacheSwitchChanged:(UISwitch *)sender {
  96. if(!sender.on)
  97. {
  98. UIAlertController *alertController = [UIAlertController alertControllerWithTitle: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) preferredStyle:UIAlertControllerStyleAlert];
  99. UIAlertAction *action_0 = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  100. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  101. [defaults setValue:@"false" forKey:@"enable_cache"];
  102. [defaults synchronize];
  103. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  104. appDelegate.bEnable_Cache = false;
  105. // 创建
  106. NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(clear) object:nil];
  107. // 启动
  108. [thread start];
  109. return;
  110. }];
  111. UIAlertAction *action_3 = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  112. self.catchSwitch.on=YES;
  113. }];
  114. [alertController addAction:action_0];
  115. [alertController addAction:action_3];
  116. [self presentViewController:alertController animated:YES completion:nil];
  117. //
  118. // 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];
  119. //
  120. //
  121. // alert.tag = AL_CACHE;
  122. //
  123. //
  124. //
  125. // [alert show];
  126. }
  127. else
  128. {
  129. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  130. [defaults setValue:@"true" forKey:@"enable_cache"];
  131. [defaults synchronize];
  132. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  133. appDelegate.bEnable_Cache = true;
  134. }
  135. }
  136. //#pragma mark - UIAlertViewDelegate
  137. //// Called when a button is clicked. The view will be automatically dismissed after this call returns
  138. //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  139. //{
  140. // if(alertView.tag==AL_CACHE)
  141. // {
  142. // if(buttonIndex!=alertView.cancelButtonIndex)
  143. // {
  144. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  145. //
  146. //
  147. // [defaults setValue:@"false" forKey:@"enable_cache"];
  148. //
  149. // [defaults synchronize];
  150. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  151. // appDelegate.bEnable_Cache = false;
  152. //
  153. //
  154. // // 创建
  155. // NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(clear) object:nil];
  156. // // 启动
  157. // [thread start];
  158. // return;
  159. // }
  160. // else
  161. // self.catchSwitch.on=YES;
  162. //
  163. //
  164. // }
  165. //
  166. //// if(buttonIndex!=alertView.cancelButtonIndex&&alertView.tag==AL_CLEAN)
  167. //// {
  168. //// // 创建
  169. //// NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(clear) object:nil];
  170. //// // 启动
  171. //// [thread start];
  172. //// return;
  173. //// }
  174. //}
  175. #pragma mark - Table view delegate
  176. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  177. {
  178. if(indexPath.row==2)
  179. {
  180. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Clear Cache Confirm", nil) message:NSLocalizedString(@"Clear cache will take some time, click YES to confirm clear.", nil) preferredStyle:UIAlertControllerStyleAlert];
  181. UIAlertAction *action_0 = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  182. // 创建
  183. NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(clear) object:nil];
  184. // 启动
  185. [thread start];
  186. return;
  187. }];
  188. UIAlertAction *action_3 = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  189. }];
  190. [alertController addAction:action_0];
  191. [alertController addAction:action_3];
  192. [self presentViewController:alertController animated:YES completion:nil];
  193. // 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];
  194. //
  195. //
  196. // alert.tag = AL_CLEAN;
  197. //
  198. //
  199. //
  200. // [alert show];
  201. }
  202. }
  203. @end