admin
2023-04-21 57e30bc2584b92ce3da3821bd0f6e040f2ad28df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
//  SettingController.m
//  BuWanVideo2.0
//
//  Created by weikou2016 on 16/8/10.
//  Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "SettingController.h"
#import "StorageSpaceTableViewCell.h"
#import "SettingWebView.h"
#import "UIView+Toast.h"
 
#define indentfly @"cell"
#define indentfly1 @"Mcell"
#define indentfly2 @"WIFIcell"
#define indentfly_logout @"LogoutCell"
#define indentfly_unregister @"UnregisterCell"
 
@interface SettingController()<UITableViewDelegate,UITableViewDataSource>{
    BOOL _userOnLine;
}
@property (nonatomic, strong) UITableView *mytableview;
@property (nonatomic , strong) NSMutableArray *dataAry;//数据源
 
@end
 
 
@implementation SettingController
 
- (void)viewWillAppear:(BOOL)animated {
    _userOnLine=[[NSUserDefaults standardUserDefaults] boolForKey:@"userOnLine"];
    if(_mytableview){
        [_mytableview reloadData];
    }
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
}
 
-(void)loadData{
    self.navigationItem.title = @"设置";
    self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor blackColor]};
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[[UIImage imageNamed:@"详情页面返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 32, 32);
    UIBarButtonItem *iconBarItem=[[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = iconBarItem;
    
    _dataAry = [[NSMutableArray alloc] initWithObjects:@[@"清空图片缓存"],@[@"手机存储"],@[@"常见问题",@"关于我们",@"免责声明",@"恢复购买"], nil];
    
    if(!_mytableview){
        _mytableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH) style:UITableViewStyleGrouped];
        _mytableview.delegate = self;
        _mytableview.dataSource = self;
        [self.view addSubview:_mytableview];
        _mytableview.backgroundColor = kGlobalBackgroundColor;
        
        _userOnLine=[[NSUserDefaults standardUserDefaults] boolForKey:@"userOnLine"];
    }
    
    //注册自定制的两个cell(非WiFi下禁止自动下载/手机存储)
    [_mytableview registerNib:[UINib nibWithNibName:@"StorageSpaceTableViewCell" bundle:nil] forCellReuseIdentifier:indentfly1];
}
 
/**
 *  总的存储空间
 *
 *  @return 手机总的字节数
 */
-(NSNumber *)totalDiskSpace{
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    return [fattributes objectForKey:NSFileSystemSize];
}
/**
 *  剩余存储空间
 *
 *  @return 手机剩余字节数
 */
-(NSNumber *)freeDiskSpace{
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    return [fattributes objectForKey:NSFileSystemFreeSize];
}
 
/**
 *  用于退出登录
 */
-(void)OutOfLogin{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"buUserInfo"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"userOnLine"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RELOAD_DATA" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
 
// 注销
-(void)unRegister{
    NSString *link= [[NSUserDefaults standardUserDefaults] stringForKey:UNREGISTER_LINK];
    
    if(link&&![link isEqual:@""])
    {
        NSLog(@"注销");
        SettingWebView *settingWebViewController = [[SettingWebView alloc] init];
        settingWebViewController.webTitle=@"账号注销";
        settingWebViewController.requestURL=link;
        [self.navigationController pushViewController:settingWebViewController animated:YES];
    }else{
        [self.view makeToast:@"尚未获取到注销链接"];
    }
}
 
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
}
#pragma mark  UITableViewDelegate UITableViewDataSource
 
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(_userOnLine){
        if (section==3||section==4) {
            return 1;
        }
    }
    return  [_dataAry[section] count];
}
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (_userOnLine) {
        return _dataAry.count+2;
    }
    return _dataAry.count;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 30;
    }else if(section==3||section==4){
        return 0;
    }
    return 22;
}
 
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(section==1){
        return [NSString stringWithFormat:@"当前空间"];
    }
    return [NSString stringWithFormat:@""];
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section==1&&indexPath.row==0){
        //手机存储空间cell
        StorageSpaceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentfly1];
        cell.text.text=[[_dataAry objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        //将字节装换成GB
        float total=[[self totalDiskSpace] floatValue]/(1024.0*1024.0*1024.0);
        cell.total.text=[NSString stringWithFormat:@"%0.2f",total];
        //将字节装换成GB
        float margin=[[self freeDiskSpace] floatValue]/(1024.0*1024.0*1024.0);
        cell.margin.text=[NSString stringWithFormat:@"%0.2f",margin];
        
        [cell.total sizeToFit];
        [cell.margin sizeToFit];
        return cell;
        
    }else if(indexPath.section==3){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentfly_logout];
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentfly_logout];
            [cell setAccessoryType:UITableViewCellAccessoryNone];
        }
        
        UIButton *outButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 44)];
        [outButton setBackgroundColor:kGlobalBlueColor];
        [outButton setTitle:@"退出登录" forState:UIControlStateNormal];
        [outButton setTintColor:[UIColor whiteColor]];
        [outButton addTarget:self action:@selector(OutOfLogin) forControlEvents:UIControlEventTouchUpInside];
        [cell.viewForLastBaselineLayout addSubview:outButton];
        
        return cell;
    }else if(indexPath.section==4){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentfly_unregister];
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentfly_unregister];
            [cell setAccessoryType:UITableViewCellAccessoryNone];
        }
        
        UIButton *outButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 44)];
        [outButton setBackgroundColor:[UIColor whiteColor]];
