|
//
// specialTopicViewModel.m
// ThePaperBase
//
// Created by scar1900 on 15/7/22.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "specialTopicViewModel.h"
@interface specialTopicViewModel()<RemoteDelegate>
@property(nonatomic, strong)SpecialObjectDB *currentSpecialDB;
@end
@implementation specialTopicViewModel
@synthesize nodeId;
@synthesize currentSpecialDB;
- (instancetype)init {
self = [super init];
if (self) {
}
return self;
}
#pragma mark - set and get method
#pragma mark - public method && private method
- (void)remoteAction {
NSDictionary *dic = @{@"c":self.nodeId};
[Remote doJsonAction:1
requestUrl:specialDetailPageURL
parameter:dic
delegate:self];
}
- (void)checkContentDataInCoreData:(NSString*)nodeIDstr table:(UITableView*)table{
self.nodeId = nodeIDstr;
NSError *error;
NSFetchRequest *request = [[NSFetchRequest alloc]init];
//为已创建好的实体利用检索到的上下文创建一个实体描述
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"SpecialObjectDB"
inManagedObjectContext:[CoreDataManager shareInstance].managedObjectContext];
[request setEntity:entityDescription];
//确定持久库中是否存在与此字段相对应的托管对象,所以穿件一个谓词来确定字段的正确对象:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"nodeId == %@",nodeIDstr];
[request setPredicate:pred];
SpecialObjectDB *specialInfoDB = nil;
NSArray *objs = [[CoreDataManager shareInstance].managedObjectContext executeFetchRequest:request error:&error];
if (objs == nil) {
TPLOG(@"there was an error!!");
}
if (objs.count > 0) {
specialInfoDB = [objs objectAtIndex:0];
self.currentSpecialDB = specialInfoDB;
specialObjectBO* specialBO = makeCoreDataModelToRemoteModel(specialInfoDB, [specialObjectBO class]);
TPLOG(@"get from core data first!!");
NSArray *tempList = specialBO.childNodeList;
NSMutableArray* nodeList = [NSMutableArray array];
NSMutableArray* contentList = [NSMutableArray array];
NSMutableArray *tempDataSource = [NSMutableArray array];
[tempDataSource addObject:specialBO];
[tempList enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) {
nodeObjectBO *nodeBO = setJsonDicToDataModel(dic, [nodeObjectBO class]);
int readmode = [[TPUserDefault instance].readModeStr intValue];
if (readmode == intelligentMode) {
if ([Remote IsEnableWIFI]) {
readmode = imageMode;
}else {
readmode = textMode;
}
}
if (readmode == imageMode) {
[tempDataSource addObject:nodeBO];
[contentList addObject:nodeBO];
}else {
if ([nodeBO.dataType integerValue] == 2 || [nodeBO.dataType integerValue] == 3) {
}else {
[tempDataSource addObject:nodeBO];
[contentList addObject:nodeBO];
}
}
}];
nodeList = [NSMutableArray arrayWithArray:tempDataSource];
if ([self.delegate respondsToSelector:@selector(returnNodeList:contentList:specialBO:)]) {
[self.delegate returnNodeList:nodeList contentList:contentList specialBO:specialBO];
}
}else {
[self remoteAction];
return;
}
if ([TPUserDefault instance].lastContentID && [[TPUserDefault instance].lastContentID isEqualToString:nodeIDstr] && objs.count > 0) {
[table setContentOffset:[TPUserDefault instance].lastContentOffset
animated:NO];
}else {
[self remoteAction];
}
}
#pragma mark - remote delgate
- (void)startWaitCursor:(int)actionTag {
}
- (void)stopWaitCursor:(int)actionTag {
}
- (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData {
if (actionTag == 1) {
if (responsData) {
NSMutableArray* nodeList = [NSMutableArray array];
NSMutableArray* contentList = [NSMutableArray array];
NSMutableArray *tempDataSource = [NSMutableArray array];
specialObjectBO* specialBO = setJsonDicToDataModel(responsData[@"specialInfo"], [specialObjectBO class]);
[tempDataSource addObject:specialBO];
NSArray *tempList = responsData[@"childNodeList"];
specialBO.childNodeList = tempList;
if (self.currentSpecialDB) {
[[CoreDataManager shareInstance].managedObjectContext deleteObject:self.currentSpecialDB];
}
SpecialObjectDB *specialInfoDB = nil;
specialInfoDB = [NSEntityDescription insertNewObjectForEntityForName:@"SpecialObjectDB"
inManagedObjectContext:[CoreDataManager shareInstance].managedObjectContext];
specialInfoDB = makeRemoteModelToCoreDataModel(specialBO, [specialObjectBO class],specialInfoDB);
specialInfoDB.date = getLocalDate();
[[CoreDataManager shareInstance] saveContext];
[tempList enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) {
nodeObjectBO *nodeBO = setJsonDicToDataModel(dic, [nodeObjectBO class]);
int readmode = [[TPUserDefault instance].readModeStr intValue];
if (readmode == intelligentMode) {
if ([Remote IsEnableWIFI]) {
readmode = imageMode;
}else {
readmode = textMode;
}
}
if (readmode == imageMode) {
[tempDataSource addObject:nodeBO];
[contentList addObject:nodeBO];
}else {
if ([nodeBO.dataType integerValue] == 2 || [nodeBO.dataType integerValue] == 3) {
}else {
[tempDataSource addObject:nodeBO];
[contentList addObject:nodeBO];
}
}
}];
nodeList = [NSMutableArray arrayWithArray:tempDataSource];
if ([self.delegate respondsToSelector:@selector(returnNodeList:contentList:specialBO:)]) {
[self.delegate returnNodeList:nodeList contentList:contentList specialBO:specialBO];
}
}
}
}
- (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code {
if ([code intValue] == 5) {
if ([self.delegate respondsToSelector:@selector(contentHaveOffline)]) {
[self.delegate contentHaveOffline];
}
}else {
BOOL isTimeOut = NO;
if (actionTag == 1 && [message isEqualToString:@"请求超时,请稍后重试!"]) {
isTimeOut = YES;
}else {
ShowTextMessage(message);
}
if ([self.delegate respondsToSelector:@selector(remoteFail:)]) {
[self.delegate remoteFail:isTimeOut];
}
}
}
@end
|