|
//
// TPUserDefault.m
// ThePaperDemo
//
// Created by Scar on 14-9-10.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "TPUserDefault.h"
#import <AdSupport/AdSupport.h>
#import "AppDelegate.h"
#import "YRSideViewController.h"
#import "SDImageCache.h"
#import "KGModal.h"
//#import "TPMainPageViewController.h"
static TPUserDefault *userDefaultsInstance = nil;
@interface TPUserDefault()<RemoteDelegate>
@property(nonatomic, strong)NSUserDefaults *defaults;
@end
@implementation TPUserDefault
@synthesize defaults;
@synthesize currentPageIndex;
@synthesize channldataList = _channldataList;
@synthesize isUseSystemFont = _isUseSystemFont;
@synthesize isNightMode = _isNightMode;
@synthesize userBO = _userBO;
@synthesize visitTime = _visitTime;
@synthesize lastContentID,lastContentOffset;
@synthesize uuid = _uuid;
@synthesize offlineList = _offlineList;
@synthesize offlineStatusList = _offlineStatusList;
@synthesize isOffline;
@synthesize umengToken;
@synthesize pushBO = _pushBO;
@synthesize isEnterFromBackground;
@synthesize imageQueue,globalQueue;
@synthesize isFirstIntoApp = _isFirstIntoApp;
@synthesize contFontSize = _contFontSize;
@synthesize hotAskColorList;
@synthesize registerBO;
@synthesize fogetPwdInfoBO;
@synthesize msgMark = _msgMark;
@synthesize updateInfo;
@synthesize ignoreVersion = _ignoreVersion;
@synthesize isOpenPush = _isOpenPush;
@synthesize appVersion = _appVersion;
@synthesize orderEventTempList;
@synthesize isIphone4;
@synthesize offlineController;
@synthesize offlinePath;
@synthesize pauseDataDic = _pauseDataDic;
@synthesize readModeStr = _readModeStr;
@synthesize noMoreReadModeAlert = _noMoreReadModeAlert;
@synthesize helpArray = _helpArray;
@synthesize searchHistoryArray = _searchHistoryArray;
@synthesize isAttention = _isAttention;
@synthesize isRegistedPush = _isRegistedPush;
+ (TPUserDefault *)instance {
@synchronized(self) {
if (userDefaultsInstance == nil) {
userDefaultsInstance = [[self alloc] init];
NSDictionary *dic = @{@"type":@"1"};
[Remote doJsonActionWithBlock:1 requestUrl:allNodeListURL parameter:dic withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) {
if (responseData && responseData[@"nodeList"]) {
NSMutableArray *channalList = [TPUserDefault instance].channldataList;
if (channalList.count == 0) {
NSArray *nodeArray = responseData[@"nodeList"];
NSMutableArray *tempArray = [NSMutableArray array];
[nodeArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *nodeID = obj[@"nodeId"];
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:obj];
[dic setValue:[[TPUserDefault instance] getContollerNameWithNodeId:nodeID] forKey:@"controller"];
NSString *color = dic[@"color"];
color = [color substringWithRange:NSMakeRange(1, 6)];
color = [NSString stringWithFormat:@"0x%@",color];
[dic setValue:color forKey:@"color"];
[tempArray addObject:dic];
}];
if (tempArray.count == 7) {
[TPUserDefault instance].channldataList = tempArray;
}
}else {
NSMutableArray *tempArray = [NSMutableArray array];
[channalList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:obj];
NSString *nodeID = obj[@"nodeId"];
NSArray *nodeArray = responseData[@"nodeList"];
__block NSMutableDictionary *oriDic = dic;
[nodeArray enumerateObjectsUsingBlock:^(id item, NSUInteger index, BOOL *stop1) {
if ([nodeID intValue] == [item[@"nodeId"] intValue]) {
oriDic = [NSMutableDictionary dictionaryWithDictionary:item];
[oriDic setValue:[[TPUserDefault instance] getContollerNameWithNodeId:nodeID] forKey:@"controller"];
NSString *color = oriDic[@"color"];
color = [color substringWithRange:NSMakeRange(1, 6)];
color = [NSString stringWithFormat:@"0x%@",color];
[oriDic setValue:color forKey:@"color"];
*stop1 = YES;
}
}];
[tempArray addObject:oriDic];
}];
if (tempArray.count == 7) {
[TPUserDefault instance].channldataList = tempArray;
}
}
}
}];
dic = @{@"type":@"4"};
[Remote doJsonActionWithBlock:2 requestUrl:allNodeListURL parameter:dic withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) {
if (responseData && responseData[@"nodeList"]) {
NSArray *nodeArray = responseData[@"nodeList"];
NSMutableArray *tempArray = [NSMutableArray array];
[nodeArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
nodeObjectBO *nodeBO = setJsonDicToDataModel(obj, [nodeObjectBO class]);
[tempArray addObject:nodeBO];
}];
[TPUserDefault instance].offlineList = [NSArray arrayWithArray:tempArray];
}
}];
}
}
return userDefaultsInstance;
}
- (NSString*)getContollerNameWithNodeId:(NSString*)nodeId {
if ([nodeId isEqualToString:@"25949"]) {
return @"channalCollectController";
}else if ([nodeId isEqualToString:@"25950"]) {
return @"channalPoliticsController";
}else if ([nodeId isEqualToString:@"25951"]) {
return @"channalEconomyController";
}else if ([nodeId isEqualToString:@"25952"]) {
return @"channalInsightsController";
}else if ([nodeId isEqualToString:@"25953"]) {
return @"channalLifeController";
}else if ([nodeId isEqualToString:@"-3"]) {
return @"channalTopicController";
}else {
return @"channalMyPaperController";
}
}
- (id)init {
if (self = [super init]) {
self.isOffline = NO;
self.registerBO = [[registerInfoBO alloc]init];
self.fogetPwdInfoBO = [[registerInfoBO alloc]init];
self.imageQueue = [[NSOperationQueue alloc]init];
self.imageQueue.maxConcurrentOperationCount = 1;
self.globalQueue = [[NSOperationQueue alloc]init];
self.globalQueue.maxConcurrentOperationCount = 1;
defaults = [NSUserDefaults standardUserDefaults];
self.currentPageIndex = 0;
NSString *isFirstIntoAppStr = [defaults objectForKey:@"isFirstIntoApp"];
self.isFirstIntoApp = isFirstIntoAppStr;
NSString *version = [defaults objectForKey:@"appVersion"];
self.appVersion = version;
NSString *uuidStr = [defaults objectForKey:@"uuid"];
self.uuid = uuidStr;
NSString *isOpen = [defaults objectForKey:@"isOpenPush"];
if (!isOpen) {
isOpen = @"1";
}
self.isOpenPush = isOpen;
NSString *fontSize = [defaults objectForKey:@"contentFontSize"];
if (isBlankString(fontSize)) {
self.contFontSize = fontSizeMiddle;
}else {
//bug:5663 升级以后字体变换成现有的字体大小
if ([fontSize floatValue] == 30) {
self.contFontSize = fontSizeExBig;
}else if([fontSize floatValue] == 13){
self.contFontSize = fontSizeExSmall;
}else{
self.contFontSize = [fontSize floatValue];
}
}
NSDictionary *userDic = [defaults objectForKey:@"userBO"];
if (userDic) {
self.userBO = setJsonDicToDataModel(userDic, [userBO class]);
}
NSString *readModeString = [defaults objectForKey:@"readMode"];
if (readModeString && !isBlankString(readModeString)) {
self.readModeStr = readModeString;
}
NSString *noMore = [defaults objectForKey:@"noMoreReadMode"];
self.noMoreReadModeAlert = noMore;
NSMutableArray *channelData = [defaults objectForKey:@"channelData"];
if (channelData && channelData.count > 0) {
self.channldataList = [NSMutableArray array];
self.channldataList = channelData;
}
self.isUseSystemFont = [defaults objectForKey:@"isUserSystemFont"];
if (!self.isUseSystemFont) {
self.isUseSystemFont = @"1";
}
self.isNightMode = [defaults objectForKey:@"isNightMode"];
if (!self.isNightMode) {
self.isNightMode = @"0";
}
self.ignoreVersion = [defaults objectForKey:@"ignoreVersion"];
if (!self.ignoreVersion) {
self.ignoreVersion = @"0";
}
self.visitTime = [defaults objectForKey:@"visitTime"];
if (!self.visitTime) {
self.visitTime = @"0";
}
self.pauseDataDic = [defaults objectForKey:@"pauseDataDic"];
if (!self.pauseDataDic) {
self.pauseDataDic = [NSMutableDictionary dictionary];
}
self.helpArray = [defaults objectForKey:@"isFirstHelp"];
if (!self.helpArray) {
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"0" forKey:@"front"];
[dic setObject:@"0" forKey:@"detail"];
[dic setObject:@"0" forKey:@"order"];
self.helpArray = dic;
}
self.searchHistoryArray = [defaults objectForKey:@"searchHistoryArray"];
if (!self.searchHistoryArray) {
self.searchHistoryArray = [NSMutableArray array];
}
NSString *registedPush = [defaults objectForKey:@"isRegistedPush"];
if (!self.isRegistedPush) {
self.isRegistedPush = @"0";
}
self.isRegistedPush = registedPush;
self.offlineList = [NSMutableArray array];
self.offlineStatusList = [NSMutableArray array];
NSArray* offlineStatusArray = [defaults objectForKey:@"offlineStatusList"];
if (offlineStatusArray && offlineStatusArray.count > 0) {
self.offlineStatusList = offlineStatusArray;
}
self.hotAskColorList = [NSArray arrayWithObjects:@{@"color":[UIColor colorWithHexString:@"0x7bc0e8"],@"image":@"other/color_1.png"},
@{@"color":[UIColor colorWithHexString:@"0x82c2cf"],@"image":@"other/color_2.png"},
@{@"color":[UIColor colorWithHexString:@"0x8c98cb"],@"image":@"other/color_3.png"},
@{@"color":[UIColor colorWithHexString:@"0xd7a36d"],@"image":@"other/color_4.png"},
@{@"color":[UIColor colorWithHexString:@"0xd28484"],@"image":@"other/color_5.png"},nil];
self.orderEventTempList = [NSMutableArray array];
if ([deviceString() isMatchedByRegex:@"iPhone 4"]) {
self.isIphone4 = @"1";
}else {
self.isIphone4 = @"0";
}
}
return self;
}
- (void)setPauseDataDic:(NSMutableDictionary *)dic {
_pauseDataDic = dic;
if (dic.allKeys.count==0) {
[defaults setObject:nil forKey:@"pauseDataDic"];
[defaults synchronize];
}else {
[defaults setObject:dic forKey:@"pauseDataDic"];
[defaults synchronize];
}
}
-(void)setHelpArray:(NSMutableDictionary *)array{
_helpArray = array;
[defaults setObject:_helpArray forKey:@"isFirstHelp"];
[defaults synchronize];
}
- (NSString*)deviceResolution {
if (!_deviceResolution) {
CGFloat scale_screen = [UIScreen mainScreen].scale;
CGFloat width = rect_screen.size.width*scale_screen;
CGFloat height = rect_screen.size.height*scale_screen;
_deviceResolution = [NSString stringWithFormat:@"%.f*%.f",width,height];
}
return _deviceResolution;
}
- (NSString*)systemInfo {
if (!_systemInfo) {
_systemInfo = [[UIDevice currentDevice] systemVersion];
}
return _systemInfo;
}
- (void)setAppVersion:(NSString *)version {
if (version && !isBlankString(version)) {
_appVersion = version;
[defaults setObject:version forKey:@"appVersion"];
[defaults synchronize];
}
}
- (NSString*)getappVer {
NSDictionary* plistInfo = [NSDictionary dictionaryWithContentsOfFile:ResourcePath(@"Info.plist")];
// return [plistInfo objectForKey:@"CFBundleVersion"];
NSString *version = [plistInfo objectForKey:@"CFBundleShortVersionString"];
return version;
}
- (NSString*)IDFA {
if (!_IDFA) {
_IDFA = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
}
return _IDFA;
}
- (void)setIsFirstIntoApp:(NSString *)isFirstIntoAppStr {
if (isFirstIntoAppStr && !isBlankString(isFirstIntoAppStr)) {
_isFirstIntoApp = isFirstIntoAppStr;
[defaults setObject:isFirstIntoAppStr forKey:@"isFirstIntoApp"];
[defaults synchronize];
}
}
- (void)setContFontSize:(CGFloat)size {
_contFontSize = size;
self.lastContentOffset = CGPointZero;
[defaults setObject:[NSString stringWithFormat:@"%f",size] forKey:@"contentFontSize"];
[defaults synchronize];
}
- (void) setUuid:(NSString *)uuidStr {
if (uuidStr && !isBlankString(uuidStr)) {
_uuid = uuidStr;
[defaults setObject:uuidStr forKey:@"uuid"];
[defaults synchronize];
}else {
CFUUIDRef puuid = CFUUIDCreate( nil );
CFStringRef uuidString = CFUUIDCreateString( nil, puuid );
NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
CFRelease(puuid);
CFRelease(uuidString);
_uuid = result;
[defaults setObject:result forKey:@"uuid"];
[defaults synchronize];
}
}
- (void)setUserBO:(userBO *)data {
_userBO = data;
if (data) {
NSDictionary *dic = setDataModelToDic(data, [userBO class]);
[defaults setObject:dic forKey:@"userBO"];
[defaults synchronize];
}else {
[defaults setObject:nil forKey:@"userBO"];
[defaults synchronize];
}
}
- (void)setReadModeStr:(NSString *)str {
_readModeStr = str;
[[NSNotificationCenter defaultCenter]postNotificationName:NEEDREFRESHCHANGEREADMODE object:nil];
if (str && !isBlankString(str)) {
[defaults setObject:str forKey:@"readMode"];
[defaults synchronize];
}else {
[defaults setObject:nil forKey:@"readMode"];
[defaults synchronize];
}
}
- (void)setNoMoreReadModeAlert:(NSString *)str {
_noMoreReadModeAlert = str;
if (str && !isBlankString(str)) {
[defaults setObject:str forKey:@"noMoreReadMode"];
[defaults synchronize];
}else {
[defaults setObject:nil forKey:@"noMoreReadMode"];
[defaults synchronize];
}
}
- (void)setIsOpenPush:(NSString *)isOpen {
_isOpenPush = isOpen;
if (isOpen) {
[defaults setObject:isOpen forKey:@"isOpenPush"];
[defaults synchronize];
}else {
[defaults setObject:nil forKey:@"isOpenPush"];
[defaults synchronize];
}
}
- (void)setOfflineList:(NSArray *)list {
_offlineList = list;
if (list.count > 0) {
NSArray* offlineStatusArray = [defaults objectForKey:@"offlineStatusList"];
if (!offlineStatusArray || offlineStatusArray.count == 0) {
NSMutableArray *tempList = [NSMutableArray array];
[list enumerateObjectsUsingBlock:^(nodeObjectBO* obj, NSUInteger idx, BOOL *stop) {
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setValue:obj.nodeId forKey:@"nodeId"];
[dic setValue:obj.name forKey:@"name"];
NSString* colorStr = [obj.color stringByReplacingOccurrencesOfString:@"#" withString:@"0x"];
[dic setValue:colorStr forKey:@"color"];
[dic setValue:@"1" forKey:@"isSelected"];
[dic setValue:nil forKey:@"updateDate"];
[dic setValue:@"0" forKey:@"progressCompletion"];
[tempList addObject:dic];
}];
self.offlineStatusList = tempList;
}else {
self.offlineStatusList = offlineStatusArray;
}
}
}
- (void)setOfflineStatusList:(NSArray *)list {
_offlineStatusList = list;
if (list.count == 0) {
return;
}
[defaults setObject:list forKey:@"offlineStatusList"];
[defaults synchronize];
}
- (void)setChannldataList:(NSMutableArray *)list {
_channldataList = list;
[defaults setObject:list forKey:@"channelData"];
[defaults synchronize];
}
- (void)setIsUseSystemFont:(NSString*)isUseSystem {
_isUseSystemFont = isUseSystem;
[defaults setObject:isUseSystem forKey:@"isUserSystemFont"];
[defaults synchronize];
}
- (void)setIsNightMode:(NSString *)nightMode {
_isNightMode = nightMode;
[defaults setObject:nightMode forKey:@"isNightMode"];
[defaults synchronize];
}
- (void)setMsgMark:(NSMutableDictionary *)msg {
_msgMark = msg;
AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
YRSideViewController *sideController=[delegate sideViewController];
if ([self isHaveUnMarkedMsg]) {
[sideController addBudgeViewInRightButton];
}else {
[sideController removeBudgeViewInRightButton];
}
}
-(void)setVisitTime:(NSString *)visitTime{
_visitTime = visitTime;
[defaults setObject:visitTime forKey:@"visitTime"];
[defaults synchronize];
}
- (void)setSearchHistoryArray:(NSMutableArray *)historyArray {
_searchHistoryArray = historyArray;
[defaults setObject:historyArray forKey:@"searchHistoryArray"];
[defaults synchronize];
}
- (BOOL)isHaveUnMarkedMsg {
if (self.msgMark[@"status"] == 0 ||[self.msgMark[@"status"] intValue] == 0) {
return NO;
}
__block BOOL isHaveUnMarked = NO;
[self.msgMark.allKeys enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL *stop) {
if (![key isEqualToString:@"status"]) {//功能页,当我的消息-回复我的和私信中有新内容时,功能页右上角三横那里没有红点提示(bug:4817)
if ([key isEqualToString:@"trackMark"] || [key isEqualToString:@"questionMark"]||[key isEqualToString:@"attendMark"] ||
[key isEqualToString:@"createdTopicMark"] || [key isEqualToString:@"attendTopicMark"] ||
[key isEqualToString:@"letterMark"] || [key isEqualToString:@"replyedMark"]) {
if ([self.msgMark[key] longValue] > 0) {
isHaveUnMarked = YES;
*stop = YES;
}
}
}
}];
return isHaveUnMarked;
}
- (void)setPushBO:(remotePushBO *)push {
if (_pushBO && [_pushBO.isComeFromRemote intValue] == 0 && [push.isComeFromRemote intValue] == 1) {
return;
}
_pushBO = push;
if (push) {
[[KGModal sharedInstance]hideAnimated:YES
withCompletionBlock:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:LINKWITHPUSH object:nil userInfo:nil];
});
}];
}
}
- (void)setIgnoreVersion:(NSString *)version {
_ignoreVersion = version;
if (version) {
[defaults setObject:version forKey:@"ignoreVersion"];
[defaults synchronize];
}else {
[defaults setObject:nil forKey:@"ignoreVersion"];
[defaults synchronize];
}
}
- (void)setIsRegistedPush:(NSString *)push {
_isRegistedPush = push;
if (push) {
[defaults setObject:push forKey:@"isRegistedPush"];
[defaults synchronize];
}else {
[defaults setObject:nil forKey:@"isRegistedPush"];
[defaults synchronize];
}
}
- (NSString*)deviceUA {
if (!_deviceUA) {
NSString *userAgent = [[[UIWebView alloc] init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
// NSString *appName = NSLocalizedStringFromTable(@"CFBundleDisplayName", @"InfoPlist", nil);
NSString * appName = [[NSBundle mainBundle] infoDictionary][@"CFBundleDisplayName"];
NSString *version = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
NSString *customUserAgent = [userAgent stringByAppendingFormat:@" %@/%@", appName, version];
_deviceUA = customUserAgent;
}
return _deviceUA;
}
-(NSString *)hostId{
if (!_hostId) {
_hostId = @"";
}
return _hostId;
}
- (void)clearCache:(id)sender {
if (sender) {
[self performSelectorOnMainThread:@selector(showClearStatus:) withObject:sender waitUntilDone:YES];
}
[[CoreDataManager shareInstance] deleteCoreDataFile];//解决版本更新数据库闪退(bug fixed:4587)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:CachePath(@"")];
NSLog(@"files: %ld",(long)files.count);
for (NSString *p in files) {
NSError *error;
NSString *path = [CachePath(@"") stringByAppendingString:p];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
}
}
[self performSelectorOnMainThread:@selector(showSuccessStatus:) withObject:sender waitUntilDone:YES];
});
}
- (void)clearCache {
[[CoreDataManager shareInstance] deleteCoreDataFileWhenUpdate]; //解决版本更新数据库闪退(bug fixed:4587)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:CachePath(@"")];
NSLog(@"files: %ld",(long)files.count);
for (NSString *p in files) {
NSError *error;
NSString *path = [CachePath(@"") stringByAppendingString:p];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
}
}
});
}
- (void)showClearStatus:(id)sender {
// ShowTextMessage(@"正在清理缓存...");
self.clearStatus = @"1";
if (sender) {
if ([sender respondsToSelector:@selector(updateCacheText:)]) {
[sender performSelectorOnMainThread:@selector(updateCacheText:) withObject:@"1" waitUntilDone:YES];
}
}
}
- (void)showSuccessStatus:(id)sender {
// ShowTextMessage(@"缓存清理成功!");
self.clearStatus = @"0";
if (sender) {
if ([sender respondsToSelector:@selector(updateCacheText:)]) {
[sender performSelectorOnMainThread:@selector(updateCacheText:) withObject:@"0" waitUntilDone:YES];
}else {
dispatch_async(dispatch_get_main_queue(), ^{
ShowMessage(@"清理成功", YES);
});
}
}else {
dispatch_async(dispatch_get_main_queue(), ^{
ShowMessage(@"清理成功", YES);
});
}
}
- (void)dismissStatus {
// [KGStatusBar dismiss];
}
- (void)checkMsgMarkStatus {
if (!self.userBO) {
return;
}
[Remote doJsonActionWithBlock:0 requestUrl:getMsgMarkURL parameter:nil withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
self.msgMark = responseData[@"data"];
}else {
ShowTextMessage(message);
}
}];
}
- (void)setIsAttention:(NSString *)obj {
_isAttention = obj;
[defaults setObject:obj forKey:@"isAttention"];
[defaults synchronize];
}
- (NSString *)isAttention {
return _isAttention;
}
@end
|