|
//
// TPSettingController.m
// ThePaperDemo
//
// Created by scar1900 on 14-10-15.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "TPSettingController.h"
#import "Util.h"
#include <sys/param.h>
#include <sys/mount.h>
//#import "LCAlertView.h"
#import "TPOffLineDownLoadController.h"
#import "AppDelegate.h"
#import "MessageTableViewCell.h"
#import "OffLineTableViewCell.h"
#import "ReadKindTableViewCell.h"
#import "CleanTableViewCell.h"
#import "UserBackTableViewCell.h"
#import "UserBackTableViewCell.h"
#import "AboutTableViewCell.h"
#import "aboutPaperController.h"
#import "TPReadKindVC.h"
#import "feedbackController.h"
#import "MessageNoOpenCell.h"
#import "MarkTableViewCell.h"
#import "CoverTableViewCell.h"
#import "AnimationWelcomeController.h"
@interface TPSettingController ()<UITableViewDataSource, UITableViewDelegate,UIAlertViewDelegate,messageDelegate,animationWelcomeDelgate> {
BOOL isOpen;
}
@property(nonatomic, strong)UITableView *settingTableView;
@property(nonatomic, strong)NSTimer *timer;
@end
@implementation TPSettingController
@synthesize timer;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.titleStr = @"更多";
[self.view addSubview:self.settingTableView];
self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
UIView *footView = [[UIView alloc]init];
UILabel *versionLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 -50, 20, 100, 20)];
versionLabel.textColor = [UIColor colorWithHexString:TextLightGray];
versionLabel.font = appFont(14, NO);
versionLabel.backgroundColor = [UIColor clearColor];
versionLabel.text = [NSString stringWithFormat:@"当前版本%@",[TPUserDefault instance].appVersion];
[footView addSubview:versionLabel];
self.settingTableView.tableFooterView = footView;
if(isIOS8 || isIOS9){
UIUserNotificationSettings *set = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (UIUserNotificationTypeNone == set.types) {
isOpen = NO;
}else{
isOpen = YES;
}
}else{
isOpen = YES;
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil];
}
- (void)viewDidLayoutSubviews {
if (isOpen)
self.settingTableView.contentSize = CGSizeMake(rect_screen.size.width, 490);
else
self.settingTableView.contentSize = CGSizeMake(rect_screen.size.width, 540);
[super viewDidLayoutSubviews];
}
- (void)needrefreshNightMode:(id)sender{
self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
self.settingTableView.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR];
[self.settingTableView reloadData];
}
- (UITableView*)settingTableView {
if (!_settingTableView) {
_settingTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.barHeight, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-self.barHeight) style:UITableViewStylePlain];
_settingTableView.delegate = self;
_settingTableView.dataSource = self;
_settingTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_settingTableView.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR];
// _settingTableView.scrollEnabled = NO;
}
return _settingTableView;
}
#pragma mark - table view delegate and datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 2;
}else if (section == 1) {
return 4;
}else {
return 5;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (0 == indexPath.section) {
if (indexPath.row == 0) {
return 10;
}
else {
if (isOpen) {
return 50;
}else{
return 100;
}
}
}else{
if (indexPath.row == 0)
return 10;
else
return 50;
}
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0:
if (indexPath.row == 0) {
UITableViewCell *spaceCell = [UITableViewCell new];
spaceCell.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR];
spaceCell.userInteractionEnabled = NO;
return spaceCell;
}
else {
if (isOpen) {
return [self getMessageCellWithtableView:tableView cellForRowAtIndexPath:indexPath];
}else{
return [self getMessageNoOpenCellWithtableView:tableView cellForRowAtIndexPath:indexPath];
}
}
break;
case 1:
if (indexPath.row == 0) {
UITableViewCell *spaceCell = [UITableViewCell new];
spaceCell.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR];
spaceCell.userInteractionEnabled = NO;
return spaceCell;
}else if (indexPath.row == 1) {
return [self getOffLineTableViewCellWithtableView:tableView cellForRowAtIndexPath:indexPath];
}else if (indexPath.row == 2) {
return [self getReadKindTableViewCellWithtableView:tableView cellForRowAtIndexPath:indexPath];
}else if (indexPath.row == 3) {
return [self getCleanTableViewCellWithtableView:tableView cellForRowAtIndexPath:indexPath];
}
break;
case 2:
if (indexPath.row == 0) {
UITableViewCell *spaceCell = [UITableViewCell new];
spaceCell.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR];
spaceCell.userInteractionEnabled = NO;
return spaceCell;
}else if (indexPath.row == 1) {
return [self getMarkTableViewCellWithtableView:tableView cellForRowAtIndexPath:indexPath];
}else if (indexPath.row == 2) {
return [self getUserBackTableViewCellWithtableView:tableView cellForRowAtIndexPath:indexPath];
}else if (indexPath.row == 3) {
return [self getAboutTableViewCellWithtableView:tableView cellForRowAtIndexPath:indexPath];
}else if (indexPath.row == 4) {
return [self getCoverTableViewCellWithtableView:tableView cellForRowAtIndexPath:indexPath];
}
break;
default:
return nil;
break;
}
return nil;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
// return 10;
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rect_screen.size.width, 10)];
// view.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR];
// return view;
//}
- (MessageNoOpenCell *)getMessageNoOpenCellWithtableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *message = @"messageNoOpen";
MessageNoOpenCell *cell = [tableView dequeueReusableCellWithIdentifier:message];
if (nil == cell) {
cell = [[MessageNoOpenCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:message];
cell.backgroundColor = [UIColor clearColor];
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
- (MessageTableViewCell *)getMessageCellWithtableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *message = @"message";
MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:message];
if (nil == cell) {
cell = [[MessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:message];
cell.backgroundColor = [UIColor clearColor];
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[cell setSwitchValue:[[TPUserDefault instance].isOpenPush boolValue]];
cell.delegate = self;
return cell;
}
- (OffLineTableViewCell *)getOffLineTableViewCellWithtableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"OffLineTableViewCell";
OffLineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (nil == cell) {
cell = [[OffLineTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
- (ReadKindTableViewCell *)getReadKindTableViewCellWithtableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"ReadKindTableViewCell";
ReadKindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (nil == cell) {
cell = [[ReadKindTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
- (CleanTableViewCell *)getCleanTableViewCellWithtableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"CleanTableViewCell";
CleanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (nil == cell) {
cell = [[CleanTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
cell.backgroundColor = [UIColor clearColor];
}
NSString *cachePath = CachePath(@"");
cell.fileString = [NSString stringWithFormat:@"碎片文件%.2fM",[self folderSizeAtPath:cachePath]];
return cell;
}
- (UserBackTableViewCell *)getUserBackTableViewCellWithtableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"UserBackTableViewCell";
UserBackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (nil == cell) {
cell = [[UserBackTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
- (AboutTableViewCell *)getAboutTableViewCellWithtableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"AboutTableViewCell";
AboutTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (nil == cell) {
cell = [[AboutTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
- (CoverTableViewCell *)getCoverTableViewCellWithtableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"CoverTableViewCell";
CoverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (nil == cell) {
cell = [[CoverTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
- (MarkTableViewCell *)getMarkTableViewCellWithtableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"MarkTableViewCell";
MarkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (nil == cell) {
cell = [[MarkTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
- (void)updateCacheText {
float folderSize = [self folderSizeAtPath:CachePath(@"")];
UITableViewCell *cell = [self.settingTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:1]];
cell.textLabel.text = [NSString stringWithFormat:@"碎片文件:%.2fM",folderSize];
UITableViewCell *cell2 = [self.settingTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:1]];
cell2.textLabel.text = [self getFreeDiskspace];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[UIApplication sharedApplication]beginIgnoringInteractionEvents];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
switch (indexPath.section) {
case 1:
switch (indexPath.row) {
case 1:
{
[MobClick event:@"29"];
if (![TPUserDefault instance].offlineController) {
TPOffLineDownLoadController *vc = TPOffLineDownLoadController.new;
[TPUserDefault instance].offlineController = vc;
[self.navigationController pushViewController:vc animated:YES];
}else {
[self.navigationController pushViewController:[TPUserDefault instance].offlineController animated:YES];
}
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
}
break;
case 2:
{
[self.navigationController pushViewController:[TPReadKindVC new] animated:YES];
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
}
break;
case 3:
{
[MobClick event:@"30"];
[[TPUserDefault instance] clearCache:self];
/**
* bug:5066(清理缓存:如果没有清理缓存,不要跳出弹出框,做成与网达一致的tips就可以了)
*/
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"确定要清除所有缓存文件吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
// [alert show];
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
}
break;
default:
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
break;
}
break;
case 2:
{
switch (indexPath.row) {
case 1:
{
[MobClick event:@"43"];
NSString *str = [NSString stringWithFormat:@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=%@&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8",@"878962716" ];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
}
break;
case 2:
{
[self.navigationController pushViewController:[feedbackController new] animated:YES];
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
break;
}
case 3:
{
[self.navigationController pushViewController:[aboutPaperController new] animated:YES];
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
}
break;
case 4:
{
AnimationWelcomeController *operationNaviController = [[AnimationWelcomeController alloc]init];
operationNaviController.delegate = self;
[self.navigationController pushViewController:operationNaviController animated:YES];
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
}
break;
default:
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
break;
}
break;
}
default:
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
break;
}
});
}
#pragma mark - animationWelcomeDelgate
- (void)goOutOperateNavigateion:(NSDictionary *)userInfo{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - alerView delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != 0) {
// NSIndexPath *te=[NSIndexPath indexPathForRow:3 inSection:0];
// [self.settingTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:te, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (void)updateCacheText:(NSString*)clearStatus {
// [self.tableView reloadData];
CleanTableViewCell *cell = (CleanTableViewCell*)[self.settingTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:1]];
if ([clearStatus intValue] == 1) {
cell.fileString = @"正在清理";
}else {
cell.fileString = @"碎片文件:0M";
dispatch_async(dispatch_get_main_queue(), ^{
ShowMessage(@"清理成功", YES);
});
}
}
#pragma mark - messageDelegate
- (void)switchValueWith:(BOOL)isOn {
AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
// [TPUserDefault instance].isOpenPush =
if (isOn) {
[delegate registUmentRemotePush];
if (![Remote IsEableNetwork]) {
return;
}
ShowTextMessage(@"开启推送") ;
[TPUserDefault instance].isOpenPush = @"1";
}else {
[delegate unRegistUmentRemotePush];
if (![Remote IsEableNetwork]) {
return;
}
ShowTextMessage(@"关闭推送");
[TPUserDefault instance].isOpenPush = @"0";
[MobClick event:@"23"];
}
}
#pragma mark - method about files
- (float) folderSizeAtPath:(NSString*) folderPath {
long long folderSize = [Util folderSizeAtPath3:folderPath];
float folderMBSize = folderSize/(1024.0*1024.0);
return folderMBSize;
}
-(NSString*)usedSpaceAndfreeSpace{
NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] ;
NSFileManager* fileManager = [[NSFileManager alloc]init];
NSDictionary *fileSysAttributes = [fileManager attributesOfFileSystemForPath:path error:nil];
NSNumber *freeSpace = [fileSysAttributes objectForKey:NSFileSystemFreeSize];
NSNumber *totalSpace = [fileSysAttributes objectForKey:NSFileSystemSize];
NSString * str= [NSString stringWithFormat:@"已占用%0.2fG/剩余%0.2fG",([totalSpace longLongValue] - [freeSpace longLongValue])/1024.0/1024.0/1024.0,[freeSpace longLongValue]/1024.0/1024.0/1024.0];
return str;
}
- (NSString *) freeDiskSpaceInBytes{
struct statfs buf;
uint64_t freespace = -1;
if(statfs("/var", &buf) >= 0){
freespace = (uint64_t)(buf.f_bsize * buf.f_bfree);
}
return [NSString stringWithFormat:@"手机剩余存储空间为:%qi MB" ,freespace/1024/1024];
}
-(NSString*)getFreeDiskspace {
uint64_t totalSpace = 0;
uint64_t totalFreeSpace = 0;
uint64_t usedSpace = 0;
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
NSString *str = @"";
if (dictionary) {
NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];
NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
usedSpace = totalSpace - totalFreeSpace;
str = [NSString stringWithFormat:@"手机已使用:%.2f GB,剩余:%.2f GB",((usedSpace/1024ll)/1024ll/1024.0), ((totalFreeSpace/1024ll)/1024ll/1024.0)];
TPLOG(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.", ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
}
return str;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
/*
#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
|