|
//
// TPWebContentViewModel.m
// ThePaperBase
//
// Created by scar1900 on 15/8/10.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "TPWebContentViewModel.h"
#define getPageInfoTag 100
#define getCommentInfoTag 101
#define getAskInfoTag 102
#define collectTag 1002
#define cancelCollectTag 1003
@interface TPWebContentViewModel()<RemoteDelegate>
@property(nonatomic, assign)NSInteger actionFlag;
@property(nonatomic, strong)contDetailPageVO *detailContentBO;
@property(nonatomic, strong)contentObjectBO *contentBO;
@end
@implementation TPWebContentViewModel
- (instancetype)init {
self = [super init];
if (self) {
}
return self;
}
#pragma mark - set and get method
- (void)setActionFlag:(NSInteger)flag {
_actionFlag = flag;
if (flag == 0) {
if ([self.delegate respondsToSelector:@selector(stopHud)]) {
[self.delegate stopHud];
}
}
}
- (void)setDetailContentBO:(contDetailPageVO *)data {
_detailContentBO = data;
self.contentBO = setJsonDicToDataModel(data.content, [contentObjectBO class]);
if ([self.delegate respondsToSelector:@selector(returnDetailPageInfo:)]) {
[self.delegate returnDetailPageInfo:data];
}
}
#pragma mark - remote action
- (void)remoteAction {
if ([self.delegate respondsToSelector:@selector(startHud)]) {
[self.delegate startHud];
}
self.actionFlag = 3;
NSDictionary *dic = @{@"c":self.nodeID};
[Remote doJsonAction:getPageInfoTag
requestUrl:contentDetailPageURL
parameter:dic
delegate:self];
[Remote doJsonAction:getCommentInfoTag
requestUrl:contentCommentURL
parameter:dic
delegate:self];
[Remote doJsonAction:getAskInfoTag
requestUrl:contentAskURL
parameter:dic
delegate:self];
}
- (void)askForContentInfo {
NSDictionary *dic = @{@"c":self.nodeID};
[Remote doJsonAction:getPageInfoTag
requestUrl:contentDetailPageURL
parameter:dic
delegate:self];
}
- (void)collectContent{
if (!self.contentBO) {
return;
}
if ([self.delegate respondsToSelector:@selector(startHud)]) {
[self.delegate startHud];
}
NSDictionary *dic = @{@"cids":self.contentBO.contId};
[Remote doJsonAction:collectTag
requestUrl:addFavoriteURL
parameter:dic
delegate:self];
}
- (void)cancelCollect {
if (!self.contentBO) {
return;
}
if ([self.delegate respondsToSelector:@selector(startHud)]) {
[self.delegate startHud];
}
NSDictionary *dic = @{@"cids":self.contentBO.contId};
[Remote doJsonAction:cancelCollectTag
requestUrl:deleteFavoriteURL
parameter:dic
delegate:self];
}
#pragma mark - remote delegate
- (void)startWaitCursor:(int)actionTag {
}
- (void)stopWaitCursor:(int)actionTag {
}
- (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData {
if (actionTag == getPageInfoTag) { //主数据源
self.actionFlag = self.actionFlag -1;
contDetailPageVO* dataModal = setJsonDicToDataModel(responsData, [contDetailPageVO class]);
self.detailContentBO = dataModal?dataModal:nil;
}else if (actionTag == getCommentInfoTag) {//评论
self.actionFlag = self.actionFlag -1;
NSMutableArray* commentList = [NSMutableArray array];
NSMutableArray *hotList = [NSMutableArray array];
NSArray *list = responsData[@"commentList"];
NSArray *hotDicList = responsData[@"hotList"];
[list enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) {
commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]);
commentBO = setStrSpaceForParagraphSpacingWithCommentBO(commentBO);
[commentList addObject:commentBO];
}];
[hotDicList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]);
commentBO = setStrSpaceForParagraphSpacingWithCommentBO(commentBO);
[hotList addObject:commentBO];
}];
NSMutableArray *commentDataList = [NSMutableArray array];
if (hotList.count > 0) {
[commentDataList addObject:hotList];
}
[commentDataList addObject:commentList];
if ([self.delegate respondsToSelector:@selector(returnCommentList:nodeId:nextUrl:commentTotalRecord:)]) {
NSMutableArray *array = [NSMutableArray array];
// if (isPad) {
// array = [NSMutableArray arrayWithArray:commentList];
// }else {
array = [NSMutableArray arrayWithArray:commentDataList];
// }
[self.delegate returnCommentList:array
nodeId:self.nodeID
nextUrl:responsData[@"nextUrl"]
commentTotalRecord:[responsData[@"recordTotal"] integerValue]];
}
}else if (actionTag == getAskInfoTag) {//热追问
self.actionFlag = self.actionFlag -1;
NSMutableDictionary *qaDataDic = [NSMutableDictionary dictionary];
NSArray *hotQaList = responsData[@"hotQaList"];
NSMutableArray *hotQaArray = [NSMutableArray array];
[hotQaList enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) {
commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]);
commentBO = setStrSpaceForParagraphSpacingWithCommentBO(commentBO);
[hotQaArray addObject:commentBO];
}];
[qaDataDic setValue:hotQaArray forKey:@"hotQaList"];
NSArray *qaTempList = responsData[@"qaList"];
NSMutableArray *qaArray = [NSMutableArray array];
[qaTempList enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) {
commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]);
commentBO = setStrSpaceForParagraphSpacingWithCommentBO(commentBO);
[qaArray addObject:commentBO];
}];
[qaDataDic setValue:qaArray forKey:@"qaList"];
if ([self.delegate respondsToSelector:@selector(returnHotAskDic:nodeId:nextUrl:askTotalRecord:)]) {
[self.delegate returnHotAskDic:qaDataDic
nodeId:self.nodeID
nextUrl:responsData[@"nextUrl"]
//无网络:外链文章,关于互动列表有两个症状(bug:6093)
// askTotalRecord:[responsData[@"recordTotal"] integerValue]
askTotalRecord:[responsData[@"qaNum"] integerValue]];
}
}else if (actionTag == collectTag) {
[MobClick event:@"7"];
ShowMessage(@"收藏成功", YES);
if ([self.delegate respondsToSelector:@selector(stopHud)]) {
[self.delegate stopHud];
}
if ([self.delegate respondsToSelector:@selector(collectResult:)]) {
[self.delegate collectResult:YES];
}
}else if (actionTag == cancelCollectTag) {
ShowMessage(@"取消收藏", YES);
if ([self.delegate respondsToSelector:@selector(stopHud)]) {
[self.delegate stopHud];
}
if ([self.delegate respondsToSelector:@selector(collectResult:)]) {
[self.delegate collectResult:NO];
}
}
}
- (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 == getPageInfoTag && [message isEqualToString:@"请求超时,请稍后重试!"]) {
isTimeOut = YES;
}else {
ShowTextMessage(message);
}
if ([self.delegate respondsToSelector:@selector(remoteFail:)]) {
[self.delegate remoteFail:isTimeOut];
}
}
}
@end
|