// // testTableViewController.m // RedAnt ERP Mobile // // Created by Ray on 14-8-5. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved. // #import "CacheViewController.h" #import "RAUtils.h" #import "AppDelegate.h" //#define AL_CACHE 1234 //#define AL_CLEAN 1235 @interface CacheViewController () @end @implementation CacheViewController - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)onCloseClick:(id)sender { [self dismissViewControllerAnimated:true completion:nil]; } - (void)viewDidLoad { [super viewDidLoad]; AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; // // if( appDelegate.bEnable_Cache) { self.catchSwitch.on=true; self.sizeLabel.text = @"Calculating"; // 创建 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(calculate) object:nil]; // 启动 [thread start]; } else { self.catchSwitch.on=false; self.sizeLabel.text = @""; [self.mum stopAnimating]; } UIBarButtonItem* closeBTN = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)]; self.navigationItem.rightBarButtonItem = closeBTN; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; } - (void)calculate { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachefolder = [paths objectAtIndex:0]; NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"]; // DebugLog(@"%@",cachefolder); // DebugLog(@"%@",img_cache); float size=[RAUtils fileSizeForDir:img_cache]; // float size1=[RAUtils fileSizeForDir:img_cache]; self.sizeLabel.text = [NSString stringWithFormat:@" %.2f MB",size]; [self.mum stopAnimating]; } - (void)clear { // __block UIAlertController * alert = [RAUtils waiting_alert:self message:@"Deleting..." title:nil completion:^{ PopWaitAlert* pop = [RAUtils waiting_pop:@"Deleting..." completion:nil]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachefolder = [paths objectAtIndex:0]; NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"]; [RAUtils deletefiles:img_cache]; [RAUtils deletefiles: cachefolder]; // [alert dismissViewControllerAnimated:YES completion:^{ [pop hide]; // [alert dismissWithClickedButtonIndex:0 animated:FALSE]; [self.mum startAnimating]; float size=[RAUtils fileSizeForDir:cachefolder]; // float size1=[RAUtils fileSizeForDir:img_cache]; self.sizeLabel.text = [NSString stringWithFormat:@"Cache size: %.2f MB",size]; [self.mum stopAnimating]; // }]; // }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)cacheSwitchChanged:(UISwitch *)sender { if(!sender.on) { 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]; UIAlertAction *action_0 = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; [defaults setValue:@"false" forKey:@"enable_cache"]; [defaults synchronize]; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.bEnable_Cache = false; // 创建 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(clear) object:nil]; // 启动 [thread start]; return; }]; UIAlertAction *action_3 = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { self.catchSwitch.on=YES; }]; [alertController addAction:action_0]; [alertController addAction:action_3]; [self presentViewController:alertController animated:YES completion:nil]; // // 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]; // // // alert.tag = AL_CACHE; // // // // [alert show]; } else { NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; [defaults setValue:@"true" forKey:@"enable_cache"]; [defaults synchronize]; AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; appDelegate.bEnable_Cache = true; } } //#pragma mark - UIAlertViewDelegate //// Called when a button is clicked. The view will be automatically dismissed after this call returns //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex //{ // if(alertView.tag==AL_CACHE) // { // if(buttonIndex!=alertView.cancelButtonIndex) // { // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; // // // [defaults setValue:@"false" forKey:@"enable_cache"]; // // [defaults synchronize]; // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; // appDelegate.bEnable_Cache = false; // // // // 创建 // NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(clear) object:nil]; // // 启动 // [thread start]; // return; // } // else // self.catchSwitch.on=YES; // // // } // //// if(buttonIndex!=alertView.cancelButtonIndex&&alertView.tag==AL_CLEAN) //// { //// // 创建 //// NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(clear) object:nil]; //// // 启动 //// [thread start]; //// return; //// } //} #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row==2) { 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]; UIAlertAction *action_0 = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // 创建 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(clear) object:nil]; // 启动 [thread start]; return; }]; UIAlertAction *action_3 = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertController addAction:action_0]; [alertController addAction:action_3]; [self presentViewController:alertController animated:YES completion:nil]; // 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]; // // // alert.tag = AL_CLEAN; // // // // [alert show]; } } @end