//        [outButton setBackgroundColor:kGlobalBlueColor];
        [outButton setTitle:@"注销账号" forState:UIControlStateNormal];
        [outButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [outButton setTintColor:[UIColor grayColor]];
        [outButton addTarget:self action:@selector(unRegister) forControlEvents:UIControlEventTouchUpInside];
        [cell.viewForLastBaselineLayout addSubview:outButton];
        
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentfly];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentfly];
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        }
        cell.textLabel.text = [[_dataAry objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        return cell;
    }
}
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section==0) {
        switch (indexPath.row) {
            case 0:{//清空图片缓存
                NSString *str = [NSString stringWithFormat:@"确认清空%.2fM图片缓存!",[self filePath]];
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:str preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    NSLog(@"点击取消");
                }];
                
                UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    [self deleteFile];
                }];
                
                [alertController addAction:cancelAction];
                [alertController addAction:otherAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
                break;
        }
    }
    if(indexPath.section==2){
        SettingWebView *settingWebViewController = [[SettingWebView alloc] init];
        switch (indexPath.row) {
            case 0:{//常见问题
                settingWebViewController.webTitle=@"常见问题";
                settingWebViewController.requestURL=changjianwenti;
                [self.navigationController pushViewController:settingWebViewController animated:YES];
            }
                break;
            case 1:{//关于我们
                settingWebViewController.webTitle=@"关于我们";
                settingWebViewController.requestURL=guanyuwomen;
                [self.navigationController pushViewController:settingWebViewController animated:YES];
            }
                break;
            case 2:{//免责声明
                settingWebViewController.webTitle=@"免责声明";
                settingWebViewController.requestURL=mianzeshengming;
                [self.navigationController pushViewController:settingWebViewController animated:YES];
            }
                break;
                
            case 3:{
                [UIApplication sharedApplication].keyWindow.userInteractionEnabled = NO;
                [SVProgressHUD show];
                [self performSelector:@selector(popTips) withObject:self afterDelay:2];
            }
                break;
            default:
                break;
        }
    }
    
    if(indexPath.section == 3 && indexPath.row == 0) {
        [self OutOfLogin];
    }
    if(indexPath.section == 4 && indexPath.row == 0) {
        [self unRegister];
    }
}
 
- (void)popTips {
    [SVProgressHUD dismiss];
    [UIApplication sharedApplication].keyWindow.userInteractionEnabled = YES;
    [self autoDisappearAlertTime:1 msg:@"没有找到可恢复购买项"];
}
 
#pragma mark 删除沙盒里的文件
-(void)deleteFile{
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];
    for ( NSString * p in files) {
        NSError * error = nil ;
        NSString * path = [cachPath stringByAppendingPathComponent :p];
        if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
            [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
        }
    }
    [_mytableview reloadData];
}
 
- (NSMutableArray *)readDataFromPath:(NSString *)filePath{
    NSMutableArray  *array = [NSMutableArray arrayWithCapacity:0];
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]) {
        array = [NSMutableArray arrayWithContentsOfFile:filePath];
        return array;
    }
    return nil;
}
 
-(void)back:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
 
-( float )filePath{
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    return [ self folderSizeAtPath :cachPath];
}
 
- (float)folderSizeAtPath:( NSString *) folderPath{
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    
    if (![manager fileExistsAtPath :folderPath]){
        return 0 ;
    }
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];
    NSString * fileName;
    long long folderSize = 0 ;
    while ((fileName = [childFilesEnumerator nextObject ]) != nil ){
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
    }
    return folderSize/( 1024.0 * 1024.0 );
}
 
- ( long long ) fileSizeAtPath:( NSString *) filePath{
    NSFileManager * manager = [ NSFileManager defaultManager ];
    if ([manager fileExistsAtPath :filePath]){
        return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];
    }
    return 0 ;
}
@end