CNMineSettingVC.m 10.8 KB
//
//  CNMineSettingVC.m
//  CNLiveCMineModule
//
//  Created by CNLive on 2022/1/28.
//

#import "CNMineSettingVC.h"
#import "CNMySettingListModel.h"
#import "CNMineSettingCell.h"
#import "CNMineChangePhoneNumController.h"
#import "CNFontSettingController.h"
#import "CNPrivacyViewController.h"
#import "CNMineAccountManageController.h"
#import "CNCancellationViewController.h"

@interface CNMineSettingVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *quitButton;
@property (nonatomic, strong) NSMutableArray <CNMySettingListModel*>*data;
@property (nonatomic, copy) NSString *cacheString;
@end


@implementation CNMineSettingVC

static NSString *settingListCellId = @"settingListCellId";

- (void)getSDCacheSize {
    weakself
    __block NSUInteger fileSize = 0;
    
    NSInteger witnessSize =  [[CTMediator sharedInstance] cn_VideoModule_GetVideoCacheSize];
    
    // chatTempSize 聊天图片和视频临时文件大小
    NSInteger chatTempSize = [self chatTempFileSizeAtPath:[CNLiveBusinessTools getChatImageAndVideoUploadIMCacheDirectory]];
    
    [[SDImageCache sharedImageCache] calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {
        
        fileSize += totalSize;
        fileSize += [[YYWebImageManager sharedManager].cache.diskCache totalCount];
        NSInteger allSize = fileSize + witnessSize + chatTempSize;
        if (allSize/(1024.0*1024) >= 1) {
            weakSelf.cacheString = [NSString stringWithFormat:@"%.1fM",allSize/(1024.0*1024)];
        } else {
            weakSelf.cacheString = [NSString stringWithFormat:@"%.1fKB",allSize/1024.0];
        }
        NSLog(@"totalSize:%zd,fileCount:%zd",totalSize,fileCount);
        for (CNMySettingListModel *model in self.data) {
            if([model.title isEqualToString:@"清除缓存"]){
                model.content = weakSelf.cacheString;
            }
        }
        [weakSelf.tableView reloadData];
        
    }];
}
//获取聊天临时图片和视频的大小
- (double)chatTempFileSizeAtPath:(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];
        if([manager fileExistsAtPath:fileAbsolutePath])
        {
            folderSize += [[manager attributesOfItemAtPath:fileAbsolutePath error:nil] fileSize];
        }
    }
    return folderSize;
}
- (void)getData{
    [CNCSubMinePresent requestIsAllowFindStrangerSwichResponse:^(NSString * _Nonnull status) {
        for (CNMySettingListModel *model in self.data) {
            if([model.title isEqualToString:@"允许陌生人查看生活圈"]){
                model.status = status;
            }
        }
        [self.tableView reloadData];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
    NSString *status = [[NSUserDefaults standardUserDefaults] valueForKey:@"findStrangerSwitch"];
    self.data = [CNCSubMinePresent settingDataSource:status?status:@"success"];
    [self getSDCacheSize];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.quitButton];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(kNavigationBarHeight);
        make.bottom.equalTo(self.view.mas_bottom).offset(-kVerticalBottomSafeHeight-60);
        
    }];
    
    [self.quitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-kVerticalBottomSafeHeight);
        make.height.mas_equalTo(60);
    }];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
    
}
//网络监听
- (void)cn_noNetworking
{
    [self.tableView reloadData];

}

