|
//
// contentHomeController.m
// ThePaperDemo
//
// Created by scar1900 on 14/11/7.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "detailContentHomeController.h"
#import "commentAndAskController.h"
#import "MLNavigationController.h"
#import "detailPageViewModel.h"
#import "writeCommentAndAskController.h"
#import "shareContent.h"
#import "loginHomeController.h"
#import "detailContentHelpController.h"
#import "AppDelegate.h"
#import "imageShareContent.h"
#import "embeddedVideo.h"
#define VIEWWIDTH CGRectGetWidth(self.view.bounds)
@interface detailContentHomeController ()<UIGestureRecognizerDelegate,detailContentDelegate,testHtmlContentDelegate,fontSizeSleideControllerDelegate,detailPageViewModelDelegate,writeContentDelegate,shareContentDelegate,detailContentHelpDelegate> {
// UIPanGestureRecognizer *panGestureRecognizer;
CGPoint startTouch;
CGPoint askStartTouch;
BOOL isHiddenStatusBar;
shareContent *shareContentView;
BOOL isFirstIntoOffline;
}
@property(nonatomic, strong)commentAndAskController *askCommentController;
@property(nonatomic, strong)UIButton *commentButton;
@property(nonatomic, strong)UIButton *fontSizeButton;
@property(nonatomic, strong)UIView *lineView;
@property(nonatomic, strong)ContDetailPageDB *currentDetaialDB;
@property(nonatomic, strong)detailPageViewModel *viewModel;
@property(nonatomic, strong)writeCommentAndAskController *writeCommentContentVC;
@property(nonatomic, strong)detailContentHelpController *detailHelpVC;
@end
@implementation detailContentHomeController
@synthesize nodeID = _nodeID;
@synthesize detailContentBO = _detailContentBO;
@synthesize currentDetaialDB;
@synthesize viewModel;
@synthesize isPlay;
- (void)viewDidLoad {
[super viewDidLoad];
[MobClick event:@"7"];
isFirstIntoOffline= YES;
self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
self.contentVC = self.detailController;
self.askVC = self.askCommentController;
[self.naviBar addSubview:self.commentButton];
[self.naviBar addSubview:self.fontSizeButton];
self.fontVC.delegate = self;
self.commentButton.titleLabel.font = appFont(12, NO);
self.commentButton.titleLabel.textAlignment = NSTextAlignmentLeft;
[self.commentButton setTitleColor:[UIColor colorWithHexString:BLUECOLOR] forState:UIControlStateNormal];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willPopBack:) name:CONTENTDETAILPOPBACK object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(linkWithPush:) name:PUSHCLICK object:nil];
if ([[TPUserDefault instance].helpArray[@"detail"] intValue] == 0) {
NSMutableDictionary *array = [NSMutableDictionary dictionaryWithDictionary:[TPUserDefault instance].helpArray];
[array setObject:@"1" forKey:@"detail"];
[TPUserDefault instance].helpArray = array;
self.detailHelpVC = [detailContentHelpController new];
self.detailHelpVC.delegate =self;
[self.view addSubview:self.detailHelpVC.view];
[self.detailHelpVC.view makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.left);
make.right.equalTo(self.view.right);
make.top.equalTo(self.view.top);
make.bottom.equalTo(self.view.bottom);
}];
}
isPlay = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationWillChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
- (void)linkWithPush:(NSNotification*)noti {
[self.detailController stopAndRemoveVideo];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (!_detailContentBO) {
[self startHud];
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (!_detailContentBO) {
[self.viewModel checkContentDataInCoreData:self.nodeID];
}
/**
* bug:5192(频道页,进入一个内容很长的文章,会有明显的卡顿)
*/
}
#pragma mark - backBtnClick
-(void)disHelp{
[UIView animateWithDuration:0.3 animations:^{
[self.detailHelpVC.view removeFromSuperview];
}completion:^(BOOL finished) {
self.detailHelpVC = nil;
}];
}
- (void)backHandler {
((MLNavigationController *)self.navigationController).canDragBack = YES;
if ([self.navigationController.topViewController isKindOfClass:[detailContentHomeController class]]) {
[self.detailController stopAndRemoveVideo];
}
[self.navigationController popViewControllerAnimated:YES];
}
- (UIButton *)commentButton {
if (!_commentButton) {
_commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
_commentButton.frame = CGRectMake(CGRectGetMaxX(self.naviBar.frame)-60, 22, 60, 40);
[_commentButton setImage:Image(@"live/commentIcon.png") forState:UIControlStateNormal];
[_commentButton addTarget:self action:@selector(gotoComment:) forControlEvents:UIControlEventTouchUpInside];
_commentButton.imageEdgeInsets = UIEdgeInsetsMake(9,
10,
13,
5);
_commentButton.titleEdgeInsets = UIEdgeInsetsMake(7,
10,
11,
0);
// _commentButton.backgroundColor = [UIColor greenColor];
// self.naviBar.backgroundColor = [UIColor redColor];
}
return _commentButton;
}
- (UIButton *)fontSizeButton {
if (!_fontSizeButton) {
_fontSizeButton = [UIButton buttonWithType:UIButtonTypeCustom];
_fontSizeButton.frame = CGRectMake(_commentButton.frame.origin.x - 35, 22, 35, 40);
[_fontSizeButton setImage:Image(@"live/fontChangeBtn.png") forState:UIControlStateNormal];
[_fontSizeButton addTarget:self action:@selector(fontSizeClick:) forControlEvents:UIControlEventTouchUpInside];
[_fontSizeButton setImageEdgeInsets:UIEdgeInsetsMake(7.5, 6, 12.5, 11)];
// _fontSizeButton.backgroundColor = [UIColor redColor];
self.lineView.frame = CGRectMake(_commentButton.frame.origin.x, 34, 0.5, 15);
// self.lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
[self.naviBar addSubview:self.lineView];
}
return _fontSizeButton;
}
-(UIView *)lineView{
if (!_lineView) {
_lineView = [UIView new];
_lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
return _lineView;
}
- (void)willPopBack:(NSNotification*)notification {
// [TPUserDefault instance].lastContentOffset = self.detailController.tableView.contentOffset;
[TPUserDefault instance].lastContentID = self.nodeID;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if ([[TPUserDefault instance].visitTime intValue] >= 10) {
[TPUserDefault instance].visitTime = @"-1"; //bug5247: 提示评分,会出现两次
AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
YRSideViewController *sideController=[delegate sideViewController];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"澎友,您已经用最新版的澎湃看了几篇文章,如果觉得好,请鼓励评分一下。" delegate:sideController cancelButtonTitle:@"鼓励一下" otherButtonTitles:@"还需努力",nil];
[alert show];
}
});
}
- (void)askButtonClick:(UIButton *)btn {
if ([TPUserDefault instance].userBO) {
btn.selected = YES;
self.panGestureRecognizer.enabled = NO;
((MLNavigationController *)self.navigationController).canDragBack = NO;
self.writeCommentContentVC = [[writeCommentAndAskController alloc]init];
self.writeCommentContentVC.type = commentType;
self.writeCommentContentVC.nodeId = self.nodeID;
self.writeCommentContentVC.delegate = self;
[self presentController:self.writeCommentContentVC animated:YES completion:^{
btn.selected = NO;
}];
}else {
loginHomeController *vc = loginHomeController.new;
[self.navigationController pushViewController:vc animated:YES];
}
}
#pragma gotoCommentAndAsk
- (void)gotoComment:(id)sender {
// if (isPlay) {
// return;
// }
[self moveToAskAndComment];
if (isPlay) {
[self showEmbeddedVideo];
}
}
- (void)moveCommentViewEnded {
// self.detailController.tableView.scrollEnabled = YES;
if (isPlay) {
[self showEmbeddedVideo];
}
}
- (void)showEmbeddedVideo {
CGRect videoRect = self.detailController.moviePlayerController.originRect;
CGFloat scale = videoRect.size.height/videoRect.size.width;
[embeddedVideo showEmbeddedVideoWithRect:CGRectMake(CGRectGetWidth(self.view.bounds)/2,
CGRectGetHeight(self.view.bounds)-50-CGRectGetWidth(self.view.bounds)/2*scale,
CGRectGetWidth(self.view.bounds)/2,
CGRectGetWidth(self.view.bounds)/2*scale)
withVideoController:self.detailController.moviePlayerController];
}
- (void)removeCommentViewEnded {
[embeddedVideo removeEmbeddedVideo:self.detailController.moviePlayerController];
}
#pragma mark - fontSizeDelegate
- (void)clickToDismiss {
[self fontSizeClick:self.fontSizeButton];
}
- (void)contentFontSizeHasChange {
[self.detailController reloadTableWithFontChange];
}
#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if (self.detailHelpVC) {
[self disHelp];
return NO;
}else{
return YES;
}
}
#pragma mark - set and get @propertys
- (void)setNodeID:(NSString *)nodeIDstr {
if (!_nodeID || isBlankString(_nodeID)) {
_nodeID = nodeIDstr;
self.askCommentController.gestureView = self.view;
self.askCommentController.nodeId = nodeIDstr;
self.viewModel = [detailPageViewModel new];
self.viewModel.delegate = self;
self.viewModel.nodeID = nodeIDstr;
if (self.offlineDetailContBO) {
self.viewModel.offlineDetailContentBO = self.offlineDetailContBO;
}
// [self.viewModel checkContentDataInCoreData:nodeIDstr tableView:self.detailController.tableView];
}else {
[self moveToAskAndComment];
}
}
- (void)fontSizeClick:(UIButton *)sender {
if (isPlay) {
return;
}
[self.fontVC reflashSize:[TPUserDefault instance].contFontSize];
if (!sender.selected) {
self.fontVC.view.hidden = NO;
[self.fontVC showfontSizeSliderCellFinishedWithCompletion:^(BOOL finished) {
sender.selected = YES;
((MLNavigationController *)self.navigationController).canDragBack = NO;
[self removePanGesture];
}];
}else {
[self.fontVC dismissfontSizeSliderCellFinishCompletion:^(BOOL finished) {
self.fontVC.view.hidden = YES;
sender.selected = NO;
((MLNavigationController *)self.navigationController).canDragBack = YES;
[self reAddpanGesture];
}];
}
}
- (void)setDetailContentBO:(contDetailPageVO *)detailBO {
if (detailBO) {
_detailContentBO = detailBO;
if (self.offlineDetailContBO) {
// self.detailController.isOffline = YES;
}else {
// self.detailController.isOffline = NO;
}
self.detailController.detailContentBO = detailBO;
}
}
- (commentAndAskController*)askCommentController {
if (!_askCommentController) {
_askCommentController = commentAndAskController.new;
}
return _askCommentController;
}
- (hotController*)detailController {
if (!_detailController) {
_detailController = hotController.new;
// _detailController.delegate = self;
}
return _detailController;
}
#pragma mark - child controller delegate
- (void)popBackButtonHandler {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)askButtonHandler {
[self moveToAskAndComment];
}
- (void)shareBtnHandler:(contentObjectBO *)contentBO WithButton:(UIButton *)btn {
if (!btn.selected) {
self.panGestureRecognizer.enabled = NO;
shareContentView = [[shareContent alloc]initWithFrame:CGRectMake(0,
0,
CGRectGetWidth(self.view.bounds),
CGRectGetHeight(self.view.bounds)-45)];
shareContentView.delegate = self;
//离线缓存:离线缓存的新闻分享异常(bug:5859)
if ([TPUserDefault instance].isOffline) {
if(isFirstIntoOffline){
NSString *sharePic = [NSString stringWithFormat:@"http://m.thepaper.cn%@",contentBO.sharePic];
contentBO.sharePic = sharePic;
shareContentView.contentBO = contentBO;
isFirstIntoOffline = NO;
}else{
shareContentView.contentBO = contentBO;
}
}else{
shareContentView.contentBO = contentBO;
}
shareContentView.baseController = self;
[shareContentView presentShareContentInView:self.view sender:btn];
}else {
if (shareContentView) {
[shareContentView dismissShareContent];
self.panGestureRecognizer.enabled = YES;
}
}
}
- (void)videoEnterFullScreen:(BOOL)isEnterFullScreen {
if (!isEnterFullScreen && self.detailController.moviePlayerController.isEmbedded) {
[self moveToAskAndComment];
}
}
- (void)videoClosedWithEmbeddedFullScrrenn {
[self moveToAskAndComment];
}
- (BOOL)prefersStatusBarHidden
{
return isHiddenStatusBar;
}
- (void)showStatusBar
{
isHiddenStatusBar = NO;
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[self prefersStatusBarHidden];
[self setNeedsStatusBarAppearanceUpdate];
}
}
- (void)hideStatusBar
{
isHiddenStatusBar = YES;
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[self prefersStatusBarHidden];
[self setNeedsStatusBarAppearanceUpdate];
}
}
- (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code{
ShowTextMessage(message);
[self.waitBackView removeFromSuperview];
}
- (void)needRefreshPage {
[self startHud];
[self.viewModel remoteAction];
}
#pragma mark - viewModel Delegate
- (void)returnDetailPageInfo:(contDetailPageVO *)detailInfoBO {
self.detailContentBO = detailInfoBO;
NSString *interactionStr = detailInfoBO.content[@"interactionNum"]?detailInfoBO.content[@"interactionNum"]:@"";
[self.commentButton setTitle:interactionStr forState:UIControlStateNormal];
}
- (void)returnCommentList:(NSMutableArray*)commentArray
nodeId:(NSString*)nodeId
nextUrl:(NSString*)nextUrl
commentTotalRecord:(NSInteger)recordTotal {
self.askCommentController.nodeId = nodeId;
self.askCommentController.commentList = commentArray;
self.askCommentController.firstCommentUrl = nextUrl;
self.askCommentController.commentSum = recordTotal;
}
- (void)returnHotAskDic:(NSMutableDictionary *)qaDic
nodeId:(NSString *)nodeId
nextUrl:(NSString *)nextUrl
askTotalRecord:(NSInteger)recordTotal {
self.askCommentController.dataDic = qaDic;
self.askCommentController.firstAskUrl = nextUrl;
self.askCommentController.qaNum = recordTotal;
}
- (void)startHud {
[self.view addSubview:self.waitBackView];
[self removePanGesture];
[self.view bringSubviewToFront:self.waitBackView];
self.waitCursor.animating = YES;
self.commentButton.enabled = NO;
}
- (void)stopHud {
[self reAddpanGesture];
[self.waitBackView removeFromSuperview];
self.waitBackView = nil;
self.commentButton.enabled = YES;
self.waitCursor.animating = NO;
}
- (void)remoteFail:(BOOL)isTimeOut {
if (isTimeOut) {
[self.detailController networkTimeOut];
}
[self stopHud];
}
- (void)contentHaveOffline {//新闻已下线页面,顶部应该只有返回按钮(bug:5001)
[self stopHud];
[self contentHaveOfflined];
self.commentButton.hidden = YES;
self.fontSizeButton.hidden = YES;
self.lineView.hidden = YES;
self.panGestureRecognizer.enabled = NO;
}
- (void)refreshDetailPage:(contentObjectBO *)content {
// self.detailController.refreshBO = content;
}
#pragma mark - writeCommentDelegatr
- (void)dismissWriteContent:(commentAndAskType)type{
__weak typeof(self) Self = self;
((MLNavigationController *)self.navigationController).canDragBack = YES;
self.panGestureRecognizer.enabled = YES;
[self dismissViewController:self.writeCommentContentVC animated:YES completion:^{
Self.writeCommentContentVC = nil;
}];
}
- (void)commentSuccess:(commentAndAskType)type {
__weak typeof(self) Self = self;
[self dismissViewController:self.writeCommentContentVC animated:YES completion:^{
self.panGestureRecognizer.enabled = YES;
((MLNavigationController *)self.navigationController).canDragBack = YES;
[self gotoComment:self.commentButton];
[self commentSuccess];
if (Self.writeCommentContentVC.type == commentType || Self.writeCommentContentVC.type == commentForUserType) {
}else if (Self.writeCommentContentVC.type == askType || Self.writeCommentContentVC.type == askForUserType) {
}
Self.writeCommentContentVC = nil;
}];
}
#pragma mark shareContent delegate
- (void)didDismissShareContent:(UIView *)contentView {
contentView = nil;
self.panGestureRecognizer.enabled = YES;
}
- (void)dealloc {
[self.detailController stopAndRemoveVideo];
self.askCommentController.gestureView = nil;
self.askCommentController.delegate = nil;
self.askCommentController = nil;
self.detailController.bridge = nil;
self.detailController.delegate = nil;
self.detailController = nil;
[self.childViewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
obj = nil;
}];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didPlayVedio {
// self.panGestureRecognizer.enabled = NO;
isPlay = YES;
}
- (void)didEndPlayVedio {
// self.panGestureRecognizer.enabled = YES;
isPlay = NO;
}
-(void) detailToShare:(UIImage *)img contentBO:(contentObjectBO *)contentBO{
imageShareContent *detailShareView = [[imageShareContent alloc]initWithFrame:CGRectMake(0,
0,
CGRectGetWidth(self.view.bounds),
CGRectGetHeight(self.view.bounds))];
detailShareView.contentBO = contentBO;
detailShareView.shareImg = img;
detailShareView.baseController = self;
detailShareView.sharestyle = shareNewsStyle;
[detailShareView presentShareContentInView:self.view sender:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dismissSelfView {
// [TPUserDefault instance].lastContentOffset = CGPointMake(0, self.detailController.tableView.contentSize.height-self.detailController.tableView.bounds.size.height);
[TPUserDefault instance].lastContentID = self.nodeID;
[CoreAnimationEffect animationEaseOut:self.navigationController.view];
[(MLNavigationController *)self.navigationController popBackWithTopSwipeGesture];
}
- (BOOL)shouldAutorotate {
if (self.detailController.moviePlayerController) {
return YES;
}else {
return NO;
}
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
if (self.detailController.moviePlayerController) {
return UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}else {
return UIInterfaceOrientationPortrait;
}
}
- (void)statusBarOrientationWillChange:(NSNotification *)note {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
orientation = UIInterfaceOrientationPortrait;
if (self.detailController.moviePlayerController) {
if (self.detailController.moviePlayerController.isEmbedded) {
[self showEmbeddedVideo];
}else {
[self.detailController turnMovieViewToPortrait];
}
[self showStatusBar];
}
}else {
orientation = UIInterfaceOrientationLandscapeLeft;
if (self.detailController.moviePlayerController) {
[self.detailController.moviePlayerController relayoutSubViewForScreen:self.view.bounds fullScreen:YES];
[self hideStatusBar];
}
}
}
- (void)removeButtonHandler {
[self removeAskAndComment];
[embeddedVideo removeEmbeddedVideo:self.detailController.moviePlayerController];
}
@end
|