//
// testHtmlContentController.m
// ThePaperBase
//
// Created by scar1900 on 16/2/16.
// Copyright © 2016年 scar1900. All rights reserved.
//
#import "testHtmlContentController.h"
#import "MGTemplateEngine.h"
#import "ICUTemplateMatcher.h"
#import "SDWebImageManager.h"
#import "SDImageCache.h"
#import "WebViewJavascriptBridge.h"
#import "TPImageScannerController.h"
#import "sinaAndWeChatCell.h"
#import "nodeInfoViewCell.h"
#import "relateNewCell.h"
#import "hitAnwerCell.h"
#import "reTopicCell.h"
#import "AskIconBaseCell.h"
#import "AskContentBaseCell.h"
#import "AnswerContentBaseCell.h"
#import "AnswerIconBaseCell.h"
#import "TrackCell.h"
#import "shareContent.h"
#import "bannerCell.h"
#import "contHeadBannerCell.h"
#import "columnManagerController.h"
#import "askDetailController.h"
#import "loginHomeController.h"
#import "detailPageViewModel.h"
#import "topicContentController.h"
#define NAVBAR_HEIGHT 63
@interface testHtmlContentController () {
CGFloat contentPadding;
CGFloat tableTotalHeight;
CGFloat webHeight;
CGRect videoRect;
}
@property(nonatomic, strong)contentObjectBO *contentBO; //内容数据
@property(nonatomic, strong)WebViewJavascriptBridge *bridge;
@property(nonatomic, strong)NSMutableArray *imageBOList;
@property(nonatomic, strong)NSMutableArray *videoList;
@property(nonatomic, strong)NSMutableArray *strongRelateList;
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *contentDataList;
@property(nonatomic, strong)NSMutableArray *heightOfCellInEachSection; //cell高度数组
@property(nonatomic, strong)detailPageViewModel *viewModel;
@property(nonatomic, strong)videoObjectBO *videoBO;
@end
@implementation testHtmlContentController
@synthesize detailContentBO = _detailContentBO;
@synthesize videoBO;
@synthesize delegate;
static NSString *reTopicCellID = @"reTopicCell";
static NSString *ShareBtnCellID = @"ShareBtnCell";
static NSString *BannerCellID = @"BannerCell";
static NSString *nodeInfoViewCellID = @"nodeInfoViewCell";
static NSString *relateNewCellID = @"relateNewCell";
static NSString *sectionFootID = @"sectionFoot";
static NSString *askContentCellID = @"askContentCell";
static NSString *answerContentCellID = @"answerContentCell";
static NSString *trackCellID = @"trackCell";
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor colorWithHexString:ContentDetailBack];
[self.view addSubview:self.webView];
[self initJSbirdge];
if (IS_IPHONE_6P) {
contentPadding = 15;
}else if (IS_IPHONE_6) {
contentPadding = 15;
}else {
contentPadding = 10;
}
self.viewModel = [detailPageViewModel new];
self.viewModel.delegate = self;
((MLNavigationController *)self.navigationController).canDragBack = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil];
}
//- (void)viewWillAppear:(BOOL)animated {
// [super viewWillAppear:animated];
// if (_tableView) {
// CGSize contentSize = self.webView.scrollView.contentSize;
// self.webView.scrollView.contentSize = CGSizeMake(contentSize.width, webHeight);
// }
//}
- (void)initJSbirdge {
// [WebViewJavascriptBridge enableLogging];
_bridge = [WebViewJavascriptBridge bridgeForWebView:_webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
// NSLog(@"ObjC received message from JS: %@", data);
responseCallback(@"Response for message from ObjC");
}];
[_bridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) {
// NSLog(@"testObjcCallback called: %@", data);
// responseCallback(@"Response from testObjcCallback");
if ([data isMatchedByRegex:@"_imageClick"]) {
NSString *imageId = [data stringByReplacingOccurrencesOfString:@"_imageClick" withString:@""];
[self imageClick:imageId];
}
if ([data isMatchedByRegex:@"_videoClick"]) {
NSString *imageId = [data stringByReplacingOccurrencesOfString:@"_videoClick" withString:@""];
[self videoClick:imageId];
}
if ([data isMatchedByRegex:@"_strongRelateClick"]) {
NSString *imageId = [data stringByReplacingOccurrencesOfString:@"_strongRelateClick" withString:@""];
[self strongRelateClick:imageId];
}
}];
}
- (void)setDetailContentBO:(contDetailPageVO *)data {
if (!_detailContentBO) {
_detailContentBO = data;
self.contentBO = setJsonDicToDataModel(data.content, [contentObjectBO class]);
self.imageBOList = [NSMutableArray array];
self.videoList = [NSMutableArray array];
self.strongRelateList = [NSMutableArray array];
[self htmlStringAfterGettingData];
}
}
#pragma mark - get method(webView and tableView)
- (UIWebView*)webView {
if (!_webView) {
_webView = [[UIWebView alloc] init];
_webView.backgroundColor = [UIColor colorWithHexString:ContentDetailBack];
_webView.scalesPageToFit = NO;
_webView.delegate = self;
_webView.scrollView.decelerationRate = 1.0f;
}
return _webView;
}
- (UITableView*)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.delaysContentTouches = NO;
_tableView.scrollEnabled = NO;
}
return _tableView;
}
#pragma mark - do something after getting data
- (void)htmlStringAfterGettingData {
MGTemplateEngine *engine = [MGTemplateEngine templateEngine];
[engine setDelegate:self];
[engine setMatcher:[ICUTemplateMatcher matcherWithTemplateEngine:engine]];
//设置字体大小
[_bridge send:[NSString stringWithFormat:@"changeFontSize,%fpx",[TPUserDefault instance].contFontSize]];
//设置夜间模式日间模式
[_bridge send:[NSString stringWithFormat:@"switchNightMode,%@",[TPUserDefault instance].isNightMode]];
NSString *fontName = @"";
if ([[TPUserDefault instance].isUseSystemFont intValue] == 1) {
fontName = @"FZBIAOYSK--GBK1-0";
}
//标题
NSString *titleTemplatePath = [[NSBundle mainBundle] pathForResource:@"title" ofType:@"html"];
NSDictionary *titleDic = @{@"title":self.contentBO.name,@"fontName":fontName};
NSString *title = [engine processTemplateInFileAtPath:titleTemplatePath withVariables:titleDic];
title = [title stringByAppendingString:@"
"];
//作者时间
NSString *authorAndTimeTemplatePath = [[NSBundle mainBundle] pathForResource:@"authorAndTime" ofType:@"html"];
NSString *time = self.contentBO.pubTime?self.contentBO.pubTime:@"";
if (self.contentBO.pubTime) {
NSString *timeStr = self.contentBO.pubTime;
NSDate *date = DateFromString(timeStr, @"yyyy-MM-dd mm:ss");
NSString *timeTextStr = StringFromDate(date, @"YYYY-MM-dd mm:ss");
time = timeTextStr;
if (!isBlankString(timeStr) && isBlankString(timeTextStr)) {
time = timeStr;
}
/**
* bug:6169(离线缓存-新闻详情页:离线缓存的新闻详情页里面没有时间显示)
*/
}else time = @"";
NSDictionary *authorAndTimeDic = @{@"author":self.contentBO.author,@"time":time,@"fontName":fontName};
NSString *authorAndTime = [engine processTemplateInFileAtPath:authorAndTimeTemplatePath withVariables:authorAndTimeDic];
authorAndTime = [authorAndTime stringByAppendingString:@"
"];
//正文,图片
NSArray *contentList = self.contentBO.content;
__block NSString *contentStr = @"";
__block BOOL hasStrongNews = YES;
[contentList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *contentObj = obj[@"content"];
//处理html换行
contentObj = [contentObj stringByReplacingOccurrencesOfString:@"\n" withString:@""];
//处理下划线距离
if (!isBlankString(fontName)) {//宋体
contentObj = [contentObj stringByReplacingOccurrencesOfString:@"" withString:@""];
}else {//系统字体
contentObj = [contentObj stringByReplacingOccurrencesOfString:@"" withString:@""];
}
contentObj = [contentObj stringByReplacingOccurrencesOfString:@"" withString:@""];
contentStr = [contentStr stringByAppendingString:contentObj];
NSArray *strongRelateArray = self.detailContentBO.strongRelateConts; //强相关新闻数据源
if (strongRelateArray.count > 0) {
if (hasStrongNews) {
[strongRelateArray enumerateObjectsUsingBlock:^(id item, NSUInteger index, BOOL *stop1) {
NSString *strongRelateTemplatePath = [[NSBundle mainBundle] pathForResource:@"strongRelateNews" ofType:@"html"];
NSDictionary *dic = item;
listContObjectVO *listBO = setJsonDicToDataModel(dic, [listContObjectVO class]);
imageObjectBO *strongImageBO = [imageObjectBO new];
strongImageBO.url = listBO.pic;
strongImageBO.width = [NSString stringWithFormat:@"%f",rect_screen.size.width-20];
strongImageBO.height = [NSString stringWithFormat:@"%f",125*(rect_screen.size.width-20)/300];
NSString *imageWidth = [NSString stringWithFormat:@"%f",[strongImageBO.width floatValue]];
NSString *imageHeigth = [NSString stringWithFormat:@"%f",[strongImageBO.height floatValue]];
NSString *totalHeight = [NSString stringWithFormat:@"%f",[strongImageBO.height floatValue] + 5];
NSString *centerImgY = [NSString stringWithFormat:@"%f",-[totalHeight floatValue]-25];
NSDictionary *strongDic = @{@"imageName":[self replaceUrlSpecialString:strongImageBO.url],@"totalHeight":totalHeight,@"width":imageWidth,@"height":imageHeigth,@"centerImgY":centerImgY,@"strongRelateText":listBO.name,@"fontName":fontName};
NSString *strongStr = [engine processTemplateInFileAtPath:strongRelateTemplatePath withVariables:strongDic];
strongStr = [strongStr stringByAppendingString:@"
"];
contentStr = [contentStr stringByAppendingString:strongStr];
[self getOrDownloadImageWithCache:strongImageBO];
[self.strongRelateList addObject:listBO];
}];
hasStrongNews = NO;
}
}
NSArray *imageList = obj[@"imageInfoList"];
if (imageList && imageList.count > 0) {
[imageList enumerateObjectsUsingBlock:^(id _Nonnull imageDic, NSUInteger index, BOOL * _Nonnull stop1) {
imageObjectBO *imageBO = setJsonDicToDataModel(imageDic, [imageObjectBO class]);
NSString *imageTemplatePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"html"];
NSString *imageWidth = [NSString stringWithFormat:@"%f",[imageBO.width floatValue]/2];
NSString *imageHeigth = [NSString stringWithFormat:@"%f",[imageBO.height floatValue]/2];
NSString *totalHeight = [NSString stringWithFormat:@"%f",[imageBO.height floatValue]/2 + 5];
NSString *centerImgY = [NSString stringWithFormat:@"%f",-[totalHeight floatValue]/2-25];
NSDictionary *imageVDic = @{@"imageName":[self replaceUrlSpecialString:imageBO.url],@"totalHeight":totalHeight,@"width":imageWidth,@"height":imageHeigth,@"centerImgY":centerImgY,@"imageText":imageBO.desc,@"fontName":fontName};
NSString *imageStr = [engine processTemplateInFileAtPath:imageTemplatePath withVariables:imageVDic];
imageStr = [imageStr stringByAppendingString:@"
"];
contentStr = [contentStr stringByAppendingString:imageStr];
[self getOrDownloadImageWithCache:imageBO];
[self.imageBOList addObject:imageBO];
}];
}
}];
NSDictionary *contentDic = @{@"content":contentStr,@"fontName":fontName};
NSString *contentTemplatePath = [[NSBundle mainBundle] pathForResource:@"content" ofType:@"html"];
NSString *contentString = [engine processTemplateInFileAtPath:contentTemplatePath withVariables:contentDic];
//图片和视频混排模板内容
NSString *structString = @"http://m.thepaper.cn";
imageObjectBO *structImageBO = [imageObjectBO new];
structImageBO.url = @"http://image.thepaper.cn/clt/image/4/812/605.jpg";
structImageBO.name = @"圣·托马斯合唱团";
structImageBO.tags = @"www_head";
structImageBO.width = @"610";
structImageBO.height = @"350";
videoObjectBO *structVideoBO = [videoObjectBO new];
structVideoBO.url = @"http://video.thepaper.cn/clt/video/0/10/285.mp4";
structVideoBO.name = @"《马太受难曲》开端合唱《来吧,女儿们,分担我的悲恸》片段。(06:55)";
structVideoBO.tags = @"";
structVideoBO.duration = @"415.0";
structVideoBO.url2 = @"http://video.thepaper.cn/clt/video/0/10/286.mp4";
imageObjectBO *structVideoImageBO = [imageObjectBO new];
structVideoImageBO.url = @"http://image.thepaper.cn/clt/image/4/812/610.jpg";
structVideoImageBO.name = @"布商大厦管弦乐团2";
structVideoImageBO.tags = @"www_video";
structVideoImageBO.width = @"600";
structVideoImageBO.height = @"338";
NSDictionary *videoDic = @{structVideoImageBO.url:structVideoBO};
NSString *struckTemplatePath = [[NSBundle mainBundle] pathForResource:@"imageAndVideo" ofType:@"html"];
NSDictionary *structDic = @{@"imageName":[self replaceUrlSpecialString:structImageBO.url],@"videoImgUrl":[self replaceUrlSpecialString:structVideoImageBO.url],@"structString":structString,@"fontName":fontName};
NSString *structStr = [engine processTemplateInFileAtPath:struckTemplatePath withVariables:structDic];
[self getOrDownloadImageWithCache:structImageBO];
[self getOrDownloadImageWithCache:structVideoImageBO];
[self.imageBOList addObject:structImageBO];
[self.videoList addObject:videoDic];
contentString = [contentString stringByAppendingString:structStr];
NSString *mainTemplatePath = [[NSBundle mainBundle] pathForResource:@"mainTemplate" ofType:@"html"];
//html最终全文
NSString *mainString = [title stringByAppendingString:authorAndTime];
mainString = [mainString stringByAppendingString:contentString];
NSDictionary *mainDic = @{@"mainNews":mainString};
NSString *result = [engine processTemplateInFileAtPath:mainTemplatePath withVariables:mainDic];
NSURL *baseURL = [NSURL fileURLWithPath:mainTemplatePath];
[self.webView loadHTMLString:result baseURL:baseURL];
}
- (void)setTableDataSource {
self.contentDataList = [NSMutableArray array];
self.heightOfCellInEachSection = [NSMutableArray array];
NSMutableArray *sectionOneArray = [NSMutableArray array];
NSMutableArray *sectionOneHeightArray = [NSMutableArray array];
tableTotalHeight = 0;
NSArray *reTopics = self.detailContentBO.relateTopics?self.detailContentBO.relateTopics:[NSArray array];//强关联话题数据源
[reTopics enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) {
TopicInfoBO *topic = setJsonDicToDataModel(dic, [TopicInfoBO class]);
[sectionOneArray addObject:topic];
if (idx != reTopics.count -1) {
tableTotalHeight = tableTotalHeight + 225*(rect_screen.size.width-2*contentPadding)/300 + 20;
[sectionOneHeightArray addObject:[NSString stringWithFormat:@"%f",225*(rect_screen.size.width-2*contentPadding)/300 + 20]];
}else{
tableTotalHeight = tableTotalHeight + 225*(rect_screen.size.width-2*contentPadding)/300 + 35;
[sectionOneHeightArray addObject:[NSString stringWithFormat:@"%f",225*(rect_screen.size.width-2*contentPadding)/300 + 35]];
}
}];
//share
[sectionOneArray addObject:@"shareCell"];
[sectionOneHeightArray addObject:@"50"];
tableTotalHeight = tableTotalHeight + 50;
//node info
nodeObjectBO *nodeInfo = setJsonDicToDataModel(self.contentBO.nodeInfo, [nodeObjectBO class]);
[sectionOneArray addObject:nodeInfo];
[sectionOneHeightArray addObject:@"40"];
tableTotalHeight = tableTotalHeight + 40;
//track
NSDictionary *dic = [[NSDictionary alloc] init];
dic = @{@"content":self.contentBO.trackKeyword,@"isTrack":self.contentBO.isTracked};
[sectionOneArray addObject:dic];
[sectionOneHeightArray addObject:@"70"];
tableTotalHeight = tableTotalHeight + 70;
//ad banner (TODO)
//secion1结束
[self.contentDataList addObject:sectionOneArray];
[self.heightOfCellInEachSection addObject:sectionOneHeightArray];
//热追问
NSMutableArray *qaList = [NSMutableArray array];
NSArray *tempQaList = self.detailContentBO.qaList; //热追问数据源
NSMutableArray *qaHeightArray = [NSMutableArray array];
[tempQaList enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) {
commentObjectVO *commentBO = setJsonDicToDataModel(dic, [commentObjectVO class]);
NSString *newNums = dic[@"newNums"];
if (newNums) {
commentBO.unNums = newNums;
}
commentBO.type = @"24";
[qaHeightArray addObject:@"50"];
[qaList addObject:commentBO];
commentObjectVO *comment = setJsonDicToDataModel(dic, [commentObjectVO class]);
comment.type = @"2";
CGFloat askHeight = 15;
int LabelHeigth = (int)[TPEmojiLabel heightForLabelWithFont:appFont(TEXT_FOUR_LEVELSIZE, NO) Width:rect_screen.size.width - 20 Text:comment.content LineSpace:10 ParagraphSpacing:0];
comment.labelHeight = LabelHeigth;
[qaList addObject:comment];
askHeight += LabelHeigth;
[qaHeightArray addObject:[NSString stringWithFormat:@"%f",askHeight]];
// CGFloat titleHeight = 65+returnTextHeightWithRTLabel(commentBO.content,rect_screen.size.width - 20, appFont(15, NO),10); //(姓名+内容高度)
// [qaHeightArray addObject:[NSString stringWithFormat:@"%f", titleHeight]];
if (commentBO.answerList && commentBO.answerList.count > 0) {
commentObjectVO *answerBO = setJsonDicToDataModel(commentBO.answerList[0], [commentObjectVO class]);
answerBO.type = @"34";
[qaHeightArray addObject:@"50"];
[qaList addObject:answerBO];
commentObjectVO *answer = setJsonDicToDataModel(comment.answerList[0], [commentObjectVO class]);
answer.type = @"3";
answer.askCommentBO = comment;
CGFloat anserHeight = 10;
int anserH = (int)[TPEmojiLabel heightForLabelWithFont:appFont(TEXT_FOUR_LEVELSIZE, NO) Width:rect_screen.size.width - 20 Text:answer.content LineSpace:10 ParagraphSpacing:0];
answer.labelHeight = anserH;
[qaList addObject:answer];
if (anserH > getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE)) {
anserH = getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE);
anserHeight += 25;
}
anserHeight += anserH;
[qaHeightArray addObject:[NSString stringWithFormat:@"%f",anserHeight]];
// [qaList addObject:answer]; //answerCommentBO
}
}];
if (qaList.count > 0) {
[qaList addObject:@"1"];
[qaHeightArray addObject:@"15"];
[self.contentDataList addObject:qaList];
[self.heightOfCellInEachSection addObject:qaHeightArray];
tableTotalHeight = tableTotalHeight + 30;
[qaHeightArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
tableTotalHeight = tableTotalHeight + [obj floatValue];
}];
}
//相关新闻
NSMutableArray* relateList = [NSMutableArray array];
NSMutableArray* relateHeightArray = [NSMutableArray array];
__block CGFloat relateHeight = 0;
NSArray *relateConts = self.detailContentBO.relateConts?self.detailContentBO.relateConts:[NSArray array]; //关联文章数据源
[relateConts enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) {
listContObjectVO *listBO = setJsonDicToDataModel(dic, [listContObjectVO class]);
[relateList addObject:listBO];
NSString *name = listBO.name;
CGFloat hei = returnTextHeightWithRTLabel(name, rect_screen.size.width-2*contentPadding, appFont(33.2/2, NO),7)+35;
relateHeight = relateHeight + hei;
[relateHeightArray addObject:[NSString stringWithFormat:@"%f", hei]];
}];
if (relateList.count > 0) {
[self.contentDataList addObject:relateList];
[self.heightOfCellInEachSection addObject:relateHeightArray];
tableTotalHeight = tableTotalHeight + 30;
[relateHeightArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
tableTotalHeight = tableTotalHeight + [obj floatValue];
}];
}
}
#pragma mark - template delegate
- (void)templateEngine:(MGTemplateEngine *)engine blockStarted:(NSDictionary *)blockInfo
{
// NSLog(@"Started block %@", [blockInfo objectForKey:BLOCK_NAME_KEY]);
}
- (void)templateEngine:(MGTemplateEngine *)engine blockEnded:(NSDictionary *)blockInfo
{
// NSLog(@"Ended block %@", [blockInfo objectForKey:BLOCK_NAME_KEY]);
}
- (void)templateEngineFinishedProcessingTemplate:(MGTemplateEngine *)engine
{
// NSLog(@"Finished processing template.");
}
- (void)templateEngine:(MGTemplateEngine *)engine encounteredError:(NSError *)error isContinuing:(BOOL)continuing;
{
// NSLog(@"Template error: %@", error);
}
#pragma mark - downloadImage
- (void)getOrDownloadImageWithCache:(imageObjectBO*)imageBO {
SDWebImageManager *imageManager = [SDWebImageManager sharedManager];
[imageManager setCacheKeyFilter:^(NSURL *url) {
url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path];
NSString *str = [self replaceUrlSpecialString:[url absoluteString]];
return str;
}];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"];
NSURL *imageUrl = [NSURL URLWithString:imageBO.url];
if ([imageManager diskImageExistsForURL:imageUrl]) {
NSString *cacheKey = [imageManager cacheKeyForURL:imageUrl];
NSString *imagePaths = [imageManager.imageCache cachePathForKey:cacheKey inPath:filePath];
// NSLog(@"imagePaths === %@",imagePaths);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_bridge send:[NSString stringWithFormat:@"replaceimage%@,%@",[self replaceUrlSpecialString:imageBO.url],imagePaths]];
[_bridge send:[NSString stringWithFormat:@"disappearCenterImg%@,%@",[self replaceUrlSpecialString:imageBO.url],imagePaths]];
});
}else {
[imageManager downloadImageWithURL:imageUrl options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image && finished) {//如果下载成功
NSString *cacheKey = [imageManager cacheKeyForURL:imageUrl];
NSString *imagePaths = [imageManager.imageCache cachePathForKey:cacheKey inPath:filePath];
// NSLog(@"imagePaths === %@",imagePaths);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_bridge send:[NSString stringWithFormat:@"replaceimage%@,%@",[self replaceUrlSpecialString:imageBO.url],imagePaths]];
[_bridge send:[NSString stringWithFormat:@"disappearCenterImg%@,%@",[self replaceUrlSpecialString:imageBO.url],imagePaths]];
});
}else {
}
}];
}
}
- (NSString *)replaceUrlSpecialString:(NSString *)string {
return [string stringByReplacingOccurrencesOfString:@"/"withString:@"_"];
}
#pragma mark - image delegate
- (void)imageClick:(NSString*)imageId {
if ([imageId isMatchedByRegex:@"http:__"]) {
imageId = [imageId stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
}
NSMutableArray *imageBOArray = [NSMutableArray array];
if (self.imageBOList.count > 0) {
[self.imageBOList enumerateObjectsUsingBlock:^(imageObjectBO* _Nonnull imageBO, NSUInteger idx, BOOL * _Nonnull stop) {
if (!isBlankString(imageBO.tags) && ![imageBO.tags isEqualToString:@"www_video"] && ![imageBO.tags isEqualToString:@"www_big"]) {
[imageBOArray addObject:imageBO];
}
}];
}
__block NSInteger currentIndex;
[imageBOArray enumerateObjectsUsingBlock:^(imageObjectBO* obj, NSUInteger idx, BOOL *stop) {
if ([obj.url isEqualToString:imageId]) {
currentIndex = idx;
}
}];
TPImageScannerController *imageScanner = [[TPImageScannerController alloc]init];
[self.navigationController pushViewController:imageScanner animated:NO];
[UIView transitionWithView:self.navigationController.view duration:0.35f options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:^(BOOL finished) {
imageScanner.currentIndex = currentIndex;
imageScanner.imageBOList = imageBOArray;
imageScanner.type = contentImageScannerType;
imageScanner.delegate = self;
}];
}
- (void)videoClick:(NSString*)videoImageId {
if ([videoImageId isMatchedByRegex:@"http:__"]) {
videoImageId = [videoImageId stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
}
[self.videoList enumerateObjectsUsingBlock:^(NSDictionary* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.allKeys containsObject:videoImageId]) {
self.videoBO = obj[videoImageId];
}
}];
if (self.moviePlayerController) {
return;
}
[MobClick event:@"9"];
if ([self.delegate respondsToSelector:@selector(didPlayVedio)]) {
[self.delegate didPlayVedio];
}
((MLNavigationController *)self.navigationController).canDragBack = NO;
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
CGFloat videoHeight = 338/2*(rect_screen.size.width-2*10)/300;
videoRect = CGRectMake(0, NAVBAR_HEIGHT, CGRectGetWidth(self.view.bounds), videoHeight);
// self.moviePlayerController = [[TPMovieContent alloc] initWithFrame:videoRect contentUrl:[NSURL URLWithString:self.videoBO.url]];
NSMutableDictionary *urlDic = [NSMutableDictionary dictionary];
[urlDic setObject:@"http://video.thepaper.cn/video/0/9/139.mp4" forKey:@"standard"];
[urlDic setObject:@"http://video.thepaper.cn/video/0/9/140.mp4" forKey:@"high"];
self.moviePlayerController = [[TPMovieContent alloc] initWithFrame:videoRect contentUrlDic:urlDic];
self.moviePlayerController.delegate = self;
[KEY_WINDOW addSubview:self.moviePlayerController.view];
[self.moviePlayerController relayoutSubViewForScreen:videoRect fullScreen:NO];
}
- (void)imageContentClick:(imageObjectBO*)imageBO {
[MobClick event:@"10"];
((MLNavigationController *)self.navigationController).canDragBack = YES;
[self.navigationController popViewControllerAnimated:NO];
[UIView transitionWithView:self.navigationController.view duration:0.35f options:UIViewAnimationOptionTransitionFlipFromRight animations:nil completion:nil];
}
- (void)strongRelateClick:(NSString*)imageId {
if ([imageId isMatchedByRegex:@"http:__"]) {
imageId = [imageId stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
}
__block listContObjectVO *currentListBO;
[self.strongRelateList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
listContObjectVO *listBO = obj;
if ([listBO.pic isEqualToString:imageId]) {
currentListBO = listBO;
}
}];
if ([self.delegate respondsToSelector:@selector(didEndPlayVedio)]) {
[self.delegate didEndPlayVedio];
}
pushContentWithListContentObject(self.navigationController, currentListBO);
}
#pragma mark - other cell delegate
- (void)goToNodeListPage:(nodeObjectBO *)nodeBO {
columnManagerController *vc = columnManagerController.new;
[self.navigationController pushViewController:vc animated:YES];
vc.nodeBO = nodeBO;
}
- (void)praiseHandlerWithIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section];
AnswerIconBaseCell *cell = (AnswerIconBaseCell *)[self.tableView cellForRowAtIndexPath:index];
[cell commentOK:cell.OkButton];
if (cell == nil) {
NSMutableArray *otherSectionArray = self.contentDataList[index.section];
commentObjectVO *obj = otherSectionArray[index.row];
NSDictionary *dic = @{@"commentId":obj.commentId};
if (obj.isPraised.intValue == 1) {
return;
}
[Remote doJsonActionWithBlock:2000 requestUrl:commentPraiseURL parameter:dic withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
[MobClick event:@"24"];
if ([responseData[@"praiseTimes"] integerValue] < 1001) {
NSString *praseNum = [NSString stringWithFormat:@"%@",responseData[@"praiseTimes"]];
obj.praiseTimes = praseNum;
TPLOG(@"点赞成功");
}
obj.isPraised = @"1";
TPLOG(@"点赞成功");
}else {
TPLOG(@"点赞失败");
}
}];
}
// [otherSectionArray replaceObjectAtIndex:indexPath.row withObject:comment];
}
- (void)tapNodeSporWithNodeInfo:(nodeObjectBO *)node {
sponsorBO *sponsor = setJsonDicToDataModel(node.sponsor, [sponsorBO class]);
if (isBlankString(sponsor.link)) {
return;
}
TPWebViewContoller *webVC = [[TPWebViewContoller alloc]init];
[self.navigationController pushViewController:webVC animated:YES];
webVC.url = sponsor.link;
/**
* bug:5299(新闻详情页点击跟踪栏中的栏目冠名无效)
*/
}
- (void)goToAskDetailPage:(commentObjectVO *)commentBO {
askDetailController *vc = askDetailController.new;
vc.commentBO = commentBO;
[self.navigationController pushViewController:vc animated:YES];
}
- (void)trackButtonClick:(UIButton *)button {
if (!button.selected) {
if (![TPUserDefault instance].userBO) {
loginHomeController *vc = loginHomeController.new;
[self.navigationController pushViewController:vc animated:YES];
return;
}
[self.viewModel trackContent:^{
button.selected = YES;
self.contentBO.isTracked = @"1";
[MobClick event:@"11"];
}];
}else {
if (![TPUserDefault instance].userBO) {
loginHomeController *vc = loginHomeController.new;
[self.navigationController pushViewController:vc animated:YES];
return;
}
[self.viewModel cancelTrackContent:^{
button.selected = NO;
self.contentBO.isTracked = @"0";
}];
}
}
- (void)orderClick:(UIButton *)btn nodeInfo:(nodeObjectBO *)nodeInfo{
if (btn.selected) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"n":nodeInfo.nodeId,@"oType":@"2"}];
[Remote doJsonActionWithBlock:1 requestUrl:orderNodeURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
btn.selected = NO;
nodeObjectBO *nodeInfo = setJsonDicToDataModel(self.contentBO.nodeInfo, [nodeObjectBO class]);
nodeInfo.isOrder = @"0";
NSDictionary *dic = setDataModelToDic(nodeInfo, [nodeObjectBO class]);
self.contentBO.nodeInfo = dic;
ShowMessage(@"取消订阅", YES);
[[NSNotificationCenter defaultCenter] postNotificationName:NEEDREFRESHORDER object:nil];
}
}];
}else {
if ([TPUserDefault instance].userBO) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"n":nodeInfo.nodeId,@"oType":@"1"}];
[Remote doJsonActionWithBlock:1 requestUrl:orderNodeURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
btn.selected = YES;
ShowMessage(@"订阅成功", YES);
nodeObjectBO *nodeInfo = setJsonDicToDataModel(self.contentBO.nodeInfo, [nodeObjectBO class]);
nodeInfo.isOrder = @"1";
NSDictionary *dic = setDataModelToDic(nodeInfo, [nodeObjectBO class]);
self.contentBO.nodeInfo = dic;
[[NSNotificationCenter defaultCenter] postNotificationName:NEEDREFRESHORDER object:nil];
}
}];
}else {
loginHomeController *vc = loginHomeController.new;
[self.navigationController pushViewController:vc animated:YES];
}
}
}
#pragma mark - table delgate and datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.contentDataList?self.contentDataList.count:0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.contentDataList.count > 0) {
NSArray *sectionArray = self.contentDataList[section];
return sectionArray.count;
}else return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *heightArray = self.heightOfCellInEachSection[indexPath.section];
return [heightArray[indexPath.row] floatValue];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 0) {
return 0;
}else {
return 30;
}
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section != 0) {
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, rect_screen.size.width, 30)];
view.backgroundColor = [UIColor colorWithHexString:CONTENTCELLBACKCOLOR];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(contentPadding, 0, rect_screen.size.width-2*contentPadding, 30)];
label.font = appFont(TEXT_THREE_LEVELSIZE, NO);
label.textColor = [UIColor colorWithHexString:TextGray];
label.backgroundColor = [UIColor colorWithHexString:CONTENTCELLBACKCOLOR];
[view addSubview:label];
NSArray *sectionArray = self.contentDataList[section];
id obj = sectionArray[0];
if ([obj isKindOfClass:[commentObjectVO class]]) {
label.text = @"热追问";
}else {
label.text = @"继续阅读";
}
return view;
}else return nil;
}
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) { //详情内容
NSArray *contentSectionList = self.contentDataList[indexPath.section];
id obj = contentSectionList[indexPath.row];
if ([obj isKindOfClass:[NSString class]]){
if ([obj isEqualToString:@"shareCell"]) { //分享
return [self getShareBtnCell:table cellForRowAtIndexPath:indexPath];
}
}else if([obj isKindOfClass:[nodeObjectBO class]]){ //栏目信息
return [self getNodeInfoViewCell:table cellForRowAtIndexPath:indexPath];
}else if ([obj isKindOfClass:[NSDictionary class]]) {
return [self TrackCell:table cellForRowAtIndexPath:indexPath];
}else if([obj isKindOfClass:[TopicInfoBO class]]){//【需求】新闻支持强关系话题显示(bug:4324)
return [self getTopicCell:table cellForRowAtIndexPath:indexPath];
}
}else {
NSArray *otherSectionArray = self.contentDataList[indexPath.section];
id obj = otherSectionArray[indexPath.row];
if ([obj isKindOfClass:[commentObjectVO class]]) { //热追问
commentObjectVO *commentBO = obj;
switch ([commentBO.type integerValue]) {
case 2://提问
return [self returnAskContentTable:table cellForRowAtIndexPath:indexPath];
break;
case 3://回答
return [self returnAnswerContentTableTable:table cellForRowAtIndexPath:indexPath];
break;
case 24://提问头像
return [self returnAskIconTable:table cellForRowAtIndexPath:indexPath];
break;
case 34://回答头像
return [self returnAnswerIconTable:table cellForRowAtIndexPath:indexPath];
break;
// case 5://图片
// return [self returnAnswerPicTable:tableView cellForRowAtIndexPath:indexPath];
// break;
default:
break;
}
}else { //继续阅读
return [self getRelateNewCell:table cellForRowAtIndexPath:indexPath];
}
} return nil;
}
- (reTopicCell*)getTopicCell:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {//【需求】新闻支持强关系话题显示(bug:4324)
reTopicCell *cell = (reTopicCell*)[tableView dequeueReusableCellWithIdentifier:reTopicCellID];
if (nil == cell) {
cell = [[reTopicCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reTopicCellID];
}
NSArray *contentSectionList = self.contentDataList[indexPath.section];
TopicInfoBO *obj = contentSectionList[indexPath.row];
cell.topic = obj;
return cell;
}
- (sinaAndWeChatCell*)getShareBtnCell:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
sinaAndWeChatCell *cell = (sinaAndWeChatCell*)[tableView dequeueReusableCellWithIdentifier:ShareBtnCellID];
if (nil == cell) {
cell = [[sinaAndWeChatCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ShareBtnCellID];
}
cell.contentBO = self.contentBO;
return cell;
}
- (nodeInfoViewCell*)getNodeInfoViewCell:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
nodeInfoViewCell *cell = (nodeInfoViewCell*)[tableView dequeueReusableCellWithIdentifier:nodeInfoViewCellID];
if (nil == cell) {
cell = [[nodeInfoViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nodeInfoViewCellID];
}
nodeObjectBO *nodeInfo = setJsonDicToDataModel(self.contentBO.nodeInfo, [nodeObjectBO class]);
cell.nodeInfoBO = nodeInfo;
cell.delegate = self;
return cell;
}
- (TrackCell *)TrackCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TrackCell *cell = (TrackCell*)[tableView dequeueReusableCellWithIdentifier:trackCellID];
if (nil == cell) {
cell = [[TrackCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:trackCellID];
}
NSArray *otherSectionArray = self.contentDataList[indexPath.section];
id obj = otherSectionArray[indexPath.row];
cell.dic = obj;
cell.delegate = self;
return cell;
}
- (relateNewCell*)getRelateNewCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
relateNewCell *cell = (relateNewCell*)[tableView dequeueReusableCellWithIdentifier:relateNewCellID];
if (nil == cell) {
cell = [[relateNewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:relateNewCellID];
}
NSArray *otherSectionArray = self.contentDataList[indexPath.section];
id obj = otherSectionArray[indexPath.row];
cell.listBO = obj;
return cell;
}
- (AskContentBaseCell *)returnAskContentTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index {
static NSString *cellId = @"AskContentBaseCellId";
AskContentBaseCell *cell = [table dequeueReusableCellWithIdentifier:cellId];
if (nil == cell) {
cell = [[AskContentBaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
NSArray *otherSectionArray = self.contentDataList[index.section];
id obj = otherSectionArray[index.row];
cell.commentBO = obj;
cell.delegate = self;
return cell;
}
- (AskIconBaseCell *)returnAskIconTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index {
static NSString *cellId = @"AskIconBaseCellId";
AskIconBaseCell *cell = [table dequeueReusableCellWithIdentifier:cellId];
if (nil == cell) {
cell = [[AskIconBaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
NSArray *otherSectionArray = self.contentDataList[index.section];
id obj = otherSectionArray[index.row];
cell.commentBO = obj;
cell.delegate = self;
return cell;
}
- (AnswerIconBaseCell *)returnAnswerIconTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index {
static NSString *cellId = @"AnswerIconBaseCellId";
AnswerIconBaseCell *cell = [table dequeueReusableCellWithIdentifier:cellId];
if (nil == cell) {
cell = [[AnswerIconBaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
NSArray *otherSectionArray = self.contentDataList[index.section];
id obj = otherSectionArray[index.row];
cell.commentBO = obj;
cell.delegate = self;
return cell;
}
- (AnswerContentBaseCell *)returnAnswerContentTableTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index {
static NSString *cellId = @"AnswerContentBaseCellId";
AnswerContentBaseCell *cell = [table dequeueReusableCellWithIdentifier:cellId];
if (nil == cell) {
cell = [[AnswerContentBaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
NSArray *otherSectionArray = self.contentDataList[index.section];
id obj = otherSectionArray[index.row];
cell.shouldNoHiddenLine = @"1";
cell.commentBO = obj;
cell.indexPath = index;
cell.delegate = self;
__block NSIndexPath *IndexPath = index;
__weak typeof(self) Self = self;
[cell expandCell:^(BOOL isExpand, CGFloat cellHeight) {
CGFloat backY = [cell.aswerContentLabel convertRect:cell.aswerContentLabel.bounds toView:KEY_WINDOW].origin.y-64;
if (isExpand) {
cellHeight = cellHeight+25 + 10;
NSMutableArray *heightarray = Self.heightOfCellInEachSection[IndexPath.section];
[heightarray replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",cellHeight]];
[Self.heightOfCellInEachSection replaceObjectAtIndex:IndexPath.section withObject:heightarray];
NSArray *array = @[IndexPath];
[Self.tableView beginUpdates];
[Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
[Self.tableView endUpdates];
}else {
cellHeight = getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE)+25+10;
NSMutableArray *heightarray = Self.heightOfCellInEachSection[IndexPath.section];
[heightarray replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",cellHeight]];
[Self.heightOfCellInEachSection replaceObjectAtIndex:IndexPath.section withObject:heightarray];
NSArray *array = @[IndexPath];
[Self.tableView beginUpdates];
[Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
[Self.tableView endUpdates];
if (backY < 0) {
[Self.tableView beginUpdates];
[Self.tableView scrollToRowAtIndexPath:IndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
[Self.tableView endUpdates];
}
}
}];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSArray *otherSectionArray = self.contentDataList[indexPath.section];
id obj = otherSectionArray[indexPath.row];
if (indexPath.section != 0) {
if ([obj isKindOfClass:[listContObjectVO class]]) {
[MobClick event:@"49"];
int readmode = [[TPUserDefault instance].readModeStr intValue];
if (readmode == intelligentMode) {
if ([Remote IsEnableWIFI]) {
readmode = imageMode;
}else {
readmode = textMode;
}
}
pushContentWithListContentObject(self.navigationController, obj);
}
}else {
if ([obj isKindOfClass:[TopicInfoBO class]]) {
[MobClick event:@"61"];
topicContentController *topic = [topicContentController new];
topic.preTopicInfo = obj;
// if (self.moviePlayerController) {
// [self stopAndRemoveVideo];
// }
if ([self.delegate respondsToSelector:@selector(didEndPlayVedio)]) {
[self.delegate didEndPlayVedio];
}
[self.navigationController pushViewController:topic animated:YES];
}
}
});
}
#pragma mark - webview delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的,原文没有提到。
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的,原文没有提到。
[[NSUserDefaults standardUserDefaults] synchronize];
[self setTableDataSource];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
CGSize contentSize = webView.scrollView.contentSize;
webHeight = height + tableTotalHeight;
webView.scrollView.contentSize = CGSizeMake(contentSize.width, webHeight);
self.tableView.frame = CGRectMake(0, height, CGRectGetWidth(self.view.bounds), tableTotalHeight);
[self.webView.scrollView addSubview:self.tableView];
[self addObserver:self forKeyPath:@"self.webView.scrollView.contentSize" options:NSKeyValueObservingOptionNew context:nil];
});
}
#pragma mark - KVO call back
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (self.webView.scrollView.contentSize.height != webHeight) {
CGSize contentSize = self.webView.scrollView.contentSize;
self.webView.scrollView.contentSize = CGSizeMake(contentSize.width, webHeight);
}
}
- (void)dealloc {
NSLog(@"dealloc:%@",self.description);
[self.webView stopLoading];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[self.webView loadHTMLString:@" " baseURL:nil];
[self.webView removeFromSuperview];
self.webView.delegate = nil;
self.webView = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self removeObserver:self forKeyPath:@"self.webView.scrollView.contentSize"];
}
#pragma mark - video method
- (void)playFinish {
UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (currentOrientation != UIInterfaceOrientationPortrait) {
[[UIApplication sharedApplication]
setStatusBarOrientation:UIInterfaceOrientationPortrait
animated:NO];
[[UIDevice currentDevice]
setValue:[NSNumber
numberWithInteger:UIInterfaceOrientationPortrait]
forKey:@"orientation"];
}
[self stopAndRemoveVideo];
if ([self.delegate respondsToSelector:@selector(didEndPlayVedio)]) {
[self.delegate didEndPlayVedio];
}
[(TPWindow*)KEY_WINDOW enabelEmbeddedVideoGesture:NO withVideoController:nil embeddedRect:CGRectZero];
}
- (void)turnMovieViewToPortrait {
[self.moviePlayerController relayoutSubViewForScreen:self.moviePlayerController.originRect fullScreen:NO];
}
- (void)stopAndRemoveVideo {
[self.moviePlayerController.view removeFromSuperview];
[self.moviePlayerController selfDealloc];
self.moviePlayerController = nil;
self.moviePlayerController.view = nil;
((MLNavigationController *)self.navigationController).canDragBack = YES;
if ([self.delegate respondsToSelector:@selector(didEndPlayVedio)]) {
[self.delegate didEndPlayVedio];
}
}
- (void)turnToright {
[[UIApplication sharedApplication]
setStatusBarOrientation:UIInterfaceOrientationLandscapeRight
animated:NO];
[[UIDevice currentDevice]
setValue:[NSNumber
numberWithInteger:UIInterfaceOrientationLandscapeRight]
forKey:@"orientation"];
}
- (void)turnToPortrait {
[[UIApplication sharedApplication]
setStatusBarOrientation:UIInterfaceOrientationPortrait
animated:NO];
[[UIDevice currentDevice]
setValue:[NSNumber
numberWithInteger:UIInterfaceOrientationPortrait]
forKey:@"orientation"];
}
- (void)enterFullScreen:(BOOL)isEnter {
if (isEnter) {
[self turnToPortrait];
}else {
[self turnToright];
}
if ([delegate conformsToProtocol:@protocol(testHtmlContentDelegate)] &&
[delegate respondsToSelector:@selector(videoEnterFullScreen:)]) {
[delegate videoEnterFullScreen:!isEnter];
}
}
#pragma mark - tpmovie delegate
- (void)closeHandler {
UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (currentOrientation != UIInterfaceOrientationPortrait) {
[[UIApplication sharedApplication]
setStatusBarOrientation:UIInterfaceOrientationPortrait
animated:NO];
[[UIDevice currentDevice]
setValue:[NSNumber
numberWithInteger:UIInterfaceOrientationPortrait]
forKey:@"orientation"];
}
if (self.moviePlayerController.isEmbedded) {
if ([self.delegate respondsToSelector:@selector(videoClosedWithEmbeddedFullScrrenn)]) {
[self.delegate videoClosedWithEmbeddedFullScrrenn];
}
}
[self stopAndRemoveVideo];
[(TPWindow*)KEY_WINDOW enabelEmbeddedVideoGesture:NO withVideoController:nil embeddedRect:CGRectZero];
}
- (void)movieTimedOut {
[self stopAndRemoveVideo];
}
- (void)moviePlayerWillMoveFromWindow {
// if (![self.view.subviews containsObject:self.moviePlayerController.view]) {
// UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
// if (currentOrientation != UIInterfaceOrientationPortrait) {
// [[UIApplication sharedApplication]
// setStatusBarOrientation:UIInterfaceOrientationPortrait
// animated:NO];
//
// [[UIDevice currentDevice]
// setValue:[NSNumber
// numberWithInteger:UIInterfaceOrientationPortrait]
// forKey:@"orientation"];
// }
// [self.view addSubview:self.moviePlayerController.view];
// [self.moviePlayerController setFrame:videoRect];
// self.moviePlayerController.controls.cutPhotoBtn.hidden = YES;
// self.moviePlayerController.controls.fullscreenButton.selected = NO;
// }
}
#pragma mark - fontSize
- (void)reloadTableWithFontChange {
dispatch_async(dispatch_get_main_queue(), ^{
[_bridge send:[NSString stringWithFormat:@"changeFontSize,%fpx",[TPUserDefault instance].contFontSize]];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
CGSize contentSize = self.webView.scrollView.contentSize;
webHeight = height + tableTotalHeight;
self.webView.scrollView.contentSize = CGSizeMake(contentSize.width, webHeight);
self.tableView.frame = CGRectMake(0, height, CGRectGetWidth(self.view.bounds), tableTotalHeight);
});
});
}
#pragma mark - night mode
- (void)needrefreshNightMode:(id)sender{
self.view.backgroundColor = [UIColor colorWithHexString:ContentDetailBack];
self.webView.backgroundColor = [UIColor colorWithHexString:ContentDetailBack];
[_bridge send:[NSString stringWithFormat:@"switchNightMode,%@",[TPUserDefault instance].isNightMode]];
self.tableView.backgroundColor = [UIColor colorWithHexString:ContentDetailBack];
[self setTableDataSource];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
NSLog(@"warning!!!!!!!!!");
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
@end