- (void)cn_wifiOrWWANNetworking
{
    [self.tableView reloadData];
}
#pragma mark - Action
- (void)quitAction:(UIButton *)sender {
    
    [self.view createAlertViewTitleArray:@[@"退出登录"] textColor:UIColorMake(40, 40, 40) font:UIFontCNMake(15.0f) actionBlock:^(UIButton *button, NSInteger didRow) {
        switch (didRow) {
            case 0:
            {
                [[CNLiveCMineService shareMineProtocol] appLoginOut:NO];
            }
                break;
                
            default:
                break;
        }
    }];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CNMineSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:settingListCellId forIndexPath:indexPath];
    cell.model = self.data[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.data[indexPath.row].rowHeight*RATIO;
    
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView qmui_clearsSelection];
   NSInteger i = 1;
    if (indexPath.row == 0) {
        // 更换手机号
            CNMineChangePhoneNumController *cpnVC = [[CNMineChangePhoneNumController alloc] init];
            [[BaseAppDelegate sharedAppDelegate] pushViewController:cpnVC withBackTitle:@" "];
    }else if (indexPath.row == 1){
        //绑定管理
        [[CNLiveCMineService shareMineProtocol] pushCMineContentForCMainPlatWithType:26 param:@{} contentVC:self];
    }else if (indexPath.row == 2){
        //帮助与反馈
        [CNLiveCMineService pushCMineIWebVCParam:@{@"path":@"bzfk",@"urlStr":@"/cnhf.html"} CurrentVC:self];
    }else if (indexPath.row == 3){
        // 字体大小
        NSLog(@"字体大小");
        CNFontSettingController  *fontSettingVC = [[CNFontSettingController alloc] init];
        [[BaseAppDelegate sharedAppDelegate] pushViewController:fontSettingVC withBackTitle:@" "];
    }else if (indexPath.row == 4){
        // 设置背景图片
        NSLog(@"设置背景图片");
        [[CNLiveCMineService shareMineProtocol] pushCMineContentForCMainPlatWithType:9 param:@{} contentVC:self ];
    }else if (indexPath.row == 5){
        //隐私
                    CNPrivacyViewController *vc = [[CNPrivacyViewController alloc] init];
                    [[BaseAppDelegate sharedAppDelegate] pushViewController:vc withBackTitle:@" "];
    }else if (indexPath.row == 6+i)
        //清除缓存
                   {
                       UIAlertController *actionAlert = [UIAlertController alertControllerWithTitle:@"清除缓存" message:@"您确定要清除本地缓存吗?" preferredStyle:UIAlertControllerStyleAlert];
                       UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                           //好友和达人头像也存在SD,暂时不能清除
                           NSString *pathOfLibrary = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                           [[NSFileManager defaultManager] removeItemAtPath:[pathOfLibrary stringByAppendingPathComponent:@"CNLiveRequestCache"] error:nil];//清除网络缓存
                           [[SDImageCache sharedImageCache] clearMemory];
                           [[YYWebImageManager sharedManager].cache.diskCache removeAllObjects];
                           [[CTMediator sharedInstance] cn_VideoModule_ClearAllVideoCache];
                           [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
                               [self getSDCacheSize];
                           }];
                           [[CNLiveCMineService shareMineProtocol] clearMemories];
                          
                           //删除聊天图片视频临时文件
                           if ([CNLiveBusinessTools deleteChatImageAndVideoUploadImCache]) {
                               NSLog(@"删除图片视频临时文件成功");
                           }
                           
                       }];
                       UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:NULL];
                       [actionAlert addAction:action2];
                       [actionAlert addAction:action1];
                       [action1 setValue:UIColorHex(23D41E) forKey:@"_titleTextColor"];
                       [action2 setValue:UIColorHex(23D41E) forKey:@"_titleTextColor"];
                       [self presentViewController:actionAlert animated:YES completion:nil];
    }else if (indexPath.row == 7+ i){
        //关于我们
                    CNMineAccountManageController *aboutVC = [[CNMineAccountManageController alloc] initWithType:SettingTypeAbout];
                    [[BaseAppDelegate sharedAppDelegate] pushViewController:aboutVC withBackTitle:@" "];
    }else if (indexPath.row == 8+ i){
        //注销账号
            CNCancellationViewController *vc = [[CNCancellationViewController alloc] init];
            [[BaseAppDelegate sharedAppDelegate] pushViewController:vc withBackTitle:@" "];
        }

}
#pragma mark - Getting
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = RGB(242, 242, 242);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [_tableView registerClass:[CNMineSettingCell class] forCellReuseIdentifier:settingListCellId];
    }
    return _tableView;
}

- (UIButton *)quitButton {
    if (!_quitButton) {
        _quitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_quitButton setTitle:@"退出登录" forState:UIControlStateNormal];
        [_quitButton setTitleColor:RGB(235, 61, 63) forState:UIControlStateNormal];
        _quitButton.backgroundColor = kWhiteColor;
        [_quitButton addTarget:self action:@selector(quitAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _quitButton;
}

- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc] init];
    }
    return _data;
}
@end