// // DRSettingViewController.m // YouHuiProject // // Created by 小花 on 2018/1/25. // Copyright © 2018年 kuxuan. All rights reserved. // #import "DRSettingViewController.h" #import "DRAuthorityManager.h" #import "CCActionSheet.h" #import #import #import "DXAlertView.h" #import "DRLoginViewController.h" #import #import "DRMySuperViewController.h" @interface DRSettingViewController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSArray *dataArr; @property (nonatomic, strong) NSArray *sexArr; @end @implementation DRSettingViewController - (void)viewDidLoad { [super viewDidLoad]; [self configNavigationBar]; [self addObserToController]; } - (void)configNavigationBar { self.view.backgroundColor = [UIColor whiteColor]; [self.navigationBar setNavTitle:@"设置"]; self.navigationBar.showNavigationBarBottomLine = YES; [self.view addSubview:self.tableView]; UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; [self.navigationBar setCustomLeftButtons:@[leftBtn]]; UILabel *logOut = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-50-BottomMargin, SCREEN_WIDTH, 50)]; logOut.backgroundColor = [UIColor whiteColor]; logOut.textColor = [UIColor homeRedColor]; logOut.textAlignment = NSTextAlignmentCenter; logOut.font = [UIFont systemFontOfSize:14]; logOut.text = [AccountTool isLogin] ? @"退出登录":@"快速登录"; logOut.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(logOutAction)]; [logOut addGestureRecognizer:tap]; [self.view addSubview:logOut];} - (void)addObserToController { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(becomActive) name:UIApplicationDidBecomeActiveNotification object:nil]; } - (void)becomActive { [self.tableView reloadData]; } /** 返回 */ - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } /** 退出登录 */ - (void)logOutAction { if ([AccountTool isLogin]) { // DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"温馨提示" message:@"是否要退出登录?" cancelBtnTitle:@"取消" otherBtnTitle:@"确定"]; // alert.clickBlock = ^(NSInteger index) { // if (index == 1) { // [self logOutManager]; // } // }; // [alert show]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否要退出登录?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { [self logOutManager]; }]; [alert addAction:cancel]; [alert addAction:sure]; [self presentViewController:alert animated:YES completion:nil]; }else { DRLoginViewController *login = [[DRLoginViewController alloc] init]; [self presentViewController:login animated:YES completion:nil]; } } - (void)logOutManager { [DRHttp post:Logout params:nil success:^(id json) { [AccountTool deleteAccount]; [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil]; [self.navigationController popViewControllerAnimated:YES]; } failure:^(NSError *error) { }]; } /** switch 点击 */ - (void)switchAction { NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if([[UIApplication sharedApplication]canOpenURL:url] ) { if (@available(iOS 10.0, *)) { [[UIApplication sharedApplication] openURL:url options:@{}completionHandler:^(BOOL success) { }]; } else { [[UIApplication sharedApplication]openURL:url]; } } } #pragma mark ------------------ - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsMake(0, 50, 0, 0)]; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArr.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 50; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellID"]; cell.textLabel.text = self.dataArr[indexPath.row][@"title"]; if (indexPath.row != 0 && indexPath.row != 3) { cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } // if (indexPath.row == 0) { // NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey]; // // cell.detailTextLabel.text = [sex isEqualToString:@"0"] ? @"女":@"男"; // } if (indexPath.row == 0) { UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-60, 10, 50, 30)]; [switchView addTarget:self action:@selector(switchAction) forControlEvents:UIControlEventTouchUpInside]; // 开关事件切换通知 [cell addSubview:switchView]; if ([DRAuthorityManager isObtainUserNotificationAuthority]) { switchView.on = YES; }else { switchView.on = NO; } } if (indexPath.row == 1) { NSUInteger intg = [[SDImageCache sharedImageCache] getSize]; NSString * currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeWithInterge:intg]]; cell.detailTextLabel.text = currentVolum; } if (indexPath.row == 2) { if ([[ALBBSession sharedInstance] isLogin]) { cell.detailTextLabel.text = @"已授权"; }else { cell.detailTextLabel.text = @"未授权"; } } NSString *imgName = self.dataArr[indexPath.row][@"image"]; cell.imageView.image = [UIImage imageNamed:imgName]; cell.textLabel.font = [UIFont systemFontOfSize:15]; cell.textLabel.textColor = [UIColor YHColorWithHex:0x666666]; cell.detailTextLabel.font = [UIFont systemFontOfSize:15]; cell.detailTextLabel.textColor = [UIColor YHColorWithHex:0x999999]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (indexPath.row == 3) { NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; cell.detailTextLabel.text = app_Version; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // if (indexPath.row == 0) { // //改变性别 // [self changeUserSex]; // } if (indexPath.row == 1) { //清理缓存 [self clearCacheWith:indexPath]; } if (indexPath.row == 2) { //淘宝授权 [self taobaoAuthor]; } if (indexPath.row == 4) { //五星好评 [self commentAndStart]; } if (indexPath.row == 5) { if (![AccountTool isLogin]) { [self goToLoginPage]; return; } DRMySuperViewController *superV = [[DRMySuperViewController alloc] init]; [self.navigationController pushViewController:superV animated:YES]; } } #pragma mark ------------ private---------- /** 五星好评 */ - (void)commentAndStart { NSString * nsStringToOpen = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review",APP_ID];//替换为对应的APPID [[UIApplication sharedApplication] openURL:[NSURL URLWithString:nsStringToOpen]]; } /** 用户登录 */ - (void)goToLoginPage { DRLoginViewController *login = [[DRLoginViewController alloc] init]; [self presentViewController:login animated:YES completion:nil]; } /** 改变性别 */ - (void)changeUserSex { CCActionSheet *actionSheet = [[CCActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:self.sexArr]; [actionSheet showInView:self.view]; } /** 清理缓存 */ - (void)clearCacheWith:(NSIndexPath *)indexPath { [[SDImageCache sharedImageCache] clearMemory]; [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{ NSUInteger intg = [[SDImageCache sharedImageCache] getSize]; NSString * currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeWithInterge:intg]]; UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; cell.detailTextLabel.text = currentVolum; }]; } /** 淘宝授权 */ - (void)taobaoAuthor { if ([[ALBBSession sharedInstance] isLogin]) { // DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"温馨提示" message:@"是否取消淘宝授权?" cancelBtnTitle:@"取消" otherBtnTitle:@"确定"]; // alert.delegate = self; // [alert show]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否要取消淘宝授权?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { ALBBSDK *albbSDK = [ALBBSDK sharedInstance]; [albbSDK logoutWithCallback:^{ [self.tableView reloadData]; }]; }]; [alert addAction:cancel]; [alert addAction:sure]; [self presentViewController:alert animated:YES completion:nil]; }else { ALBBSDK *albbSDK = [ALBBSDK sharedInstance]; [albbSDK setAppkey:ALBC_APP_KEY]; [albbSDK setAuthOption:NormalAuth]; [albbSDK auth:self successCallback:^(ALBBSession *session){ // ALBBUser *user = [session getUser]; [self.tableView reloadData]; } failureCallback:^(ALBBSession *session,NSError *error){ NSLog(@"session == %@,error == %@",session,error); }]; } } #pragma mark ---------------- DXAlertView delegate --------- - (void)dxAlertView:(DXAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { ALBBSDK *albbSDK = [ALBBSDK sharedInstance]; [albbSDK logoutWithCallback:^{ [self.tableView reloadData]; }]; } } //计算出大小 - (NSString *)fileSizeWithInterge:(NSInteger)size{ // 1k = 1024, 1m = 1024k if (size < 1024) {// 小于1k return [NSString stringWithFormat:@"%ld B",(long)size]; }else if (size < 1024 * 1024){// 小于1m CGFloat aFloat = size/1024; return [NSString stringWithFormat:@"%.0fK",aFloat]; }else if (size < 1024 * 1024 * 1024){// 小于1G CGFloat aFloat = size/(1024 * 1024); return [NSString stringWithFormat:@"%.1fM",aFloat]; }else{ CGFloat aFloat = size/(1024*1024*1024); return [NSString stringWithFormat:@"%.1fG",aFloat]; } } #pragma mark ------------------- CCActionSheet Delegate --------- - (void)actionSheet:(CCActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSDictionary *para = @{@"sex":@(buttonIndex)}; if ([AccountTool isLogin]) { [DRHttp post:MySetting params:para success:^(id json) { NSInteger localSex = [[[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey] integerValue]; if (buttonIndex != localSex) { [self saveSexWithButtonIndex:buttonIndex]; [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil]; } } failure:^(NSError *error) { }]; }else { [self saveSexWithButtonIndex:buttonIndex]; [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil]; } } - (void)saveSexWithButtonIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:UserSexKey]; }else { [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:UserSexKey]; } [[NSUserDefaults standardUserDefaults] synchronize]; [self.tableView reloadData]; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain]; _tableView.estimatedSectionHeaderHeight = 0; _tableView.estimatedSectionFooterHeight = 0; _tableView.sectionFooterHeight = 0; _tableView.sectionHeaderHeight = 0; _tableView.delegate = self; _tableView.dataSource = self; _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; _tableView.backgroundColor = [UIColor yhGrayColor]; _tableView.bounces = YES; _tableView.showsVerticalScrollIndicator = NO; [_tableView setSeparatorColor:[UIColor YHColorWithHex:0xdddddd]]; } return _tableView; } - (NSArray *)dataArr { if (!_dataArr) { _dataArr = @[@{@"title":@"推送设置",@"image":@"push_img"}, @{@"title":@"清理缓存",@"image":@"clear_cache"}, @{@"title":@"淘宝授权",@"image":@"taobao_man"}, @{@"title":@"版本信息",@"image":@"verinfo"}, ]; } return _dataArr; } - (NSArray *)sexArr { if (!_sexArr) { _sexArr = @[@"女",@"男"]; } return _sexArr; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (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