IVTMySettingViewController.m 9.9 KB
//
//  IVTMySettingViewController.m
//  IVTMeLiveTwo
//
//  Created by XRG on 16/8/23.
//  Copyright © 2016年 QLQ. All rights reserved.
//

#import "IVTMySettingViewController.h"
#import "IVTEditUserInfoViewController.h"
#import "BindingTelAndThirdpartyViewController.h"
#import "IVTMyBlackListViewController.h"
#import "IVTNewMessageRemindViewController.h"
#import "IVTMyFeedBackViewController.h"
#import "AboutUsViewController.h"
#import "AppDelegate.h"

@interface IVTMySettingViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *titlesArray;
    UILabel *miJiaNumberLabel;
    UILabel *cacheLabel;
    UIButton *copyButton;
}
@end

@implementation IVTMySettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavigationBar];
    
    titlesArray = @[@[@"蜜家号",@"编辑资料",@"账号和安全"],@[@"黑名单",@"新消息提醒",@"清理缓存"],@[@"意见反馈",@"关于我们"],@[@"退出登录"]];

    __weak __typeof(self)weakSelf = self;
    UITableView *myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64) style:UITableViewStyleGrouped];
    myTableView.separatorColor = UIColorFromRGBAlphaHex(0xdcdcdc, 1.0);
    myTableView.dataSource = self;
    myTableView.delegate = self;
    myTableView.sectionFooterHeight =  1.0f;
    [self.view addSubview:myTableView];
    [myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view.mas_left);
        make.top.equalTo(weakSelf.view.mas_top);
        make.right.equalTo(weakSelf.view.mas_right);
        make.bottom.equalTo(weakSelf.view.mas_bottom);
    }];
}

- (void)setNavigationBar{
    self.navigationItem.title = @"设置";
}

#pragma mark - UITableViewDelegate / UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return titlesArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[titlesArray objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 46;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return  21.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.backgroundColor  =[UIColor whiteColor];
        cell.textLabel.font = [UIFont IVTH3Font];
        cell.textLabel.textColor = [UIColor IVTMainTextColor];
        cell.selectionStyle  =UITableViewCellSelectionStyleNone;
        cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"我的更多键"]];
    }
    cell.textLabel.text = titlesArray[indexPath.section][indexPath.row];
    
    if (indexPath.section == 0 && indexPath.row ==0) {
        cell.accessoryView.hidden = YES;

        miJiaNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth-195, 0, 135, 46)];
        miJiaNumberLabel.text =[IVTAccountTool getUserId];
        miJiaNumberLabel.textColor = UIColorFromRGBAlphaHex(0x5A5A5A, 1.0);
        miJiaNumberLabel.font = [UIFont IVTH3Font];
        miJiaNumberLabel.textAlignment = NSTextAlignmentRight;
        [cell.contentView addSubview:miJiaNumberLabel];

        copyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        copyButton.frame = CGRectMake(kScreenWidth-52, 13, 38, 19);
        copyButton.layer.cornerRadius = 5.0f;
        [copyButton setTitle:@"复制" forState:UIControlStateNormal];
        copyButton.titleLabel.font = [UIFont YXFontOfSize:10];
        [copyButton setBackgroundImage:[UIImage imageNamed:@"设置--复制按钮"] forState:UIControlStateNormal];
        [copyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [copyButton addTarget:self action:@selector(copyNum:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:copyButton];
    }else if (indexPath.section ==1&&indexPath.row==2){
        NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
        NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
        cell.accessoryView.hidden = YES;

        cacheLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth-70, 0, 50, 46)];
        cacheLabel.text = [NSString stringWithFormat:@"%luMB",(unsigned long)[files count]];
        cacheLabel.textColor = [UIColor YXColorWithHexCode:@"#5A5A5A"];
        cacheLabel.font = [UIFont YXFontOfSize:10];
        cacheLabel.textAlignment = NSTextAlignmentRight;
        [cell.contentView addSubview:cacheLabel];
    }else if(indexPath.section == 3 && indexPath.row == 0){
        cell.accessoryView = nil;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row==0) {
            //Me+号
        }else if (indexPath.row==1){
            //编辑资料
            IVTEditUserInfoViewController *editData = [[IVTEditUserInfoViewController alloc]init];
            [self.navigationController pushViewController:editData animated:YES];
        }else if (indexPath.row==2){
            //账号和安全
            BindingTelAndThirdpartyViewController *unBindingTelVC = [[BindingTelAndThirdpartyViewController alloc] init];
            [self.navigationController pushViewController:unBindingTelVC animated:YES];
        }
    }
    if (indexPath.section==1) {
        if (indexPath.row==0) {//黑名单
            IVTMyBlackListViewController *blackList = [[IVTMyBlackListViewController alloc]init];
            [self.navigationController pushViewController:blackList animated:YES];
        }else if(indexPath.row ==1){//新消息提醒
            IVTNewMessageRemindViewController *newMessageRemindVC = [[IVTNewMessageRemindViewController alloc] init];
            [self.navigationController pushViewController:newMessageRemindVC animated:YES];
        }else if (indexPath.row ==2){//清理缓存
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否清理缓存" message:@"点击确认清理缓存" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                    for (NSString *p in files) {
                        NSError *error;
                        NSString *path = [cachPath stringByAppendingPathComponent:p];
                        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                            [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                        }
                    }
                });
                [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }
    }
    if (indexPath.section==2) {
        if (indexPath.row==0) {//意见反馈
            IVTMyFeedBackViewController *feedBack = [[IVTMyFeedBackViewController alloc]init];
            [self.navigationController pushViewController:feedBack animated:YES];
        }else if (indexPath.row==1){//关于我们
            AboutUsViewController *aboutUs = [[AboutUsViewController alloc]initWithNibName:@"AboutUsViewController" bundle:nil];
            [self.navigationController pushViewController:aboutUs animated:YES];
        }
    }
    if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否要退出登录" message:@"点击确认退出登录" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [[AppDelegate appDelegate]  quitAndShowLoginView];
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }
    }
}

- (void)copyNum:(id)sender{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = miJiaNumberLabel.text;
    [SVProgressHUD setMinimumDismissTimeInterval:2];
    [SVProgressHUD showSuccessWithStatus:@"复制成功"];
}

-(void)clearCacheSuccess{
    cacheLabel.text = @"0MB";
    [SVProgressHUD setMinimumDismissTimeInterval:2];
    [SVProgressHUD showSuccessWithStatus:@"已清理"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end