CNLiveSettingController.m 12.6 KB
//
//  CNLiveSettingController.m
//  CNLiveBMineModule
//
//  Created by 153993236@qq.com on 11/12/2019.
//  Copyright (c) 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveSettingController.h"
#import "MJExtension.h"
#import <SDWebImage/SDWebImage.h>
#import "QMUIKit.h"

#import "CNLiveUserAgreementManager.h"
#import "CNUserInfoManager.h"

#import "CNLiveSettingModel.h"
#import "CNLiveSettingCell.h"
#import "CNLiveBServices.h"
#import "CNLiveNavigationBar.h"

#import "CNLiveUpdateTelController.h"//修改手机号
#import "CNLiveCancellationController.h"//注销
#import "CNLiveAboutUsController.h"//关于我们
#import "CNLiveBindingManageController.h"//绑定管理
#import "CNLiveBaseKit.h"

@interface CNLiveSettingController ()
<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) CNLiveNavigationBar *navigationBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *logout;
@property (nonatomic, strong) NSMutableArray *data;

@end

@implementation CNLiveSettingController
static const NSInteger timeValue = 1.5;
#pragma mark - 加载数据
- (void)loadData {
    NSArray *data = @[
                      @{
                          @"style":@"1",
                          @"title":@"更换手机号",
                          @"content":CNUserShareModel.mobile?CNUserShareModel.mobile:@"",
                          @"height":@"60",
                          @"lineHeight":@"10"
                          },
                      @{
                          @"style":@"0",
                          @"title":@"绑定管理",
                          @"content":@"",
                          @"height":@"60",
                          @"lineHeight":@"10"
                          },
                      @{
                          @"style":@"0",
                          @"title":@"帮助与反馈",
                          @"content":@"",
                          @"height":@"60",
                          @"lineHeight":@"10"
                          },
                      @{
                          @"style":@"1",
                          @"title":@"清除缓存",
                          @"content":@"30k",
                          @"height":@"51",
                          @"lineHeight":@"1"
                          },
                      @{
                          @"style":@"0",
                          @"title":@"关于我们",
                          @"height":@"51",
                          @"lineHeight":@"1"
                          },
                      @{
                          @"style":@"0",
                          @"title":@"注销账号",
                          @"height":@"51",
                          @"lineHeight":@"1"
                          }
                     
    ];
    for(NSDictionary *dic in data){
        [self.data addObject:[CNLiveSettingModel mj_objectWithKeyValues:dic]];
    }
    [self getSDCacheSize];
    [self.tableView reloadData];
    
}
- (void)getSDCacheSize {
    __weak typeof(self) weakSelf = self;
    [[SDImageCache sharedImageCache] calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {
        NSString *cacheString = @"";
        if (totalSize/(1024.0*1024.0) >= 1.0) {
            cacheString = [NSString stringWithFormat:@"%.1fMB",totalSize/(1024.0*1024)];
        } else {
            cacheString = [NSString stringWithFormat:@"%.1fKB",totalSize/1024.0];
        }
        for (CNLiveSettingModel *model in self.data) {
            if([model.title isEqualToString:@"清除缓存"]){
                model.content = cacheString;
            }
        }
        [weakSelf.tableView reloadData];
        
    }];
}

#pragma mark - 生命周期
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [self.tableView reloadData];
    self.logout.titleLabel.font = UIFontBoldCNMake(17.0);

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if (@available(iOS 11, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

    [self createSubViews];
    [self loadData];
    
}

- (void)dealloc {
    
    
}

- (void)createSubViews {
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.logout];

}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CNLiveSettingModel *model = self.data[indexPath.row];
    return model.height;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){ // 更换手机号
        CNLiveUpdateTelController *vc = [[CNLiveUpdateTelController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];

    }
    else if(indexPath.row == 1){ //绑定管理
        CNLiveBindingManageController *vc = [[CNLiveBindingManageController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if(indexPath.row == 2){ //帮助手册
        [CNLiveUserAgreementManager jumpToAgreementH5WithType:CNLiveUserAgreementHelp];

    }
    else if(indexPath.row == 3){ // 清除缓存
        __weak typeof(self) weakSelf = self;
        QMUIAlertAction *sure = [QMUIAlertAction actionWithTitle:@"确定" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
           __strong typeof(weakSelf) strongSelf = weakSelf;
           if(!strongSelf) return ;
            [strongSelf cleanLocalCache];

        }];
        QMUIAlertAction *cancel = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
              __strong typeof(weakSelf) strongSelf = weakSelf;
              if(!strongSelf) return ;
              self.navigationBar.right.userInteractionEnabled = YES;

          }];
               
        QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"确定清除本地缓存吗?" message:@"" preferredStyle:QMUIAlertControllerStyleAlert];
        [alertController addAction:sure];
        [alertController addAction:cancel];
        
        NSMutableDictionary *titleAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
               
        titleAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:72/255.0 green:167/255.0 blue:249/255.0 alpha:1.0];
        
        NSMutableDictionary *cancelAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
        cancelAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];

        sure.buttonAttributes = titleAttributs;
        cancel.buttonAttributes = cancelAttributs;

        [alertController showWithAnimated:YES];
        
    }else if (indexPath.row == 4){
        CNLiveAboutUsController *vc = [[CNLiveAboutUsController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.row == 5) {
        CNLiveCancellationController *vc = [[CNLiveCancellationController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)data {
    if (!_data) {
        _data = [NSMutableArray array];
    }
    return _data;
}

- (CNLiveNavigationBar *)navigationBar {
    if (!_navigationBar) {
        _navigationBar = [CNLiveNavigationBar navigationBar];
        _navigationBar.title.text = @"设置";
        __weak typeof(self) weakSelf = self;
        _navigationBar.onClickLeftButton = ^{
            [weakSelf goBack];
        };
    }
    return _navigationBar;
}

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop-50) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[CNLiveSettingCell class] forCellReuseIdentifier:kCNLiveSettingCell];
    }
    return _tableView;
}

- (UIButton *)logout{
    if(!_logout){
        _logout = [UIButton buttonWithType:UIButtonTypeCustom];
        _logout.frame = CGRectMake(0, SCREEN_HEIGHT-SafeAreaInsetsConstantForDeviceWithNotch.bottom-50, SCREEN_WIDTH, 50);
        _logout.backgroundColor = [UIColor whiteColor];
        _logout.titleLabel.font = UIFontBoldCNMake(17.0);
        [_logout setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_logout setTitle:@"退出登录" forState:UIControlStateNormal];
        _logout.adjustsImageWhenHighlighted = NO;
        [_logout addTarget:self action:@selector(logoutDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _logout;
}

#pragma mark - 响应方法
- (void)goBack {
    if (self.presentingViewController != nil) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)logoutDidClicked:(UIButton *)btn{
    QMUIAlertAction *sure = [QMUIAlertAction actionWithTitle:@"退出登录" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
        [UIApplication sharedApplication].delegate.window.rootViewController = nil;
        [self cleanLocalCache];
        id<CNLiveBLoginServiceProtocol> login = [[BeeHive shareInstance] createService:@protocol(CNLiveBLoginServiceProtocol)];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[login getLoginViewController]];
        [UIApplication sharedApplication].delegate.window.rootViewController = nav;
    }];
    
    QMUIAlertAction *cancel = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:nil];
           
    QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:QMUIAlertControllerStyleActionSheet];
    [alertController addAction:sure];
    [alertController addAction:cancel];
    
    NSMutableDictionary *titleAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
           
    titleAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:72/255.0 green:167/255.0 blue:249/255.0 alpha:1.0];
    
    NSMutableDictionary *cancelAttributs = [[NSMutableDictionary alloc] initWithDictionary:alertController.alertTitleAttributes];
    cancelAttributs[NSForegroundColorAttributeName] = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];

    sure.buttonAttributes = titleAttributs;
    cancel.buttonAttributes = cancelAttributs;

    [alertController showWithAnimated:YES];
    
}

#pragma mark - 私有方法
// 清除本地缓存
- (void)cleanLocalCache{
//    [XWUserManager removeUserInfo];
    [[SDImageCache sharedImageCache] clearMemory];
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
        [QMUITips showSucceed:@"清除成功" inView:self.view hideAfterDelay:timeValue];
        [self getSDCacheSize];
          
    }];
}

#pragma mark - 公共方法
// 清除本地缓存
+ (void)cleanLocalCache{
//    [XWUserManager removeUserInfo];
    [[SDImageCache sharedImageCache] clearMemory];
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
          
    }];
}

@end